Messi
BAN USERdef subsequence(T, S):
i = 0
j = 0
if len(S) > len(T):
return False
while(i< len(T) and j< len(S)):
if T[i] == S[j]:
print "Matched : {0}".format(T[i])
if j == len(S)-1:
return True
i += 1
j += 1
else:
print "Skip character".format(T[i])
i += 1
return False
subsequence('abbebcd','bbcd')
Asynchronous code is possible due to multiple threads of execution within a process. However, it can be effectively achieved by spawning multiple processes. Now it depends on whether operating system can support ipc comunication. Most of the operating systems does. We can use the simplest of the ipc communication like sockets. I think, non-blocking sockets are possible. I am still not sure, if this answers me completely.
- Messi November 17, 2013def remove_duplicate_chars(st):
seen_chars = set()
actual_len = 0
for s in st:
s_lc = s.lower()
if s_lc not in seen_chars:
seen_chars.add(s_lc)
# replace current character with non-duplicate character
st[actual_len] = s
actual_len += 1
# clean up the extra space caused due to duplicates
# Note the reverse order below
for s in range(actual_len, len(st))[::-1]:
del st[s]
return st
Driver program
print remove_duplicate_chars(list("Today is the day"))
['T', 'o', 'd', 'a', 'y', ' ', 'i', 's', ' ', 't', 'h', 'e', ' ', 'd', 'a', 'y']
['T', 'o', 'd', 'a', 'y', ' ', 'i', 's', 'h', 'e']
question.replace("in Java", "in Python")
Hash table implementation in python
class Element(object):
def __init__(self, key, value):
self.key = key
self.value = value
class HashTable(object):
"""
Hash table implementation using modulo hash algorithm and linked list for storing hashed elements
"""
def __init__(self):
self.size = 8 # Lets start with 8 buckets
self.table = list((None for i in range(self.size)))
self.num_elements = 0
def set_key(self, key, value):
h = self._hash(key)
if not self.table[h]:
self.table[h] = list()
bucket = self.table[h]
bucket.append(Element(key, value))
self.num_elements = self.num_elements + 1
return True
def get_key(self, key):
h = self._hash(key)
if self.table[h] is None:
return False
bucket = self.table[h]
for item in bucket:
if item.key == key:
return item.value
def _hash(self, key):
if type(key) == str:
value = sum((ord(x) for x in key))
elif type(key) == int:
value = key
else:
value = 0
return value % self.size
def get_size(self):
return self.num_elements
def print_table(self):
for i, bucket in enumerate(self.table):
print "{0}: {1}".format(i, self.print_bucket(bucket))
def print_bucket(self, bucket):
if bucket is None:
return "None"
return ", ".join("-->{0}::{1}".format(element.key, element.value) for element in bucket)
Driver program
myhash = HashTable()
print myhash.print_table()
for i in range(10):
print myhash.set_key(i,i)
for i in range(10):
print myhash.get_key(i)
print myhash.get_size()
myhash.print_table()
First of all, there are two things.
1. Front end
2. Backend
Front end is again composed of two things
1. Static content
2. Dynamic content
Backend is composed of two things.
1. Database backed REST Services
2. Pre-generated machine learning algorithms output
Database backed rest services are the simplest to test, because they are effectively Java code. Machine learning algorithms by having a validation set. The recent trend is to gather feedback of machine learning algorithms by releasing to small group of users and then to broader set of people.
Static content of front end can be testing by effective regexes. As long as the page is built modularly, each module can be tested separately.
If there was a wrapper added to every REST service output to convert from json to html, then the html can be tested similar to above approach.
Front Javascript can be tested by black box testing tools like Selenium or server-side js engines (I cannot remember others now).
Feel free to add if I missed anything.
Hashing is a way of indexing the data, to guarantee O(1) access in the average case.
INT - mod
float - convert into int by multiplying by 10, then mod.
strings - convert to int assuming base 26
objects - convert to int base 128 and then mod(you can also consider only allowed objects)
10. differences between
char *str = "SIVA" and char str1[]="SIVA"
str is a pointer to a constant string. str1 is a constant pointer.
sub questions:
1) where string literal i.e "SIVA" will be stored ( heap , stack, data or code segments) in above both cases.
str - code segment
str1 - stack
2) char *str = "SIVA";
str[0] = 'f' ;
any error ? compile time error or run time error ?
run time error, as you trying to modify a constant string.
3) char *p = (char *) str;
p[0] = 'f' ;
any error ? compile time error or run time error ?
2) reasoning still applies here. runtime error.
Lets represent 7 in base 5. It will require 2 bits. So, call Rand5() for selecting the zeroth bit and rand5() for calling 1st bit. If the result is greater than 7, discard it. Note that the first bit doesn't depend on the second bit. So, probably of each bit is random, hence the final number is also random. I think this approach works for all these sort of examples.
Please correct me if I am wrong.
Anyway, you need to store some kind of information about every word seen, you can optimize it further in below ways.
1. For word in dictionary, maintain a array and increment a score there.(Note that you are not storing the word here)
2. Create an index for non-dictionary words and maintain in a hashmap, and update the count in the array as above.
Now construct a heap to find the top 100 occurences. Note that, heap is better than sort, because we don't care about the order of remaining elements.
Assuming that you want a prime number in a given range. It is a known theorem that there are n/log(n) prime numbers till N.
So, given a and b, find the number of prime numbers(say p) from 1 to b, and also find the number of primes(say q) from 1 to a.
So, there are q-p prime numbers between a and b. So, for every (b-a)/(q-p), there should be a prime number, so try all numbers from a till a+( (b-a)/(q-p) ) and you should find one.
void merge_sort(arr,low,high)
{
if(high-low == 0)
return;
else
mid = low + (high-low)/2;
merge_sort(arr,low,mid);
merge_sort(arr,mid,high);
merge(arr,low,mid,high);
}
merge(arr,low,mid,high)
{
int i = 0;
int j = mid+1;
int* c;
c = malloc(sizeof(int)*(high-low));
k = 0;
while((i <= mid) && (j <= high))
{
if(a[i] < a[j])
{
c[k] = a[i]
i++;
}
else
{
c[k] = a[j];
j++;
}
k++;
}
for(int t = i; t <= mid; t++)
{
c[k++] = a[i++]
}
for(int t = mid+1; t <= high; t++)
{
c[k++] = a[i++]
}
for(int i = low; i < high; i++)
{
a[i] = c[i];
}
}
lets suppose memory has M blocks and you want to sort B blocks. Then you can pull M blocks into memory and sort them and write output to disk. This will take B/M steps.
Now pick the first block of each of (B/M) blocks written previously and merge them and write them to disk. If B/M > M, then you will need to repeat the process to till all the blocks are sorted. Now merge the sorted blocks and repeat.
Visualize like a tree structure.
This is not necessarily true. I think the correct case should be as below.
Lets consider a,b,c,d(consider clockwise) the four line segments, if one end of line segment lies to the top of b, and right of a and left of c, and the other end lies lies to to the bottom of b and right of a and left of c, then only the two lines intersect.
This is not necessarily true. I think the correct case should be as below.
Lets consider a,b,c,d(consider clockwise) the four line segments, if one end of line segment lies to the top of b, and right of a and left of c, and the other end lies lies to to the bottom of b and right of a and left of c, then only the two lines intersect.
Kadane algorithm
Modified for this problem:
- Messi November 17, 2013