Here is the link.
First practice - C# - Check list - Take more than one hour
Dec. 30, 2020
Introduction
I came cross the algorithm in Leetcode premium Facebook mock onsite, last algorithm in 4. I spent over 35 minutes but I could not finish it.
Check list
I worked on coding using Visual studio, make sure that the code works for "123", "123456", "1234567890".
Here are the check list to record my learning:
- Design digts, secondDigit, teens array, every thing should start from index 0 to 9 to match digit 0 to 9, easy to call.
- More on item 1, I added "Zero" into digits, secondDigit, missing Eighteen in teens array;
- Failed test case 0, add code to return "Zero";
- Work on space to separate word; Call C# string.TrimEnd, assign to a variable before return;
- Failed edge case "millon thousand", so I added the array called zeroChecking;
- I tried to make the code easy to write, so I design a while loop using repeat, for million three digits, thousand three digits, rightmost three digits.
- The integer is converted to a C# List, so it is always to work on first char in the list, since List.RemoveAt is called after each visit.
- Logic of length > 8 meaning length at least 9, which should not be length >= 8.
- Ideas to improve - I need to think about ideas to code in less than 20 minutes.
public class Solution {
public string NumberToWords(int num) {
var chars = num.ToString().ToCharArray();
var expr = new StringBuilder();
var digits = new string[] {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine" };
var secondDigit = new string[] {"Zero","Ten","Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };
var teens = new string[] { "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen","Nineteen" };
var length = chars.Length;
if(num == 0)
return "Zero";
var list = new List<char>(chars);
if (length > 9)
{
var digit = list[0] - '0';
if (digit >= 1)
{
expr.Append(digits[digit] + " Billion ");
}
list.RemoveAt(0);
}
int repeat = 0;
var zeroChecking = new int[3];
while (repeat < 3)
{
if ((repeat == 0 && length > 8) ||
(repeat == 1 && length > 5) ||
(repeat == 2 && length > 2)
)
{
var digit = list[0] - '0';
if (digit >= 1)
{
expr.Append(digits[digit] + " Hundred ");
zeroChecking[repeat] = 1;
}
list.RemoveAt(0);
}
var isTeen = false;
if ((repeat == 0 && length > 7) ||
(repeat == 1 && length > 4) ||
(repeat == 2 && length > 1))
{
var digit = list[0] - '0';
if (digit > 1)
{
expr.Append(secondDigit[digit] + " ");
zeroChecking[repeat] = 1;
}
else if (digit == 1)
{
// expr.Append(secondDigit[digit]);
isTeen = true;
zeroChecking[repeat] = 1;
}
list.RemoveAt(0);
}
if ((repeat == 0 && length > 6) ||
(repeat == 1 && length > 3) ||
(repeat == 2 && length >= 0))
{
var digit = list[0] - '0';
if(digit > 0)
{
zeroChecking[repeat] = 1;
}
if (isTeen)
{
expr.Append(teens[digit] + " ");
}
else if (digit > 0)
{
expr.Append(digits[digit] + " ");
}
list.RemoveAt(0);
}
if (repeat == 0 && length > 6)
{
if(zeroChecking[repeat] > 0)
expr.Append("Million ");
}
else if (repeat == 1 && length > 3)
{
if(zeroChecking[repeat] > 0)
expr.Append("Thousand ");
}
repeat++;
}
var trimed = expr.ToString().TrimEnd();
return trimed;
}
}
No comments:
Post a Comment