ayush3504

Count Sequences with Non Duplicate Entries

Feb 7th, 2015
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.99 KB | None | 0 0
  1. """
  2. A brute-force method to calculate status LED color sequences where colors do not repeat consecutively.
  3. """
  4.  
  5. def cartesianProduct(lists):
  6.     '''Returns cartesian product of lists (operands) in the given list of operands)'''
  7.     if lists == []: return [[]]
  8.     return [x + [y] for x in cartesianProduct(lists[:-1]) for y in lists[-1]]
  9.  
  10. def countDuplicateSequences(colorList, sequenceLength):
  11.     '''Prints no. of sequences where entries are non-repeating'''
  12.  
  13.     count = 0
  14.     i = 0  
  15.     operandList = []
  16.  
  17.     for i in range(sequenceLength):
  18.         operandList.append(colorList)
  19.  
  20.     allSequences = cartesianProduct(operandList)  
  21.  
  22.     for sequence in allSequences:
  23.         for i in range(sequenceLength-1):
  24.             if sequence[i]==sequence[i+1]:
  25.                 count += 1
  26. #               print sequence
  27.  
  28.     print 'Total count = ' + str(len(allSequences))            
  29.     print 'Duplicates = ' + str(count)
  30.     print 'Non duplicate count = ' + str(len(allSequences) - count)
  31.  
  32. countDuplicateSequences(['R','G','B','C','M','Y','W'], 4)
Advertisement
Add Comment
Please, Sign In to add comment