schroeder.carl.j
BAN USERThis returns true if any of the names in the list occur in the tweet.
import sys
def isNameInTweet(names, tweet):
# insert names into dictionary
prev = None
nametable = {}
tweetwords = []
for n in names:
nametable[n] = True
# tokenize tweet by word
tweetwords = tweet.split()
print tweetwords
# combine words into pairs in order, lookup in dict
for w in tweetwords:
if not prev:
prev = w
continue
print "looking for " + prev + " " + w
if str(prev + " " + w) in nametable:
return True
prev = w
return False
def main():
names = ['Katy Perry', 'Russell Brand', 'Russel Crowe']
tweet = 'Katy Perry and Russel Brand make a nice couple!'
print str(names) + " " + str(tweet) + " " +str( isNameInTweet(names, tweet))
if __name__ == "__main__":
sys.exit(main())
Question: all the nodes together? all the nodes together in order? any 2 nodes together? any N nodes together?
If the question is: Do all the strings together in order form a palindrome?
Assign a to point to the first character of the first node.
Traverse to the end and assign this to the tail pointer.
Assign b to point to the last character of the tail node.
Check that the value of b == the value of a.
Move a 1 character towards the tail.
Move b 1 character towards the head.
When you run out of characters in a string, move to the next node in the direction you are going.
If b == a, and all values are the same, it is a palindrome.
I believe it is possible for a square to exist in a 2D plane where none of it's vertices share x or y. An example would be tilting a square {0,1}, {1,1}, {0,1}, {0,0} by 20 degrees. This square would not be found by your method as there would never be 2 points from it at the same time on your sweep line.
- schroeder.carl.j August 08, 2013This python might do it, test included:
import sys
class Node:
def __init__(self,data):
self.right = None
self.left = None
self.data = data
class Tracker:
def __init__(self,data,dist):
self.data = data
self.dist = float(dist)
def leftNodes(n,lvl,q,dist):
if n.left:
if len(q) < lvl + 1:
q.append(Tracker(n.left.data,dist))
elif q[lvl].dist > dist:
q[lvl] = Tracker(n.left.data,dist)
leftNodes(n.left,lvl + 1,q,dist)
if n.right:
if len(q) < lvl + 1:
q.append(Tracker(n.right.data,dist + 1.0/(lvl+1)))
elif q[lvl].dist > dist:
q[lvl] = Tracker(n.right.data,dist + 1.0/(lvl+1))
leftNodes(n.right,lvl + 1,q,dist + 1.0/(lvl+1))
def main():
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
root.right.left = Node(6)
root.right.right = Node(7)
root.right.left.left = Node(8)
root.right.left.right = Node(9)
root.right.left.right.left = Node(10)
root.right.left.right.right = Node(11)
q = []
leftNodes(root,0,q,0)
for i in q:
print i.data
if __name__ == "__main__":
sys.exit(main())
I ran that in Python and it did not generate 8 (i,j,k,l){3,0,0,0} or 12 (i,j,k,l){2,1,0,0} which should be part of the list of numbers generated.
- schroeder.carl.j July 30, 2013Meh, going to have to heap it after all.
Coffee first, then code :(
vgeeks algorithm is the best I can come up with. Here it is in Python:
def printMultiples(n):
op = array.array('i')
i,j,k,l = 0,0,0,0
mi,mj,mk,ml = 2,3,5,7
op.append(1)
print op[0]
for x in range(1,n):
op.append(min(mi,mj,mk,ml))
print op[x]
if mi == op[x]:
i = i+1
mi = op[i]*2
if mj == op[x]:
j = j+1
mj = op[j]*3
if mk == op[x]:
k = k+1
mk = op[k]*5
if ml == op[x]:
l = l+1
ml = op[l]*7
The odds will change if other candidates are also interviewing for multiple posts.
- schroeder.carl.j August 25, 2013