0% found this document useful (0 votes)
11 views4 pages

Week 5

Uploaded by

riyaranirr2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views4 pages

Week 5

Uploaded by

riyaranirr2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

The Joy of programming using Python

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":

- Rock wins against Scissors.


- Scissors wins against Paper.
- Paper wins against Rock.
- Rock wins against Lizard.
- Lizard wins against Spock.
- Spock wins against Scissors.
- Scissors wins against Lizard
- Lizard wins against Paper
- Paper wins against Spock.
- Spock wins against Rock.

We have total 5 tools that can be chosen in the game.

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

3. How does Bubble Sort compare elements in an array?


a. Compares adjacent elements and swaps if they are in the wrong order
b. Compares random elements in the array
c. Compares all elements with the first element
d. Compares elements in reverse order
Answer: Option a

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

8. Select the correct statements among the following


a. In the presence of duplicate elements in a list, binary search always finds the
first occurrence of the element in the list
b. In the presence of duplicate elements in a list, binary search may or may
not find the first occurrence of the element in the list
c. In the presence of duplicate elements in a list, the existing binary search
algorithm with minor modifications can be used to find the first occurrence of
the target element in the list
d. In the presence of duplicate elements in a list , the existing binary search can be
used to find any kth(k can be any number from 1 to number of times the element has been
repeated) of target element in the list
Answer: Option b, c

9. Swapping of two numbers is an intermediate step in any sorting algorithm, In the


lecture we use a temporary variable to swap two elements in the list.
Can swapping of two numbers be done without using a temporary variable?
a. Yes
b. No
c. Depends on size of list
d. Depends on nature of elements in the list
Answer: option a
Programming problems

Write a program that takes a string as input from the user and prints the frequency of
each letter in the string.

User Input: ram is a boy

r–1

a–2

m–1

i -1

s–1

b–1

o–1

y-1

You might also like