From 7c96dade888920ea3051ec580328760b75f117cd Mon Sep 17 00:00:00 2001 From: Correl Roush Date: Mon, 12 Apr 2010 15:53:20 +0000 Subject: [PATCH] Problem 17 git-svn-id: file:///srv/svn/euler@42 e5f4c3ec-3c0c-11df-b522-21efaa4426b5 --- e017.py | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 e017.py diff --git a/e017.py b/e017.py new file mode 100644 index 0000000..6f26b1b --- /dev/null +++ b/e017.py @@ -0,0 +1,61 @@ +NUMBERS = [ + '', + 'one', + 'two', + 'three', + 'four', + 'five', + 'six', + 'seven', + 'eight', + 'nine' +] +TENS = [ + 'n/a', + 'n/a', + 'twenty', + 'thirty', + 'forty', + 'fifty', + 'sixty', + 'seventy', + 'eighty', + 'ninety' +] +TEENS = [ + 'ten', + 'eleven', + 'twelve', + 'thirteen', + 'fourteen', + 'fifteen', + 'sixteen', + 'seventeen', + 'eighteen', + 'nineteen' +] + +def format(n): + s = [] + if n // 1000: + s.append(NUMBERS[n // 1000] + ' thousand') + n = n % 1000 + if n // 100: + s.append(NUMBERS[n // 100] + ' hundred') + nn = n % 100 + if n // 100 and nn: + s.append('and') + if nn // 10: + if nn < 20: + s.append(TEENS[nn % 10]) + else: + s.append(TENS[nn // 10] + ('-' + NUMBERS[nn % 10] if nn % 10 else '')) + else: + s.append(NUMBERS[nn % 10]) + return ' '.join(s) + +if __name__ == '__main__': + chars = [] + for i in range(1, 1001): + chars = chars + list(format(i).replace(' ', '').replace('-', '')) + print 'Chars:', len(chars)