**NPTEL THE ****JOY OF COMPUTING USING PYTHON** ** **A fun-filled whirlwind tour of 30 hrs, covering everything you need to know to fall in love with the most sought-after skill of the 21st century. The course brings programming to your desk with anecdotes, analogies, and illustrious examples.

**NPTEL THE JOY OF COMPUTING USING PYTHON** is a MOOC course offered by IIT Ropar on the NPTEL platform. This course helps the students to achieve learning how to practice and culture the art of programming with Python as a language. At the end of the course, we introduce some of the current advances in computing to motivate the enthusiastic learner to pursue further directions. The course is developed by

**Prof. Sudarshan Iyengar**is a Ph.D. from the Indian Institute of Science and is currently working as an assistant professor at IIT Ropar and has been teaching this course for the past 4 years.

Elective course for UG, PG, BE, ME, MS, M.Sc, PhD**INTENDED AUDIENCE:****Requirements/Prerequisites:**Basic programming skills (in Python), algorithm design, basics of probability & statistics**INDUSTRY SUPPORT:**Data science companies and many other industries value machine learning skills.

**CRITERIA TO GET A CERTIFICATE**

Average assignment score = 25% of the average of the best 8 assignments out of the total 12 assignments given in the course joy of computing using python nptel assignment answers.

Exam score = 75% of the proctored certification exam score out of 100

Final score = Average assignment score + Exam score

Students will be eligible for CERTIFICATE ONLY IF AVERAGE ASSIGNMENT SCORE >=10/25 AND EXAM SCORE >= 30/75. If any of the 2 criteria are not met, the student will not get the certificate even if the Final score >= 40/100.

## NPTEL THE JOY OF COMPUTING USING PYTHON ASSIGNMENT WEEK 12 ANSWERS:-

Contents

**Q1.** Prof. S is on a trip with a set of students from his course. He has several packets of

**Code:- **

**FOR QUIZ ASSIGNMENT VISIT**:- NPTEL THE JOY OF COMPUTING PROGRAMMING ASSIGNMENT 2021

```
l = list(map(int,input().split()))
m = 0
total = sum(l)
if total % len(l) != 0:
print('-1',end='')
else:
avg = total//len(l)
for i in l:
if i > avg:
m += (i-avg)
print(m,end='')
```

**Q2.** Four persons A, B, C and D are on a trip. Each of the four have one square shaped suitcase

**Code:- **

**FOR QUIZ ASSIGNMENT VISIT**:- NPTEL THE JOY OF COMPUTING PROGRAMMING ASSIGNMENT 2021

```
sb=list()
for a in(input().split()):
sb.append(int(a))
sb.sort()
print(sb[-1]+sb[-2],end='')
```

**Q3.** Krishna and Balarama are playing a game of strength. There are n walls arranged in a linear fashion and each wall has a strength value associated with it (the value is guaranteed to be distinct for each wall). The strength value indicates the amount of strength required to break a wall.

**Code:- **

```
for t in range(int(input())):
n = int(input())
a = [int(i1) for i1 in input().split()]
mn,mnidx,mx,mxidx=a[0],0,a[0],0
for i in range(0,n):
if a[i]<mn:
mn=a[i]
mnidx=i
if a[i]>mx:
mx=a[i]
mxidx=i
val1 = max(mnidx,mxidx)
val2 = min(mnidx,mxidx)
print(min([val2-val1+1+n,val1+1,n-val2]))
```

## NPTEL THE JOY OF COMPUTING USING PYTHON ASSIGNMENT WEEK 11 ANSWERS:-

**Q1.** Write a program to read a comma-separated set of words as input and print the words in a comma-separated sequence after sorting them alphabetically.

**Code:-**

```
print(",".join(sorted(input().split(","))),end="")
```

**Q2.** Given a string, remove all the punctuations in it and print the remaining characters.

**Code:-**

```
punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
my_str = input()
no_punct = ""
for char in my_str:
if char not in punctuations:
no_punct = no_punct + char
print(no_punct,end="")
```

**Q3.** You are given a set of riddles. Each riddle will be in the form of a long string of jumbled alphabets (all the alphabets will be lowercase). As an answer to the riddle, you have to tell the length of the longest possible palindrome that can be formed from the given alphabet sequence. Note that the alphabets can be arranged in any sequence to form the palindrome.

**Code:-**

```
def findLongestPalindrome(strr):
count = [0]*256
for i in range(len(strr)):
count[ord(strr[i])] += 1
beg = ""
mid = ""
end = ""
ch = ord('a')
while ch <= ord('z'):
if (count[ch] & 1):
mid = ch
count[ch] -= 1
ch -= 1
else:
for i in range(count[ch]//2):
beg += chr(ch)
ch += 1
end = beg
end = end[::-1]
return beg + chr(mid) + end
n=int(input())
L=[]
for a in range(n):
L.append(len(findLongestPalindrome(input())))
for b in L:
print(b)
```

## NPTEL THE JOY OF COMPUTING USING PYTHON ONLINE TEST ANSWERS:-

**Q1.** You are given a string *S*. Suppose a character ‘*c*‘ occurs consecutively *X *times in the string. Write a program to replace these consecutive occurrences of the character with ‘*(X,c) *‘ in the string.

**Input Format**

A single line of input consisting of the string .

**Output Format**

A single line of output consisting of the modified string.

**Sample Input 0:**

1222311

**Sample Output 0:**

**Code:-**

```
#for week 12 codes join us on telegram
s = input()
new = True
curC = ''
cnt = 0
for i in s:
if i == curC:
cnt = cnt + 1
else:
if curC != '':
print ('(%d,%s)'% (cnt,curC),end=''),
curC = i
cnt = 1
print ('(%d,%s)'% (cnt,curC) )
```

## NPTEL THE JOY OF COMPUTING USING PYTHON ONLINE TEST ANSWERS:-

**Q1.** Given a string and width, write a program to convert the given string into different strings of given width separated by newline.

**Input Format**

The first line contains a string.

The second line contains the width.

**Output Format**

Print the text wrapped paragraph.

**Sample Input 0:**

ABCDEFGHIJKLIMNOQRSTUVWXYZ

4

**Code:-**

```
import textwrap
s = input()
w = int(input())
wrapper = textwrap.TextWrapper(width=w)
word_list = wrapper.wrap(text=s)
# Print each line.
for element in word_list:
print(element)
```

## NPTEL THE JOY OF COMPUTING USING PYTHON ASSIGNMENT WEEK 10 ANSWERS:-

**Q1.** Given a large string and a set of small strings, determine if it is possible to exactly

decompose the given large string into the set of small strings. (Assume that all the strings

are given in lowercase)

**Code:-**

```
s=input()
a=input().split()
b=""
for i in a:
b+=i
if(set(s)==set(b) and len(s)==len(b)):
print("Yes",end="")
else:
print("No",end="")
```

**Q2.** The alphabets are enumerated as A = 0, B = 1, C = 2, … , Z = 25. Consider an encryption

scheme where a character with value C i in the plaintext is replaced by another character

with value C j using the formula C j = (C i + 5) % 26. After replacement, the resulting string is

shuffled (permuted) at random to obtain the cipher text.

Given a plain text and a possible cipher text, your task is to determine whether the cipher

text can be formed from the plain text using the above mentioned scheme.

(Assume that all the strings are in uppercase)

**Code:-**

**If you want help with 10th october online programming tests and extra material, for final exam preparation, take our membership for a better score in exam read more here:- Final Exam Membership **

```
m=input()
n=input()
s=''
for i in range(65,91):
s+=chr(i)
ans=''
for i in m:
c=s.index(i)
cj=(c+5)%26
ans+=s[cj]
if(sorted(n)==sorted(ans)):
print("Yes",end='')
else:
print("No",end='')
```

## NPTEL THE JOY OF COMPUTING USING PYTHON ASSIGNMENT WEEK 9 ANSWERS:-

**Q1.** Given a configuration of a Snakes and Ladders board in the form of a mapping, determine the number of snakes and ladders.

**Code:-**

**FOR QUIZ ASSIGNMENT VISIT**:- NPTEL THE JOY OF COMPUTING PROGRAMMING ASSIGNMENT 2021

```
con=input().split(",")
d={}
snake=0
ladder=0
for c in con:
k=c.split(":")
d[int(k[0])]=int(k[1])
for key in d:
if key>d[key]:
snake+=1
else:
ladder+=1
print(snake,ladder,end="")
```

**Q2.** Given the configuration of a Snakes and Ladders board and a series of numbers obtained when the die is rolled, determine if the sequence of the die rolls leads to a win.

**Code:-**

**If you want help with 10th october online programming tests and extra material, for final exam preparation, take our membership for a better score in exam read more here:- Final Exam Membership **

```
con=input().split(",")
throw=input().split(",")
d={}
for c in con:
y=c.split(":")
d[int(y[0])]=int(y[1])
p=0
for t in throw:
p+=int(t)
if p in d.keys():
p=d[p]
if p>99:
print("Yes",end="")
else:
print("No",end="")
```

## NPTEL THE JOY OF COMPUTING USING PYTHON ASSIGNMENT WEEK 8 ANSWERS:-

**Q1.** Given a string as input, determine if it is a palindrome or not. (Ignore the spaces, case and any punctuation or special characters present in the string).

**Code:-**

**FOR QUIZ ASSIGNMENT VISIT**:- NPTEL THE JOY OF COMPUTING PROGRAMMING ASSIGNMENT 2021

```
def isPalindrome(s):
return new_string.lower()==new_string[::-1].lower()
s=input()
new_string=''.join(char for char in s if char.isalnum())
ans=isPalindrome(s)
if ans:
print("Yes",end="")
else:
print("No",end="")
```

**Q2.** Given two strings as input, determine if they are anagrams or not. (Ignore the spaces, case and any punctuation or special characters).

**Code:-**

```
str_1=input()
str_2=input()
sorted_str1=[]
sorted_str2=[]
k=sorted(str_1.upper())
l=sorted(str_2.upper())
for i in k:
if i.isalnum():
sorted_str1.append(i)
for j in l:
if j.isalnum():
sorted_str2.append(j)
if sorted_str1==sorted_str2:
print("Yes", end="")
else:
print("No", end="")
```

**If you want help with 10th october online programming tests and extra material, for final exam preparation, take our membership for a better score in exam read more here:- Final Exam Membership **

**Q3.** Given an English sentence, check whether it is a pangram or not. A pangram is a sentence containing all 26 letters in the English alphabet

**Code:-**

```
def checkPangram(s):
List = []
# create list of 26 charecters and set false each entry
for i in range(26):
List.append(False)
#converting the sentence to lowercase and iterating
# over the sentence
for c in s.lower():
if not c == " ":
# make the corresponding entry True
List[ord(c) -ord('a')]=True
#check if any charecter is missing then return False
for ch in List:
if ch == False:
return False
return True
# Driver Program to test above functions
sentence = input()
if (checkPangram(sentence)):
print("Yes")
else:
print("No")
```

## NPTEL THE JOY OF COMPUTING USING PYTHON ASSIGNMENT WEEK 7 ANSWERS:-

**Q1.** Given a matrix with M rows and N columns, you are required to check if the matrix is a Zero-One Matrix. A Zero-One or a Binary matrix is a matrix in which all the elements are either 0 or 1.

**Code:-**

```
p = ['0','1']
flag = 0
l, s = map(int,input().split())
for i in range(l):
t = input().split()
for item in t:
if item not in p:
flag = 1
if(flag):
print('No',end='')
else:
print('Yes',end='')
```

**FOR QUIZ ASSIGNMENT VISIT**:- NPTEL THE JOY OF COMPUTING PROGRAMMING ASSIGNMENT 2021

**Q2.** Given an integer input ‘n’, print a palindromic triangle of n lines as shown in the example.

**Code:-**

```
n=int(input())
for i in range(1,n+1):
for j in range(1,i+1):
print(j,end="")
if i==j:
for k in range(i-1,0,-1):
print(k,end="")
if i!=n:
print("")
```

**Q3.** Given a N X N square matrix, transform the Matrix into a Lower Triangular Matrix by setting all the elements except the lower triangle as zero.

**Code:-**

```
n=int(input())
q=[]
a=[]
for i in range(n):
q.append(input().split())
for i in range(n):
for j in range(0,i+1):
a.append(q[i][j])
for j in range(i+1,n):
a.append("0")
s=0
for i in range(n):
for j in range(n):
if j!=n-1:
print(a[s],"",end="")
else:
print(a[s],end="")
s+=1
if i!=n-1:
print("")
```

## NPTEL THE JOY OF COMPUTING USING PYTHON ASSIGNMENT WEEK 6 ANSWERS:-

**Q1.** Given two integers r and c, indicating the number of rows and columns, print a two-dimensional matrix such that the elements of the matrix are in an increasing sequence from 1 to rXc, in a row-major order.

**Code:-**

```
a,b=input().split()
a,b=int(a),int(b)
c=1
for i in range(1,a+1):
for j in range(1,b+1):
if j!=b:
print(c,"",end="")
else:
print(c,end="")
c+=1
if i!=a:
print("")
else:
print("",end="")
```

**FOR QUIZ ASSIGNMENT VISIT**:- NPTEL THE JOY OF COMPUTING PROGRAMMING ASSIGNMENT 2021

**Q2.** Given an integer input ‘n’, print a number triangle of n lines as shown in the example.

**Code:-**

```
n=int(input())
for i in range(1,n+1):
for j in range(1,i+1):
print(i,end="")
if i!=n:
print("")
else:
print("",end="")
```

**Q3.** Given a N X N square matrix, determine if it is a Symmetric Matrix.

**Code:-**

```
n=int(input())
mat=[]
f=0
for i in range(n):
mat.append(input().split())
#print(mat)
for a in range(len(mat)):
for b in range(n):
if mat[a][b]!=mat[b][a]:
f=1
if f==0:
print("Yes",end="")
else:
print("No",end="")
```

## NPTEL THE JOY OF COMPUTING USING PYTHON ASSIGNMENT WEEK 5 ANSWERS:-

**Q1.** Given a list of integers and a value k, print the number in the list that appears exactly k times. (It is guaranteed that there’s exactly one integer that appears k times).

**Code:-**

```
n=input().split()
k=int(input())
for i in set(n):
if n.count(i)==k:
print(i,end="")
```

**FOR QUIZ ASSIGNMENT VISIT**:- NPTEL THE JOY OF COMPUTING PROGRAMMING ASSIGNMENT 2021

**Q2.** Given a list of integers and a value k, print the kth largest element in the list. (All the elements in the list are guaranteed to be distinct).

**Code:-**

```
ar = input().split()
k=int(input())
ans=[]
for i in ar:
ans.append(int(i))
ans.sort(reverse=True)
#print(*ans)
print(ans[k-1],end="")
```

**Q3.** Given a list of n integers, print a new list such that every element in the new list represents the cumulative sum of all the elements until that position.

**Code:-**

```
n=input().split()
ans=[]
a=0
for i in n:
a+=int(i)
ans.append(a)
print(*ans,end="")
```

## NPTEL THE JOY OF COMPUTING USING PYTHON ASSIGNMENT WEEK 4 ANSWERS:-

**Q1.** Write a program that takes a number as input and prints its factorial as output.

**Code:-**

```
n=int(input())
fact=1
for a in range(1,n+1):
fact*=a;
print(fact,end="")
```

**FOR QUIZ ASSIGNMENT VISIT**:- NPTEL THE JOY OF COMPUTING PROGRAMMING ASSIGNMENT 2021

**Q2.** Given a list of n integers, count the number of integers in the list that are either a multiple of 3 or a multiple of 5. (All the numbers are guaranteed to be distinct).

**Code:-**

```
n=input().split()
c=0
for a in n:
if int(a)%5==0 or int(a)%3==0:
c+=1
print(c,end="")
```

**Q3.** Given a list of binary numbers (0s and 1s), determine the number of possible arrangements of distinct binary sequences using given 0s and 1s.

**Code:-**

```
import math
n = input().split()
ans = math.factorial(len(n))//(math.factorial(n.count("1"))*math.factorial(n.count("0")))
print(ans,end="")
```

## NPTEL THE JOY OF COMPUTING USING PYTHON ASSIGNMENT WEEK 3 ANSWERS:-

**Q1.** Write a program to accept the length and breadth of a rectangle and print its area.

**Code:-**

```
a=input().split()
print(int(a[0])*int(a[1]),end="")
```

**Q2.** Write a program that takes 3 integers as input and prints the difference between the largest and smallest value among the three.me as input and print “Hello <input name> ! Welcome to JOCP” as the output.

**Code:-**

**FOR QUIZ ASSIGNMENT VISIT**:- NPTEL THE JOY OF COMPUTING PROGRAMMING ASSIGNMENT 2021

```
n=input().split()
print(abs(int(max(n))-int(min(n))),end="")
```

**Q3.** Write a program that takes 3 integers as input and checks whether they can form the sides of a right angled triangle or not. Print YES if they can form a right angled triangle. NO, otherwise.

**Code:-**

```
s = list(map(int, input().split()))
bh=[a for a in s if a!=max(s)]
if max(s)**2== (bh[0]**2+bh[1]**2):
print("YES",end="")
else:
print("NO",end="")
```

## THE JOY OF COMPUTING USING PYTHON ASSIGNMENT WEEK 2 ANSWERS:-

**Q1.** Write a program to accept a name as input and print “Hello <input name> ! Welcome to JOCP” as the output.

**Code:-**

```
name=input()
print("Hello",name,"! Welcome to JOCP",end="")
```

**Q2.** Which of these statements in python will throw anerror when Write a program to accept a number as input and display the square of the number as the output.

**Code:- **

```
number=int(input())
print(number**2,end="")
```

**Q3.** A newly opened store is offering a 15% discount on the MRP value of any product.

**Code:- **

```
n=int(input())
print((85/100)*n,end="")
```

**FOR QUIZ ASSIGNMENT VISIT**:- NPTEL THE JOY OF COMPUTING PROGRAMMING ASSIGNMENT 2021

**Also check :- INTERNSHIP OPPORTUNITIES**