Data science: Keep everyone with a left join

Some students have no grades yet, and some grades belong to ids that are not on the list. Show every student, and count the grades that match nobody.

What to build

A program that joins students to their grades with a left join.

Requirements

  • For each student, in input order, print NAME: SUBJECT SCORE, SUBJECT SCORE, ... with their grades in input order, or NAME: no grades.
  • Finally print unmatched grades: K, the number of grades whose id is not a student's.

Input

Line 1: s. Then s lines ID,NAME. Next line: g. Then g lines ID,SUBJECT,SCORE.

Output

s lines, then the count.

Constraints

1 ≤ s ≤ 1000; 0 ≤ g ≤ 10000; student ids are distinct.

Example

Input:

3
1,Asha
2,Ben
3,Chen
4
1,math,80
3,math,70
1,art,90
7,math,50

Output:

Asha: math 80, art 90
Ben: no grades
Chen: math 70
unmatched grades: 1

Why it works

Starting from the students and looking up their grades keeps every student, which is what a left join means, and counting the grades that found no student exposes the orphan rows an inner join would hide.

Examples

Example 1 · Input
3
1,Asha
2,Ben
3,Chen
4
1,math,80
3,math,70
1,art,90
7,math,50
Expected output
Asha: math 80, art 90
Ben: no grades
Chen: math 70
unmatched grades: 1
Example 2 · Input
1
5,Eve
0
Expected output
Eve: no grades
unmatched grades: 0

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

Group the grades by student id first: a dictionary from id to a list of (subject, score).

Hint 2

Then go through the students in order and look up their grades.

Solution

Show one correct solution (try it yourself first)
s = int(input())
students = [input().split(",") for _ in range(s)]
g = int(input())
by_student = {}
for _ in range(g):
    sid, subject, score = input().split(",")
    by_student.setdefault(sid, []).append(f"{subject} {score}")

ids = {sid for sid, _ in students}
for sid, name in students:
    grades = by_student.get(sid)
    print(f"{name}: {', '.join(grades) if grades else 'no grades'}")
print("unmatched grades:", sum(len(v) for sid, v in by_student.items() if sid not in ids))

Lesson: Joining tables →