Commits

Christoph Schindler committed a5ff19b

16. Dojo: Numbers 2 English

Comments (0)

Files changed (3)

numbers2words/numbers2words.py

+#!/usr/bin/env python
+# encoding: utf-8
+"""
+numbers2words.py
+
+Created by Coding Dojo on 2014-01-15.
+Copyright (c) 2014 PyUGAT. All rights reserved.
+"""
+
+DIGITS= ["zero", "one", "two", "three", "four", "five",
+ "six", "seven", "eight", "nine", "ten"]
+
+TEENS = ["ten", "eleven", "twelve", "thirteen", "fourteen",
+         "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"]
+
+
+TENS = ["", "", "twenty", "thirty", "forty", "fifty", "sixty",
+        "seventy", "eighty", "ninety"]
+
+def numbers2words(number):
+    if number <= 10:
+        return DIGITS[number]
+    elif number < 20:
+        return TEENS[number-10]
+    else:
+        if number%10 == 0:
+            return TENS[number//10]
+        return TENS[number//10] + " " + numbers2words(number%10)

numbers2words/setup.cfg

+[nosy]
+base_path = ./
+glob_pattern = *.py
+extra_paths = setup.cfg
+options = --stop --with-yanc

numbers2words/test_numbers2words.py

+#!/usr/bin/env python
+# encoding: utf-8
+"""
+test_numbers2words.py
+
+Created by Coding Dojo on 2013-05-21.
+Copyright (c) 2013 PyUGAT. All rights reserved.
+"""
+
+from nose.tools import *
+
+import numbers2words
+
+def test_zero():
+    assert_equal(numbers2words.numbers2words(0), "zero")
+
+def test_one():
+    assert_equal(numbers2words.numbers2words(1), "one")
+
+def test_two():
+    assert_equal(numbers2words.numbers2words(2), "two")
+    
+def test_three():
+    assert_equal(numbers2words.numbers2words(3), "three")
+    
+def test_four():
+    assert_equal(numbers2words.numbers2words(4), "four")
+    
+def test_five():
+    assert_equal(numbers2words.numbers2words(5), "five")
+
+def test_ten():
+    assert_equal(numbers2words.numbers2words(10), "ten")
+
+def test_twenty_one():
+    assert_equal(numbers2words.numbers2words(21), "twenty one")
+
+def test_twenty_seven():
+    assert_equal(numbers2words.numbers2words(27), "twenty seven")
+
+def test_twenty():
+    assert_equal(numbers2words.numbers2words(20), "twenty")
+    
+def test_thirty_one():
+    assert_equal(numbers2words.numbers2words(31), "thirty one")
+
+def test_11():
+    assert_equal(numbers2words.numbers2words(11), "eleven")
+
+def test_17():
+    assert_equal(numbers2words.numbers2words(17), "seventeen")
+
+def test_99():
+    assert_equal(numbers2words.numbers2words(99), "ninety nine")
+def test_30():
+    assert_equal(numbers2words.numbers2words(30), "thirty")
+