forked from sanaengineer/PythonLearning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyMath.py
More file actions
207 lines (180 loc) · 5.44 KB
/
MyMath.py
File metadata and controls
207 lines (180 loc) · 5.44 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
#!/usr/bin/env python
__copyright__ = "Copyright (c) 2017. Sudheer Veeravalli. Free for general use and re-distribution"
__license__ = "Public Domain"
__version__ = "1.0"
def is_int(x: int) -> bool:
"""
Returns True if a number is a true integer; False otherwise
:param x: int
:return: bool
"""
if x - round(x) == 0: # If Difference between he number and it's rounded value is Zero, then it's a true integer
return True
else:
return False
def sum_of_digits(x: int) -> int:
"""
Returns the sum of all numbers in a given integer; if less than zero, it will return zero
:param x: int
:return: int
"""
result = 0
if x >= 0:
for digit in str(x):
result += int(digit)
return result
def is_prime(x: int) -> bool:
"""
Returns True if the given number is prime number; False otherwise
:param x: int
:return: bool
"""
result = True
if x <= 1:
result = False
else:
for n in range(2, x-1):
if x % n == 0:
result = False
return result
def factorial(x: int) -> int:
"""
Returns factorial value for given number
:param x: int
:return: int
"""
if x == 1:
return 1
else:
return x * factorial(x-1)
def biggest_number(*args) -> int:
"""
Returns the biggest number in a given list of integers
:param args: list
:return: int
"""
valid = True
for arg in args:
if not isinstance(arg, int):
valid = False
break
if valid:
return max(args)
else:
raise Exception('Exception in MyMath.biggest_number(): The data type of the arguments is not unique.')
def smallest_number(*args) -> int:
"""
Returns the smallest number in a given list of integers
:param args: list
:return: int
"""
valid = True
for arg in args:
if not isinstance(arg, int):
valid = False
break
if valid:
return min(args)
else:
raise Exception('Exception in MyMath.smallest_number(): The data type of the arguments is not unique.')
def distance_from_zero(arg) -> int:
"""
Returns the distance from Zero
:param arg: int/float
:return: int int
"""
if type(arg) == int or type(arg) == float:
return abs(arg)
else:
raise Exception('Exception in MyMath.distance_from_zero(): The data type of the argument(s) is not supported.')
def validate_collections(*args) -> bool:
"""
Returns True if the parameters are all of the same type. Else returns False
:param args: list
:return: bool
"""
valid = False
for index in range(1, len(args)):
if isinstance(args[index], type(args[0])):
valid = True
else:
valid = False
break
return valid
def custom_add(*args) -> object:
"""
Adds/appends the two or more objects/collections into a single one
:param args: Collection of objects
:return: object
Usage:
custom_add(["a"], ["b"]); custom_add([1, 2, 3], [4, 5, 6], [4, 5, 6])
custom_add(1, 2); custom_add(1.5, 2.3); custom_add(1.5, 2.3, 3.8)
"""
if validate_collections(*args):
if isinstance(args[0], int) or isinstance(args[0], float):
result = sum(args)
elif isinstance(args[0], list):
result = []
for list_object in args:
for item in list_object:
result.append(item)
else:
raise Exception('Exception in MyMath.custom_add(): The data type of the arguments is not implemented.')
else:
raise Exception('Exception in MyMath.custom_add(): The data type of the arguments is not the same.')
return result
def custom_subtract(*args) -> object:
"""
Subtracts/removes the two or more objects/collections into a single one
:param args: Collection of objects
:return: object
Usage:
custom_subtract(["a"], ["b"]); custom_subtract([1, 2, 3], [4, 5, 6], [4, 5, 6])
custom_subtract(1, 2); custom_subtract(1.5, 2.3); custom_subtract(1.5, 2.3, 3.8)
"""
if validate_collections(*args):
if isinstance(args[0], int) or isinstance(args[0], float):
result = args[0]
for index in range(1, len(args)):
result -= args[index]
else:
raise Exception('Exception in MyMath.custom_subtract(): The data type of the arguments is not implemented.')
else:
raise Exception('Exception in MyMath.custom_subtract(): The data type of the arguments is not the same.')
return result
def count_occurrences(x: list, num: int) -> int:
"""
Returns the count of occurrences of num in list
:param x: list
:param num: int
:return: int
"""
result = 0
for i in x:
if i == num:
result += 1
return result
def remove_duplicates(x: list) -> list:
"""
Returns the list removing the duplicates
:param x: list
:return: list
"""
result = []
for num in x:
if not result.__contains__(num):
result.append(num)
return result
def median(x: list) -> int:
"""
Returns the list removing the duplicates
:param x: list
:return: int
"""
x.sort()
middle = int(len(x) / 2)
if len(x) % 2 == 0:
return (x[middle-1] + x[middle]) / 2.0
else:
return x[middle]