generated from mwc/problemset_numberwords
Numbers up to 100
I was a bit surprised when I opened this code and started to read more of the problem set because it involved writing out the numbers in a different way than I had put in my plan. Once I got going, I discovered both ways were pretty similar and this way involves breaking down the number less and in different ways, such as using the divide_with_remainder function rather than breaking down the number by each place value. This method also involves less string combination, which will make the code shorter. Ultimatley I found the coding to be pretty straightforawrd and so far am encountering less problems than compared to some of the labs. This is likely because the increased time spent on the labs has helped me to master the skills and I am able to more easily implement them here.
This commit is contained in:
parent
ddc79e7125
commit
237224805e
|
@ -15,19 +15,32 @@ TENS_NAMES = [
|
|||
]
|
||||
|
||||
def int_under_1000000_to_str(number):
|
||||
return "umm..."
|
||||
return None
|
||||
|
||||
def int_under_1000_to_str(number):
|
||||
return "umm..."
|
||||
return None
|
||||
|
||||
def int_under_100_to_str(number):
|
||||
return "umm..."
|
||||
if number <20:
|
||||
return int_under_20_to_str
|
||||
elif number <100:
|
||||
prefix = divide_with_remainder(number,10)
|
||||
if number % 10==0:
|
||||
return TENS_NAMES[prefix[0]]
|
||||
else:
|
||||
return (TENS_NAMES[prefix[0]]) + "-" +(DIGIT_NAMES[prefix[1]])
|
||||
|
||||
|
||||
def int_under_20_to_str(number):
|
||||
return "umm..."
|
||||
if number <10:
|
||||
return int_under_10_to_str(number)
|
||||
elif number <20:
|
||||
return TWEEN_AND_TEEN_NAMES[number-10]
|
||||
else:
|
||||
return "Error"
|
||||
|
||||
def int_under_10_to_str(number):
|
||||
return "umm..."
|
||||
return DIGIT_NAMES[number]
|
||||
|
||||
def divide_with_remainder(dividend, divisor):
|
||||
"""Divides one number by another, using whole-number division.
|
||||
|
|
4
nw.py
4
nw.py
|
@ -4,11 +4,11 @@
|
|||
# Ex: python nw.py 145
|
||||
|
||||
from argparse import ArgumentParser
|
||||
from numberwords import int_under_1000000_to_str
|
||||
from numberwords import int_under_100_to_str
|
||||
|
||||
parser = ArgumentParser("Print out a number as it is spoken in English.")
|
||||
parser.add_argument("number", type=int)
|
||||
args = parser.parse_args()
|
||||
text = int_under_1000000_to_str(args.number)
|
||||
text = int_under_100_to_str(args.number)
|
||||
print(text)
|
||||
|
||||
|
|
Loading…
Reference in New Issue