-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path17.rs
113 lines (92 loc) · 2.8 KB
/
17.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
Problem 17 - Number letter counts
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.
*/
use phf::phf_map;
use std::ops::Div;
const TRANSLATIONS: phf::Map<u16, &'static str> = phf_map! {
0u16 => "",
1u16 => "one",
2u16 => "two",
3u16 => "three",
4u16 => "four",
5u16 => "five",
6u16 => "six",
7u16 => "seven",
8u16 => "eight",
9u16 => "nine",
10u16 => "ten",
11u16 => "eleven",
12u16 => "twelve",
13u16 => "thirteen",
14u16 => "fourteen",
15u16 => "fifteen",
16u16 => "sixteen",
17u16 => "seventeen",
18u16 => "eighteen",
19u16 => "nineteen",
20u16 => "twenty",
30u16 => "thirty",
40u16 => "forty",
50u16 => "fifty",
60u16 => "sixty",
70u16 => "seventy",
80u16 => "eighty",
90u16 => "ninety"
};
fn number_to_words(number: usize) -> String {
let mut n = number.clone();
let mut out = String::new();
// Thousands
let thousands = n.div(1000);
n -= thousands * 1000;
if thousands > 0 {
out = format!("{}thousand", TRANSLATIONS.get(&(thousands as u16)).unwrap());
}
// Hundreds
let hundreds = n.div(100);
n -= hundreds * 100;
if hundreds > 0 {
out = format!(
"{out}{}hundred",
TRANSLATIONS.get(&(hundreds as u16)).unwrap()
);
}
// Tens
let tens = n.div(10);
n -= tens * 10;
if tens > 0 {
let translation: &str;
let and = if hundreds > 0 { "and" } else { "" };
if n % 10 > 0 && tens == 1 {
translation = TRANSLATIONS.get(&((10 + (n % 10)) as u16)).unwrap();
n -= n % 10;
} else {
translation = TRANSLATIONS.get(&(tens as u16 * 10)).unwrap();
}
out = format!("{out}{and}{}", translation);
}
// Remainder
if n > 0 {
let and = if hundreds > 0 && tens <= 0 { "and" } else { "" };
out = format!("{out}{and}{}", TRANSLATIONS.get(&(n as u16)).unwrap());
}
out
}
fn letter_count(upper_bound: usize) -> usize {
let mut sum = 0;
for i in 1..(upper_bound + 1) {
println!("{} {}", number_to_words(i), number_to_words(i).len());
sum += number_to_words(i).len();
}
sum
}
pub fn main() {
let sum = letter_count(1000);
println!(
"{} letters would be used to write out all of the numbers from 1 to 1000!",
sum
);
}