karat student courses intersection
unknown
plain_text
4 years ago
938 B
7
Indexable
def solve(pairs: List[Tuple[str, str]]) -> Dict[Tuple[str, str], List[str]]:
student_courses: Dict[str, Set[str]] = {}
for student, course in pairs:
if student not in student_courses:
student_courses[student] = set()
student_courses[student].add(course)
ret = {}
students = list(student_courses.keys())
for i in range(len(students)):
for j in range(i + 1, len(students)):
pair = (students[i], students[j])
student_i_courses = student_courses[students[i]]
student_j_courses = student_courses[students[j]]
intersect_courses = student_i_courses.intersection(student_j_courses)
ret[pair] = list(intersect_courses)
return ret
pairs = [
("s1", "a"),
("s2", "a"),
("s2", "b"),
("s3", "b"),
("s1", "c"),
("s1", "d"),
("s1", "e"),
("s3", "d"),
("s2", "c"),
]
print(solve(pairs))Editor is loading...