0

I made a python scrambler but whenever I run my program, it strangely does not print anything, however, when I close my program, the python dialogue says the program is still running. This is my code:

def differ(a1, a2):
    diff = 0
    for i in range(len(a2)):
        if a1[i] != a2[i]:
            diff += 1
    return diff

import random

def scramble(arr):
    scrambled = arr
    while differ(arr, scrambled) < len(arr)-1:
        popped = scrambled.pop(random.randrange(1, len(scrambled)))
        scrambled.insert(popped, 0)
    return scrambled

print(scramble([1,2,3]))

does anybody know what's currently happening and knows how to fix it and possibly cleanse the code out?

thing10
  • 140
  • 1
  • 9
  • https://stackoverflow.com/questions/2612802/list-changes-unexpectedly-after-assignment-why-is-this-and-how-can-i-prevent-it – Woodford Jul 27 '21 at 20:26
  • `scrambled` and `arr` are both the same list. So `a1[i] != a2[i]` will never be true. – Barmar Jul 27 '21 at 20:28
  • For reference, the quantity your differ function is computing is usually called the Hamming distance (the number of elements in a pair of lists that differ). Also be aware of random.shuffle, which will randomize a list, but it doesn't ensure that hamming_distance(orig, shuffled) == len(orig). Also if too many of the elements of the list are equal, the you can never satisfy the condition and the scramble function will never return. – jpkotta Jul 27 '21 at 21:35

0 Answers0