Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # roman numeral problem solution from:
- # http://www.johndcook.com/blog/2012/01/14/roman-numeral-puzzle/comment-page-1/
- def roman_numerals():
- import itertools
- values = list('IVXLCDM')
- index = dict((j,i) for i,j in enumerate(values))
- prefix = {0: (1,2), 2: (3,4), 4: (5,6)}
- for i in xrange(2, 7):
- for perm in itertools.permutations(values[:7], i):
- for j, v in enumerate(map(index.get, perm[:-1])):
- vn = index[perm[j+1]]
- if vn > v and vn not in prefix.get(v, ()):
- break
- else:
- values.append(''.join(perm))
- return values
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement