Created
May 23, 2023 17:13
-
-
Save ByWaleed/fc396814e8debbdeefd4ce4bf3ca7109 to your computer and use it in GitHub Desktop.
LH Solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
units = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"] | |
tens = ["", "ten", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety"] | |
scales = ["", "thousand", "million", "billion", "trillion"] | |
def conver_number_to_word(number): | |
if number < 20: | |
return units[number] | |
elif number < 100: | |
unit = conver_number_to_word(number % 10) | |
if number % 10 == 0: | |
return tens[number // 10] | |
else: | |
return str(tens[number // 10]) + " " + str(unit) | |
elif number < 1000: | |
unit = conver_number_to_word(number % 100) | |
if number % 100 == 0: | |
return units[number // 100] + " hundred" | |
else: | |
return units[number // 100] + " hundred and " + unit | |
else: | |
words = [] | |
scale = 0 | |
while number > 0: | |
words.append(conver_number_to_word(number % 1000) + " " + scales[scale]) | |
number = number // 1000 | |
scale += 1 | |
return ' '.join(reversed(words)) | |
if __name__ == "__main__": | |
print(conver_number_to_word(11) == "eleven") | |
print(conver_number_to_word(12) == "twelve") | |
print(conver_number_to_word(7) == "seven") | |
print(conver_number_to_word(52) == "fifty two") | |
print(conver_number_to_word(10) == "ten") | |
print(conver_number_to_word(100) == "one hundred") | |
print(conver_number_to_word(300) == "three hundred") | |
print(conver_number_to_word(325) == "three hundred and twenty five") | |
print(conver_number_to_word(202) == "two hundred and two") | |
print(conver_number_to_word(1235) == "one thousand two hundred and thirty five ") | |
print(conver_number_to_word(12345) == "twelve thousand three hundred and fourty five ") | |
print(conver_number_to_word(123123) == "one hundred and twenty three thousand one hundred and twenty three ") | |
print(conver_number_to_word(9123123) == "nine million one hundred and twenty three thousand one hundred and twenty three ") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment