# The Joy of Computing using Python | Week 5

**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

* The material and content uploaded on this website are for general information and reference purposes only. Please do it by your own first. COPYING MATERIALS IS STRICTLY PROHIBITED.