Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@xziyue
Last active November 2, 2020 06:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xziyue/df10b65938450e154284ff3cede21b52 to your computer and use it in GitHub Desktop.
Save xziyue/df10b65938450e154284ff3cede21b52 to your computer and use it in GitHub Desktop.
english_digits = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
# check if string s1 starts with s2
def startswith(s1, s2):
if len(s2) > len(s1):
return False
for i in range(len(s2)):
if s1[i] != s2[i]:
return False
return True
def parse_integer(s):
digits = []
while len(s) > 0:
for ind, digit in enumerate(english_digits):
if startswith(s, digit):
digits.append(str(ind))
# truncate the string
s = s[len(digit):]
return int(''.join(digits))
def to_my_string(p):
# think about why we need this
if p == 0:
return english_digits[0]
s = []
while p > 0:
lowest_digit = p % 10
p = p // 10 # division and floor operation (/ operation in Java/C++)
# e.g. 5/2=2
s.append(english_digits[lowest_digit])
s.reverse()
return ''.join(s)
print(parse_integer('onetwothreefourfiveseventwo'))
print(to_my_string(5130798273))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment