dasari4kntr Posted February 25 Report Posted February 25 input: 375 output: three hundred seventy five ….….input: 760000 output: seven hundred sixty thousand Quote
nag Posted February 25 Report Posted February 25 bro.. peddha kastam emi kaadu.. ey pootaki inka opika ledhu. High schooler ki manchi coding exercise idhi below is from chatgpt def number_to_words(n): ones = ["", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"] tens = ["", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"] thousands = ["", "Thousand", "Million", "Billion", "Trillion", "Quadrillion", "Quintillion", "Sextillion", "Septillion", "Octillion", "Nonillion", "Decillion"] if n == 0: return "Zero" def three_digit_to_words(num): result = "" if num >= 100: result += ones[num // 100] + " Hundred" num %= 100 if num > 0: result += " " if num >= 20: result += tens[num // 10] num %= 10 if num > 0: result += " " if 0 < num < 20: result += ones[num] return result.strip() result = "" chunk_count = 0 while n > 0: chunk = n % 1000 if chunk > 0: if result: result = " " + result result = three_digit_to_words(chunk) + (" " + thousands[chunk_count] if thousands[chunk_count] else "") + result n //= 1000 chunk_count += 1 return result.strip() # Example usage num = int(input("Enter a number: ")) print(number_to_words(num)) ============================================ Crux idea is to extract each digit from the input number (using loop construct) and determine its place value ..like Units, tens, hundreds and so on .. we can use mod operand or % operand to extract the right most digit and use string operations.. similar excercise chesa recent gaa Quote
7691 Posted February 25 Report Posted February 25 https://leetcode.com/problems/integer-to-english-words/ Naa inspiration tho start aina AI ni adigithey idi cheppindi 1 Quote
kevinUsa Posted February 25 Report Posted February 25 from num2words import num2words # Function to convert number to words def number_to_words(): while True: try: # Take input from the user num = int(input("Enter a number: ")) # Convert the number to words words = num2words(num) print(f"Output: {words}") except ValueError: print("Please enter a valid number.") except KeyboardInterrupt: print("\nExiting...") break Quote
dasari4kntr Posted February 25 Author Report Posted February 25 1 minute ago, nag said: bro.. peddha kastam emi kaadu.. ey pootaki inka opika ledhu. High schooler ki manchi coding exercise idhi below is from chatgpt def number_to_words(n): ones = ["", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"] tens = ["", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"] thousands = ["", "Thousand", "Million", "Billion", "Trillion", "Quadrillion", "Quintillion", "Sextillion", "Septillion", "Octillion", "Nonillion", "Decillion"] if n == 0: return "Zero" def three_digit_to_words(num): result = "" if num >= 100: result += ones[num // 100] + " Hundred" num %= 100 if num > 0: result += " " if num >= 20: result += tens[num // 10] num %= 10 if num > 0: result += " " if 0 < num < 20: result += ones[num] return result.strip() result = "" chunk_count = 0 while n > 0: chunk = n % 1000 if chunk > 0: if result: result = " " + result result = three_digit_to_words(chunk) + (" " + thousands[chunk_count] if thousands[chunk_count] else "") + result n //= 1000 chunk_count += 1 return result.strip() # Example usage num = int(input("Enter a number: ")) print(number_to_words(num)) with this program…whats the output of 760001…? i am expecting…”seven hundred sixty thousand and one”… Quote
dasari4kntr Posted February 25 Author Report Posted February 25 3 minutes ago, kevinUsa said: from num2words import num2words # Function to convert number to words def number_to_words(): while True: try: # Take input from the user num = int(input("Enter a number: ")) # Convert the number to words words = num2words(num) print(f"Output: {words}") except ValueError: print("Please enter a valid number.") except KeyboardInterrupt: print("\nExiting...") break lol… 1 Quote
dasari4kntr Posted February 25 Author Report Posted February 25 6 minutes ago, nag said: bro.. peddha kastam emi kaadu.. ey pootaki inka opika ledhu. High schooler ki manchi coding exercise idhi below is from chatgpt def number_to_words(n): ones = ["", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"] tens = ["", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"] thousands = ["", "Thousand", "Million", "Billion", "Trillion", "Quadrillion", "Quintillion", "Sextillion", "Septillion", "Octillion", "Nonillion", "Decillion"] if n == 0: return "Zero" def three_digit_to_words(num): result = "" if num >= 100: result += ones[num // 100] + " Hundred" num %= 100 if num > 0: result += " " if num >= 20: result += tens[num // 10] num %= 10 if num > 0: result += " " if 0 < num < 20: result += ones[num] return result.strip() result = "" chunk_count = 0 while n > 0: chunk = n % 1000 if chunk > 0: if result: result = " " + result result = three_digit_to_words(chunk) + (" " + thousands[chunk_count] if thousands[chunk_count] else "") + result n //= 1000 chunk_count += 1 return result.strip() # Example usage num = int(input("Enter a number: ")) print(number_to_words(num)) ============================================ Crux idea is to extract each digit from the input number (using loop construct) and determine its place value ..like Units, tens, hundreds and so on .. we can use mod operand or % operand to extract the right most digit and use string operations.. similar excercise chesa recent gaa i dont understand this approach… simple గా… modular of base 10 ట్రై చేస్తే సరిపోదా…with additional logic… Quote
kevinUsa Posted February 25 Report Posted February 25 12 minutes ago, nag said: bro.. peddha kastam emi kaadu.. ey pootaki inka opika ledhu. High schooler ki manchi coding exercise idhi below is from chatgpt def number_to_words(n): ones = ["", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"] tens = ["", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"] thousands = ["", "Thousand", "Million", "Billion", "Trillion", "Quadrillion", "Quintillion", "Sextillion", "Septillion", "Octillion", "Nonillion", "Decillion"] if n == 0: return "Zero" def three_digit_to_words(num): result = "" if num >= 100: result += ones[num // 100] + " Hundred" num %= 100 if num > 0: result += " " if num >= 20: result += tens[num // 10] num %= 10 if num > 0: result += " " if 0 < num < 20: result += ones[num] return result.strip() result = "" chunk_count = 0 while n > 0: chunk = n % 1000 if chunk > 0: if result: result = " " + result result = three_digit_to_words(chunk) + (" " + thousands[chunk_count] if thousands[chunk_count] else "") + result n //= 1000 chunk_count += 1 return result.strip() # Example usage num = int(input("Enter a number: ")) print(number_to_words(num)) def num_to_words(n😞 ones = ['', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'] teens = ['ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'] tens = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'] thousands = ['', 'thousand', 'million', 'billion', 'trillion', 'quadrillion', 'quintillion', 'sextillion', 'septillion', 'octillion', 'nonillion', 'decillion', 'undecillion', 'duodecillion', 'tredecillion', 'quattuordecillion', 'quindecillion', 'sexdecillion', 'septendecillion', 'octodecillion', 'novemdecillion', 'vigintillion', 'unvigintillion', 'duovigintillion', 'tresvigintillion', 'quattuorvigintillion', 'quinvigintillion', 'sexvigintillion', 'septenvigintillion', 'octovigintillion', 'novemvigintillion', 'trigintillion', 'untrigintillion', 'duotrigintillion', 'trestrigintillion', 'quattuortrigintillion', 'quintrigintillion', 'sextrigintillion', 'septentrigintillion', 'octotrigintillion', 'novemtrigintillion', 'quadragintillion', 'unquadragintillion', 'duoquadragintillion', 'tresquadragintillion', 'quattuorquadragintillion', 'quinquadragintillion', 'sexquadragintillion', 'septenquadragintillion', 'octoquadragintillion', 'novemquadragintillion', 'quinquagintillion' ] def helper(num😞 if num < 10: return ones[num] elif num < 20: return teens[num - 10] elif num < 100: return tens[num // 10] + ('' if num % 10 == 0 else ' ' + ones[num % 10]) elif num < 1000: return ones[num // 100] + ' hundred' + ('' if num % 100 == 0 else ' ' + helper(num % 100)) result = '' i = 0 while n > 0: if n % 1000 != 0: result = helper(n % 1000) + ('' if result == '' else ' ') + thousands[i] + ('' if result == '' else ' ') + result n //= 1000 i += 1 return result.strip() def main(): num = int(input("Enter a number: ")) if num < 0: print("Please enter a non-negative integer.") else: print(num_to_words(num)) if __name__ == "__main__": main() Quote
ARYA Posted February 25 Report Posted February 25 20 minutes ago, dasari4kntr said: input: 375 output: three hundred seventy five ….….input: 760000 output: seven hundred sixty thousand You can use the inflect library in Python to convert numbers to words. Here’s a simple script to achieve this: import inflect def number_to_words(number): p = inflect.engine() return p.number_to_words(number, andword="").replace(",", "") # Test cases print(number_to_words(375)) # Output: three hundred seventy five print(number_to_words(760000)) # Output: seven hundred sixty thousand If you don’t have inflect installed, you can install it using: pip install inflect This script removes the default “and” that inflect adds (e.g., “three hundred and seventy five”) to match your expected format. Let me know if you need any modifications! 1 Quote
kevinUsa Posted February 25 Report Posted February 25 def num_to_words(n😞 ones = ['', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'] teens = ['ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'] tens = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'] thousands = ['', 'thousand', 'million', 'billion', 'trillion', 'quadrillion', 'quintillion', 'sextillion', 'septillion', 'octillion', 'nonillion', 'decillion', 'undecillion', 'duodecillion', 'tredecillion', 'quattuordecillion', 'quindecillion', 'sexdecillion', 'septendecillion', 'octodecillion', 'novemdecillion', 'vigintillion', 'unvigintillion', 'duovigintillion', 'tresvigintillion', 'quattuorvigintillion', 'quinvigintillion', 'sexvigintillion', 'septenvigintillion', 'octovigintillion', 'novemvigintillion', 'trigintillion', 'untrigintillion', 'duotrigintillion', 'trestrigintillion', 'quattuortrigintillion', 'quintrigintillion', 'sextrigintillion', 'septentrigintillion', 'octotrigintillion', 'novemtrigintillion', 'quadragintillion', 'unquadragintillion', 'duoquadragintillion', 'tresquadragintillion', 'quattuorquadragintillion', 'quinquadragintillion', 'sexquadragintillion', 'septenquadragintillion', 'octoquadragintillion', 'novemquadragintillion', 'quinquagintillion' ] def helper(num😞 if num < 10: return ones[num] elif num < 20: return teens[num - 10] elif num < 100: return tens[num // 10] + ('' if num % 10 == 0 else ' ' + ones[num % 10]) elif num < 1000: return ones[num // 100] + ' hundred' + ('' if num % 100 == 0 else ' ' + helper(num % 100)) result = '' i = 0 while n > 0: if n % 1000 != 0: result = helper(n % 1000) + ('' if result == '' else ' ') + thousands[i] + ('' if result == '' else ' ') + result n //= 1000 i += 1 return result.strip() def main(): num = int(input("Enter a number: ")) if num < 0: print("Please enter a non-negative integer.") else: print(num_to_words(num)) if __name__ == "__main__": main() Result 1234567890234567890234567890234567 one decillion two hundred thirty four nonillion five hundred sixty seven octillion eight hundred ninety septillion two hundred thirty four sextillion five hundred sixty seven quintillion eight hundred ninety quadrillion two hundred thirty four trillion five hundred sixty seven billion eight hundred ninety million two hundred thirty four thousand five hundred sixty seven Quote
dasari4kntr Posted February 25 Author Report Posted February 25 1 minute ago, kevinUsa said: def num_to_words(n😞 ones = ['', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'] teens = ['ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'] tens = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'] thousands = ['', 'thousand', 'million', 'billion', 'trillion', 'quadrillion', 'quintillion', 'sextillion', 'septillion', 'octillion', 'nonillion', 'decillion', 'undecillion', 'duodecillion', 'tredecillion', 'quattuordecillion', 'quindecillion', 'sexdecillion', 'septendecillion', 'octodecillion', 'novemdecillion', 'vigintillion', 'unvigintillion', 'duovigintillion', 'tresvigintillion', 'quattuorvigintillion', 'quinvigintillion', 'sexvigintillion', 'septenvigintillion', 'octovigintillion', 'novemvigintillion', 'trigintillion', 'untrigintillion', 'duotrigintillion', 'trestrigintillion', 'quattuortrigintillion', 'quintrigintillion', 'sextrigintillion', 'septentrigintillion', 'octotrigintillion', 'novemtrigintillion', 'quadragintillion', 'unquadragintillion', 'duoquadragintillion', 'tresquadragintillion', 'quattuorquadragintillion', 'quinquadragintillion', 'sexquadragintillion', 'septenquadragintillion', 'octoquadragintillion', 'novemquadragintillion', 'quinquagintillion' ] def helper(num😞 if num < 10: return ones[num] elif num < 20: return teens[num - 10] elif num < 100: return tens[num // 10] + ('' if num % 10 == 0 else ' ' + ones[num % 10]) elif num < 1000: return ones[num // 100] + ' hundred' + ('' if num % 100 == 0 else ' ' + helper(num % 100)) result = '' i = 0 while n > 0: if n % 1000 != 0: result = helper(n % 1000) + ('' if result == '' else ' ') + thousands[i] + ('' if result == '' else ' ') + result n //= 1000 i += 1 return result.strip() def main(): num = int(input("Enter a number: ")) if num < 0: print("Please enter a non-negative integer.") else: print(num_to_words(num)) if __name__ == "__main__": main() అన్నా…ఈ less than 20 and less than 100 ఏంది…? decimal system is base 10 kada… first digit nunch module with 10 chesukunta pothe vache శేషాన్ని…ఒక stack lo push చేసి…pop చేసేటప్పుడు ఆ verbage assign చెయ్యలేమా…? Quote
dasari4kntr Posted February 25 Author Report Posted February 25 3 minutes ago, ARYA said: You can use the inflect library in Python to convert numbers to words. Here’s a simple script to achieve this: import inflect def number_to_words(number): p = inflect.engine() return p.number_to_words(number, andword="").replace(",", "") # Test cases print(number_to_words(375)) # Output: three hundred seventy five print(number_to_words(760000)) # Output: seven hundred sixty thousand If you don’t have inflect installed, you can install it using: pip install inflect This script removes the default “and” that inflect adds (e.g., “three hundred and seventy five”) to match your expected format. Let me know if you need any modifications! not using libraries….i am asking you to write library… Quote
dasari4kntr Posted February 25 Author Report Posted February 25 this is simple example how AI based code solutions works… Quote
ARYA Posted February 25 Report Posted February 25 6 minutes ago, dasari4kntr said: not using libraries….i am asking you to write library… 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 Quote
Anta Assamey Posted February 25 Report Posted February 25 num2words module install chesukoni...taruvata.. from num2words import num2words def number_to_words(n): return num2words(n).replace(',', '') 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.