NZPUG Meeting - August 2010
- When
- Wednesday, 18 August 2010, 18.30 - 20:00 - please be on time
- Where
Engineering Building (Bldg. 403, UoA, Symonds St., Auckland - Room 403.402 aka 3.402
(Further info to the venue.)
Coding Dojo
Based on the advice from this site http://codingdojo.org/ we ran a coding dojo.
Watched video http://www.youtube.com/watch?v=gav9fLVkZQc
Then set to work on http://codingdojo.org/cgi-bin/wiki.pl?KataRomanNumerals
The code produced is as follows:
1 #!/usr/bin/env python
2 """
3 This is our fancy arab to roman numeral conversion module.
4 """
5 import sys
6
7
8 ROM2DEC = {
9 "I": 1,
10 "IV": 4,
11 "V": 5,
12 "IX": 9,
13 "X": 10,
14 "XL": 40,
15 "L": 50,
16 "XC": 90,
17 "C": 100,
18 "CD": 400,
19 "D": 500,
20 "CM": 900,
21 "M": 1000
22 }
23 DEC2ROM = dict((value, key) for key, value in ROM2DEC.items())
24 ROMAN_VALUES = sorted(ROM2DEC.values(), reverse=True)
25
26 def convert2roman(number):
27 """
28 This is the translator to roman numerals
29 >>> convert2roman(5)
30 'V'
31 >>> convert2roman(1)
32 'I'
33 >>> convert2roman(2)
34 'II'
35 >>> convert2roman(9)
36 'IX'
37 >>> convert2roman(3)
38 'III'
39 >>> convert2roman(4)
40 'IV'
41 >>> convert2roman(42)
42 'XLII'
43 >>> convert2roman(19)
44 'XIX'
45 >>> convert2roman(32)
46 'XXXII'
47 >>> convert2roman(800)
48 'DCCC'
49 >>> convert2roman(1984)
50 'MCMLXXXIV'
51 >>> convert2roman(2999)
52 'MMCMXCIX'
53 >>> convert2roman(3000)
54 'MMM'
55 >>> convert2roman(3001)
56 Traceback (most recent call last):
57 ...
58 ValueError: Input out of range
59 >>> convert2roman(0)
60 Traceback (most recent call last):
61 ...
62 ValueError: Input out of range
63 """
64 if number > 3000 or number < 1:
65 raise ValueError('Input out of range')
66 result = ''
67 while number > 0:
68 value = 0
69 for value in ROMAN_VALUES:
70 if value <= number:
71 break
72 result += DEC2ROM[value]
73 number -= value
74 return result
75
76
77
78 if __name__ == '__main__':
79 import doctest
80 doctest.testmod()
