Skip to content

Instantly share code, notes, and snippets.

@debetimi
Last active August 29, 2015 13:58
Show Gist options
  • Save debetimi/10215748 to your computer and use it in GitHub Desktop.
Save debetimi/10215748 to your computer and use it in GitHub Desktop.
That problem
#If the integers from 1 to 999,999,999 are written as words, sorted alphabetically, and concatenated, what is the 51 billionth letter?
#where strings have no spaces
import time
singles = ["", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
singles.sort()
teens = ["ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen",
"seventeen", "eighteen", "nineteen"]
teens.sort()
decades = ["twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]
decades.sort()
def get_full_decade():
for x in decades:
for y in singles:
yield x + y;
def create_century():
c = [x for x in get_full_decade()]
c.extend(singles)
c.extend(teens)
c.sort()
return c
century = create_century()
def get_hundreds():
for x in century:
if not x == "":
yield x
if not x in singles:
continue
for c in century:
if not x == "":
yield x + "hundred" + c
def get_thousands():
for x in get_hundreds():
yield x
yield x + "thousand"
for y in get_hundreds():
yield x + "thousand" + y
def get_millions():
for x in get_hundreds():
yield x
yield x + "million"
for y in get_thousands():
yield x + "million" + y
#base 1
def main(index_to_find):
count = 0;
start = time.time()
for x in get_millions():
prev = count
count += len(x)
if count >= index_to_find:
idx = index_to_find - prev
print "this is the number ", x
print "this is character", x[idx - 1]
print "elapsed: ", time.time() - start
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment