Guest User

Untitled

a guest
Jan 14th, 2012
116
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # roman numeral problem solution from:
  2. # http://www.johndcook.com/blog/2012/01/14/roman-numeral-puzzle/comment-page-1/
  3.  
  4. def roman_numerals():
  5.     import itertools
  6.     values = list('IVXLCDM')
  7.     index = dict((j,i) for i,j in enumerate(values))
  8.     prefix = {0: (1,2), 2: (3,4), 4: (5,6)}
  9.     for i in xrange(2, 7):
  10.         for perm in itertools.permutations(values[:7], i):
  11.             for j, v in enumerate(map(index.get, perm[:-1])):
  12.                 vn = index[perm[j+1]]
  13.                 if vn > v and vn not in prefix.get(v, ()):
  14.                     break
  15.             else:
  16.                 values.append(''.join(perm))
  17.     return values
RAW Paste Data