File /srv/datasets/number_names.txt (also available via URL here) contains information on the (U.S. English) names we give
to different numeric values. Take a look at the file to get a feel for its contents.
For the purposes of this assignment, all values between 0 and 999 constitute one order of magnitude, and all other orders increase by a
factor of one thousand, e.g. thousands (103), millions (106), billions (1012), etc.
You shall define a function named verbalize, in a module named verbalize.
1. The function shall accept one argument, assumed to be a nonnegative int, and shall return a list[str] containing
the verbal equivalent of each order of magnitude of the argument
, as described in the following code and
2. Make sure your module loads data from number_names.txt only once, regardless of how many
times verbalize() is called.
3. Make sure you solve this problem recursively: You are allowed one loop total in the module (for or while) without
penalty, which you will probably use for loading the contents of the number_names.txt file. Using recursion and
built-in functions like next(), reversed(), sorted(), etc. will allow you to accomplish the rest without
loops. Each subsequent loop will reduce your grade by 20%.
def verbalize(value: int) -> list[str]:
Returns a list of the verbalized orders of magnitude of a natural number, e.g.:
verbalize(0) –> [‘zero’]
verbalize(42) –> [‘forty-two’]
verbalize(101) –> [‘one hundred one’]
verbalize(9999) –> [‘nine thousand’, ‘nine hundred ninety-nine’]
[‘one million’, ‘two hundred thirty-four thousand’, ‘five hundred sixty-seven’]
Since you are to solve this problem recursively, remember to think in terms of base cases (where you can immediately
return a result) and recursive steps (where you make recursive calls that change the arguments in order to proceed
toward a base case).
Hint: If the number is 123456, consider how you might verbalize the number of thousands:
1. Determining the count is simple division: 123456 // 1000 == 123
2. You know the order of magnitude is named thousand. If your function works correctly, you could make a
recursive call with 123 as the argument and receive the return value [‘one hundred twenty-three’].
3. The remaining verbalization should be of the value 123456 % 1000 == 456
本网站支持 Alipay WeChatPay PayPal等支付方式
E-mail: firstname.lastname@example.org 微信号:vipnxx