Skip to content

Latest commit

 

History

History
231 lines (169 loc) · 7.42 KB

bonus10Logic.md

File metadata and controls

231 lines (169 loc) · 7.42 KB

Code Recap:

class Solution {
    // Arrays for single digits, tens, and numbers between 10 and 19
    private static final String[] ones = { "", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
    private static final String[] tens = { "", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
    private static final String[] thousands = { "", "Thousand", "Million", "Billion", "Trillion"};

    // Helper function to convert numbers less than 1000 into words
    private String convertLessThan1000(int num) {
        if (num == 0) {
            return "";
        }
        
        String result = "";

        if (num >= 100) {
            result += ones[num / 100] + " Hundred ";
            num %= 100;
        }

        if (num >= 20) {
            result += tens[num / 10] + " ";
            num %= 10;
        }

        if (num > 0) {
            result += ones[num] + " ";
        }

        return result.trim();
    }

    // Main function to convert the number to words
    public String convertToWords(int n) {
        if (n == 0) {
            return "Zero";
        }

        String result = "";

        int i = 0;
        while (n > 0) {
            if (n % 1000 != 0) {
                result = convertLessThan1000(n % 1000) + " " + thousands[i] + " " + result;
            }
            n /= 1000;
            i++;
        }

        // Remove trailing space
        return result.trim();
    }
}

Let's perform a dry run of the code you provided, which converts an integer to its English words representation, for better understanding.

Code Overview:

This code contains:

  1. Three arrays (ones, tens, thousands) that help map numbers to their English equivalents.

    • ones[] holds English words for numbers 0 to 19.
    • tens[] holds English words for multiples of Ten from 20 onward (20, 30, 40, ..., 90).
    • thousands[] holds English words for large place values like Thousand, Million, Billion, etc.
  2. The helper function convertLessThan1000(int num) is responsible for converting any number less than 1000 to English words.

  3. The convertToWords(int n) method processes the number, breaking it down into groups of three digits (representing thousands, millions, etc.) and converts each group using the helper function.

Dry Run:

Let's dry-run the function with different example inputs.


Example 1: n = 12345

Step-by-Step Breakdown:

  1. The convertToWords() function is called with n = 12345.

  2. Initialization:

    • result = "" (This will hold the final result.)
    • i = 0 (This tracks the thousands, millions, etc. placeholders.)
  3. First iteration (n = 12345):

    • n % 1000 = 345, which is not 0, so we call convertLessThan1000(345).
      • convertLessThan1000(345):
        • 345 >= 100, so it adds "Three Hundred ".
        • Then, 345 % 100 = 45.
        • 45 >= 20, so it adds "Forty ".
        • Finally, 45 % 10 = 5, so it adds "Five ".
        • The result of convertLessThan1000(345) is "Three Hundred Forty Five".
    • thousands[i] = "" (empty string, because this is for the "ones" place).
    • The result for this iteration is: "Three Hundred Forty Five ".
  4. Update:

    • n /= 1000, so now n = 12.
    • i++, so now i = 1.
  5. Second iteration (n = 12):

    • n % 1000 = 12, which is not 0, so we call convertLessThan1000(12).
      • convertLessThan1000(12):
        • Since 12 <= 19, it directly adds "Twelve".
      • thousands[i] = "Thousand".
      • The result for this iteration is: "Twelve Thousand ".
  6. Update:

    • n /= 1000, so now n = 0.
    • i++, so now i = 2.
  7. Termination:

    • Since n = 0, the while loop terminates.
  8. Final result:

    • Combine the results from both iterations: "Three Hundred Forty Five Twelve Thousand ".
    • Remove the trailing spaces: "Twelve Thousand Three Hundred Forty Five".

Output: "Twelve Thousand Three Hundred Forty Five".


Example 2: n = 1001

Step-by-Step Breakdown:

  1. The convertToWords() function is called with n = 1001.

  2. Initialization:

    • result = ""
    • i = 0
  3. First iteration (n = 1001):

    • n % 1000 = 1, which is not 0, so we call convertLessThan1000(1).
      • convertLessThan1000(1):
        • Since 1 <= 19, it directly adds "One".
      • thousands[i] = "" (empty string, because this is for the "ones" place).
      • The result for this iteration is: "One ".
  4. Update:

    • n /= 1000, so now n = 1.
    • i++, so now i = 1.
  5. Second iteration (n = 1):

    • n % 1000 = 1, which is not 0, so we call convertLessThan1000(1).
      • convertLessThan1000(1):
        • Since 1 <= 19, it directly adds "One".
      • thousands[i] = "Thousand".
      • The result for this iteration is: "One Thousand ".
  6. Update:

    • n /= 1000, so now n = 0.
    • i++, so now i = 2.
  7. Termination:

    • Since n = 0, the while loop terminates.
  8. Final result:

    • Combine the results from both iterations: "One One Thousand ".
    • Remove the trailing spaces: "One Thousand".

Output: "One Thousand".


Example 3: n = 0

Step-by-Step Breakdown:

  1. The convertToWords() function is called with n = 0.

  2. Special Case:

    • Since n == 0, the function immediately returns "Zero".

Output: "Zero".


Example 4: n = 99999

Step-by-Step Breakdown:

  1. The convertToWords() function is called with n = 99999.

  2. Initialization:

    • result = ""
    • i = 0
  3. First iteration (n = 99999):

    • n % 1000 = 999, which is not 0, so we call convertLessThan1000(999).
      • convertLessThan1000(999):
        • 999 >= 100, so it adds "Nine Hundred ".
        • Then, 999 % 100 = 99.
        • 99 >= 20, so it adds "Ninety ".
        • Finally, 99 % 10 = 9, so it adds "Nine ".
        • The result of convertLessThan1000(999) is "Nine Hundred Ninety Nine".
      • thousands[i] = "" (empty string).
      • The result for this iteration is: "Nine Hundred Ninety Nine ".
  4. Update:

    • n /= 1000, so now n = 99.
    • i++, so now i = 1.
  5. Second iteration (n = 99):

    • n % 1000 = 99, which is not 0, so we call convertLessThan1000(99).
      • convertLessThan1000(99):
        • 99 >= 20, so it adds "Ninety ".
        • Then, 99 % 10 = 9, so it adds "Nine ".
        • The result of convertLessThan1000(99) is "Ninety Nine".
      • thousands[i] = "Thousand".
      • The result for this iteration is: "Ninety Nine Thousand ".
  6. Update:

    • n /= 1000, so now n = 0.
    • i++, so now i = 2.
  7. Termination:

    • Since n = 0, the while loop terminates.
  8. Final result:

    • Combine the results from both iterations: "Nine Hundred Ninety Nine Ninety Nine Thousand ".
    • Remove the trailing spaces: "Ninety Nine Thousand Nine Hundred Ninety Nine".

Output: "Ninety Nine Thousand Nine Hundred Ninety Nine".


Conclusion:

This code successfully converts a number into its English words representation, breaking down the number into groups of three digits and converting them using predefined mappings for numbers less than 1000, then combining the results with appropriate place values (Thousand, Million, etc.). The logic handles edge cases like 0 and trailing spaces effectively.