Python Lab
Python Lab
Python Lab
LEVEL 1
POSITIVE NEGATIVE PAIRS
Given list of integers, print all the pairs having a positive value and negative value of
a number that exists in the list. We need to print pairs in order of their occurrences.
A pair whose any element appears first should be printed first.
Input Format
Constraints
N >0.
Output Format
Sample Input 0
8
4 8 9 -4 1 -1 -8 -9
Sample Output 0
4 -4 8 -8 9 -9 1 -1
CODE
n=int(input())
lst=[int(x) for x in input().split(' ')]
emp=[]
pos=[]
neg=[]
for i in lst:
if i >0:
pos.append(i)
else:
neg.append(i)
for i in pos:
if -abs(i) in neg:
emp.append(i)
emp.append(-abs(i))
print(*emp)
RANGE COUNT
Count the number of list elements with values given in the range.
Given an unsorted list of size n, find number of elements between the range i and j
(both inclusive).
Input Format
Constraints
Nil
Output Format
Output consists of the number of list elements lying between the closed interval of
the given range.
Sample Input 0
6
1 3 3 9 10 4
14
Sample Output 0
4
Sample Input 1
6
1 3 3 9 10 4
12 9
Sample Output 1
2
CODE
n=int(input())
lst=[int(x) for x in input().split(' ')]
x, y=input().split()
m=int(x)
t=int(y)
count=0
if m>t:
m,t=t,m
for i in lst:
if i>=m and i<=t:
count+=1
print(count)
MULTIPLICATION CHALLENGE
Janu and Banu are playing a multiplication game, If Janu tells a set of numbers,
Banu needs to multiply first number with second number, third number with fourth
number , fifth number with six number and so on. Help Banu to do this calculation.
Also, if Janu tells odd set of numbers, Banu should tell "You are FOOL"
Input Format
Number of elements in first line(N)
Constraints
Nil
Output Format
Sample Input 0
6
1 23 10 5 7 11
Sample Output 0
23 50 77
Sample Input 1
5
23 10 5 7 11
Sample Output 1
You are FOOL
CODE
n=int(input())
lst=list(map(int,input().split()))
ans=[]
for i in range(0,len(lst)-1,2):
if n%2==0:
t=lst[i]*lst[i+1]
ans.append(t)
else:
print("You are FOOL")
break
print(*ans)
MARKS RANGE 1
Develop a program that will read a list of student marks in the range 0 ... 100 and
tell you how many students scored in each range of 10 - how many scored 0 - 9,
how many 10 -19, 20 - 29 ... and so on.
Input Format
Input consists of one positive integer which intimates the number of elements in
the list
Constraints
Nil
Output Format
Sample Input 0
7
23 45 76 103 87 -12 84
Sample Output 0
0-9: 0
10-19: 0
20-29: 1
30-39: 0
40-49: 1
50-59: 0
60-69: 0
70-79: 1
80-89: 2
90-100: 0
Invalid: 2
Sample Input 1
7
23 45 76 99 87 12 84
Sample Output 1
0-9: 0
10-19: 1
20-29: 1
30-39: 0
40-49: 1
50-59: 0
60-69: 0
70-79: 1
80-89: 2
90-100: 1
Invalid: 0
CODE
n=int(input())
lst=[int(x) for x in input().split(' ')]
a,b,c,d,e,f,g,h,m,j,k=0,0,0,0,0,0,0,0,0,0,0,
for i in lst:
if i>=0 and i<=9:
a+=1
elif i>=10 and i<=19:
b+=1
elif i>=20 and i<=29:
c+=1
elif i>=30 and i<=39:
d+=1
elif i>=40 and i<=49:
e+=1
elif i>=50 and i<=59:
f+=1
elif i>=60 and i<=69:
g+=1
elif i>=70 and i<=79:
h+=1
elif i>=80 and i<=89:
m+=1
elif i>=90 and i<=100:
j+=1
else:
k+=1
print('0-9: {}'.format(a))
print('10-19: {}'.format(b))
print('20-29: {}'.format(c))
print('30-39: {}'.format(d))
print('40-49: {}'.format(e))
print('50-59: {}'.format(f))
print('60-69: {}'.format(g))
print('70-79: {}'.format(h))
print('80-89: {}'.format(m))
print('90-100: {}'.format(j))
print('Invalid: {}'.format(k))
Input Format
Constraints
Nil
Output Format
CODE
n=int(input())
a=list(map(int, input().split(' ')))
b=[]
c=[]
n=len(a)
b=[a[i:i+j] for i in range(n) for j in range(1,n-i+1)]
for i in b:
c.append(sum(i))
d=c.index(max(c))
print('Sum : {}'.format(c[d]))
print(*b[d])
LEVEL 2
IDENTITY MATRIX 4
Write a program to check if a given matrix is an Identity Matrix
Input Format
Matrix elements
Constraints
Nil
Output Format
Identity matrix or Not an identity matrix
Sample Input 0
33
111
222
333
Sample Output 0
Not an identity matrix
Sample Input 1
33
100
010
001
Sample Output 1
Identity matrix
CODE
m,n=map(int,input().split(' '))
matrix=[]
ans=1
for i in range(m):
a=[]
a=[int(x) for x in input().split(' ')]
matrix.append(a)
EQUAL MATRIX
Write a Program to check if two Matrices are Equal or not
Input Format
Constraints
Nil
Output Format
Sample Input 0
23
123
654
123
654
Sample Output 0
Equal
Sample Input 1
23
123
654
123
655
Sample Output 1
Not equal
CODE
m,n=map(int,input().split(' '))
matrix=[]
matrix2=[]
ans=0
for i in range(m):
a=[]
a=[int(x) for x in input().split(' ')]
matrix.append(a)
for i in range(m):
a=[]
a=[int(x) for x in input().split(' ')]
matrix2.append(a)
for i in range(m):
for j in range(n):
if matrix[i][j]==matrix2[i][j]:
ans=1
else:
ans=0
break
if ans==1:
print('Equal')
else:
print('Not equal')
BINARY MATRIX UNIQUE
Check if the rows of a binary matrix (matrix having 0s and 1s) can be made unique
by removing a single column.
Example
Input: 3 3
101
000
100
Output: Yes
Input Format
Binary matrix
Constraints
Binary matrix
Output Format
Yes or No
Sample Input 0
33
101
000
100
Sample Output 0
Yes
Sample Input 1
33
101
101
111
Sample Output 1
No
CODE
m, n = map(int, input().split(' '))
matrix = []
check=0
for i in range(m):
res=0
a = []
a = [int(x) for x in input().split(' ')]
res=int("".join(map(str,a)))
matrix.append(res)
ans=[]
for i in matrix:
if i not in ans:
ans.append(i)
else:
check=1
if check:
print('No')
else:
print('Yes')
MATRIX NORMAL AND TRACES
Write a program to find the Normal and Trace of a matrix. They are only defined for
a square matrix.
Normal: Square root of the sum of the squares of each element of the matrix.
Input Format
Constraints
Nil
Output Format
Output prints the trace and normal separated by a space. Refer sample output.
Sample Input 0
33
123
456
789
Sample Output 0
15 16.88
Sample Input 1
45
Sample Output 1
Invalid Input
CODE
m,n=map(int,input().split(' '))
matrix=[]
if m!=n:
print('Invalid Input')
else:
for i in range(m):
a=[]
a=[int(x) for x in input().split(' ')]
matrix.append(a)
res=0
ans=0
for i in range(m):
for j in range(n):
if i==j:
res+=matrix[i][j]
for i in range(m):
for j in range(n):
temp=matrix[i][j]
ans=ans+(temp*temp)
ans=ans**0.5
print(res,end=' ')
print('%.2f'%ans)
TRANSPOSE MATRIX 7
Write a program to find the transpose of a given matrix.
Input Format
Matrix elements
Constraints
Nil
Output Format
Sample Input 0
33
123
456
789
Sample Output 0
147
258
369
CODE
m,n=map(int,input().split(' '))
matrix=[]
for i in range(m):
a=[]
a=[int(x) for x in input().split(' ')]
matrix.append(a)
for i in range(n):
for j in range(m):
print(matrix[j][i],end=' ')
print()