You are on page 1of 12

Week-7, Graded

Common Data for Questions (1), (2), (3), (4) and (5)
Statement
Common Data for Questions (1) to (5)

Prerequisites

Courses in any degree program have prerequisites. For example, CT is a prerequisite for Python,
Maths-1 is a prerequisite for Maths-2, which is in turn a prerequisite for MLF. All eight courses in
the foundational level are prerequisites for any diploma course, such as PDSA and so on. We wish
to construct a table (dictionary), where each course is mapped to the list of its prerequisites, and
solve some problems based on this.

The following information is necessary to solve all the problems:

(1) The program has courses in total.

(2) Out of the courses in the program, have at least one prerequisite. The courses
that don't have any prerequisite are called basic courses.

(3) Each basic course is a prerequisite of at least one course in the program.

(4) A course could be a prerequisite of multiple courses.

(5) A course cannot be its own prerequisite.

Answer questions (1) to (5) using this information.


Question-1
Statement
The first line of input is the positive integer which represents the number of courses that have
at least one prerequisite. Each of the next lines of input is a sequence of comma-separated
courses.

Let us call the first course in a sequence the main course. All other courses in the sequence that
follow it are the prerequisites of the main course. For example:

1 2
2 Python,CT
3 MLF,Maths-2,Stats-2

Here, CT is a prerequisite of Python, Maths-2 and Stats-2 are prerequisites of MLF. Represent each
sequence as a list, and store all these lists in L . That is, L should be a list of lists. Select the
correct implementation of this program.

Options
(a)

1 n = int(input())
2 L = input().split(',')

(b)

1 n = int(input())
2 L = [ ]
3 for i in range(n):
4    line = input().split(',')
5    L.append(line)

(c)

1 n = int(input())
2 for i in range(n):
3    line = input().split(',')
4    L.append(line)

(d)

1 n = int(input())
2 L = [ ]
3 for i in range(n):
4    L.append(input())
Answer
(b)

Feedback
Option-(a) is incorrect as it accepts only one sequence and stores it in L .
Option-(b) is correct. Each sequence is represented as a list — line — which is then
appended to L .
Option-(c) is incorrect as L hasn't been initialized.
Option-(d) is incorrect as it represents the sequence as a string and not a list of strings.
Question-2
Statement
Using the list L obtained in the previous question, write a function named prereq that accepts
L as an argument. It should return a dictionary D with the following structure:

key: course name


value: list of courses that are prerequisites for the above course (key)

Note that this dictionary D will have keys that represent the courses that have at least one
prerequisite. Recall that L is a list of lists. Select all the correct implementations of this function.
(MSQ)

Options
(a)

1 def prereq(L):
2    D = dict()
3    for clist in L:
4        main = clist[0]
5        pre_main = clist[1: ]
6        D[main] = pre_main
7    return D

(b)

1 def prereq(L):
2    D = dict()
3    for i in range(len(L)):
4        D[L[0]] = L[1: ]
5    return D

(c)

1 def first(L):
2    return L[0]
3
4 def rest(L):
5    return L[1: ]
6
7 def prereq(L):
8    D = dict()
9    for clist in L:
10        main = first(clist)
11        pre_main = rest(clist)
12        D[main] = pre_main
13    return D
(d)

1 def prereq(L):
2    D = dict()
3    main = L[0]
4    for course in L[1: ]:
5        if main in D:
6            D[main].append(course)
7        else:
8            D[main] = [course]
9    return D

Answer
(a), (c)

Feedback
Options (a) and (c) are correct. Both are based on the same idea. clist is one of the inner lists of
L and represents a main course and its prerequisites. clist[0] is the main course. clist[1: ]
is the list of all courses that are prerequisites of the main course. clist[0] becomes the key of
the dictionary D , while clist[1: ] becomes its value.

Options (b) and (d) are wrong because they miss the fact that L is a list of lists.
Question-3
Statement
We now have the dictionary D obtained from the previous question. Recall that each key of the
dictionary corresponds to one of the courses that have at least one prerequisite.

Write a function named get_basic that accepts the dictionary D as an argument. It should
return the set of all basic courses in the program, that is, courses that do not have any
prerequisites. The value returned by the function should be a set and not a list .

Select all correct implementations of this function. Refer to the points (1), (2), (3) and (4) in the
common-data to answer this question. (MSQ)

Options
(a)

1 def get_basic(D):
2    basic = [ ]
3    for main in D:
4        basic.append(main)
5        for course in D[main]:
6            if course not in D:
7                basic.append(course)
8    return set(basic)

(b)

1 def get_basic(D):
2    basic = [ ]
3    for main in D:
4        for course in D[main]:
5            if course not in D:
6                basic.append(course)
7    return set(basic)

(c)

1 def get_basic(D):
2    all_courses = set()
3    for main in D:
4        all_courses.add(main)
5        for course in D[main]:
6            all_courses.add(course)
7
8    basic = set()
9    for course in all_courses:
10        if course not in D:
11            basic.add(course)
12    return basic
(d)

1 def get_basic(D):
2    all_courses = set()
3    for main in D:
4        all_courses.add(main)
5        for course in D[main]:
6            all_courses.add(course)
7            
8    prereq_courses = set()
9    for main in D:
10        prereq_courses.add(main)
11
12 basic = all_courses - prereq_courses
13    return basic

Answer
(b), (c), (d)

Feedback
Option-(a) is wrong because it ends up adding courses which have prerequisites also. The rest of
the options are correct. The basic idea is this:

Each basic course in the program is a prerequisite for at least one course. This implies that
every basic course in the program should appear in the list of values for at least one key in
the dictionary D .
But we also know that the keys of the dictionary are the courses that have at least one
prerequisite.

Therefore, If a course c is present in the list D[main] but is itself not a key of D , then this has to
be a basic course. Option (b) uses these two facts to generate the set basic .

Options (c) and (d) take a slightly different approach. They generate the set of all courses in the
program: all_courses . Option-(c) filters the basic courses from this set. Option-(d) subtracts all
courses that have prerequisites, so that what we are left with is the set of basic courses.
Question-4
Statement
Write a function named pinnacle that accepts the prerequisites dictionary D as an argument. It
should return a set of courses, where each course in this set has at least one prerequisite, but is
not a prerequisite of any other course in the program.

Options
(a)

1 def pinnacle(D):
2    S = set()
3    for main_1 in D:
4        flag = True
5        for main_2 in D:
6            if main_1 == main_2:
7                continue
8            if main_1 in D[main_2]:
9                flag = False
10                break
11        if flag:
12            S.add(main_1)
13    return S

(b)

1 def pinnacle(D):
2    S = set()
3    for main_1 in D:
4        flag = True
5        for main_2 in D:
6            if main_1 in D[main_2]:
7                flag = False
8                break
9        if flag:
10            S.add(main_1)
11    return S

(c)
1 def pinnacle(D):
2    S = set()
3    for main_1 in D:
4        flag = False
5        for main_2 in D:
6            if main_1 in D[main_2]:
7                flag = True
8                break
9        if flag:
10            S.add(main_1)
11    return S

Answer
(a), (b)

Feedback
The basic idea is to have a nested loop that goes over a pair of courses (main_1, main_2) . Both
of these courses are drawn from the keys of D . This is because we are looking for courses that
have at least one prerequisite. For main_1 to be a pinnacle course, it should not occur in any of
the lists D[main_2] . If it occurs in even one list, then it becomes the prerequisite of some course
in the program.

Options-(a) and (b) are essentially the same with a subtle difference. The following conditional
statement is present in (a) and absent in (b):

1 if main_1 == main_2:
2    continue

We can actually dispense with this statement as a course cannot be its own prerequisite.
Question-5
Statement
A course is called deep if it has the maximum number of prerequisites among all courses in the
program. Write a function named get_deep that accepts the prerequisites dictionary D as
argument. It should return a list of deep courses. You can assume that there is at least one course
in the program which has at least two prerequisites.

Options
(a)

1 def get_deep(D):
2    deep = [ ]
3    maxpre = 1
4    for main in D:
5        if len(D[main]) > maxpre:
6            maxpre = len(D[main])
7            deep = [main]
8    return deep

(b)

1 def get_deep(D):
2    deep = [ ]
3    maxpre = 1
4    for main in D:
5        if len(D[main]) > maxpre:
6            maxpre = len(D[main])
7            deep = D[main]
8        elif len(D[main]) == maxpre:
9            deep.append(main)
10    return deep

(c)

1 def get_deep(D):
2    deep = [ ]
3    maxpre = 1
4    for main in D:
5        if len(D[main]) > maxpre:
6            maxpre = len(D[main])
7            deep = [main]
8        elif len(D[main]) == maxpre:
9            deep.append(main)
10    return deep
Answer
(c)

Feedback
We only need to check for courses that have prerequisites. This translates to looping through the
keys of D . The number of prerequisites of main is given by len(D[main]) . This problem is
essentially about finding all those keys in the dictionary that have the maximum value. Since there
could be multiple courses that share the same maximum number of prerequisites, we are
returning a list of such courses.

You might also like