The Joy of Computing using Python NPTEL Week 11 Programming Assignment 1 Solution 2021 April | Week 11
Week 11 Programming Assignment 1 Due on 2021-04-08, 23:59 IST
Let ‘n’ be a positive even integer. Print the total number of ways to express ‘n’ as a sum of only even positive integers.
Input format :
The positive even integer
Output format :
the total number of ways to express ‘n’ as a sum of only even positive integers else invalid
Example
input
6
output
4
Explanation: There only 4 ways to express ‘6’ as sum of even integers:
First way: 6
Second way: 2 + 4
Third way: 4 + 2
Fourth way: 2 + 2 + 2 + 2
Assignment 1 Solution :
MOD = 1e9 + 7
def power(a, b, p) :
res = 1
a = a % p
while (b > 0) :
b=int(b)
if (b& 1) :
res = (1 * res * a) % p
b = b >> 1 # b = b/2
a = (1 * a * a) % p
return(res)
def countEvenWays(n) :
return power(2, n/2 - 1, MOD)
n=int(input())
if n%2==0:
print (int(countEvenWays(n)),end="")
else:
print("invalid",end="")
Week 11 Programming Assignment 2 Due on 2021-04-08, 23:59 IST
Give a string, remove all the punctuations in it and print only the words in it.
Input format :
the input string with punctuations
Output format :
the output string without punctuations
Example
input
“Wow!!! It’s a beautiful morning”
output
Wow Its a beautiful morning
Assignment 2 Solution :
string=input()
punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
for x in string.lower():
if x in punctuations:
string = string.replace(x, "")
print(string,end="")
Week 11 Programming Assignment 3 Due on 2021-04-08, 23:59 IST
print the number of binary sequences of length ‘n’ that have no consecutive 0’s
Input format :
A number 'n'
Output format :
number of binary sequences of length 'n' with no consecutive 0's else invalid
Example
input
3
output
5
Explanation:
When n = 3, number of binary digits is 3.
all possible combinations of 3 binary digits are
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
out of these eight possibilities, 5 have binary sequences and have no consecutive 0’s. Hence the number of binary sequences of length 3 is 5.
Assignment 3 solution :
def countStrings(n):
A=[0 for a in range(n)]
B=[0 for a in range(n)]
A[0] = B[0] = 1
for a in range(1,n):
A[a] = A[a-1] + B[a-1]
B[a] = A[a-1]
return(A[n-1] + B[n-1])
n=int(input())
if n>0:
print(countStrings(n),end="")
else:
print("invalid",end="")
0 Post a Comment:
Post a Comment
Please do not enter any spam link in the comment box