Avatar of mard

mard's solution

to Say in the C# Track

Published at Jun 13 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

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.Linq;

public static class Say
{
    private static readonly string[] EnglishGroup3 = new[] { "", "thousand", "million", "billion" };
    private static readonly string[] EnglishGroup1 = new[] { "", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };
    private static readonly string[] EnglishGroup0 = new[] { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
    private static readonly string EnglishHundred = "hundred";

    public static string InEnglish(long number)
    {
        if (number < 0 || number > 999999999999) throw new ArgumentOutOfRangeException("Number out of range");
        var value = string.Join(string.Empty,
            Enumerable.Range(0, EnglishGroup3.Length)
            .Reverse()
            .Select(x => ProcessEnglishGroup3(EnglishGroup3[x], (int)((number / SimplePow(1000, x)) % 1000)))
        ).Trim();
        return string.IsNullOrEmpty(value) ? EnglishGroup0[0] : value;
    }

    private static string ProcessEnglishGroup3(string suffix, int value)
    {
        if (value == 0) return string.Empty;

        var valueText = string.Empty;
        var hundreds = value / 100;
        if (hundreds > 0)
        {
            valueText = $" {EnglishGroup0[hundreds]} {EnglishHundred}";
            value = value % 100;
        }
        if (value >= 20)
        {
            valueText += $" {EnglishGroup1[value / 10]}";
            var ones = value % 10;
            if (ones > 0) valueText += $"-{EnglishGroup0[ones]}";
        }
        else if (value > 0)
        {
            valueText += $" {EnglishGroup0[value]}";
        }
        return $"{valueText} {suffix}".TrimEnd();
    }

    //helpers
    private static long SimplePow(int v, int x)
    {
        long result = 1;
        while (x-- > 0) result *= v;
        return result;
    }
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

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?
  • Are there new concepts here that you could read more about to improve your understanding?