use std::string::ToString;

fn singler<'a>(digit: u32) -> &'a str {
  match digit {
    0 => "zero",
    1 => "one",
    2 => "two",
    3 => "three",
    4 => "four",
    5 => "five",
    6 => "six",
    7 => "seven",
    8 => "eight",
    9 => "nine",
     _  => "",
  }
}

fn twentor<'a>(num: u32) -> &'a str 


fn hunnert<'a>(num: u32) -> String {
  match num {
    0...19  => twentor(num).to_string(),
    20      => "twenty".to_string(),
    21...29 => format!("twenty-{}", singler(num % 20)),
    30      => "thirty".to_string(),
    31...39 => format!("thirty-{}", singler(num % 30)),
    40      => "forty".to_string(),
    41...49 => format!("forty-{}", singler(num % 40)),
    50      => "fifty".to_string(),
    51...59 => format!("fifty-{}", singler(num % 50)),
    60      => "sixty".to_string(),
    61...69 => format!("sixty-{}", singler(num % 60)),
    70      => "seventy".to_string(),
    71...79 => format!("seventy-{}", singler(num % 70)),
    80      => "eighty".to_string(),
    81...89 => format!("eighty-{}", singler(num % 80)),
    90      => "ninety".to_string(),
    91...99 => format!("ninety-{}", singler(num % 90)),
    _       => "".to_string(),
  }
}

fn thouse(num: u32) -> String {
  match num {
    0...99    => hunnert(num).to_string(),
    100       => "one hundred".to_string(),
    101...199 => format!("one hundred and {}", hunnert(num % 100)),
    200       => "two hundred".to_string(),
    201...299 => format!("two hundred and {}", hunnert(num % 100)),
    300       => "three hundred".to_string(),
    301...399 => format!("three hundred and {}", hunnert(num % 100)),
    400       => "four hundred".to_string(),
    401...499 => format!("four hundred and {}", hunnert(num % 100)),
    500       => "five hundred".to_string(),
    501...599 => format!("five hundred and {}", hunnert(num % 100)),
    600       => "six hundred".to_string(),
    601...699 => format!("six hundred and {}", hunnert(num % 100)),
    700       => "seven hundred".to_string(),
    701...799 => format!("seven hundred and {}", hunnert(num % 100)),
    800       => "eight hundred".to_string(),
    801...899 => format!("eight hundred and {}", hunnert(num % 100)),
    900       => "nine hundred".to_string(),
    901...999 => format!("nine hundred and {}", hunnert(num % 100)),
    1000      => "one thousand".to_string(),
    _       => "".to_string(),
  }
}

fn count(s: String) -> u32 {
  s.chars().fold(0, |memo, ch| {
    memo + match ch {
      'a'...'z' => 1,
      _   => 0,
    }
  })
}

pub fn run() {
  let sum: u32 = (1..1001).map(|i| count(thouse(i))).sum();
  println!("Sum of number words: {}", sum);
}
{
  match num {
    0...9 => singler(num),
    10 => "ten",
    11 => "eleven",
    12 => "twelve",
    13 => "thirteen",
    14 => "fourteen",
    15 => "fifteen",
    16 => "sixteen",
    17 => "seventeen",
    18 => "eighteen",
    19 => "nineteen",
    _  => "",
  }
}