0% found this document useful (0 votes)
6 views

Python DataStructure

Uploaded by

Chanchal jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Python DataStructure

Uploaded by

Chanchal jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Python-DataStructure.

ipynb - Colab 06/08/24, 3:17 PM

1 # Stack demonstration using lists


2
3 S = []
4
5 S.append(10)
6 S.append(20)
7 S.append(30)
8
9 print(S)
10
11 print(S.pop())
12 print(S)
13 print(S[-1])
14
15 print(len(S))
16
17 print(len(S)==0)

[10, 20, 30]


30
[10, 20]
20
2
False

https://colab.research.google.com/drive/1OOQLIsjPx5OK7NW94nh4IEv_W61pl2JS Page 1 of 11
Python-DataStructure.ipynb - Colab 06/08/24, 3:17 PM

q.append(20)
1 # Queue demonstration using collections.deque
q.append(30)
2
3 from collections import deque
4
5 q = deque() # Creation of a queue
6
7 q.append(10)
8 q.append(20)
9 q.append(30)
10 print(q)
11
12 print(q.popleft()) # Deletes first element i.e. 10
13 print(q)
14 print(q[0]) # Peek
15
16 print(len(q))
17 print(len(q)==0)

deque([10, 20, 30])


10
deque([20, 30])
20
2
False

1 # map() function
2
3 def f(x):
4 return 2*x
5
6 L = [1, 6, 4, 9, 7]
7
8 M = list(map(f, L))
9 print(M)

[2, 12, 8, 18, 14]

1 def f(x): return x.upper()


2 L = ["Hello", "world"]
3 M = list(map(f, L))
4 print(M)

['HELLO', 'WORLD']

https://colab.research.google.com/drive/1OOQLIsjPx5OK7NW94nh4IEv_W61pl2JS Page 2 of 11
Python-DataStructure.ipynb - Colab 06/08/24, 3:17 PM

1 L = [1, 0, 6, 4, 0, 9, 7]
2
3 M = list(filter(None, L))
4
5 print(M)

[1, 6, 4, 9, 7]

1 from itertools import filterfalse


2
3 L = [1, 0, 6, 4, 0, 9, 7]
4
5 M = list(filterfalse(None, L))
6
7 print(M)

[0, 0]

1 from functools import reduce


2
3 def f(x, y): return x+y
4
5 L = [1,6,4,9,7]
6
7 sum=reduce(f,L)
8
9 sum

27

1 from functools import reduce


2
3 def f(x, y): return x+y
4
5 L = [6,4,9,7]
6
7 sum=reduce(f,L,2)
8
9 sum

28

https://colab.research.google.com/drive/1OOQLIsjPx5OK7NW94nh4IEv_W61pl2JS Page 3 of 11
Python-DataStructure.ipynb - Colab 06/08/24, 3:17 PM

1 from functools import reduce


2
3 def f(x, y): return x*y
4
5 L = [1,6,4,9,7]
6
7 product=reduce(f,L,1)
8
9 product

1512

1 L = [1,6,4,9,7]
2 M = list(map(lambda x: 2*x, L))
3 M

[2, 12, 8, 18, 14]

1 L = ["Hello", "WorlD"]
2 M = list(map(lambda x: x.upper(), L))
3 M

['HELLO', 'WORLD']

1 L1=[1,2,3]
2 L2=[4,5,6]
3 L3=[7,8,9]
4 M = list(map(lambda x,y,z: x+y+z, L1,L2,L3))
5 M

[12, 15, 18]

1 L = [1,6,4,9,7]
2 M = list(map(lambda x: x%2==0, L))
3 M

[False, True, True, False, False]

1 L = [1,6,4,9,7,0]
2 M = list(filter(lambda x: x%2==0, L))
3 M

[6, 4, 0]

https://colab.research.google.com/drive/1OOQLIsjPx5OK7NW94nh4IEv_W61pl2JS Page 4 of 11
Python-DataStructure.ipynb - Colab 06/08/24, 3:17 PM

1 from itertools import filterfalse


2 L = [1,6,4,9,7,0]
3 M = list(filterfalse(lambda x: x%2==0, L))
4 M

[1, 9, 7]

1 from functools import reduce


2 L = [1,6,4,9,7]
3 M = reduce(lambda x,y: x+y, L)
4 M

27

1 from functools import reduce


2 L = [1,6,4,9,7]
3 M = reduce(lambda x,y: x*y, L, 1)
4 M

1512

1 n=3
2 for i in range(2,n):
3 if n%i==0:
4 print(False)
5 else:
6 print(True)

True

1 def isPrime(n):
2 if n == 2:
3 return True
4 for i in range(2,n):
5 if n%i==0:
6 return False
7 else:
8 return True
9
10 P=[x for x in range(2,100) if isPrime(x)]
11 print(P)

[2, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41

https://colab.research.google.com/drive/1OOQLIsjPx5OK7NW94nh4IEv_W61pl2JS Page 5 of 11
Python-DataStructure.ipynb - Colab 06/08/24, 3:17 PM

1 def isPrime(n):
2 if n == 2:
3 return True
4 for i in range(2,n):
5 if n%i==0:
6 return False
7 else:
8 return True
9
10 P=[x for x in range(2,100) if isPrime(x) if not x%10 == 9]
11 print(P)

[2, 3, 5, 7, 11, 13, 15, 17, 21, 23, 25, 27, 31, 33, 35, 37, 41, 43, 45, 47, 5

1 def isPrime(n):
2 if n == 2:
3 return True
4 for i in range(2,n):
5 if n%i==0:
6 return False
7 else:
8 return True
9
10 P=[x for x in range(2,100) if isPrime(x) if x%10 != 9]
11 print(P)

[2, 3, 5, 7, 11, 13, 15, 17, 21, 23, 25, 27, 31, 33, 35, 37, 41, 43, 45, 47, 5

1 [(x,y,z) for x in range(1,4) for y in range(1,x) for z in range(1, y+1)]

[(2, 1, 1), (3, 1, 1), (3, 2, 1), (3, 2, 2)]

1 [[x,y,z] for x in range(1,4) for y in range(1,x) for z in range(1, y+1)]

[[2, 1, 1], [3, 1, 1], [3, 2, 1], [3, 2, 2]]

1 for j in range(0,4):
2 print(j,end='')

0123

https://colab.research.google.com/drive/1OOQLIsjPx5OK7NW94nh4IEv_W61pl2JS Page 6 of 11
Python-DataStructure.ipynb - Colab 06/08/24, 3:17 PM

1 M=[[j+i for j in range(1,5)] for i in range(0,12,4)]


2 M

[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]

1 M=[[j+i for j in range(1,5)] for i in range(0,12,4)]


2 print([[row[i] for row in M] for i in range(4)])

[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]

1 m,n = map(lambda x: int(x), input("Enter the order of the matrix: ").split(' '))
2
3 print("Enter the matrix elements rowwise:")
4 matrix = [list(map(lambda x: int(x),input().split(' '))) for row in range(m)]
5 print("Here is the matrix of order {}x{}:".format(m,n))
6
7 for i in range(m):
8 for j in range(n):
9 print("{:5}".format(matrix[i][j]),end=' ')
10 print()

Enter the order of the matrix: 3 4


Enter the matrix elements rowwise:
1 2 3 4
2 3 4 5
5 6 7 8
Here is the matrix of order 3x4:
1 2 3 4
2 3 4 5
5 6 7 8

https://colab.research.google.com/drive/1OOQLIsjPx5OK7NW94nh4IEv_W61pl2JS Page 7 of 11
Python-DataStructure.ipynb - Colab 06/08/24, 3:17 PM

1 m,n = map(lambda x: int(x), input("Enter the order of the matrix: ").split(' '))
2 print("Enter the matrix elements rowwise:")
3 matrix = [list(map(lambda x: int(x),input().split(' '))) for row in range(m)]
4 transpose = [[None] * m for i in range(n)]
5
6 for i in range(m):
7 for j in range(n):
8 transpose[j][i] = matrix[i][j]
9
10 for i in range(n):
11 for j in range(m):
12 print("{} ".format(transpose[i][j]), end='')
13 print()

Enter the order of the matrix: 3 4


Enter the matrix elements rowwise:
1 2 3 4
5 6 7 8
9 10 11 12
1 5 9
2 6 10
3 7 11
4 8 12

https://colab.research.google.com/drive/1OOQLIsjPx5OK7NW94nh4IEv_W61pl2JS Page 8 of 11
Python-DataStructure.ipynb - Colab 06/08/24, 3:17 PM

1 # Program to find the sum of all elements:


2 # 1. On the principal diagonal
3 # 2. Above the principal diagonal
4 # 3. Below the principal diagonal
5
6 m = int(input("Enter the rows of the matrix: "))
7
8 print("Enter the matrix elements rowwise: ")
9 matrix = [list(map(lambda x: int(x), input().split(' '))) for row in range(m)]
10
11 sumAbove, sumBelow, trace = 0, 0, 0
12
13 for i in range(m):
14 for j in range(m):
15 if i<j: sumAbove += matrix[i][j]
16 elif i>j: sumBelow += matrix[i][j]
17 else: trace += matrix[i][j]
18
19 print("Sum of all elements: ")
20 print("\tAbove the principal diagonal: ", sumAbove)
21 print("\tOn the principal diagonal: ", trace)
22 print("\tBelow the principal diagonal: ", sumBelow)

Enter the rows of the matrix: 4


Enter the matrix elements rowwise:
1 2 3 4
5 6 7 8
2 2 3 3
3 3 4 4
Sum of all elements:
Above the principal diagonal: 27
\On the principal diagonal: 14
Below the principal diagonal: 19

https://colab.research.google.com/drive/1OOQLIsjPx5OK7NW94nh4IEv_W61pl2JS Page 9 of 11
Python-DataStructure.ipynb - Colab 06/08/24, 3:17 PM

1 # Matrix Addition and Subtraction


2
3 m, n = map(lambda x: int(x), input("Enter the order of the matrices: ").split('
4
5 print("Enter the elements of the first matrix rowwise: ")
6 matrix1 = [list(map(lambda x: int(x), input().split(' '))) for row in range(m)]
7
8 print("Enter the elements of the second matrix rowwise: ")
9 matrix2 = [list(map(lambda x: int(x), input().split(' '))) for row in range(m)]
10
11 matrixSum = list(map(lambda rowMatrix1, rowMatrix2: list(map(lambda a, b: a+b, r
12
13 matrixDiff = list(map(lambda rowMatrix1, rowMatrix2: list(map(lambda a, b: a-b,
14
15 print("Sum of the matrices:")
16
17 for i in range(m):
18 for j in range(n):
19 print("{} ".format(matrixSum[i][j]), end='')
20 print()
21
22 print("Difference of the matrices:")
23
24 for i in range(m):
25 for j in range(n):
26 print("{} ".format(matrixDiff[i][j]), end='')
27 print()

Enter the order of the matrices: 2 3


Enter the elements of the first matrix rowwise:
1 2 3
4 5 6
Enter the elements of the second matrix rowwise:
2 2 2
3 3 3
Sum of the matrices:
3 4 5
Difference of the matrices:
7 8 9
Difference of the matrices:
-1 0 1
1 2 3

1 # Matrix Multiplication
2
3

https://colab.research.google.com/drive/1OOQLIsjPx5OK7NW94nh4IEv_W61pl2JS Page 10 of 11
Python-DataStructure.ipynb - Colab 06/08/24, 3:17 PM

https://colab.research.google.com/drive/1OOQLIsjPx5OK7NW94nh4IEv_W61pl2JS Page 11 of 11

You might also like