# The Joy of Computing using Python | Week 5

**Session: JULY-DEC 2023**

**Course Name: The Joy of Computing using Python**

**Course Link: Click Here**

**These are answers of The Joy of Computing using Python Assignment 5 Answers**

**These are answers of The Joy of Computing using Python Assignment 5 Answers**

**Q1. Which is the fastest sorting algorithm?**

Bubble Sort

Bucket Sort

Quick Sort

Insertion Sort

**Answer: Quick Sort**

**Q2. How can you remove all items from a dictionary?**

dict.clear()

del dict

dict.remove_all()

dict.pop()

**Answer: dict.clear()**

**Q3. What happens if you try to add a new key to a dictionary that already exists?**

The key and its associated value will be updated.

The key and its associated value will be added.

The key will be added, but the associated value will remain unchanged.

An error will occur.

**Answer: The key and its associated value will be updated.**

**These are answers of The Joy of Computing using Python Assignment 5 Answers**

**Q4. Which of the following is true about dictionaries?**

There can be multiple same keys.

Every value must be unique.

Every key must be unique.

We can’t get every key from the dictionary.

**Answer: Every key must be unique.**

**Q5. What is the syntax to create a dictionary?**

D = []

D = {}

D = ()

D = dictionary()

**Answer: D = {}**

**Q6. What will be the output of the following code?**

‘a’ ‘b’ ‘c’

20 30 50

(‘a’, 20), (‘b’, 30), (‘c’, 50)

(‘a’, ‘b’, ‘c’) (20, 30, 50)

**Answer: (‘a’, 20), (‘b’, 30), (‘c’, 50)**

**These are answers of The Joy of Computing using Python Assignment 5 Answers**

**Q7. In the Monte Hall Problem, what is the probability of winning if you switch doors after the host reveals a goat behind one of the other doors?**

1/3

1/2

2/3

3/4

**Answer: 2/3**

**These are answers of The Joy of Computing using Python Assignment 5 Answers**

**Q8. In the Monte Hall Problem, what is the probability of winning if you do not switch doors after the host reveals a goat behind one of the other doors?**

1/3

1/2

2/3

3/4

**Answer: 1/3**

**These are answers of The Joy of Computing using Python Assignment 5 Answers**

**Q9. What is the name of the game show that the Monte Hall Problem was based on?**

Jeopardy

Wheel of Fortune

The Price is Right

Let’s Make a Deal

**Answer: Let’s Make a Deal**

**These are answers of The Joy of Computing using Python Assignment 5 Answers**

**Q10. Which module in Python can be used for Speech to Text conversion?**

SpeechRecognition

PyAudio

Wave

all of the above

**Answer: SpeechRecognition**

**These are answers of The Joy of Computing using Python Assignment 5 Answers**

**The Joy of Computing using Python Programming Assignmnet**

**Question 1You are given a string S. Write a function count_letters which will return a dictionary containing letters (including special character) in string S as keys and their count in string S as values.(input and output will be handled by us you just need to write the function and return the dictionary)InputThe Joy of computing**

**Output{‘T’: 1, ‘h’: 1, ‘e’: 1, ‘ ‘: 3, ‘j’: 1, ‘o’: 3, ‘y’: 1, ‘f’: 1, ‘c’: 1, ‘m’: 1, ‘p’: 1, ‘u’: 1, ‘t’: 1, ‘i’: 1, ‘n’: 1, ‘g’: 1}Explanation: T is appeared once in the string, similarly o is appeared 3 times in the string and so on. (You do not have to worry about the order of arrangement in your dictionary)**

**Solution:**

```
def count_letters(S):
ans = {}
for j in S:
if j in ans:
ans[j] += 1
else:
ans[j] = 1
return(ans)
```

**These are answers of The Joy of Computing using Python Assignment 5 Answers**

**Question 2You are given a list L. Write a function uniqueE which will return a list of unique elements is the list L in sorted order. (Unique element means it should appear in list L only once.)Input will be handled by usInput[1,2,3,3,4,4,2,5,6,7]Output[1,5,6,7]ExplanationElements 1,5,6,7 appears in the input list only once.**

**Solution:**

```
def uniqueE(L):
ans=list()
for a in L:
if L.count(a)==1:
ans.append(a)
return(sorted(ans))
```

**These are answers of The Joy of Computing using Python Assignment 5 Answers**

**Question 3You are given a list L. Write a program to print first prime number encountered in the list L.(Treat numbers below and equal to 1 as non prime)Input will be handled by us.Input[1,2,3,4,5,6,7,8,9]output2ExplanationSince 2 is the first prime number is list L, therefor it is printed.**

**Solution:**

```
prime=[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37,
41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89,
97, 101, 103, 107, 109, 113, 127, 131, 137, 139,
149, 151, 157, 163, 167, 173, 179, 181, 191, 193,
197, 199, 211, 223, 227, 229, 233, 239, 241, 251,
257, 263, 269, 271, 277, 281, 283, 293, 307, 311]
for j in L:
if j in prime:
print(j,end="")
break
```

**These are answers of The Joy of Computing using Python Assignment 5 Answers**

More Weeks of The Joy of Computing Using Python: Click here

More Nptel Courses: Click here

**Session Jan-Apr 2023**

**Course Name: The Joy of Computing using Python**

**Course Link: Click Here**

**These are answers of The Joy of Computing using Python Assignment 5 Answers**

**Q1. Binary search can be applied on ***_*.

a. Sorted list

b. Unsorted list

c. Both A and B

d. Any list with any elements

*_*.

**Answer: a. Sorted list**

**Q2. Which of the following is a Waveform Audio File Format.**

a. Wav

b. Wave

c. Wv

d. Waves

**Answer: a, b**

**Q3. Which of the following libraries can help us to convert audio into lyrics?**

a. speech_recognition

b. text_to_speech

c. speech_to_text

d. text_translate

**Answer: a. speech_recognition**

**These are answers of The Joy of Computing using Python Assignment 5 Answers**

**Q4. State True or False: In the monte hall problem, swapping the choice does not increase the chance of winning. (For the large number of experiments)**

a. Swapping will decrease the chance of winning.

b. Swapping will increase the chance of winning.

c. Swapping will not affect the chance of winning.

d. Swapping may or may not increase the chance of winning.

**Answer: b. Swapping will increase the chance of winning.**

**Q5. What is the correct way to initialize a dictionary?**

a. D = {a-10, b-20, c-30}

b. D = {‘a’-10, ‘b’-20, ‘c’-30}

c. D = {a:10, b:20, c:30}

d. D = {‘a’:10, ‘b’:20, ‘c’:30}

**Answer: d. D = {‘a’:10, ‘b’:20, ‘c’:30}**

**Q6. What is the correct syntax to get all the keys only from a dictionary d?**

a. d.key()

b. d.item()

c. d.value()

d. d.keys()

**Answer: d. d.keys()**

**These are answers of The Joy of Computing using Python Assignment 5 Answers**

**Q7. Which of the following is valid?**

a. D = {‘a’: {‘a’: 10}, ‘b’: 10}

b. D = {‘a’: ‘a’: 10, ‘b’: 10}

c. D = {‘a’: {‘a’: 10}, ‘b’: {‘b’: 10}}

d. D = {‘a’: ‘a’: 10, ‘b’: ‘b’: 10}

**Answer: a, c**

**Q8. For bubble sort, which of the following statements is true?**

a. If the list is sorted, the algorithm won’t work.

b. In each iteration consecutive pairs of elements are compared with each other.

c. Every element is compared with every other element in the list in each iteration.

d. Swapping never happens in bubble sort.

**Answer: b. In each iteration consecutive pairs of elements are compared with each other.**

**Q9. Which error is faced while accessing an element that is not there in a dictionary?**

a. KeyError

b. IndexError

c. RunTimeError

d. ValueError

**Answer: a. KeyError**

**These are answers of The Joy of Computing using Python Assignment 5 Answers**

**Q10. In dictionaries, d.items() will return _**

a. Pairs of all (key, value) together.

b. All (keys) and (values) separately.

c. All (values) and (keys) separately.

d. Pairs of all (value, key) together.

**Answer: a. Pairs of all (key, value) together.**

**The Joy of Computing using Python Programming Assignmnet**

**Question 1**

**write a Python program that finds the Greatest Common Divisor (GCD) of two numbers. Your program should take two input numbers from the user, and use a function named ‘gcd’ to find the GCD of those two numbers. Your program should then print out the GCD of the two numbers as the output.**

**Solution:**

```
def gcd(a, b):
if b == 0:
return a
else:
return gcd(b, a % b)
num1 = int(input())
num2 = int(input())
result = gcd(num1, num2)
print(result)
```

**These are answers of The Joy of Computing using Python Assignment 5 Answers**

**Question 2**

**Write a Python program that calculates the dot product of two lists containing the same number of elements. The program should take two lists of integers as input from the user, and then call a function named dot_product to find the dot product of the two lists. The dot_product function should take two lists a and b as input, and should return the dot product of those two lists.**

**Your program should first prompt the user to enter the two lists of integers, which should be entered as strings separated by spaces. Your program should then convert the input strings into lists of integers. Your program should then call the dot_product function with the two lists as arguments, and store the resulting dot product in a variable named result. Finally, your program should print out the dot product of the two lists as the output.**

**You can assume that the two input lists will always contain the same number of elements. Your program should output an error message and exit gracefully if the two lists have different lengths.In your implementation, you should define the dot_product function using a loop to calculate the dot product of the two input lists.**

**Solution:**

```
def dot_product(a, b):
if len(a) != len(b):
print("Error: the two input lists have different lengths.")
return None
else:
result = 0
for i in range(len(a)):
result += a[i] * b[i]
return result
# Get input from user
a_str = input()
b_str = input()
# Convert input strings to lists of integers
a = [int(x) for x in a_str.split()]
b = [int(x) for x in b_str.split()]
# Calculate dot product
result = dot_product(a, b)
# Output result
if result is not None:
print(result)
```

**These are answers of The Joy of Computing using Python Assignment 5 Answers**

**Question 3**

**Write a Python function that takes a string s as input and returns the length of the largest streak of 0s in the string. For example, if the input string is “10001000110000000001”, the function should return 6.**

**Input The input string s is guaranteed to contain only 0s and 1s.OutputThe function should return an integer, representing the length of the largest streak of 0s in the input string.Your program should also print the result.**

**Solution:**

```
def max_zero_streak(s):
max_streak = 0
current_streak = 0
for digit in s:
if digit == '0':
current_streak += 1
if current_streak > max_streak:
max_streak = current_streak
else:
current_streak = 0
return max_streak
s = input()
result = max_zero_streak(s)
print(result)
```

**These are answers of The Joy of Computing using Python Assignment 5 Answers**

More Weeks of The Joy of Computing using Python: Click Here

More Nptel courses: https://progiez.com/nptel/

**Session JUL-DEC 2022**

**Course name: The Joy of Computing Using Python**

Link to Enroll: Click here

**These are answers of The Joy of Computing using Python Assignment 5 Answers**

**These are answers of The Joy of Computing using Python Assignment 5 Answers**

**Q1. What is the correct way to initialize a dictionary?**

a. D = {a-10, b-20, c-30}

b. D = {‘a’-10, ‘b’-20, ‘c’-30}

c. D = {a:10, b:20, c:30}

d. D = {‘a’:10, ‘b’:20, ‘c’:30}

**Answer: d**

**Q2. What is the correct syntax to get all the keys only from a dictionary d?**

a. d.key()

b. d.item()

c. d.value()

d. d.keys()

**Answer: d**

**These are answers of The Joy of Computing using Python Assignment 5 Answers**

**Q3. Which of the following statements are true about dictionaries in python?**

a. The keys of a dictionary must be unique values.

b. The keys of a dictionary can or cannot be unique.

c. The values of a dictionary must be unique values.

d. The values of a dictionary can or cannot be unique.

**Answer: a, d**

**Q4. State True or False: In the monte hall problem, swapping the choice does not increase the chance of winning.**

a. True

b. False

**Answer: b**

**These are answers of The Joy of Computing using Python Assignment 5 Answers**

**Q5. In dictionaries, d.items() will return**

a. Pairs of all (key, value) together.

b. All (keys) and (values) separately.

c. All (values) and (keys) separately.

d. Pairs of all (value, key) together.

**Answer: a**

**Q6. What will be the output of the following program?**

a. A dictionary with all letters as keys and 0 as values.

b. A dictionary with some letters as keys and 0 as values.

c. A dictionary with all letters as keys and some random numbers as values.

d. A dictionary with some letters as keys and some random numbers as values.

**Answer: d**

**These are answers of The Joy of Computing using Python Assignment 5 Answers**

**Q7. Binary search can be applied on ___.**

a. Sorted list in ascending order.

b. Unsorted list

c. Both A and B

d. Sorted list in descending order

**Answer: a, d**

**Q8. Which error is encountered while accessing a position that is not present in a list**

a. KeyError

b. IndexError

c. RunTimeError

d. ValueError

**Answer: b**

**These are answers of The Joy of Computing using Python Assignment 5 Answers**

**Q9. Which of the following command is correct to delete a key from a dictionary ‘d’**

a. d.pop(‘key’)

b. d.del(‘key’)

c. d.remove(‘key’)

d. d.delete(‘key’)

**Answer: a**

**Q10. Which of the following is/are correct regarding dictionaries?1) One can make a dictionary inside a dictionary in python.2) Keys in the dictionary are mutable.**

a. Option 1 is correct, option 2 is correct. Option 2 is the correct explanation for option 1.

b. Option 1 is correct, option 2 is incorrect. Option 2 is not the correct explanation for option 1.

c. Option 1 is correct, option 2 is correct. Option 2 is not the correct explanation for option 1.

d. None of these

**Answer: b**

**These are answers of The Joy of Computing using Python Assignment 5 Answers**

## Python Assignment 5 Programming Solutions

**Question 1You are given a string S. Write a function count_letters which accepts the string S and returns a dictionary containing letters (including special character) in string S as keys and their count in string S as values.(input and output is handled by us, you just need to write the function and return the dictionary)InputThe Joy of computingOutput{‘T’: 1, ‘h’: 1, ‘e’: 1, ‘ ‘: 3, ‘j’: 1, ‘o’: 3, ‘y’: 1, ‘f’: 1, ‘c’: 1, ‘m’: 1, ‘p’: 1, ‘u’: 1, ‘t’: 1, ‘i’: 1, ‘n’: 1, ‘g’: 1}**

**Solution:**

```
def count_letters(S):
d={}
for i in S:
d[i]=0
for i in S:
d[i]+=1
return d
if __name__ == "__main__":
S = input()
d = count_letters(S)
#~~~THERE IS SOME INVISIBLE CODE HERE~~~
```

**These are answers of The Joy of Computing using Python Assignment 5 Answers**

**Question 2You are given a list L. Write a function uniqueE which will return a list of unique elements is the list L in sorted order. (Unique element means it should appear in list L only once.)Input is handled by us.Input[1,2,3,3,4,4,2,5,6,7]Output[1,5,6,7]ExplanationElements 1,5,6,7 appears in the input list only once.**

**Solution:**

```
def uniqueE(L):
d = {}
for i in L:
d[i]=0
for i in L:
d[i]+=1
res = []
for key, value in d.items():
if value==1:
res.append(key)
res.sort()
return res
L = [int(i) for i in input().split()]
print(uniqueE(L))
```

**These are answers of The Joy of Computing using Python Assignment 5 Answers**

**Question 3You are given a list L. Write a program to print first prime number encountered in the list L.(Treat numbers below and equal to 1 as non prime)Input is handled by us.Input[1,2,3,4,5,6,7,8,9]output2ExplanationSince 2 is the first prime number is list L, therefor it is printed.**

**Solution:**

```
#~~~THERE IS SOME INVISIBLE CODE HERE~~~
def isprime(num):
if num<=1:
return False
if num==2:
return True
for i in range(3,num-1):
if num%i == 0:
return False
return True
for k in L:
if isprime(k):
print(k,end="")
break
else:
continue
```

**These are answers of The Joy of Computing using Python Assignment 5 Answers**

More NPTEL Solution: https://progiez.com/nptel

This content is uploaded for study, general information, and reference purpose only.