You are on page 1of 23

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

li=list(map(int,input().split()))

catli=[]

for i in li:

if i not in catli and -1*i in li and i>0:

print(i,-i,end=" ")

catli.append(i)
catli.append(-1*i)

Congratulations, you passed the sample test case.


Click the Submit Code button to run your code against all the test cases.
Input (stdin)

8
4 8 9 -4 1 -1 -8 -9

Your Output (stdout)

4 -4 8 -8 9 -9 1 -1

Expected Output

4 -4 8 -8 9 -9 1 -1

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

li=list(map(int,input().split()))

key=list(map(int,input().split()))

cnt=0

if key[0]>key[1]:

temp=key[0]

key[0]=key[1]

key[1]=temp

for i in li:

if i>=key[0] and i<=key[1]:

cnt=cnt+1

print(cnt)

Congratulations, you passed the sample test case.


Click the Submit Code button to run your code against all the test cases.
Input (stdin)
6
1 3 3 9 10 4
14

Your Output (stdout)

Expected Output

Congratulations, you passed the sample test case.


Click the Submit Code button to run your code against all the test cases.
Input (stdin)

6
1 3 3 9 10 4
12 9

Your Output (stdout)

Expected Output

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

Congratulations, you passed the sample test case.


Click the Submit Code button to run your code against all the test cases.
Input (stdin)

6
1 23 10 5 7 11

Your Output (stdout)

23 50 77

Expected Output

23 50 77

Congratulations, you passed the sample test case.


Click the Submit Code button to run your code against all the test cases.
Input (stdin)

5
23 10 5 7 11

Your Output (stdout)


You are FOOL

Expected Output

You are FOOL

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

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

Congratulations, you passed the sample test case.


Click the Submit Code button to run your code against all the test cases.
Input (stdin)

7
23 45 76 103 87 -12 84

Your Output (stdout)

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

Expected Output

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

Congratulations, you passed the sample test case.


Click the Submit Code button to run your code against all the test cases.
Input (stdin)

7
23 45 76 99 87 12 84

Your Output (stdout)

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

Expected Output
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

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

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

Congratulations, you passed the sample test case.


Click the Submit Code button to run your code against all the test cases.
Input (stdin)

9
-2 1 -3 4 -1 2 1 -5 4

Your Output (stdout)

Sum : 6
4 -1 2 1

Expected Output

Sum : 6
4 -1 2 1

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

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

Congratulations, you passed the sample test case.


Click the Submit Code button to run your code against all the test cases.
Input (stdin)

33
111
222
333

Your Output (stdout)

Not an identity matrix

Expected Output

Not an identity matrix

Congratulations, you passed the sample test case.


Click the Submit Code button to run your code against all the test cases.
Input (stdin)

33
100
010
001

Your Output (stdout)

Identity matrix

Expected Output

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

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')
Congratulations, you passed the sample test case.
Click the Submit Code button to run your code against all the test cases.
Input (stdin)

23
123
654
123
654

Your Output (stdout)

Equal

Expected Output

Equal

Congratulations, you passed the sample test case.


Click the Submit Code button to run your code against all the test cases.
Input (stdin)

23
123
654
123
655

Your Output (stdout)

Not equal

Expected Output

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

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

Congratulations, you passed the sample test case.


Click the Submit Code button to run your code against all the test cases.
Input (stdin)

33
101
000
100

Your Output (stdout)

Yes

Expected Output
Yes

Congratulations, you passed the sample test case.


Click the Submit Code button to run your code against all the test cases.
Input (stdin)

33
101
101
111

Your Output (stdout)

No

Expected Output

No

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

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)

Congratulations, you passed the sample test case.


Click the Submit Code button to run your code against all the test cases.
Input (stdin)

33
123
456
789

Your Output (stdout)

15 16.88
Expected Output

15 16.88

Congratulations, you passed the sample test case.


Click the Submit Code button to run your code against all the test cases.
Input (stdin)

45

Your Output (stdout)

Invalid Input

Expected Output

Invalid Input

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

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()
Congratulations, you passed the sample test case.
Click the Submit Code button to run your code against all the test cases.
Input (stdin)

33
123
456
789

Your Output (stdout)

147
258
369

Expected Output

147
258
369

You might also like