forked from extinctsion/easyPythonpi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule.py
More file actions
301 lines (241 loc) · 10.6 KB
/
module.py
File metadata and controls
301 lines (241 loc) · 10.6 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
""" A python module that helps you to calculate some of the most used calculations.....
usage--
Just download the file from git and unzip in ur system.
And while using this module, just write as code-
'from module import *' and u r good to go...
~Happy programming"""
def add(x, y): # For addition of 2 numbers
return x+y
def sub(x, y): # For substraction of 2 numbers
return x-y
def multi(x, y): # For multiplication of 2 numbers
return x*y
def div(x, y): # For division of 2 numbers
return x/y
def mod(x, y): # For finding the modulus of 2 numbers
return x%y
def factorial(n): # To find the factorial of 2 numbers
# single line to find factorial
return 1 if (n == 1 or n == 0) else n * factorial(n - 1)
def Area_circle(r): # To find the area of a circle using the radius r
PI = 3.142
return PI * (r * r)
def Perimeter_circle(r): # To find the perimeter of a circle using the radius r
PI = 3.142
return 2 * PI * r
def fibonacci(n): #To find the nth fibonacci series
if n<0:
print("Incorrect input")
# First Fibonacci number is 0
elif n==1:
return 0
# Second Fibonacci number is 1
elif n==2:
return 1
else:
return fibonacci(n-1)+fibonacci(n-2)
def sort(list): # To bubble sort and array or list
for i in range(len(list) - 1, 0, -1):
for j in range(i):
if list[j] > list[j + 1]:
temp = list[j]
list[j] = list[j + 1]
list[j + 1] = temp
#method to print the 1st prime number between the range
def printprime(start,end):
if start<=0:
start=1
for i in range(start,end+1):
j=0
for k in range(2,i):
if i%k==0:
j=1
if j==0:
return i
#A method to convert Hexadecimal input to binary numbers
def hex2bin(x):
x=str(x)
r=''
for i in x:
if i=='A':
r=r+'1010'
elif i=='B':
r=r+'1011'
elif i=='C':
r=r+'1100'
elif i=='D':
r=r+'1101'
elif i=='E':
r=r+'1110'
elif i=='F':
r=r+'1111'
else:
h=bin(int(i))
n=h[2:]
for j in range(4):
if len(n)<4:
n='0'+n
r=r+n
return r
#A method to convert Octal input to binary numbers
def oct2bin(x):
r=''
x=str(x)
for i in x:
h=bin(int(i))
n=h[2:]
for i in range(3):
if len(n)<3:
n='0'+n
r=r+n
return r
#A method to convert binary input to decimal numbers
def bin2dec(x):
x=list(str(x))
l=len(x)
a=0
r=0
for i in range(l-1,-1,-1):
r=r+(int(x[i])*(2**a))
a=a+1
return r
def createarray(length,dtype='int'): # To create an array of entered length and entered data type(interger data type is a default data type)
import numpy as np
a=[] #empty list
for i in range(length):
# if entered dtype is an interger
if dtype=='int':
e=int(input(f"Enter {i+1} element : "))
a.append(e)
# if entered dtype is a string
elif dtype=='str' or dtype=='string':
e=str(input("Enter {i+1} element : "))
a.append(e)
# if entered dtype is a float
elif dtype=='float':
e=float(input("Enter {i+1} element : "))
a.append(e)
b=np.array(a)
return b
def arrayrev(array): # To reverese the array elements
import numpy as np
r=[]
for i in range(len(array)-1,-1,-1):
r.append(array[i])
a=np.array(r)
return a
def ispalindrome(x): # To check if the given parameter is palindrome or not
x=str(x) #explicitly convert into string data type so as to iterate through each character
r=''
for i in range(len(x)-1,-1,-1):
r=r+x[i]
if x==r: # if the parameter get matched with its reverse then returns true othewise false
return True
else:
return False
def even_or_odd(data):
try :
if data%2==0:
return 'even'
else:
return 'odd'
except:
print("\nError occured, parameter passed should be purely numeric")
#Linked list
def create_node(data):
class node:
def __init__(self,data):
self.data=data
self.next=None
a=node(data)
return a
# to link a node with another node
def node_link(a,b):
a.next=b
b.next=None
#a=node(data1)
# to count number of nodes
def count_node(head):
if head is None:
return 0
else:
temp=head
count=0
while(temp!=None):
count=count+1
temp=temp.next
return count
# to diplay a linked list whose header node is passed as an argument
def display_nodes(head):
t=head
while t is not None:
print(t.data,"->",end="")
t=t.next
print("NULL")
# Matrix problems
def matrix_add(array1,array2):
import numpy as np
result=np.array(array1)+np.array(array2)
return result
def matrix_sub(array1,array2):
import numpy as np
result=np.array(array1)-np.array(array2)
return result
# Multiplication of two
def matrix_mul(matrix1,matrix2):
import numpy as np
matrix1=np.array(matrix1) # converting list into array
matrix2=np.array(matrix2)
a=list(matrix1.shape) # getting the shape of the array
b=list(matrix2.shape)
if len(a)==1:
k=a[0] # suppose if row is one , for eg [1,2,3] ,then shape returns (3,) instead of [1,3]..
a[1]=k
a[0]=1 # here first element becomes last element and in place of first element , 1 is appended..
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
tt=[]
for i in range(b[0]):
u=[]
for j in range(b[0]):
u.append(matrix2[j][i])
tt.append(u)
t=np.array(tt) # arrays of coloumn of second matrix
pp=[]
for k in range(b[0]):
ar=[]
for l in range(b[0]):
y=matrix1[k]*t[l] # multiplication of rows and columns
ar.append(list(y)) # appending the result into a list
pp.append(ar)
l=[]
for i in pp:
zz=[]
for j in i:
sum1=0
for c in j:
sum1=sum1+c # sum all the element of each row each column
zz.append(sum1)
l.append(zz) # appending the sum of each row and column of result matrix into a list
l=np.array(l) # convert the list of result matrix into array
return l
def matrix_shape(matrix1):
import numpy as np
matrix1=np.array(matrix1)
a=list(matrix1.shape)
if len(a)==1:
k=a[0]
a[1]=k
a[0]=1
return a #returns shape of a matrix
def matrix_transpose(matrix1):
import numpy as np
matrix1=np.array(matrix1) # converting list into array
a=list(matrix1.shape) # getting the shape of the array
tt=[]
for i in range(a[0]):
u=[]
for j in range(len(a)):
u.append(matrix1[j][i])
tt.append(u)
t=np.array(tt) # get a transpose of matrix1
return t