-1

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.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • You could create a list with the keys of `data` and sort it. Then you iterate over the sorted keys instead of the key, value pairs. – Ignatius Reilly Oct 14 '22 at 19:32
  • Does this answer your question? [Compare strings, allowing one character difference](https://stackoverflow.com/questions/25216328/compare-strings-allowing-one-character-difference) – Ignatius Reilly Oct 14 '22 at 19:35

1 Answers1

0

To get the output in the correct order, loop from 1 to n instead of looping over the elements of the dictionary.

for i in range(1, n+1):
    print(" ".join(data.get(str(i), [])))

To handle similar names, write a function that tells if two names are similar:

def similar(name1, name2):
    if len(name1) != len(name2):
        return false
    diffcount = sum(c1 != c2 for c1, c2 in zip(name1, name2))
    return diffcount <= 1

Then use it when testing whether the name is already in the course student list:

    if not any(similar(name, s) for s in data[course]):
        data[course].append(name)
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • The question specifically says that the Nth row of output is for course number N. Your code won't print anything for courses with no students, and there won't be any way to tell which courses are missing. – Barmar Oct 14 '22 at 19:38
  • The question also says "must output n amount of lines" – Barmar Oct 14 '22 at 19:39