Tuesday, March 10, 2015

Integer to String


"write a function that has an int as input and return the equivalent String as an output


12 -> 'twelve'
4345 -> 'four thousand three hundred and forty-five'
7654567643 -> 'seven billion six hundred and fifty-four million five hundred and sixty-seven thousand six hundred and forty-three'"

I parse the integer to a string thus we can use recursion to solve the problem. Given any integer, 
1, 111, 111, 111
The rule follows XXX billion XXX million XXX thousand and XXX, where XXX is in hundred, for example, one hundred and eleven. 
For numbers less than 20, the nomination is different, so we need to store each corresponding string in the map separately, for numbers larger than 20, store the tenth, e.g., thirty, forty, etc. We don't need to store hundred since it will be X hundred, where X is already in the map, and XX, which is a two digit number, and we can use recursion to get that. 
The code could be neater, I just don't have enough time to clean it. 

import java.util.*;
public class IntegerToString {
 private static Map numbers;
 static{
  numbers = new HashMap ();
  numbers.put(0, "zero");
  numbers.put(1, "one");
  numbers.put(2, "two");
  numbers.put(3, "three");
  numbers.put(4, "four");
  numbers.put(5, "five");
  numbers.put(6, "six");
  numbers.put(7, "seven");
  numbers.put(8, "eight");
  numbers.put(9, "nine");
  numbers.put(10, "ten");
  numbers.put(11, "eleven");
  numbers.put(12, "twelve");
  numbers.put(13, "thirteen");
  numbers.put(14, "fourteen");
  numbers.put(15, "fifteen");
  numbers.put(16, "sixteen");
  numbers.put(17, "seventeen");
  numbers.put(18, "eighteen");
  numbers.put(19, "nineteen");
  numbers.put(20, "twenty");
  numbers.put(30, "thirty");
  numbers.put(40, "forty");
  numbers.put(50, "fifty");
  numbers.put(60,"sixty");
  numbers.put(70, "seventy");
  numbers.put(80, "eighty");
  numbers.put(90, "ninety");
  
 }
 public static String read(int n){
  return read(String.valueOf(n));
 }
 public static String read(String nStr){
  int len = nStr.length();
  if(len < 2)
   return numbers.get(Integer.parseInt(nStr));
  else if(len < 3) {
   int tenth = Character.getNumericValue(nStr.charAt(0)) * 10;
   if (tenth == 0)
    return read(nStr.substring(1));
   if(tenth < 20)
    return numbers.get(Integer.parseInt(nStr));
   int single = Character.getNumericValue(nStr.charAt(1));
   return single == 0 ? numbers.get(tenth) : numbers.get(tenth) + "-" + numbers.get(single);
  }
  else if(len < 4){
   if(Integer.parseInt(nStr) == 0)
    return "";
   int hund = Character.getNumericValue(nStr.charAt(0));
   if(Integer.parseInt(nStr.substring(1)) == 0)
    return numbers.get(hund) + " hundred";
   return hund == 0 ? read(nStr.substring(1)) : numbers.get(hund) + " hundred and " + read(nStr.substring(1));
  }
  else if(len < 7){
   if(Integer.parseInt(nStr) == 0)
    return "";
   return read(nStr.substring(0, len - 3)) + " thousand " + read(nStr.substring(len - 3, len));
  }
  else if(len < 10){
   if(Integer.parseInt(nStr) == 0)
    return "";
   return read(nStr.substring(0, len - 6)) + " million " + read(nStr.substring(len - 6, len));
  }
  else
   return read(nStr.substring(0, 1)) + " billion " + read(nStr.substring(1));
 }
 public static void main(String[] args) {
  System.out.println(read(1));
  System.out.println(read(11));
  System.out.println(read(100));
  System.out.println(read(1000));
  System.out.println(read(1111));
  System.out.println(read(10000));
  System.out.println(read(11111));
  System.out.println(read(1000000));
  System.out.println(read(1111111));
  System.out.println(read(10000000));
  System.out.println(read(11111111));
  System.out.println(read(100000000));
  System.out.println(read(111111111));
  System.out.println(read(1000000000));
  System.out.println(read(1111111111));
  System.out.println(read(4345));
 }
}

No comments:

Post a Comment