The Joy of Computing using Python Week 5 Assignment

                     

Course name: The Joy of Computing Using Python

Link to Enroll: Click here

This is an assignment for The Joy of Computing using Python Week 5 Assignment


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


This is an assignment for The Joy of Computing using Python Week 5 Assignment


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


This is an assignment for The Joy of Computing using Python Week 5 Assignment


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


This is an assignment for The Joy of Computing using Python Week 5 Assignment


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


This is an assignment for The Joy of Computing using Python Week 5 Assignment


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


This is an assignment for The Joy of Computing using Python Week 5 Assignment


Python Assignment 5 Programming Solutions

Question 1
You 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)
Input
The 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}

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

This is an assignment for The Joy of Computing using Python Week 5 Assignment


Question 2
You 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]
Explanation
Elements 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))

This is an assignment for The Joy of Computing using Python Week 5 Assignment


Question 3
You 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]
output
2
Explanation
Since 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

This is an assignment for The Joy of Computing using Python Week 5 Assignment

More Weeks solutions of this course: https://progies.in/answers/nptel/the-joy-of-computing-using-python

More NPTEL Solution: https://progies.in/answers/nptel


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


This is an assignment for The Joy of Computing using Python Week 5 Assignment