Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters
- used in total.
- If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?
- NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115
- (one hundred and fifteen) contains 20 letters.
- The use of "and" when writing out numbers is in compliance with British usage.
- '''
- units = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
- string2 = ["eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"]
- decs = ["ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]
- thousand = "onethousand"
- AND = 3
- HUNDRED = 7
- # 1. From 1 to 9
- sum1 = sum(len(units[k]) for k in range(len(units)))
- print("sum1 = " + str(sum1))
- # 2. Only ten
- sum2 = len(decs[0])
- print("sum2 = " + str(sum2))
- # 3. From 11 to 19
- sum3 = sum(len(string2[k]) for k in range(len(string2)))
- print("sum3 = " + str(sum3))
- # 4. From 20 to 99
- sum4 = 0
- for i in range(1, len(decs)):
- sum4 += 10*len(decs[i]) # I use 10 times the words "twenty", "thirty", "forty", ......
- for j in range(len(units)):
- sum4 += 8*len(units[j]) # I use 8 times the words "one", "two", "three" when counting from 20 to 99
- print("sum4 = " + str(sum4)) # Example: 21, 31, 41, 51, 61, 71, 81, 91 ----> I use 8 times the "one"
- # SUM1TO99
- sum1to99 = sum1 + sum2 + sum3 + sum4
- print("sum1to99 = " + str(sum1to99))
- # 5. From 100 to 999, there are exactly 900 numbers
- # 5-a: Counting "and"
- counterAND = (900 - 9) * AND
- # 900 = all the numbers from 100 to 999
- # 9 = the numbers from 100 to 999 where I dont use "the and-word" ---> 100, 200, ...., 800, 900
- # 5-b: Counting "hundred"
- counterHUNDRED = 900 * HUNDRED
- # 5-c: Now, its time to count the real numbers (without "and" and "hundred")
- # I will use 9 more times the sum1to99
- counter1 = 9 * sum1to99
- # 5-d: Last task is to count the "one", "two", .... before the word "HUNDRED"
- counter2 = 0
- for i in range(len(units)):
- counter2 += 100*len(units[i]) # "One" is used 100 times from 100 to 199, "two" is used 100 times from 200 to 299
- sum100to999 = counter1 + counter2 + counterAND + counterHUNDRED
- print("sum100to999 = " + str(sum100to999))
- # 6. "1000"
- thous = len(thousand)
- print("thous = " + str(thous))
- # MAIN FUNCTION
- sumALL = sum1to99 + sum100to999 + thous
- print()
- print("SUM = " + str(sumALL))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement