Peer Instruction: Tuples Multiple Choice QuestionsΒΆ
- It never fails
- Incorrect! Because 'num = lst[i]' goes out of range when the list is empty or the value is not found in the list
- It fails when the list is empty
- Incorrect! Because 'num = lst[i]' goes out of range when the list is empty or the value is not found in the list
- It fails when the value is not found in the list
- Incorrect! Because 'num = lst[i]' goes out of range when the list is empty or the value is not found in the list
- Both B and C will fail
- Correct! Because 'num = lst[i]' goes out of range when the list is empty or the value is not found in the list
11-9-1: In what situation does the below code fail?
def find(lst, value):
'''(list, value) -> int
Return the first occurrence of value in lst.
If value is not found, return -1.
>>> find([20, 40, 60], 40)
1
'''
i = 0
num = lst[i]
while num != value:
i = i + 1
num = lst[i]
if i < len(lst):
return i
else: return -1
- is_ok([[1, 2], [2, 3]], [1, 2, 3])
- Incorrect! Because 2 is in two different groups- [1, 2] and [2, 3]
- is_ok([[1, 2], [4]], [1, 2, 3])
- Incorrect! Because 3 is not in the group_list and 4 is not the class_list
- is_ok([], [1, 2, 3])
- Incorrect! Because there's no group list at all
- is_ok([[1, 2], [4]], [1, 2, 3, 4])
- Incorrect! Because 3 has not be assigned to any group
- None will return True
- Correct! Because there's no option where the group list has assigned every student to exactly one group
11-9-2: To represent groups of students, we can use a nested list. For example, in the following list, students 1, 3, and 4 are together in a group, and student 2 is working alone: [[1, 3, 4], [2]]. In the following code, which call would return True?
def is_ok(group_list, class_list):
'''(list of list of int, list of int) -> bool
Return True iff every student in class_list is in exactly
one group according to group_list.
'''
You have attempted of activities on this page