Skip to content

Instantly share code, notes, and snippets.

@tomkaith13
Created May 17, 2014 03:13
Show Gist options
  • Save tomkaith13/06b60b623bdc2a7852dc to your computer and use it in GitHub Desktop.
Save tomkaith13/06b60b623bdc2a7852dc to your computer and use it in GitHub Desktop.
predict the number in a sequence of 3 billion
'''
Sequence 011212201220200112 ... constructed as follows: first is 0, then repeated the following action: already written part is attributed to the right with replacement 0 to 1, 1 to 2, 2 to 0. E.g.
0 -> 01 -> 0112 -> 01121220 -> ...
Create an algorithm which determines what number is on the N-th position in the sequence.
INPUT SAMPLE:
Your program should accept as its first argument a path to a filename. Each line in this file contains an integer N such as
0 <= N <= 3000000000. E.g.
0
5
101
25684
OUTPUT SAMPLE:
Print out the number which is on the N-th position in the sequence. E.g.
0
2
1
0
'''
s=[0]
def mapper(b):
num_dict = {0:1 , 1:2 , 2:0}
return num_dict[b]
class concat():
def __init__(self, s1):
self.s1 = s1
self.s2 = tmap(map,self.s1)
self.cachedlen = len(s1) * 2
def __getitem__(self, i):
if i < len(self.s1):
return self.s1[i]
else:
return self.s2[i - len(self.s1)]
def __len__(self):
return self.cachedlen
class tmap():
def __init__(self, fun, s):
self.fun = fun
self.list = s
self.cachedlen = len(self.list)
def __getitem__(self, i):
return self.fun(self.list[i])
def __len__(self):
return self.cachedlen
for i in xrange(30):
s = concat(s)
''' USE s[n] to obtain the n th number in the sequence '''
print len(s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment