Skip to content

Instantly share code, notes, and snippets.

@Tsunamicom
Last active February 4, 2019 10:55
Show Gist options
  • Save Tsunamicom/a02e0ca7bd70fc839e94348492aa7b84 to your computer and use it in GitHub Desktop.
Save Tsunamicom/a02e0ca7bd70fc839e94348492aa7b84 to your computer and use it in GitHub Desktop.
Represent a number string as verbose
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Linq;
public static class Program
{
static void Main()
{
using (StreamReader reader = new StreamReader(Console.OpenStandardInput()))
while (!reader.EndOfStream)
{
string line = reader.ReadLine();
//Debug.WriteLine(VerboseNumber("1")); // OneDollars (always represent as Plural)
//Debug.WriteLine(VerboseNumber("12")); // TwelveDollars
//Debug.WriteLine(VerboseNumber("123")); // OneHundredTwentyThreeDollars
//Debug.WriteLine(VerboseNumber("1234")); // OneThousandTwoHundredThirtyFourDollars
//Debug.WriteLine(VerboseNumber("123456789")); // OneHundredTwentyThreeMillionFourHundredFiftySixThousandSevenHundredEightyNine
//Debug.WriteLine(VerboseNumber("100000000")); // OneHundredMillionDollars
//Debug.WriteLine(VerboseNumber("100000399")); // OneHundredMillionThreeHundredNinetyNineDollars
//Debug.WriteLine(VerboseNumber("100000099")); // OneHundredMillionNinetyNineDollars
//Debug.WriteLine(VerboseNumber("0")); // ZeroDollars
Console.WriteLine(VerboseNumber(line));
}
}
static readonly Dictionary<string, string> ones = new Dictionary<string, string>()
{
{ "0", ""},
{ "1", "One"},
{ "2", "Two"},
{ "3", "Three"},
{ "4", "Four" },
{ "5", "Five" },
{ "6", "Six"},
{ "7", "Seven"},
{ "8", "Eight" },
{ "9", "Nine"}
};
static readonly Dictionary<string, string> tens = new Dictionary<string, string>()
{
{ "10", "Ten"},
{ "11", "Eleven"},
{ "12", "Twelve"},
{ "13", "Thirteen"},
{ "14", "Fourteen"},
{ "15", "Fifteen"},
{ "16", "Sixteen"},
{ "17", "Seventeen"},
{ "18", "Eighteen"},
{ "19", "Nineteen" }
};
static readonly Dictionary<string, string> tensSuper = new Dictionary<string, string>()
{
{ "0", "" },
{ "2", "Twenty"},
{ "3", "Thirty"},
{ "4", "Forty"},
{ "5", "Fifty"},
{ "6", "Sixty"},
{ "7", "Seventy"},
{ "8", "Eighty"},
{ "9", "Ninety"}
};
static string VerboseNumber(string line)
{
if (line == "0")
{
return "ZeroDollars";
}
// Given a number, represent as verbose
// Ex: 123 would be OneHundredTwentyThreeDollars
StringBuilder verboseNumString = new StringBuilder();
// handle each set of 3
List<string> lineSets = GetLineSets(line);
for (int i = lineSets.Count - 1; i >= 0; i--)
{
string handleString = lineSets[i];
//Debug.WriteLine(handleString);
char[] handleChars = handleString.ToCharArray();
if (handleChars.All(c => c == '0'))
{
continue;
}
verboseNumString.Append(HandleHundreds(handleChars));
verboseNumString.Append(HandleTeens(handleChars));
verboseNumString.Append(HandlePrefix(i));
}
return $"{verboseNumString}Dollars";
}
public static string HandlePrefix(int idx)
{
switch (idx)
{
case 1:
{
return "Thousand";
}
case 2:
{
return "Million";
}
case 3:
{
return "Billion";
}
}
return string.Empty;
}
public static string HandleTeens(char[] handleChars)
{
int charSize = handleChars.Length;
if (charSize >= 2)
{
string teen = $"{handleChars[charSize - 2]}{handleChars[charSize - 1]}"; ;
if (teen.Substring(0, 1) == "1")
{
return tens[teen];
}
else
{
return $"{tensSuper[teen.Substring(0, 1)]}{ones[teen.Substring(1, 1)]}";
}
}
else
{
return $"{ones[handleChars[0].ToString()]}";
}
}
public static string HandleHundreds(char[] handleChars)
{
if (handleChars.Length == 3)
{
if (handleChars[0] != '0')
{
return $"{ones[handleChars[0].ToString()]}Hundred";
}
}
return string.Empty;
}
public static List<string> GetLineSets(string line)
{
char[] lineChars = line.ToCharArray();
// handle each set of 3
List<string> lineSets = new List<string>();
string currSet = string.Empty;
for (int i = lineChars.Length - 1; i >= 0; i--)
{
currSet += lineChars[i];
if ((currSet.Length == 3) || (i == 0))
{
char[] tmpArr = currSet.ToCharArray();
Array.Reverse(tmpArr);
currSet = String.Join("", tmpArr);
lineSets.Add(currSet);
currSet = string.Empty;
}
}
return lineSets;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment