ayush3504 icon

Count Sequences with Non Duplicate Entries

ayush3504 | PRO | 02/07/15 11:31:41 PM UTC | 0 ⭐ | 390 👁️ | Never ⏰ | []
Python |

990 B

|

None

|

0 👍

/

0 👎

"""
A brute-force method to calculate status LED color sequences where colors do not repeat consecutively.
[email protected]
"""
 
def cartesianProduct(lists):
    '''Returns cartesian product of lists (operands) in the given list of operands)'''
    if lists == []: return [[]]
    return [x + [y] for x in cartesianProduct(lists[:-1]) for y in lists[-1]]
 
def countDuplicateSequences(colorList, sequenceLength):
    '''Prints no. of sequences where entries are non-repeating'''
 
    count = 0
    i = 0  
    operandList = []
 
    for i in range(sequenceLength):
        operandList.append(colorList)
 
    allSequences = cartesianProduct(operandList)  
 
    for sequence in allSequences:
        for i in range(sequenceLength-1):
            if sequence[i]==sequence[i+1]:
                count += 1
#               print sequence
 
    print 'Total count = ' + str(len(allSequences))            
    print 'Duplicates = ' + str(count)
    print 'Non duplicate count = ' + str(len(allSequences) - count)
 
countDuplicateSequences(['R','G','B','C','M','Y','W'], 4)

Comments