Premium problem135. Expand a Many-to-Many Relationship

Medium Locked

student_courses has columns student_id and course_id. course_instructors has columns course_id and instructor_id. A course can have several instructors and a student can take several courses.

Return every valid student_id, course_id, instructor_id combination, sorted by all three columns, with the index renumbered from 0. A course taken by 2 students and taught by 3 instructors produces 6 rows.

Input

student_courses =
   student_id course_id
0           1        m1
1           2        m1
2           1        m2
course_instructors =
  course_id instructor_id
0        m1            i1
1        m1            i2
2        m2            i3
3        m3            i4

Output

   student_id course_id instructor_id
0           1        m1            i1
1           1        m1            i2
2           1        m2            i3
3           2        m1            i1
4           2        m1            i2

Premium problem

This one's part of Premium. Unlock the full Pandas track plus every other premium problem on the site.

Implement solve(...)