patternpythonMinor
Numbers to Strings in Python part 2
Viewed 0 times
numbersstringspythonpart
Problem
After implementing most of the suggestions from the first question, This is the code I got:
```
from math import log
zero_to_nineteen = ("zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine",
"ten", "eleven", "twelve", "thirteen", "fourteen",
"fifteen", "sixteen", "seventeen", "eighteen", "nineteen")
tens = ("zero", "ten", "twenty", "thirty", "forty",
"fifty", "sixty", "seventy", "eighty", "ninety")
# Powers of a thousand (US system of naming)
suffixes = ("zero", "thousand", "million", "billion", "trillion",
"quadrillion", "quintillion", "sextillion", "septillion",
"octillion", "nonillion", "decillion", "undecillion",
"duodecillion", "tredecillion", "quattuordecillion",
"quinquadecillion", "sedecillion", "septendecillion",
"octodecillion", "novendecillion", "vigintillion",
"unvigintillion", "duovigintillion", "tresvigintillion",
"quattuorvigintillion", "quinquavigintillion", "sesvigintillion",
"septemvigintillion", "octovigintillion", "novemvigintillion",
"trigintillion", "untrigintillion", "duotrigintillion",
"trestrigintilion", "quattuortrigintillion",
"quinquatrigintillion", "sestrigintillion", "septentrigintillion",
"octotrigintillion", "noventrigintillion", "quadragintillion")
def spell_out(number):
"""Returns a string representation of the number, in the US system"""
try:
number = int(number)
except OverflowError:
# This will be triggered with large float values such as 1e584
return "infinity"
if number < 0:
return "negative " + spell_out(-1 * number)
if number < 20:
return zero_to_nineteen[number]
if number < 100:
tens_digit, ones_digit = divmod(number, 10)
return _createNumber(tens[tens_digit], ones_digit, "-")
if number < 1000:
```
from math import log
zero_to_nineteen = ("zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine",
"ten", "eleven", "twelve", "thirteen", "fourteen",
"fifteen", "sixteen", "seventeen", "eighteen", "nineteen")
tens = ("zero", "ten", "twenty", "thirty", "forty",
"fifty", "sixty", "seventy", "eighty", "ninety")
# Powers of a thousand (US system of naming)
suffixes = ("zero", "thousand", "million", "billion", "trillion",
"quadrillion", "quintillion", "sextillion", "septillion",
"octillion", "nonillion", "decillion", "undecillion",
"duodecillion", "tredecillion", "quattuordecillion",
"quinquadecillion", "sedecillion", "septendecillion",
"octodecillion", "novendecillion", "vigintillion",
"unvigintillion", "duovigintillion", "tresvigintillion",
"quattuorvigintillion", "quinquavigintillion", "sesvigintillion",
"septemvigintillion", "octovigintillion", "novemvigintillion",
"trigintillion", "untrigintillion", "duotrigintillion",
"trestrigintilion", "quattuortrigintillion",
"quinquatrigintillion", "sestrigintillion", "septentrigintillion",
"octotrigintillion", "noventrigintillion", "quadragintillion")
def spell_out(number):
"""Returns a string representation of the number, in the US system"""
try:
number = int(number)
except OverflowError:
# This will be triggered with large float values such as 1e584
return "infinity"
if number < 0:
return "negative " + spell_out(-1 * number)
if number < 20:
return zero_to_nineteen[number]
if number < 100:
tens_digit, ones_digit = divmod(number, 10)
return _createNumber(tens[tens_digit], ones_digit, "-")
if number < 1000:
Solution
It looks pretty nice! I can only nitpick.
Instead of:
You could simply:
Some PEP8 recommendations:
Instead of:
return "negative " + spell_out(-1 * number)You could simply:
return "negative " + spell_out(-number)Some PEP8 recommendations:
- Use 2 blank lines before each function definition
- Use snake case in function names,
_create_numberinstead of_createNumber
Code Snippets
return "negative " + spell_out(-1 * number)return "negative " + spell_out(-number)Context
StackExchange Code Review Q#59837, answer score: 2
Revisions (0)
No revisions yet.