I see an interesting python exercise in codewars.it is about convert strings to numbers.I want some suggestions or guidance to solve this python exercise.Thank you
this is the exercise:In this kata we want to convert a string into an integer. The strings simply represent the numbers in words. Examples: "one"1
and this is my code:
def parse_int(string):
dict_of_numbers={ "zero":0, "one":1, "two":2, "three":3, "four":4, "five":5, "six":6, "seven":7, "eight":8, "nine":9,"ten":10, "eleven":11, "twelve":12, "thirteen":13, "fourteen":14, "fifteen":15, "sixteen":16, "seventeen":17, "eighteen":18, "nineteen":19, "twenty":20, "thirty":30, "forty":40, "fifty":50, "sixty":60, "seventy":70, "eighty":80, "ninety":90,"thousand":1000,"hundred":100}
string=string.replace(' ','-')
numbers=string.split('-')
created_number=0
for number in numbers:
for key,value in dict_of_numbers.items():
if number==key:
created_number+=value
return created_number
one hundred.seven hundred. You must multiply the number before the hundred with 100.word2numbersolved this, as the translation from spoken numbers to integers is not that easy due to the issue @MSH mentioned: GitHub