简体   繁体   English

如何使用python限制同一字母的最长序列

[英]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? 如何使用python确定同一字母的最长序列?

For example I use the following code to print a shuffled list with 3 conditions A,B and C 例如,我使用以下代码来打印具有3个条件A,B和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) 例如,允许:[A,B,C,A,B,B,C,C,C,A,B…。]不允许:[A,A,B,B,B,B,C,A,B ...](因为连续有四个B)

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. 创建isValidShuffle将返回True的A,B,C混合序列的最简单方法。

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]))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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