Created
November 23, 2008 06:52
-
-
Save jeremyBanks/28046 to your computer and use it in GitHub Desktop.
[2010-01] i meant to code golf numbers to words but failed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| # encoding: utf-8 | |
| from __future__ import division, with_statement | |
| import sys | |
| # http://stackoverflow.com/questions/309884/code-golf-number-to-words | |
| # http://en.wikipedia.org/wiki/Long_and_short_scales | |
| """Converts numbers to words using the English short scale.""" | |
| NEGATIVE = "negative" | |
| ZERO = "zero" | |
| AND = "and" | |
| DIGITS = (None, "one", "two", "three", "four", | |
| "five", "six", "seven", "eight", "nine") | |
| TENS = (None, "ten", "twenty", "thirty", "fourty", | |
| "fifty", "sixty", "seventy", "eighty", "ninety") | |
| HUNDRED = "hundred" | |
| ORDERS = (None, "thousand", "million", "billion", "trillion", | |
| "quadrillion", "quintillion", "sextillion", "septillion", | |
| "octillion", "nonillion", "decillion", "undecillion", | |
| "duodecillion", "tredecillion", "quattuordecillion", | |
| "quindecillion", "sexdecillion", "septendecillion", | |
| "octodecillion", "novemdecillion ", "vigintillion") | |
| class Error(Exception): | |
| """Base exception for all defined in this module.""" | |
| class NotIntegerError(ValueError, Error): | |
| pass | |
| def _underOneThousand(number): | |
| """Returns a number in the range of 1-999 converted to words!""" | |
| if not (0 < number < 1000): | |
| raise(Error("WTF")) | |
| parts = [] | |
| hundreds = number // 100 | |
| tens = (number % 100) // 10 | |
| ones = (number % 10) | |
| if hundreds: | |
| parts.append(DIGITS[hundreds]) | |
| parts.append(" ") | |
| parts.append(HUNDRED) | |
| number -= number // 100 | |
| if tens or ones: | |
| parts.append(" ") | |
| parts.append(AND) | |
| parts.append(" ") | |
| if tens: | |
| parts.append(TENS[number // 10]) | |
| if ones: | |
| if tens: | |
| parts.append("-") | |
| parts.append(DIGITS[ones]) | |
| return("".join(parts)) | |
| def toWords(number): | |
| """Converts a number to words using the English short scale.""" | |
| if number == 0: | |
| return ZERO | |
| parts = [] | |
| if number < 0: | |
| parts.append(NEGATIVE) | |
| number *= -1 | |
| # parts.append(DIGITS[number]) | |
| parts.append(_underOneThousand(number)) | |
| return "".join(parts) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| # encoding: utf-8 | |
| from __future__ import division, with_statement | |
| import sys | |
| import numberwords | |
| import unittest | |
| """Unit test for numberwords.py.""" | |
| class KnownValues(unittest.TestCase): | |
| knownValues = ( | |
| (0, "zero"), | |
| (1, "one"), | |
| (2, "two"), | |
| (3, "three"), | |
| (4, "four"), | |
| (5, "five"), | |
| (6, "six"), | |
| (7, "seven"), | |
| (8, "eight"), | |
| (9, "nine"), | |
| (10, "ten"), | |
| # (11, "eleven"), | |
| # (12, "tweleve"), | |
| # (13, "thirteen"), | |
| # (14, "fourteen"), | |
| # (15, "fifteen"), | |
| # (16, "sixteen"), | |
| # (17, "seventeen"), | |
| # (18, "eighteen"), | |
| # (19, "nineteen"), | |
| (20, "twenty"), | |
| (21, "twenty-one"), | |
| (22, "twenty-two"), | |
| (23, "twenty-three"), | |
| (24, "twenty-four"), | |
| (25, "twenty-five"), | |
| (26, "twenty-six"), | |
| (27, "twenty-seven"), | |
| (28, "twenty-eight"), | |
| (29, "twenty-nine"), | |
| (30, "thirty"), | |
| (35, "thirty-five"), | |
| (40, "fourty"), | |
| (42, "fourty-two"), | |
| (50, "fifty"), | |
| (51, "fifty-one"), | |
| (60, "sixty"), | |
| (64, "sixty-four"), | |
| (70, "seventy"), | |
| (72, "seventy-two"), | |
| (80, "eighty"), | |
| (89, "eighty-nine"), | |
| (90, "ninety"), | |
| (99, "ninety-nine"), | |
| (100, "one hundred"), | |
| (101, "one hundred and one"), | |
| (102, "one hundred and two"), | |
| # (111, "one hundred and eleven"), | |
| (123, "one hundred and twenty-three"), | |
| (256, "two hundred and fifty-six"), | |
| ) | |
| def testKnownValues(self): | |
| for number, words in self.knownValues: | |
| """Function should correctly respond to known values.""" | |
| result = numberwords.toWords(number) | |
| print(result) | |
| self.assertEqual(result, words) | |
| class BadInput(unittest.TestCase): | |
| pass | |
| def main(*args): | |
| unittest.main(argv=[sys.argv[0], "-v"]) | |
| if __name__ == "__main__": sys.exit(main(*sys.argv[1:])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment