-2

For example, given a list:

    binary = [] #only 0, 1 allowed

Now I put it in a loop which append 0, 1 value randomly using randint.

1st loop:


    binary = [1]

2nd loop:


    binary = [1, 1]

Now, if the third the random number also return 1 which is:


    binary = [1, 1, 1] # not allowed

but this case is ok:


    binary = [1, 1, 0, 1] #ok

The point is, I want to prevent continuously duplication in a list, where [1, 1, 1] is not allowed. How can I do it?

  • Can you share some sample code so that we an help you – Amit Nanaware May 12 '21 at 08:43
  • What have you tried so far? – Klaus D. May 12 '21 at 08:43
  • `if len(binary) >= 2 and all(i == generated_number for i in binary[-2:]): continue # and don't append`…? – deceze May 12 '21 at 08:44
  • It really depends on what your goal is! If you want to block that behaviour at the top level you could create your own list (see https://stackoverflow.com/questions/6560354/how-would-i-create-a-custom-list-class-in-python). Otherwise if you only append and never insert any element into your list you could just check the last two digits of your list and check if they are the same as the digit you are trying to append. – Gaspard Merten May 12 '21 at 08:45
  • I tried to check previous item in a list, but my naive way which is minus current index in the loop by 1, is too risky that it can let Index out of bound happens. – Johny Dunnash May 12 '21 at 08:46

2 Answers2

3

Add check for length of binary and then check for last two elements of binary list and then append the next bit into the list.

you can try something like this:

binary = []
def add_binary(bit):
    if len(binary) >=2:
        if binary[-1] == binary[-2] == bit:
            print("Bit can not be added")
        else:
            binary.append(bit)
    else:
        binary.append(bit)
    return binary
for i in [1,1,1,0,1,0,1,1]:
    add_binary(i)
print(binary)
Amit Nanaware
  • 3,203
  • 1
  • 6
  • 19
  • My problem is index out of bound happen even though I let them out of normal condition, but thanks for your answer, I realized that I just accidently let out one important condition, thank you! – Johny Dunnash May 13 '21 at 01:52
0

You can try my code:

import random

iterations=int(input("Enter no. of times:")) #No. of iteration
flag_counter=0
prev_dig=-1
binary=[]

for x in range(iterations):
    #Generate digit
    current_dig=random.randint(0,1)
    print(current_dig)
    #Check if current digit is not previous
    if current_dig!=prev_dig:
        binary.append(current_dig)
        #Set the previous and counter
        prev_dig=current_dig
        flag_counter=1
    #Check if 3 repetations occur
    elif current_dig==prev_dig and flag_counter==2:
        print("Not allowed")
    #If 1 time repeated then allow 2 consecutive repetation
    else:
        binary.append(current_dig)
        #Increment counter
        flag_counter+=1
        
print(binary)

Here I have used prev_dig to keep track of previously generated random digits and a counter to keep track of repetations. As soon as I see that there are 3 repetations then I discard that.
You can edit this code to your will.

projjal
  • 264
  • 3
  • 10