dasari4kntr Posted February 25 Author Report Posted February 25 Just now, ARYA said: def number_to_words(n): if n == 0: return "zero" ones = ["", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"] teens = ["", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"] tens = ["", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"] thousands = ["", "thousand", "million", "billion", "trillion"] def three_digit_to_words(num): result = [] if num >= 100: result.append(ones[num // 100] + " hundred") num %= 100 if 10 < num < 20: result.append(teens[num - 10]) else: if num >= 20: result.append(tens[num // 10]) num %= 10 if num > 0: result.append(ones[num]) return " ".join(result) parts = [] thousand_index = 0 while n > 0: chunk = n % 1000 if chunk > 0: words = three_digit_to_words(chunk) if thousands[thousand_index]: words += " " + thousands[thousand_index] parts.append(words) n //= 1000 thousand_index += 1 return " ".join(reversed(parts)) # Test cases print(number_to_words(375)) # Output: three hundred seventy five print(number_to_words(760000)) # Output: seven hundred sixty thousand print(number_to_words(123456789)) # Output: one hundred twenty three million four hundred fifty six thousand seven hundred eighty nine print(number_to_words(0)) # Output: zero bro… lets take 375…as input… while loop lo module 10 చేస్తే… 375%10=5 37%10=7 3%10=3 if push these values in stack while module …next while pop one by one first 3 comes…based on stack length you will know its in 100 position or thousands position…so here 3 is in houndred position and next 7 is tenths position…and last 5 is in digits position… not sure am i missing something..? Quote
nag Posted February 25 Report Posted February 25 9 minutes ago, dasari4kntr said: with this program…whats the output of 760001…? i am expecting…”seven hundred sixty thousand and one”… just checked.. it gave the output as " Seven Hundred Sixty Thousand One" "and" use cheyaledu Quote
nag Posted February 25 Report Posted February 25 1 minute ago, dasari4kntr said: bro… lets take 375…as input… while loop lo module 10 చేస్తే… 375%10=5 37%10=7 3%10=3 if push these values in stack while module …next while pop one by one first 3 comes…based on stack length you will know its in 100 position or thousands position…so here 3 is in houndred position and next 7 is tenths position…and last 5 is in digits position… not sure am i missing something..? nenu adhey logic cheppina.. please check my explanation below the code i posted 1 Quote
nag Posted February 25 Report Posted February 25 24 minutes ago, dasari4kntr said: i dont understand this approach… simple గా… modular of base 10 ట్రై చేస్తే సరిపోదా…with additional logic… code lo adhey vundhi bro.. i explained at the bottom of the code. not sure if you saw it Quote
nag Posted February 25 Report Posted February 25 5 minutes ago, dasari4kntr said: bro… lets take 375…as input… while loop lo module 10 చేస్తే… 375%10=5 37%10=7 3%10=3 if push these values in stack while module …next while pop one by one first 3 comes…based on stack length you will know its in 100 position or thousands position…so here 3 is in houndred position and next 7 is tenths position…and last 5 is in digits position… not sure am i missing something..? please copy paste the code i posted.. it works Quote
dasari4kntr Posted February 25 Author Report Posted February 25 1 minute ago, nag said: nenu adhey logic cheppina.. please check my explanation below the code i posted Just now, nag said: code lo adhey vundhi bro.. i explained at the bottom of the code. not sure if you saw it i had problem..in your code with this function… def three_digit_to_words(num): looks like it took american number system…whta if i convert this entire program to Indian number system…(lacks and crores…) this entire function has to change…isnt it..? Quote
dasari4kntr Posted February 25 Author Report Posted February 25 2 minutes ago, nag said: please copy paste the code i posted.. it works bro..i know it works… i am just behaving like…code skeptic here… Quote
nag Posted February 25 Report Posted February 25 2 minutes ago, dasari4kntr said: i had problem..in your code with this function… def three_digit_to_words(num): looks like it took american number system…whta if i convert this entire program to Indian number system…(lacks and crores…) this entire function has to change…isnt it..? Yes. Universal way to use Millions .. modern world is using US system Indian system lo million ledhu kada.. "10 lakhs" or "Ten Lakhs" replace chey wherever it is spelled as Million may be you can input the question US system or british system based conversion even before reading a number from the user. Quote
dasari4kntr Posted February 25 Author Report Posted February 25 1 minute ago, Abhagyudu said: @dasari4kntr faang ki trying? no…next four years…survive అయ్యి…india కి పోయి రిటైర్మెంట్ తీసుకుంటా… my target is survive for next four years in usa…honestly saying.. Quote
nag Posted February 25 Report Posted February 25 1 minute ago, dasari4kntr said: bro..i know it works… i am just behaving like…code skeptic here… ah function name three_digit_to_words is misleading there.. ignore it .. replace it with any other name like 'convert_to_num_words' Quote
dasari4kntr Posted February 25 Author Report Posted February 25 1 minute ago, nag said: Yes. Universal way to use Millions .. modern world is using US system Indian system lo million ledhu kada.. "10 lakhs" or "Ten Lakhs" replace chey wherever it is spelled as Million may be you can input the question US system or british system based conversion even before reading a number from the user. yes…how minimalistically…convert our code from one number system to other number system … your approach and code works…but doing extra drilling…అంతే… Quote
ARYA Posted February 25 Report Posted February 25 13 minutes ago, dasari4kntr said: bro… lets take 375…as input… while loop lo module 10 చేస్తే… 375%10=5 37%10=7 3%10=3 if push these values in stack while module …next while pop one by one first 3 comes…based on stack length you will know its in 100 position or thousands position…so here 3 is in houndred position and next 7 is tenths position…and last 5 is in digits position… not sure am i missing something..? def number_to_words(n): if n == 0: return "zero" ones = ["", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"] teens = ["", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"] tens = ["", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"] thousands = ["", "thousand", "million", "billion", "trillion"] # Step 1: Extract digits and push them into a stack stack = [] temp = n while temp > 0: stack.append(temp % 10) temp //= 10 # Step 2: Process digits from stack num_words = [] position = len(stack) # Determines the place value (hundreds, thousands, etc.) while stack: digit = stack.pop() position -= 1 # Decrease position to track place value # Handle Hundreds place if position % 3 == 2 and digit > 0: num_words.append(ones[digit] + " hundred") # Handle Tens and Ones place elif position % 3 == 1: if digit == 1 and stack: # Handle 'teens' case next_digit = stack.pop() position -= 1 num_words.append(teens[next_digit]) elif digit > 0: num_words.append(tens[digit]) # Handle Ones place elif position % 3 == 0 and digit > 0: num_words.append(ones[digit]) # Append thousand/million/billion as needed if position % 3 == 0 and position > 0: num_words.append(thousands[position // 3]) return " ".join(num_words) # Test cases print(number_to_words(375)) # Output: three hundred seventy five print(number_to_words(760000)) # Output: seven hundred sixty thousand print(number_to_words(123456789)) # Output: one hundred twenty three million four hundred fifty six thousand seven hundred eighty nine print(number_to_words(0)) # Output: zero Quote
dasari4kntr Posted February 25 Author Report Posted February 25 1 minute ago, ARYA said: def number_to_words(n): if n == 0: return "zero" ones = ["", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"] teens = ["", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"] tens = ["", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"] thousands = ["", "thousand", "million", "billion", "trillion"] # Step 1: Extract digits and push them into a stack stack = [] temp = n while temp > 0: stack.append(temp % 10) temp //= 10 # Step 2: Process digits from stack num_words = [] position = len(stack) # Determines the place value (hundreds, thousands, etc.) while stack: digit = stack.pop() position -= 1 # Decrease position to track place value # Handle Hundreds place if position % 3 == 2 and digit > 0: num_words.append(ones[digit] + " hundred") # Handle Tens and Ones place elif position % 3 == 1: if digit == 1 and stack: # Handle 'teens' case next_digit = stack.pop() position -= 1 num_words.append(teens[next_digit]) elif digit > 0: num_words.append(tens[digit]) # Handle Ones place elif position % 3 == 0 and digit > 0: num_words.append(ones[digit]) # Append thousand/million/billion as needed if position % 3 == 0 and position > 0: num_words.append(thousands[position // 3]) return " ".join(num_words) # Test cases print(number_to_words(375)) # Output: three hundred seventy five print(number_to_words(760000)) # Output: seven hundred sixty thousand print(number_to_words(123456789)) # Output: one hundred twenty three million four hundred fifty six thousand seven hundred eighty nine print(number_to_words(0)) # Output: zero nice…can you ask how to convert to indian number system…? i am expecting minimal changes… Quote
nag Posted February 25 Report Posted February 25 5 minutes ago, dasari4kntr said: yes…how minimalistically…convert our code from one number system to other number system … your approach and code works…but doing extra drilling…అంతే… bro.. don't know how time sensitive is your request.. basically I would start it a smaller code and enhance it in iterations.. naa coding style kooda adhey.. as i said I'm done coding for the code.. been a long day if you have time..i can give it you later 1 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.