Sie sind auf Seite 1von 16

EXERCISE 3

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

The input consists of the number of elements.

List elements in second line(separated by space).

Constraints

N >0.

Output Format

Display the pairs, as a positive number followed by a negative number.

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

Input consists of (n+3) integer inputs,

First input corresponds to the number of elements in the list,

Followed by the list elements,

Followed by the range elements.

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)

N number of elements in second line separated by space

Constraints

Nil

Output Format

Multiplied output as shown in sample output

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

Followed by the elements of the integer list.

Constraints

Nil

Output Format

Output consists of number of elements present in the predefined ranges.

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

SUB LIST SUM


Write a program to find the contiguous sub-list within a list (containing at least one
number) which has the largest sum.

For example, given the list [−2,1,−3,4,−1,2,1,−5,4],

The contiguous sub-list [4,−1,2,1] has the largest sum = 6.

Input Format

Number of list elements

List elements separated by space

Constraints

Nil

Output Format

Contiguous sub-list Sum

Contiguous sub list values


Sample Input 0
9
-2 1 -3 4 -1 2 1 -5 4
Sample Output 0
Sum : 6
4 -1 2 1

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

Number of rows and columns in first line separated by space

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)

for i in range(0, m):


for j in range(0, n):
if(i == j and matrix[i][j] != 1):
ans=0
break

if(i != j and matrix[i][j] != 0):


ans=0
break
if ans:
print('Identity matrix')
else:
print('Not an identity matrix')

EQUAL MATRIX
Write a Program to check if two Matrices are Equal or not

Input Format

Number of rows and number of columns in first line separated by space

First matrix elements

Second matrix elements

Constraints

Nil

Output Format

Equal or Not Equal

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

2nd column can be removed to make rows of a binary matrix unique

Input Format

Number of rows and columns

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.

Square Matrix: Matrix in which, the number of rows = number of columns.

Normal: Square root of the sum of the squares of each element of the matrix.

Trace: Sum of the diagonal elements of a matrix.

Input Format

First line of the input consists of m and n.

Next input is the matrix.

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

Number of rows and columns in first line separated by space

Matrix elements

Constraints

Nil

Output Format

Transpose of matrix elements

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()

Das könnte Ihnen auch gefallen