How to make 'copies' of variables that all connect back to the original variable

43 Views Asked by At

I need to sort students into classes based on their preferences and I am planning to use the Hungarian Algorithm to sort them. The problem that I am running into though is that there are more people than classes and each class has a minimum number of people that it needs to have.

In my dataset, there are ~550 students, and each one has a list of top 5 preferences. Every preference is an ID that corresponds to a class. Each class has a minimum and maximum capacity (in my case a min cap of 15 people and a max cap of 27 people) and there are 21 classes in the dataset.

Here is an example dataset for every student:

Email first choice second choice third choice fourth choice fith choice
[email protected] 4 7 1 8 21
[email protected] 6 9 14 17 2

Here is an example dataset for every class:

Class Title Class ID Min Cap Max Cap
Class Title1 1 15 27
Class Title2 2 15 27
Class Title3 3 15 27

Because there are ~550 students and 21 classes and for the Hungarian algorithm to work, I was planning to make "copies" of the classes. I would first make 15 copies of every class (like class 1.1, 1.2, 1.3, 1.4, 2.1, 2.2, 2.3, etc.) to fill the minimum requirement of the class and then would add even more copies to the most popular classes among the students until there is an equal number of students and copies of classes.

My question is: how would I loop the copying of the variables that would, in the algorithm act like their own classes or choices for classes (since the choices would also need to be different as to put the people to different copies of the same class and not have them compete for only one variable when there are other copies of it) but then after the sorting is done, the copies could be traced back to the original?

Thank you in advance and let me know if there is anything I can clarify

1

There are 1 best solutions below

0
FLAK-ZOSO On

You should know that some types in Python are mutable types, one of them is the list type:

>>> a = []
>>> a.append('.')
>>> a
['.']
>>> b = a
>>> b.append('$')
>>> a
['.', '$']

You may describe a list in Python like a pointer that points toward the cells.


An other example may be this:

>>> def foo(n: int):
        i+=1
>>> i = 2
>>> foo(i)
>>> i
2

As you can see the i variable is not passed by reference, since it's an immutable typed variable. But look at this:

>>> def baz(n: list[int]):
        n[0]+=1
>>> i = [2]
>>> baz(i)
>>> i
[3]

The same thing is true for dicts, so that if you want to have a copy [indipendent from the original] of that dictionary you will have to do something like this:

newDict = oldDict.copy()

So, if you need a short answer:

How to make 'copies' of variables that all connect back to the original variable?

You just have to create them with b = a