Skip to content

Commit 68224a7

Browse files
Matrix
1 parent 4403a2a commit 68224a7

File tree

4 files changed

+205
-0
lines changed

4 files changed

+205
-0
lines changed

10X/Python/Array/Good_Pair.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""
2+
Good Pairs
3+
Given an array of integers nums (length of nums > 1).
4+
5+
A pair (i,j) is called good if nums[i] == nums[j] and i < j.
6+
7+
Return the number of good pairs.
8+
9+
Input
10+
Single line containing a list of numbers separated by spaces
11+
12+
Output
13+
Single integer representing total number of good pairs
14+
15+
Example
16+
Input:
17+
18+
1 2 3 1 1 3
19+
20+
Output:
21+
22+
4
23+
24+
Explanation:
25+
(0,3), (0,4), (3,4), (2,5) index position elements
26+
27+
"""
28+
from typing import Counter
29+
30+
31+
a=list(map(int,input().split()))
32+
count=0
33+
for i in range(len(a)):
34+
for j in range(len(a)):
35+
if a[i] == a[j] and i < j:
36+
count+=1
37+
print(count)

10X/Python/Array/Matrix_Diagonal.py

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
'''
2+
Matrix Diagonal
3+
Write a function which takes a 2 dimentional array of size nxn where n > 0 and changes its diagonal according to the following conditions
4+
5+
if an element e < 0 replace it with 0
6+
If element e >= 0 replace it with 1
7+
Input
8+
The first line contains n, denoting the number of lists.
9+
10+
This is followed by n lines. Each line contains n integers separated by a space
11+
12+
Output
13+
n lines, each line representing a list of numbers separated by a space.
14+
15+
Example
16+
Input:
17+
18+
4
19+
20+
2 0 1 4
21+
22+
0 -1 1 10
23+
24+
0 0 0 0
25+
26+
1 2 3 4
27+
28+
Output:
29+
30+
1 0 1 4
31+
32+
0 0 1 10
33+
34+
0 0 1 0
35+
36+
1 2 3 1
37+
'''
38+
# name your function as change_diagonal and it should take list as input
39+
def change_diagonal(lst):
40+
for i in range(val):
41+
for j in range(val):
42+
if i==j:
43+
if lst[i][j]<0:
44+
lst[i][j]=0
45+
elif lst[i][j]>=0:
46+
lst[i][j]=1
47+
return lst
48+
49+
50+
51+
52+
# Do not change anything below this line.
53+
if __name__ == "__main__":
54+
val = int(input())
55+
lst = []
56+
for index in range(0, val):
57+
lst.append([int(i) for i in input().split(' ')])
58+
out = change_diagonal(lst)
59+
for lst1 in out:
60+
print(" ".join(str(i) for i in lst1))
+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'''
2+
Right to Left Diagonal
3+
Write a function right_to_left_diagonal which a matrix as a list of lists of size mxm and returns a list of numbers containing elements of diagonal from right to left.
4+
5+
Input
6+
M size of matrix
7+
8+
M lines containing M elements in each line separated by space
9+
10+
Output
11+
list of m elements 1 per each line.
12+
13+
Example
14+
Input: 4
15+
16+
1 2 3 4
17+
18+
5 6 7 8
19+
20+
9 10 11 12
21+
22+
13 14 15 16
23+
24+
Output:
25+
26+
4
27+
28+
7
29+
30+
10
31+
32+
13
33+
34+
'''
35+
# name your function as right_to_left_diagonal
36+
def right_to_left_diagonal(lst):
37+
out=[]
38+
for i in range(m):
39+
for j in range(m):
40+
if (m-1)==j+i:
41+
out.append(lst[i][j])
42+
return out
43+
44+
# Do not change anything below this line
45+
if __name__ == "__main__":
46+
m = int(input())
47+
lst = []
48+
for val in range(0, m):
49+
lst.append([int(i) for i in input().split(' ')])
50+
out = right_to_left_diagonal(lst)
51+
[print(val) for val in out]

10X/Python/Array/SteepArray.py

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
'''
2+
Steep array
3+
Given a list of integers. The steepness score of an index i is computed in the following way-
4+
5+
Steepness Score(i) = (maximum element to the right of index i(including i) )- A[i])
6+
7+
Find out the total steepness score of the array, i.e - sum of steepness scores of each index in the array.
8+
9+
Constraints
10+
1<= length of array<= 100000
11+
12+
Input
13+
first line of the input contains N, the size of the array. the second line contains the N space separated integers representing the N elements of the array.
14+
15+
Output
16+
print the total steepness score of the array.
17+
18+
Example
19+
Input:
20+
21+
5
22+
23+
1 2 3 5 4
24+
25+
Output:
26+
27+
9
28+
29+
Explanation:
30+
31+
for 1-> greatest element to right of 1 including 1 is 5, SS= 5-1=4
32+
33+
for 2-> greatest element to right of 2 including 2 is 5, SS= 5-2=3
34+
35+
for 3-> greatest element to right of 3 including 3 is 5, SS= 5-3=2
36+
37+
for 5-> greatest element to right of 5 including 5 is 5, SS= 5-5=0
38+
39+
for 4-> greatest element to right of 4 including 4 is 4, SS= 4-4=0
40+
41+
hence total SS= 4+3+2=9
42+
'''
43+
import array
44+
n=int(input())
45+
su=0
46+
b=[]
47+
#b=array.array('i',[])
48+
b=list(map(int,input().split()))
49+
#for i in range(n):
50+
# b.append(c[i])
51+
while len(b)>1:
52+
ma=max(b)
53+
su=su+ma-b[0]
54+
55+
del b[0]
56+
57+
print(su)

0 commit comments

Comments
 (0)