Avatar of tkalyoncuoglu

tkalyoncuoglu's solution

to Say in the C# Track

Published at Oct 01 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. 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.

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

public static class Say
{
    private static List<string> numbersBelow20 = new List<string>()
    {
        "zero",
        "one",
        "two",
        "three",
        "four",
        "five",
        "six",
        "seven",
        "eight",
        "nine",
        "ten",
        "eleven",
        "twelve",
        "thirteen",
        "fourteen",
        "fifteen",
        "sixteen",
        "seventeen",
        "eighteen",
        "nineteen"
    };

    private static List<string> powersOfThousand = new List<string>()
    {
        null,
        "thousand",
        "million",
        "billion"
    };

    private static List<string> multiplesOfTen = new List<string>
    {
        "twenty",
        "thirty",
        "forty",
        "fifty",
        "sixty",
        "seventy",
        "eighty",
        "ninety"
    };


    public static string InEnglish(long number)
    {
        if (number < 20)
            return numbersBelow20[(int)number];

        var numbersBelow100 = Enumerable.Range(0, 100).Select(GenerateNumber).ToList();

        var k = new List<string>();

        var o = number.ToString().Reverse().ToList();

        for (int i = 0; i < o.Count; i += 3)
        {
            var j = o.GetChunk(i, 3);

            j.Reverse();

            if (!j.AllZero())
                k.Add(powersOfThousand[i / 3]);

            if (j.Count == 1 || (j.Count == 2 && !j.AllZero(2)))
            {
                k.Add(numbersBelow100[Convert.ToInt32(string.Concat(j))]);
            }

            if (j.Count == 3 && !j.AllZero())
            {
                var g = j.GetChunk(1, 2);

                if (!(g[0] == '0' && g[1] == '0'))
                {
                    k.Add(numbersBelow100[Convert.ToInt32(string.Concat(g))]);
                }

                if (j[0] != '0')
                {
                    k.Add("hundred");

                    k.Add(numbersBelow20[Convert.ToInt32(j[0].ToString())]);
                }
            }
        }

        k.Reverse();

        var u = k.Aggregate((x, y) => !string.IsNullOrEmpty(y) ? x + " " + y : x);

        return u;

    }

    private static string GenerateNumber(int x)
    {
        return x < 20 ? numbersBelow20[x] : (multiplesOfTen[x / 10 - 2] + ((x % 10 != 0) ? "-" : "") + ((x % 10 != 0) ? numbersBelow20[x % 10] : ""));
    }

    private static List<char> GetChunk(this List<char> list, int i, int j)
    {
        return list.Skip(i).Take(j).ToList();
    }

    private static bool AllZero(this List<char> list, int? i = null)
    {
        int k = i ?? list.Count();

        return list.Take(k).All(x => x == '0');
    }
}

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?