-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4th assignment.txt
More file actions
193 lines (132 loc) · 3.13 KB
/
4th assignment.txt
File metadata and controls
193 lines (132 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
ASSIGNMENT IN PYTHON PROGRAMMING (TECHFORTUNE TECHNOLOGIES)
NAME :: THARUN KUMAR T
COLLEGE NAME :: BNM INSTITUTE OF TECHNOLOGY
MORNING BATCH -- 7 30 AM
1) multiply matrices
import numpy
mata = numpy.array([[1,2],[3,4]])
matb = numpy.array([[5,6],[7,8]])
print(numpy.dot(mata,matb))
2)find the sum of diagonal elements of a matrix
a = [[1,2],[3,4]]
sum = 0
for row in range(len(a)):
sum+=a[row][row]
3)check given matrix are equal r not
def eq(a,b):
if(len(a),len(a[0]) == len(b),len(b[0])):
for row in range(len(a)):
for col in range(len(a[0])):
if a[row][col] != b[row][col]:
return 'not equal'
print('The given matrix are equal\n')
else:
print('the order of the matrix itself not same hence not eqaul\n')
a
Out[48]: [[1, 2], [3, 4]]
b
Out[49]: [[5, 6], [7, 8]]
eq(a,b)
Out[55]: 'not equal'
eq([[1,2],[3,4]],[[1,2],[3,4]])
The given matrix are equal
4)find row and column sums of the given matrix
a= [[1, 2], [3, 4]]
r_sum = 0
c_sum = 0
for i in a:
for j in i:
r_sum+=j
for row in range(len(a)):
for col in range(len(a[0])):
c_sum+=a[col][row]
r_sum
Out[69]: 10
c_sum
Out[79]: 12
5)add two matrices
import numpy
mata = numpy.array([[1,2],[3,4]])
matb = numpy.array([[5,6],[7,8]])
print(numpy.add(mata,matb))
6) subtract two matrices
import numpy
mata = numpy.array([[1,2],[3,4]])
matb = numpy.array([[5,6],[7,8]])
print(numpy.subtract(mata,matb))
7)transpose of a matrix
def transpose(a):
for row in range(len(a[0])):
for col in range(len(a)):
l[r][c] = a[c][r]
a=[[12,7],[4 ,5],[3 ,8]]
res = [a[j][i] for j in range(len(a)) for i in range(len(a[0]))]
8)find length of a string
s = 'hello'
le = 0
for i in s:
le+=1
(or)
le = len(s)
9) copy one string to another string
s = 'hello'
new_s = ''
for i in s:
new_s+=i
10)concatenate two strings
s1 = 'hello this is'
s2 = 'python assig by tharun'
print(s1+s2)
11)comparing two strings
s1 = 'hello this is'
s2 = 'python assig by tharun'
if s1 == s2:
print('the given strings are eqaul\n')
else:
print('the given strings are not equal\n')
12)convert the given string to lower case
s='HELLO'
print(s.lower())
13) convert the given string to uppercase
s='hello'
print(s.upper())
14)find the determinent of a matrix
def det(a):
p_d = 0
o_d = 0
if len(a) != len(a[0]):
return False
else:
for r in range(len(a)):
p_d*=a[r][r]
o_d = a[1][0]*a[0][1]
return p_d-o_d
15)find total number of alphabets digits and special characters in a string
s = 'hello123@#%'
for i in s:
if(i.isalpha()):
al+=1
elif(i.isdigit()):
nu+=1
else:
sp+=1
al
Out[38]: 5
nu
Out[39]: 3
sp
Out[40]: 3
19)to count vowels and consonents in a string
s = 'hello'
v = ['a','e','i','o','u']
for i in s:
if i in v:
v_count+=1
else:
c_count+=1
20)reverse a string
s='hello'
n = ''
for i in range(len(s),0,-1):
n+=s[i]
21)