ctci/16. Moderate problems/16.8. english_int.md

685 B

16.8. English int

Given any integer, print an english phrase that describes the integer

example:

  • input = 1234
  • output = "one thousand two hundred thirty four"

First idea

Two tasks here, 1. int to char, and 2. int to thousand/hundred thirt(y) whatever. what about 11, 12, 35?

inttochar = {
    1: "one",
    2: "two",
    3: "three",
    4: "four",
    5: "five",
    6: "six",
    7: "seven",
    8: "eight",
    9: "nine"
}

inttodec = {
    10: "ten",
    11: "eleven",
    12: "twelve",
    3: "thir",
    5: "fif"
}

inttoext = {
    1: "teen",
    0: "ty"
}

This isn't a difficult problem, it's just cumbersome and long to make good-looking code.