 ## to Say in the C# Track

Published at Jul 22 2018 · 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.

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

## 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.1.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;
using System.Text.RegularExpressions;

/*
*   What I learn ?
*   1. Extract method while found a common pattern.
*
*  */
public static class Say
{
public static string InEnglish(long number)
{
if (number < 0 || number > 999_999_999_999) { throw new ArgumentOutOfRangeException(); }
if (number == 0L) { return "zero"; }
var numGroup = _engdecimal.SkipLast(1).Select(s =>
{
var div = number / s.Key;
number %= s.Key;
return (digit: div, eng: s.Value);
}).Where(d => d.digit > 0);
numGroup = numGroup.Append((number % 1000, ""));
var rrs = string.Empty;
foreach (var ng in numGroup)
{
rrs += StringBetweenOneToThousand(ng.digit, ng.eng);
}
return rrs.Trim();
}

private static string StringBetweenOneToThousand(long number, string eng)
{
var more100 = number / 100 * 100;
var metaNum = _engdecimal.Reverse().Select(e =>
{
var div = more100 / e.Key;
if (div >= 1)
{
more100 %= e.Key;
return (div: div, digit: e.Key, eng: e.Value);
}
return (0, -1, "");
}).Where(e => e.digit != -1);

var rrs = string.Empty;
foreach (var k in metaNum)
{
rrs += \$" {_engNum[k.div]} {k.eng}";
}
var less100 = number % 100;
metaNum = _engNum.Reverse().Select(e =>
{
var div = less100 / e.Key;
if (div >= 1)
{
less100 %= e.Key;
return (div: div, digit: e.Key, eng: e.Value);
}
return (0, -1, "");
}).Where(e => e.digit != -1);

foreach (var k in metaNum)
{
rrs += \$" {k.eng}";
}

rrs = Regex.Replace(rrs, @"(.*)(ty|teen)\s(.*)", @"\$1\$2-\$3");
return rrs + \$" {eng}";
}

private static Dictionary<long, string> _engdecimal
= new Dictionary<long, string> {
{1_000_000_000L, "billion" },
{1_000_000L, "million" },
{1_000L, "thousand" },
{100L, "hundred" },
};

private static Dictionary<long, string> _engNum
= new Dictionary<long, string> {
{1L, "one" },
{2L, "two" },
{3L, "three" },
{4L, "four" },
{5L, "five" },
{6L, "six" },
{7L, "seven" },
{8L, "eight" },
{9L, "nine" },
{10L, "ten" },
{11L, "eleven" },
{12L, "twelve" },
{13L, "thirteen" },
{14L, "fourteen" },
{15L, "fifteen" },
{16L, "sixteen" },
{17L, "seventeen" },
{18L, "eighteen" },
{19L, "nineteen" },
{20L, "twenty" },
{30L, "thirty" },
{40L, "forty" },
{50L, "fifty" },
{60L, "sixty" },
{70L, "seventy" },
{80L, "eighty" },
{90L, "ninety" },
{100L, "hundred" },
};
}``````