Week 5
Week 5
Rupesh Kumar
23 August 2024
6-8pm
1. What happens if you try to add a new key-value pair to a dictionary using a key that
already exists in the dictionary?
a. The new value overwrites the existing value
b. An error is raised
c. The dictionary becomes immutable
d. The new value is appended to the existing value
Answer: Option a
The new value overwrites the existing value.
2. The extended version of "Rock, Paper, Scissors" with "Lizard" and "Spock" was
popularized by the TV show "The Big Bang Theory." Here are the rules for "Rock,
Paper, Scissors, Lizard, Spock":
Each option can defeat two other options, lose to two other options, and draw with
one other option.
Now, the major steps involved in the algorithm for coding a rock-paper-scissors game
as discussed in the lecture include:
1) Assigning values to the choices: The algorithm involves assigning numerical
values to the rock, paper, and scissors choices, such as 1 for rock, 2 for paper, and 3
for scissors. - Now the program has to assign 5 numerical values to the choices
2) Inputting user choices: The algorithm prompts the users to input their choices for
the game, such as rock, paper, or scissors.
3) Secret bit positions: The algorithm uses secret bit positions to determine the winner
of the game, which involves bitwise operations to compare the choices and determine
the outcome. - We are comparing for 5 bits
4) Coding the rules: The algorithm includes coding the rules for determining the
winner based on the choices made by the players, such as using if-else conditions to
compare the choices and declare the winner.
5) Running the program: The algorithm allows for running the program to test the
rock- paper-scissors game with different user inputs and provides examples of the
game outcomes.
What would be the possible change in steps for running “Rock, Paper, Scissors,
Lizard, Spock"?
a. Step 1 would involve assigning 5 values to choices instead of 3
b. The bitwise operations involved in Step 3 would involve modulo operation
with 5 instead of 3
c. None of the above
d. All the above
Answer: options a, b
4. To sort a list in ascending order when does Bubble Sort exhibit poor performance?
a. When the list is sorted in ascending order
b. When the list is sorted in descending order
c. When the list contains unique elements
d. Bubble Sort always exhibits poor performance
Answer: Option b
5. How many passes does Bubble Sort make through the array in the worst-case scenario
for sorting n elements?
a. n
b. n-1
c. 2n
d. n2
Answer: Option b (n-1)
6. How many pairs of adjacent elements are compared in a single pass of the bubble sort
algorithm?
a. n
b. n-1
c. n/2
d. 2n
Answer: Option b (n-1)
7. The following steps are required to implement the binary search for an array sorted in
ascending order.
1)Initialize Pointers:
- Set two pointers, `low` and `high`, initially pointing to the start and end of
the array, respectively.
2) Calculate Midpoint:
- Calculate the midpoint index using the formula `mid = (low + high) // 2`.
3) Compare Midpoint Element:
- Compare the element at the midpoint with the target element you are
searching for:
- If the midpoint element is equal to the target, the search is successful, and
you can return the index.
- If the midpoint element is less than the target, update `low` to `mid + 1` and
continue the search in the right half.
- If the midpoint element is greater than the target, update `high` to `mid - 1`
and continue the search in the left half.
4) Repeat:
- Repeat steps 3-4 until the `low` pointer is greater than the `high` pointer.
This means the search range has become empty.
5) Return Result:
- If the target element is found, return its index. If the target is not found,
return a sentinel value (e.g.,-1) to indicate that the element is not in the array.
Which steps would require modification if the array were sorted in descending order?
a. Step 2
b. Step 4
c. Steps 3 and 4
d. No modification in steps
Answer: option c
Write a program that takes a string as input from the user and prints the frequency of
each letter in the string.
r–1
a–2
m–1
i -1
s–1
b–1
o–1
y-1