Skip to content

Instantly share code, notes, and snippets.

@ayushgoel
Created May 11, 2015 04:23
Show Gist options
  • Save ayushgoel/1c39f2e2960a415d3a9d to your computer and use it in GitHub Desktop.
Save ayushgoel/1c39f2e2960a415d3a9d to your computer and use it in GitHub Desktop.
Numbers
# Aloha number - number with all digits '4' or '7'
# Q - Given a number, find the aloha number just greater or equal to given number.
def alt(s):
# print "Number: ", s
if s == '4':
return '7'
elif s == '7':
return '44'
# s = str(n)
i = s.find('8') if s.find('8') != -1 else s.find('9')
if i == -1:
new = toaloha(s)
if new == s:
new = toaloha(str(int(s) + 1))
return new
t = '4' * len(s[i:])
x = '4' if s[:i] == '' else alt(s[:i])
# print "T", x, t
return x + t
def toaloha(s):
# s = str(n)
if (s.find('8') != -1) or (s.find('9') != -1):
return alt(s)
nu = [int(x) for x in s]
fir = nu[0]
if fir < 4:
return '4' * len(s)
elif fir == 4:
return ('4' + toaloha(s[1:])) if len(nu) > 1 else '4'
elif fir < 7:
return '7' + '4' * (len(s) - 1)
elif fir == 7:
return ('7' + toaloha(s[1:])) if len(nu) > 1 else '7'
else:
return 'xyz'
if __name__ == '__main__':
last = 0
for i in range(10000):
new = toaloha(str(i))
try:
assert(int(new) >= int(last))
except AssertionError:
print "Assertion failes", new, last, i
exit(1)
print i, new
last = new
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment