Flip the class roster
You are given n lines, each listing the courses a student takes.
Build and print the following with set and dict comprehensions instead of loops.
The first line has n, and the next n lines have "name course course ...".
Some lines have no courses.
The same student may appear on several lines, and that student's courses are all of those lines combined.
A course repeated within a line counts once.
1) Print the number of different students S, then S lines "name k course1 ...
coursek" in name order.
k is the number of different courses the student takes, and the courses are written sorted.
A student with no courses gives "name 0".
2) Print the number of different courses C, then C lines "course m name1 ...
namem" in course order.
m is the number of students taking the course, and the names are written sorted.
3) Print, sorted on one line separated by spaces, the courses that have exactly one student.
If there is no such course, print -.
All sorting is by the code order of strings (Python string comparison).
- Input
- 1 ≤ n ≤ 1000.
Names are 1 to 8 lowercase English letters.
Courses are 2 to 6 uppercase English letters and digits, 0 to 10 per line, and there are at most 200 different courses. - Output
- 1) S + 1 lines, 2) C + 1 lines, 3) one line
4 kim ART MATH lee MATH kim BIO kim
2 kim 3 ART BIO MATH lee 1 MATH 3 ART 1 kim BIO 1 kim MATH 2 kim lee ART BIO
3 mia CS CS joy CS ART ART zoe
3 joy 2 ART CS mia 1 CS zoe 0 2 ART 1 joy CS 2 joy mia ART