What I've been tasked to do is to take in an input in the form "n m" where n= number of courses and m= number of students. next, i must take in m more inputs, each input being in the form "a b" where a is the number of the course that the student is taking and b is the students name. Then, I must filter out the similar names that are taking the same courses and output the names of students taking each course. For example consider this sample input:
2 4
2 David
1 john
2 davin
1 johnn
I must output:
john johnn
David
in that order as john and johnn took course 1 so they will be outputted in the first line of output (i must output n amount of lines) then David is in the next line of output as he is in course 2. Notice how davin wasn't outputted, this is because he is taking same course as David and name is too similar (similar names meaning both names are same length and only have 1 letter difference so davis
and Davit
are too similar but davis
and daviss
aren't)
x = input()
n, m = x.split(' ')
n = int(n)
m = int(m)
data = {}
dict()
for a in range(m):
line = input()
course, name = line.split(' ')
if course not in data:
data[course] = [] # list()
if name not in data[course]:
data[course].append(name)
for key, value in data.items():
print( " ".join(value) )
This is what I've come up with for now, the only issue is, if "
2 davin
1 john
are the inputs, then the outs puts are
davin
john
but i need it to be
john
davin
along with that, I need to find a way to eliminate similar names and only allow the first of the two similar names to be outputted. Any help would be appreciated thanks.