 # fdellabetta's solution

## to Say in the C# Track

Published at Jan 17 2019 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution was written.

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".

## Running the tests

To run the tests, run the command `dotnet test` from within the exercise directory.

Initially, only the first test will be enabled. This is to encourage you to solve the exercise one step at a time. Once you get the first test passing, remove the `Skip` property from the next test and work on getting that test passing. Once none of the tests are skipped and they are all passing, you can submit your solution using `exercism submit Say.cs`

## Further information

For more detailed information about the C# track, including how to get help if you're having trouble, please visit the exercism.io C# language page.

## 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.

### SayTest.cs

``````// This file was auto-generated based on version 1.2.0 of the canonical data.

using System;
using Xunit;

public class SayTest
{
[Fact]
public void Zero()
{
Assert.Equal("zero", Say.InEnglish(0));
}

[Fact(Skip = "Remove to run test")]
public void One()
{
Assert.Equal("one", Say.InEnglish(1));
}

[Fact(Skip = "Remove to run test")]
public void Fourteen()
{
Assert.Equal("fourteen", Say.InEnglish(14));
}

[Fact(Skip = "Remove to run test")]
public void Twenty()
{
Assert.Equal("twenty", Say.InEnglish(20));
}

[Fact(Skip = "Remove to run test")]
public void Twenty_two()
{
Assert.Equal("twenty-two", Say.InEnglish(22));
}

[Fact(Skip = "Remove to run test")]
public void One_hundred()
{
Assert.Equal("one hundred", Say.InEnglish(100));
}

[Fact(Skip = "Remove to run test")]
public void One_hundred_twenty_three()
{
Assert.Equal("one hundred twenty-three", Say.InEnglish(123));
}

[Fact(Skip = "Remove to run test")]
public void One_thousand()
{
Assert.Equal("one thousand", Say.InEnglish(1000));
}

[Fact(Skip = "Remove to run test")]
public void One_thousand_two_hundred_thirty_four()
{
Assert.Equal("one thousand two hundred thirty-four", Say.InEnglish(1234));
}

[Fact(Skip = "Remove to run test")]
public void One_million()
{
Assert.Equal("one million", Say.InEnglish(1000000));
}

[Fact(Skip = "Remove to run test")]
public void One_million_two_thousand_three_hundred_forty_five()
{
Assert.Equal("one million two thousand three hundred forty-five", Say.InEnglish(1002345));
}

[Fact(Skip = "Remove to run test")]
public void One_billion()
{
Assert.Equal("one billion", Say.InEnglish(1000000000));
}

[Fact(Skip = "Remove to run test")]
public void A_big_number()
{
Assert.Equal("nine hundred eighty-seven billion six hundred fifty-four million three hundred twenty-one thousand one hundred twenty-three", Say.InEnglish(987654321123));
}

[Fact(Skip = "Remove to run test")]
public void Numbers_below_zero_are_out_of_range()
{
Assert.Throws<ArgumentOutOfRangeException>(() => Say.InEnglish(-1));
}

[Fact(Skip = "Remove to run test")]
public void Numbers_above_999_999_999_999_are_out_of_range()
{
Assert.Throws<ArgumentOutOfRangeException>(() => Say.InEnglish(1000000000000));
}
}``````
``````﻿using System;
using System.Collections.Generic;
using System.Linq;
public static class Say
{
private static string[] first = {
"zero","one","two","three","four","five","six","seven",
"eight","nine","ten","eleven","twelve","thirteen","fourteen",
"fifteen","sixteen","seventeen","eighteen","nineteen"
};
private static string[] ten = {
"NOTUSED","NOTUSED","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"
};
private static string[] big = { " billion", " million", " thousand"};
public static string InEnglish(long number)
{
if (number < 0 || number >= 1e12){
throw new ArgumentOutOfRangeException();
}
if (number < 20){
return first[number];
} else if (number < 100) {
var parts = new List<string> { ten[number / 10] };
if ( number % 10 != 0 ) parts.Add( first[number % 10] );
return  string.Join('-', parts.ToArray());
} else if (number < 1000){
var parts = new List<string> { first[number / 100] + " hundred" };
if ( number % 100 != 0 ) parts.Add( InEnglish( number % 100) );
return string.Join(' ', parts.ToArray());
} else {
var parts = new List<string>();
long first = 1000000000;
for ( var i = 0; i < 3; i++, number %= first, first /= 1000 ){
parts.Add( InEnglish(number / first ) + big[i] );
}
if (number != 0) parts.Add( InEnglish(number));
return string.Join(' ', parts.Where( c => !c.StartsWith("zero ")).ToArray() );
}
}
}``````