Commits

Shu Zong Chen committed 8be25d6

did a to r part

Comments (0)

Files changed (1)

 class RAAR(object):
 	@staticmethod
 	def r2a(r):
+		"""
+		Convert roman numeral to int
+		"""
 		r = r.upper().strip()
 		if len(r):
 			_r = RAAR._fission(r)
 		raise ValueError
 
 	@staticmethod
-	def a2r(a):
-		print _val_in_order
-
-	@staticmethod
 	def _fission(r):
 		"""
-		Reduces from right to left chunks preceded by smaller chunks.
+		Reduces romain numeral from right to left chunks preceded by smaller chunks.
 		"""
-		chunks = RAAR._chunk(r)
+		chunks = RAAR._r_chunk(r)
 		ret = None
 		while 1:
 			if len(chunks) is 0:
 				ret.append(old)
 				ret.append(_cur)
 
-
 	@staticmethod
-	def _chunk(r):
+	def _r_chunk(r):
 		"""
 		Takes a string of atoms and returns a list of
 		chunks of similiar atoms in same order.
 			i = i+1
 		ret.append(_cur)
 		return ret
-	
+
+	@staticmethod
+	def a2r(a):
+		"""
+		Convert int to string containing roman numeral representation
+		"""
+		chunks = RAAR._a_chunk(a)
+		ret = "".join(RAAR.a_val(i[0])* i[1] for i in chunks)
+		return RAAR._fusion(ret)
+
+	@staticmethod
+	def _fusion(r):
+		"""
+		Take replacable chunks and fuse them together
+		"""
+		fusable = [
+			('DCCCC', 'CM'),
+			('CCCC', 'CD'),
+			('LXXXX', 'XC'),
+			('XXXX', 'XL'),
+			('VIIII', 'IX'),
+			('IIII', 'IV'),
+		]
+		for i, j in fusable:
+			if i in r:
+				ret = r.split(i, 1)
+				if len(i) == 4:
+					return ret[0] + j + RAAR._fusion(ret[1])
+				else:
+					return ret[0] + j + RAAR._fusion(ret[1])
+					return j + ret[0] + RAAR._fusion(ret[1])
+		return r
+
+	@staticmethod
+	def _a_chunk(a):
+		"""
+		Takes a integer and returns a list of
+		chunks of basic value atoms.
+
+		The return value is a list of (value, num_in_chunk) tuples
+		"""
+		ret = []
+		rem = a
+		for i in _val_in_order:
+			v = RAAR.r_val(i)
+			n_i = rem/v
+			ret.append((v, n_i))
+			rem = rem % v
+		return ret
+
 	@staticmethod
 	def r_val(r):
-		return _val_dict.get(r.upper())
+		return _val_dict.get(r)
+
+	@staticmethod
+	def a_val(a):
+		return [k for k, v in _val_dict.iteritems() if v == a][0]
 
 if __name__ == '__main__':
-	assert RAAR.r2a('MCCCCCCVI') == 1606
-	assert RAAR.r2a('XXVIIII') == 29
-	assert RAAR.r2a('IIX') == 8
-	assert RAAR.r2a('IX') == 9
-	assert RAAR.r2a('IIII') == 4
-	assert RAAR.r2a('XC') == 90
-	assert RAAR.r2a('VV') == 10
-	assert RAAR.r2a('XLIIII') == 44
-	assert RAAR.r2a('XCIX') == 99
-	assert RAAR.r2a('MDCCCCX') == 1910
-	assert RAAR.r2a('MCMLIV') == 1954
-	assert RAAR.r2a('I') == 1
-	assert RAAR.r2a('II') == 2
-	assert RAAR.r2a('III') == 3
-	assert RAAR.r2a('IV') == 4
-	assert RAAR.r2a('V') == 5
+	r2a_tests = {
+		'MCCCCCCVI': 1606,
+		'XXVIIII': 29,
+		'IIX': 8,
+		'IX': 9,
+		'IIII': 4,
+		'XC': 90,
+		'VV': 10,
+		'XLIIII': 44,
+		'XCIX': 99,
+		'MDCCCCX': 1910,
+		'MCMLIV': 1954,
+		'I': 1,
+		'II': 2,
+		'III': 3,
+		'IV': 4,
+		'V': 5,
+	}
+	for r, a in r2a_tests.iteritems():
+		assert RAAR.r2a(r) == a
+
+	a2r_tests = {
+		'I': 1,
+		'II': 2,
+		'III': 3,
+		'IV': 4,
+		'V': 5,
+		'VI': 6,
+		'VII': 7,
+		'VIII': 8,
+		'IX': 9,
+		'X': 10,
+		'MCMLXXXV': 1985,
+		'MCMLIV': 1954,
+		'XCIX': 99,
+	}
+	for r, a in a2r_tests.iteritems():
+		assert RAAR.a2r(a) == r
+