Advertisement
ayush3504

Count Sequences with Non Duplicate Entries

Feb 7th, 2015
234
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. ayush@tesseract.in
  4. """
  5.  
  6. def cartesianProduct(lists):
  7.     '''Returns cartesian product of lists (operands) in the given list of operands)'''
  8.     if lists == []: return [[]]
  9.     return [x + [y] for x in cartesianProduct(lists[:-1]) for y in lists[-1]]
  10.  
  11. def countDuplicateSequences(colorList, sequenceLength):
  12.     '''Prints no. of sequences where entries are non-repeating'''
  13.  
  14.     count = 0
  15.     i = 0  
  16.     operandList = []
  17.  
  18.     for i in range(sequenceLength):
  19.         operandList.append(colorList)
  20.  
  21.     allSequences = cartesianProduct(operandList)  
  22.  
  23.     for sequence in allSequences:
  24.         for i in range(sequenceLength-1):
  25.             if sequence[i]==sequence[i+1]:
  26.                 count += 1
  27. #               print sequence
  28.  
  29.     print 'Total count = ' + str(len(allSequences))            
  30.     print 'Duplicates = ' + str(count)
  31.     print 'Non duplicate count = ' + str(len(allSequences) - count)
  32.  
  33. countDuplicateSequences(['R','G','B','C','M','Y','W'], 4)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement