# rghack's solution

## to Say in the Rust Track

Published at Mar 27 2021 · 0 comments
Instructions
Test suite
Solution

Given a number from 0 to 999,999,999,999, spell out that number in English.

## Step 1

Handle the basic case of 0 through 99.

If the input to the program is 22, then the output should be 'twenty-two'.

Your program should complain loudly if given a number outside the blessed range.

Some good test cases for this program are:

• 0
• 14
• 50
• 98
• -1
• 100

### Extension

If you're on a Mac, shell out to Mac OS X's say program to talk out loud. If you're on Linux or Windows, eSpeakNG may be available with the command espeak.

## Step 2

Implement breaking a number up into chunks of thousands.

So 1234567890 should yield a list like 1, 234, 567, and 890, while the far simpler 1000 should yield just 1 and 0.

The program must also report any values that are out of range.

## Step 3

Now handle inserting the appropriate scale word between those chunks.

So 1234567890 should yield '1 billion 234 million 567 thousand 890'

The program must also report any values that are out of range. It's fine to stop at "trillion".

## Step 4

Put it all together to get nothing but plain English.

12345 should give twelve thousand three hundred forty-five.

The program must also report any values that are out of range.

### Extensions

Use and (correctly) when spelling out the number in English:

• 14 becomes "fourteen".
• 100 becomes "one hundred".
• 120 becomes "one hundred and twenty".
• 1002 becomes "one thousand and two".
• 1323 becomes "one thousand three hundred and twenty-three".

## Rust Specific Exercise Notes

This is slightly changed in the Rust version, compared to other language versions of this exercise. Instead of requiring you to return errors for out of range, we are using Rust's strong type system to limit input. It is much easier to make a function deal with all valid inputs, rather than requiring the user of your module to handle errors.

There is a -1 version of a test case, but it is commented out. If your function is implemented properly, the -1 test case should not compile.

Adding 'and' into number text has not been implemented in test cases.

### Extension

Add capability of converting up to the max value for u64: 9,223,372,036,854,775,807.

For hints at the output this should have, look at the last test case.

## Rust Installation

Refer to the exercism help page for Rust installation and learning resources.

## Writing the Code

Execute the tests with:

\$ cargo test

All but the first test have been ignored. After you get the first test to pass, open the tests source file which is located in the tests directory and remove the #[ignore] flag from the next test and get the tests to pass again. Each separate test is a function with #[test] flag above it. Continue, until you pass every test.

If you wish to run all ignored tests without editing the tests source file, use:

\$ cargo test -- --ignored

To run a specific test, for example some_test, you can use:

\$ cargo test some_test

If the specific test is ignored use:

\$ cargo test some_test -- --ignored

## Further improvements

After you have solved the exercise, please consider using the additional utilities, described in the installation guide, to further refine your final solution.

To format your solution, inside the solution directory use

cargo fmt

To see, if your solution contains some common ineffective use cases, inside the solution directory use

cargo clippy --all-targets

## Submitting the solution

Generally you should submit all files in which you implemented your solution (src/lib.rs in most cases). If you are using any external crates, please consider submitting the Cargo.toml file. This will make the review process faster and clearer.

## Feedback, Issues, Pull Requests

The exercism/rust repository on GitHub is the home for all of the Rust exercises. If you have feedback about an exercise, or want to help implement new exercises, head over there and create an issue. Members of the rust track team are happy to help!

If you want to know more about Exercism, take a look at the contribution guide.

## Source

A variation on JavaRanch CattleDrive, exercise 4a http://www.javaranch.com/say.jsp

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### say.rs

// Note: No tests created using 'and' with numbers.
// Apparently Most American English does not use the 'and' with numbers,
// where it is common in British English to use the 'and'.

#[test]
fn test_zero() {
assert_eq!(say::encode(0), String::from("zero"));
}

//
// If the below test is uncommented, it should not compile.
//
/*
#[test]
#[ignore]
fn test_negative() {
assert_eq!(say::encode(-1), String::from("won't compile"));
}
*/

#[test]
#[ignore]
fn test_one() {
assert_eq!(say::encode(1), String::from("one"));
}

#[test]
#[ignore]
fn test_fourteen() {
assert_eq!(say::encode(14), String::from("fourteen"));
}

#[test]
#[ignore]
fn test_twenty() {
assert_eq!(say::encode(20), String::from("twenty"));
}

#[test]
#[ignore]
fn test_twenty_two() {
assert_eq!(say::encode(22), String::from("twenty-two"));
}

#[test]
#[ignore]
fn test_one_hundred() {
assert_eq!(say::encode(100), String::from("one hundred"));
}

// note, using American style with no and
#[test]
#[ignore]
fn test_one_hundred_twenty() {
assert_eq!(say::encode(120), String::from("one hundred twenty"));
}

#[test]
#[ignore]
fn test_one_hundred_twenty_three() {
assert_eq!(say::encode(123), String::from("one hundred twenty-three"));
}

#[test]
#[ignore]
fn test_one_thousand() {
assert_eq!(say::encode(1000), String::from("one thousand"));
}

#[test]
#[ignore]
fn test_one_thousand_two_hundred_thirty_four() {
assert_eq!(
say::encode(1234),
String::from("one thousand two hundred thirty-four")
);
}

// note, using American style with no and
#[test]
#[ignore]
fn test_eight_hundred_and_ten_thousand() {
assert_eq!(
say::encode(810_000),
String::from("eight hundred ten thousand")
);
}

#[test]
#[ignore]
fn test_one_million() {
assert_eq!(say::encode(1_000_000), String::from("one million"));
}

// note, using American style with no and
#[test]
#[ignore]
fn test_one_million_two() {
assert_eq!(say::encode(1_000_002), String::from("one million two"));
}

#[test]
#[ignore]
fn test_1002345() {
assert_eq!(
say::encode(1_002_345),
String::from("one million two thousand three hundred forty-five")
);
}

#[test]
#[ignore]
fn test_one_billion() {
assert_eq!(say::encode(1_000_000_000), String::from("one billion"));
}

#[test]
#[ignore]
fn test_987654321123() {
assert_eq!(
say::encode(987_654_321_123),
String::from(
"nine hundred eighty-seven billion \
six hundred fifty-four million \
three hundred twenty-one thousand \
one hundred twenty-three"
)
);
}

/*
These tests are only if you implemented full parsing for u64 type.
*/
#[test]
#[ignore]
fn test_max_i64() {
assert_eq!(
say::encode(9_223_372_036_854_775_807),
String::from(
"nine quintillion two hundred twenty-three \
quadrillion three hundred seventy-two trillion \
thirty-six billion eight hundred fifty-four million \
seven hundred seventy-five thousand eight hundred seven"
)
);
}

#[test]
#[ignore]
fn test_max_u64() {
assert_eq!(
say::encode(18_446_744_073_709_551_615),
String::from(
"eighteen quintillion four hundred forty-six \
quadrillion seven hundred forty-four trillion \
seventy-three billion seven hundred nine million \
five hundred fifty-one thousand six hundred fifteen"
)
);
}
fn encode_u(n: u64) -> String {
match n {
1 => "one".to_string(),
2 => "two".to_string(),
3 => "three".to_string(),
4 => "four".to_string(),
5 => "five".to_string(),
6 => "six".to_string(),
7 => "seven".to_string(),
8 => "eight".to_string(),
9 => "nine".to_string(),
_ => "bye".to_string(),
}
}

fn encode_cdu(n: u64) -> String {
match (n / 100, n % 100) {
(0, _) => encode_du(n % 100),
(_, 0) => encode_u(n / 100) + " hundred ",
//(_,_) => encode_u(n/100) + " hundred and " + &encode_du(n%100),
(_, _) => encode_u(n / 100) + " hundred " + &encode_du(n % 100),
}
}

fn encode_du(n: u64) -> String {
match n {
0..=99 => {
let d = match n / 10 {
0 => "".to_string(),
1 => "".to_string(),
2 => "twenty".to_string(),
3 => "thirty".to_string(),
4 => "forty".to_string(),
5 => "fifty".to_string(),
6 => "sixty".to_string(),
7 => "seventy".to_string(),
8 => "eighty".to_string(),
9 => "ninety".to_string(),
_ => "bye".to_string(),
};
let s = match (n / 10, n % 10) {
(_, 0) | (0, _) | (1, _) => "",
(_, _) => "-",
};
let u = match (n / 10, n % 10) {
(1, 0) => "ten".to_string(),
(1, 1) => "eleven".to_string(),
(1, 2) => "twelve".to_string(),
(1, 3) => "thirteen".to_string(),
(1, 4) => "fourteen".to_string(),
(1, 5) => "fifteen".to_string(),
(1, 6) => "sixteen".to_string(),
(1, 7) => "seventeen".to_string(),
(1, 8) => "eighteen".to_string(),
(1, 9) => "nineteen".to_string(),
(0, 0) => "zero".to_string(),
(_, 0) => "".to_string(),
(_, _) => encode_u(n % 10),
};
d + s + &u
}
_ => "INCORRECT NUMBER".to_string(),
}
}

fn split_thousands(n: u64) -> Vec<u64> {
let mut ret = Vec::new();
let mut n = n;
if n == 0 {
ret.push(0);
} else {
while n / 1000 != 0 {
ret.push(n % 1000);
n /= 1000;
}
if n != 0 {
ret.push(n);
}
}
ret.into_iter().rev().collect()
}

fn intercalate(v: Vec<u64>) -> Vec<String> {
let mut ret = Vec::new();
let scales = vec![
"quintillion",
"trillion",
"billion",
"million",
"thousand",
"",
];
let mut scales_idx = scales.len() - v.len();
if v.len() == 1 {
ret.push(encode_cdu(v[0]));
} else {
for n in v {
if n != 0 {
ret.push(encode_cdu(n));
ret.push(scales[scales_idx].to_string());
}
scales_idx += 1;
}
}
ret
}

pub fn encode(n: u64) -> String {
intercalate(split_thousands(n))
.into_iter()
.fold("".to_string(), |x, y| x + " " + &y)
.trim()
.to_string()
}

### What can you learn from this solution?

A huge amount can be learned from reading other peopleโs code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

• What compromises have been made?