Skip to content

Commit c9cd4ec

Browse files
authored
Merge pull request #9 from Jugalcody/py2
updated functions
2 parents 9810e5f + 509680d commit c9cd4ec

1 file changed

Lines changed: 176 additions & 0 deletions

File tree

module.py

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,182 @@ def bin2dec(x):
119119
return r
120120

121121

122+
def createarray(length,dtype='int'): # To create an array of entered length and entered data type(interger data type is a default data type)
123+
import numpy as np
124+
a=[] #empty list
125+
for i in range(length):
126+
# if entered dtype is an interger
127+
if dtype=='int':
128+
e=int(input(f"Enter {i+1} element : "))
129+
a.append(e)
130+
# if entered dtype is a string
131+
elif dtype=='str' or dtype=='string':
132+
e=str(input("Enter {i+1} element : "))
133+
a.append(e)
134+
# if entered dtype is a float
135+
elif dtype=='float':
136+
e=float(input("Enter {i+1} element : "))
137+
a.append(e)
138+
139+
140+
b=np.array(a)
141+
return b
142+
143+
def arrayrev(array): # To reverese the array elements
144+
import numpy as np
145+
r=[]
146+
for i in range(len(array)-1,-1,-1):
147+
r.append(array[i])
148+
a=np.array(r)
149+
return a
150+
151+
def ispalindrome(x): # To check if the given parameter is palindrome or not
152+
x=str(x) #explicitly convert into string data type so as to iterate through each character
153+
r=''
154+
for i in range(len(x)-1,-1,-1):
155+
r=r+x[i]
156+
if x==r: # if the parameter get matched with its reverse then returns true othewise false
157+
return True
158+
else:
159+
return False
160+
161+
162+
163+
def even_or_odd(data):
164+
try :
165+
if data%2==0:
166+
return 'even'
167+
else:
168+
return 'odd'
169+
170+
except:
171+
print("\nError occured, parameter passed should be purely numeric")
172+
173+
174+
#Linked list
175+
176+
def create_node(data):
177+
class node:
178+
def __init__(self,data):
179+
self.data=data
180+
self.next=None
181+
182+
a=node(data)
183+
return a
184+
# to link a node with another node
185+
186+
def node_link(a,b):
187+
a.next=b
188+
b.next=None
189+
#a=node(data1)
190+
191+
192+
# to count number of nodes
193+
194+
def count_node(head):
195+
if head is None:
196+
return 0
197+
else:
198+
temp=head
199+
count=0
200+
while(temp!=None):
201+
count=count+1
202+
temp=temp.next
203+
return count
204+
205+
# to diplay a linked list whose header node is passed as an argument
206+
207+
def display_nodes(head):
208+
t=head
209+
while t is not None:
210+
print(t.data,"->",end="")
211+
t=t.next
212+
print("NULL")
213+
214+
215+
# Matrix problems
216+
217+
def matrix_add(array1,array2):
218+
import numpy as np
219+
220+
result=np.array(array1)+np.array(array2)
221+
return result
222+
223+
224+
def matrix_sub(array1,array2):
225+
import numpy as np
226+
227+
result=np.array(array1)-np.array(array2)
228+
return result
229+
230+
# Multiplication of two
231+
def matrix_mul(matrix1,matrix2):
232+
import numpy as np
233+
matrix1=np.array(matrix1) # converting list into array
234+
matrix2=np.array(matrix2)
235+
a=list(matrix1.shape) # getting the shape of the array
236+
b=list(matrix2.shape)
237+
if len(a)==1:
238+
k=a[0] # suppose if row is one , for eg [1,2,3] ,then shape returns (3,) instead of [1,3]..
239+
a[1]=k
240+
a[0]=1 # here first element becomes last element and in place of first element , 1 is appended..
241+
if a[1]==b[0]: # from matrix multiplication convention, number of columns of first matrix needs to be equal to number of rows of second matrix
242+
tt=[]
243+
for i in range(b[0]):
244+
u=[]
245+
for j in range(b[0]):
246+
u.append(matrix2[j][i])
247+
tt.append(u)
248+
t=np.array(tt) # arrays of coloumn of second matrix
249+
pp=[]
250+
251+
for k in range(b[0]):
252+
ar=[]
253+
for l in range(b[0]):
254+
y=matrix1[k]*t[l] # multiplication of rows and columns
255+
ar.append(list(y)) # appending the result into a list
256+
pp.append(ar)
257+
l=[]
258+
for i in pp:
259+
zz=[]
260+
for j in i:
261+
sum1=0
262+
for c in j:
263+
sum1=sum1+c # sum all the element of each row each column
264+
zz.append(sum1)
265+
l.append(zz) # appending the sum of each row and column of result matrix into a list
266+
l=np.array(l) # convert the list of result matrix into array
267+
return l
268+
269+
270+
271+
def matrix_shape(matrix1):
272+
import numpy as np
273+
matrix1=np.array(matrix1)
274+
a=list(matrix1.shape)
275+
if len(a)==1:
276+
k=a[0]
277+
a[1]=k
278+
a[0]=1
279+
return a #returns shape of a matrix
280+
281+
282+
283+
284+
def matrix_transpose(matrix1):
285+
import numpy as np
286+
matrix1=np.array(matrix1) # converting list into array
287+
a=list(matrix1.shape) # getting the shape of the array
288+
tt=[]
289+
for i in range(a[0]):
290+
u=[]
291+
for j in range(len(a)):
292+
u.append(matrix1[j][i])
293+
tt.append(u)
294+
t=np.array(tt) # get a transpose of matrix1
295+
return t
296+
297+
122298

123299

124300

0 commit comments

Comments
 (0)