简体   繁体   中英

How to restrict the longest sequence of the same letter using python

How can I determine the longest sequence of the same letter using python?

For example I use the following code to print a shuffled list with 3 conditions A,B and C

from random import shuffle
condition = ["A"]*20
condition_B = ["B"]*20
condition_C = ["C"]*20

condition.extend(condition_B)
condition.extend(condition_C)
shuffle(condition)
print(condition)

Now i want to make sure that the same condition does not happen more than three times in a row.

Eg, allowed: [A, B, C, A, B, B, C, C, C, A, B….] Not allowed: [A, A, B, B, B, B, C, A, B...] (because of four B's in a row)

How can I solve this problem? Thank you in advance.

Maybe you should build the list sequentially, rather than shuffling:

result = []
for i in range(60):     # for each item in original list
  start = true          # we haven't found a suitable one yet
  if start or i>2:      # don't do checking unless 3 items in list
    while start or (
           c==shuf[-1] and     # is the chosen value
           c==shuf[-2] and     # the same as any of
           c==shuf[-3] ):      # the last 3 items?
      idx = random.randint(0,len(condition))  # chose a new one
      c = condition[idx]
      start = false            
  result.append(c)      # add to result
  del condition[i]      # remove from list

Warning! not tested - just conceptual...

# Validate with this function it return false if more than three consecutive characters are same else True.


def isValidShuffle( test_condition):

    for i in range(len(test_condition)-4):
        if len(set(test_condition[ i:i+4])) == 1:
            # set size will be 1 all four consecutive chars are same
            return False
    return True

Simplest way to create shuffled sequence of A,B,C for which isValidShuffle will return True.

from random import shuffle

# condition list contains 20 A's 20 B's 20 C's

seq = ['A','B','C']
condition = []

for seq_i in range(20):
    shuffle(seq)
    condition += seq

print(condition) # at most two consecutive characters will be same 

print(isValidShuffle(condition))

-----------------------------------------------------------------------------

Output
['A', 'B', 'C', 'B', 'C', 'A', 'C', 'B', 'A', 'C', 'B', 'A', 'C', 'A', 'B', 'C', 'B', 'A', 'B', 'A', 'C', 'B', 'C', 'A', 'B', 'C', 'A', 'C', 'A', 'B', 'B', 'C', 'A', 'B', 'A', 'C', 'A', 'B', 'C', 'C', 'A', 'B', 'A', 'B', 'C', 'B', 'A', 'C', 'C', 'A', 'B', 'B', 'C', 'A', 'B', 'A', 'C', 'A', 'B', 'C']

...............................................................................................................................................................
This is not imposing your restriction while creating shuffled sequence but keeps on trying until it find the sequence which meets your consecutive char restriction.

validshuffle = False 

condition = ['A']*20 + ['B']*20 + ['C']*20

while not validshuffle:

    shuffle(condition)

    if isValidShuffle(condition):
        validshuffle = True

print(condition)

-------------------------------------------------------------------------------
Output

try
try
['A', 'C', 'A', 'B', 'B', 'C', 'B', 'C', 'A', 'C', 'A', 'C', 'B', 'B', 'B', 'C', 'A', 'A', 'B', 'C', 'A', 'A', 'B', 'B', 'C', 'B', 'B', 'C', 'B', 'C', 'C', 'B', 'A', 'B', 'B', 'A', 'C', 'A', 'A', 'C', 'A', 'C', 'B', 'C', 'A', 'A', 'C', 'A', 'C', 'A', 'C', 'B', 'B', 'B', 'A', 'B', 'C', 'A', 'C', 'A']

If you just want to know, how long is the longest subsequence, you could do this. This is iterating over it the sequence and recording the length of the subsequences of the same character, saving it, getting the max for each subsequence, and then, getting the max of characters.

This is not exactly the problem you mention, but It could be useful.

from random import shuffle

sequence = ['A']*20 + ['B']*20 + ['C']*20 
sequences = {'A': [], 'B':[], 'C':[]}
shuffle(sequence)
current = sequence[0]
acc = 0
for elem in sequence:
    if elem == current:
        acc += 1
    else:
        sequences[current].append(acc)
        current = elem
        acc = 1
else:
    sequences[current].append(acc)
for key, seqs in sequences.items():
    sequences[key] = max(seqs)
print(max(sequences.items(), key=lambda i: i[1]))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM