1

I am writing my first python program for a school project. It is a program to simulate an experiment where you randomly move ones and zeroes around in a matrix. I already wrote 90 % of it and it works ok. I am struggling with one last part - I wrote a for loop that swaps the positions of all ones and zeroes in a list, nested it in a loop which repeats that process a number of times, and am trying to nest it in another loop which would append the resulting lists to an empty matrix one after another a given number of times. The problem I am facing is that all these lists are the same and are supposed to be different.

this is the piece of code I am using. n is the number of the matrix' s columns, m is the number of rows and x is the number of times the ones and zeroes get swapped around in a single row. I am using a swap positions function from geeksforgeeks.

for example when we have a 4x4 matrix and want to mix 2 times:

n = 4
m = 4
x = 2
podlista = [0, 0, 1, 1]


def swapPositions(list, pos1, pos2):
    first_ele = list.pop(pos1)
    second_ele = list.pop(pos2 - 1)

    list.insert(pos1, second_ele)
    list.insert(pos2, first_ele)

    return list


macierz = []

for w in range(m):
    for y in range(x):
        for k in range(n):
            import random
            swapPositions(podlista, k, random.randint(0, (n - 1)))
    macierz.append(podlista)

print(macierz)

where podlista means row and macierz is matrix This is the only way I can think of doing this I would really apreciate some help on this matter

slow
  • 11
  • 2

0 Answers0