NPTEL The Joy of Computing Using Python.
Week 4 Assignment 01
r,c=input().split()
r=int(r)
c=int(c)
element =0
bank=[]
for i in range(1,r+1):
drop=[]
for j in range(1,c+1):
element=element+1
drop.append(element)
bank.append(drop)
if len(bank)==1:
for b in range(len(bank)):
print(*bank[b],sep = " ",end="")
else:
for b in range(len(bank)-1):
print(*bank[b],sep = " ")
last=len(bank)-1
print(*bank[last],sep = " ",end="")
Week 4 Assignment 02
k=int(input())
ans=1
for a in range(1,k+1):
ans=ans*a
print(ans, end="")
Week 4 Assignment 03
import random
n=int(input())
list1=[]
flag=0
for i in range(n):
list1.append(int(input()))
while(flag==0):
i=random.randint(0,len(list1)-1)
j=random.randint(0,len(list1)-1)
if i!=j:
#print("i and j before",list1[i],list1[j])
a=list1[i]
list1[i]=list1[j]
list1[j]=a
#print("i and j after",list1[i],list1[j])
if(list1==sorted(list1)):
flag=1
break
print(*list1,end="")
Week 6 Assignment 01
L = []
L=input()
Y=L.split(" ")
L = list(dict.fromkeys(Y))
for i in range(0,len(L)-1):
print(L[i],end=" ")
print(L[-1],end="")
Week 6 Assignment 02
num=int(input())
n=0
while True:
a=2**n
if(a>=num):
break
else:
n=n+1
b=2**(n)
if(num==b):
print("YES", end="")
else:
print("NO", end="")
Week 6 Assignment 03
matrix=[]
n=int(input())
for i in range(0,n):
a =[]
a=input()
for j in range(0,n):
y=a.split(" ")
matrix.append(y)
for i in range(n):
for j in range(n):
if(i<j):
if(j==n-1):
print("0",end="")
else:
print("0",end=" ")
else:
if(j==n-1):
print(matrix[i][j],end="")
else:
print(matrix[i][j],end=" ")
if i!=n-1:
print()
Week 7 Assignment 01
n=int(input())
for i in range(1,n+1):
for j in range(1,i+1):
if j==i:
print(j,end="")
else:
print(j,end=" ")
if(i!=n):
print()
Week 7 Assignment 02
def rotMatrix(mat):
if not len(mat):
return
top = 0
bottom = len(mat)-1
left = 0
right = len(mat[0])-1
while left < right and top < bottom:
prev = mat[top+1][left]
for i in range(left, right+1):
currow = mat[top][i]
mat[top][i] = prev
prev = currow
top += 1
for i in range(top, bottom+1):
currow = mat[i][right]
mat[i][right] = prev
prev = currow
right -= 1
for i in range(right, left-1, -1):
currow = mat[bottom][i]
mat[bottom][i] = prev
prev = currow
bottom -= 1
for i in range(bottom, top-1, -1):
currow = mat[i][left]
mat[i][left] = prev
prev = currow
left += 1
return mat
l=[]
a=int(input())
for i in range(a):
l1=[x for x in input().split()]
l.append(l1)
r1=[rotMatrix(l)]
for i in range(a-1):
print(' '.join(l[i]))
print(' '.join(l[a-1]),end='')
Week 7 Assignment 03
a = int(input())
mat=[]
for i in range(0,a):
l = list(map(int, input ().split ()))
mat.append(l)
m=a
n=a
k = 0
l = 0
count = 0
total = a*a
while (k < m and l < n) :
if (count == total) :
break
for i in range(k, m) :
print(mat[i][l], end = " ")
count += 1
l += 1
if (count == total) :
break
for i in range (l, n) :
print( mat[m - 1][i], end = " ")
count += 1
m -= 1
if (count == total) :
break
if (k < m) :
for i in range(m - 1, k - 1, -1) :
print(mat[i][n - 1], end = " ")
count += 1
n -= 1
if (count == total) :
break
if (l < n) :
for i in range(n - 1, l - 1, -1) :
print( mat[k][i], end = " ")
count += 1
k += 1
Comments
Post a Comment