Let's assume we have the following situation:
- Multiple project topics exist, each corresponding to a natural number
- students can sign in and choose two projects and give them either
- The name and the time when they sign in is tracked as well.
With all of the information above assume you receive data in the from of a
txt-file looking like this
Martin 16:46:32 8 19 Alice 15:22:56 8 12 Alex 17:23:11 19 1 John 19:02:11 11 13 Phillip 19:03:11 11 13 Diego 15:23:57 14 5 Jack 16:46:45 8 3
where the columns represent the
priority 1 and
priority 2 in this order. You can assume that all of them signed in on the same day and students which signed in first have a higher priority in comparison to the others.
I wanted to write a program that takes this
txt-file as input and returns a
txt-file with output
Name Assigned Project Alice 8 Diego 14 Martin 19 Jack 3 Alex 1 John 11 Phillip 13
The solution I came up with looks like this:
import numpy as np dat = np.genfromtxt("data.txt", dtype="str") class person: def __init__(self, name, time, prio1, prio2): self.name = name sp = time.split(":") t = sp*3600 + sp*60 + sp self.time = t self.prio1 = prio1 self.prio2 = prio2 names = dat[:, 0] time = dat[:, 1] prio1 = dat[:, 2] prio2 = dat[:, 3] people =  for i,j,k,l in zip(names, time, prio1, prio2): people.append(person(i,j,k,l)) people_sorted_time = sorted(people, key=lambda x: x.time) for k in range(len(people_sorted_time)): for j in range(k + 1, len(people_sorted_time)): if people_sorted_time[k].prio1 == people_sorted_time[j].prio1: people_sorted_time[j].prio1 = people_sorted_time[j].prio2 res = open("results","w") res.write("Name"+"\t"+"Assigned Project"+"\n") for k in range(len(people_sorted_time)): res.write(people_sorted_time[k].name + "\t" + people_sorted_time[k].prio1 + "\n")
The code seems to work fine, but I'm not sure if I actually was able to take care of all edge-cases. I'm also not sure if this is really a efficient way to solve the problem, I rarely code stuff like that (mostly computational physics stuff), and would appreciate any kind of suggestions on how one could improve the code in general.
EDIT: What I realized after some thinking is that it would probably be quite hard do implement further
priority variables (like
prio 4, etc.). If someone could suggest a better way of deciding how to assign the projects in terms of priority, I'd be glad to see it.