Skip to content

Commit be05166

Browse files
Andrew hung nguyen (#65)
* Adding a gitignore to avoid adding pyc * Separated string, statistics, and bitwise operations to separate files and created unit tests
1 parent 94c7f74 commit be05166

9 files changed

Lines changed: 336 additions & 245 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# prevents all pycaches from being added
2+
__pycache__
3+
easyPythonpi/__pycache__

easyPythonpi/methods/basics.py

Lines changed: 2 additions & 237 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python
22
#-*- coding: utf-8 -*-
33

4-
import easyPythonpi.easyPythonpi as pi
4+
import easyPythonpi as pi
55
import regex as re
66

77
def add(x:'float', y:'float')->'float': # For addition of 2 numbers
@@ -13,25 +13,13 @@ def sub(x:'float', y:'float')->'float': # For substraction of 2 numbers
1313
def multi(x:'float', y:'float')->'float': # For multiplication of 2 numbers
1414
return x*y
1515

16-
17-
def calculate_average(*args, **kwargs):
18-
total_sum = sum(args)
19-
count = len(args) + len(kwargs.values())
20-
21-
for value in kwargs.values():
22-
total_sum += value
23-
24-
average = total_sum / count
25-
return average
26-
2716
def div(x:'float', y:'float')->'float': # For division of 2 numbers
2817
return x/y
2918

3019
def mod(x:'float', y:'float')->'float': # For finding the modulus of 2 numbers
3120
return x%y
3221

3322
def calculate_percentage(part, whole):
34-
3523
if whole == 0:
3624
return 0 # Avoid division by zero
3725
return (part / whole) * 100
@@ -57,7 +45,6 @@ def Perimeter_circle(r:'float')->'float': # To find the perimeter of a circle us
5745
PI = 3.142
5846
return 2 * PI * r
5947

60-
6148
def fibonacci(n:'int')->'int': #To find the nth fibonacci series
6249
"""Finds the fibonacci of the nth sequence.
6350
@@ -93,8 +80,6 @@ def fibonacci(n:'int')->'int': #To find the nth fibonacci series
9380
fib2 = fibN
9481

9582
return fib1 +fib2
96-
97-
9883

9984
#method to print the 1st prime number between the range
10085
def printprime(start:'int',end:'int')->'list':
@@ -110,197 +95,8 @@ def printprime(start:'int',end:'int')->'list':
11095
if j==0:
11196
p.append(i)
11297

113-
return p
114-
#A method to convert Hexadecimal input to binary numbers
115-
def hex2bin(x:'hex')->'bin':
116-
x=str(x)
117-
r=''
118-
for i in x:
119-
if i=='A':
120-
r=r+'1010'
121-
elif i=='B':
122-
r=r+'1011'
123-
elif i=='C':
124-
r=r+'1100'
125-
elif i=='D':
126-
r=r+'1101'
127-
elif i=='E':
128-
r=r+'1110'
129-
elif i=='F':
130-
r=r+'1111'
131-
else:
132-
h=bin(int(i))
133-
n=h[2:]
134-
for j in range(4):
135-
if len(n)<4:
136-
n='0'+n
137-
138-
r=r+n
139-
return r
140-
141-
142-
#A method to convert Octal input to binary numbers
143-
def oct2bin(x:'oct')->'bin':
144-
r=''
145-
x=str(x)
146-
for i in x:
147-
h=bin(int(i))
148-
n=h[2:]
149-
for i in range(3):
150-
if len(n)<3:
151-
n='0'+n
152-
r=r+n
153-
return r
154-
155-
#A method to convert binary input to decimal numbers
156-
def bin2dec(x:'bin')->'int':
157-
x=list(str(x))
158-
l=len(x)
159-
a=0
160-
r=0
161-
for i in range(l-1,-1,-1):
162-
163-
r=r+(int(x[i])*(2**a))
164-
165-
a=a+1
166-
return r
167-
168-
# A function that converts a binary string to hexadecimal
169-
def bin2hex(x:'bin')->'hex':
170-
"""Converts a binary string to a hexadecimal string.
171-
172-
This function converts a binary string to hexadecimal. If the binary
173-
string's length is a multiple of 4, simply break up the string into
174-
substrings of 4 binary digits and determine their hexadecimal digit.
175-
If the binary string's length not a multiple of 4 prepend the
176-
necessary number of 0's to make the string a multiple of 4.
177-
178-
Args:
179-
x ('bin') : Binary string to convert to hexadecimal.
180-
181-
Returns:
182-
The binary converted to hexadecimal.
183-
184-
Raises:
185-
No errors
186-
"""
187-
188-
h = '' # hexadecimal number converted from binary and to be returned
189-
190-
191-
x=str(x)
192-
193-
# Determine if the string has invalid characters
194-
if re.search('[^(0-1)]', x):
195-
raise pi.InvalidBinaryException
196-
197-
# Get the length of the string
198-
l=len(x)
199-
200-
# Begin the process of converting x to its hexadecimal number
201-
202-
# If the length is not a multiple of 4, prepend 0's before converting
203-
if l % 4 != 0:
204-
numZerosPrepended = 4 - ( l % 4 ) # number of zeros to prepend
205-
x = (numZerosPrepended * '0') + x # concatenate numZerosPrepended to x
206-
207-
208-
for i in range(len(x)-1, 0, -4):
209-
substring = x[i-3:i+1] # The substring converted to a hex character
210-
211-
# Determine the binary substring's hex and prepend to h
212-
if substring == '0000':
213-
h = '0' + h
214-
elif substring == '0001':
215-
h = '1' + h
216-
elif substring == '0010':
217-
h = '2' + h
218-
elif substring == '0011':
219-
h = '3' + h
220-
elif substring == '0100':
221-
h = '4' + h
222-
elif substring == '0101':
223-
h = '5' + h
224-
elif substring == '0110':
225-
h = '6' + h
226-
elif substring == '0111':
227-
h = '7' + h
228-
elif substring == '1000':
229-
h = '8' + h
230-
elif substring == '1001':
231-
h = '9' + h
232-
elif substring == '1010':
233-
h = 'A' + h
234-
elif substring == '1011':
235-
h = 'B' + h
236-
elif substring == '1100':
237-
h = 'C' + h
238-
elif substring == '1100':
239-
h = 'C' + h
240-
elif substring == '1101':
241-
h = 'D' + h
242-
elif substring == '1110':
243-
h = 'E' + h
244-
elif substring == '1111':
245-
h = 'F' + h
246-
247-
return h
248-
249-
250-
def bin2oct(x:'bin')->'oct':
251-
o = '' # hexadecimal number converted from binary and to be returned
252-
253-
x=str(x)
254-
255-
# Determine if the string has invalid characters
256-
if re.search('[^(0-1)]', x):
257-
raise pi.InvalidBinaryException
258-
259-
# Get the length of the string
260-
l=len(x)
261-
262-
# Begin the process of converting x to its hexadecimal number
263-
264-
# If the length is not a multiple of 3, prepend 0's before converting
265-
if l % 3 != 0:
266-
numZerosPrepended = 3 - ( l % 3 ) # number of zeros to prepend
267-
x = (numZerosPrepended * '0') + x # concatenate numZerosPrepended to x
268-
269-
for i in range(len(x), 0, -3):
270-
substring = x[i-3:i]
271-
272-
if substring == '000':
273-
o = '0' + o
274-
elif substring == '001':
275-
o = '1' + o
276-
elif substring == '010':
277-
o = '2' + o
278-
elif substring == '011':
279-
o = '3' + o
280-
elif substring == '100':
281-
o = '4' + o
282-
elif substring == '101':
283-
o = '5' + o
284-
elif substring == '110':
285-
o = '6' + o
286-
elif substring == '111':
287-
o = '7' + o
288-
289-
return o
290-
291-
292-
def ispalindrome(x:'str')->'str': # To check if the given parameter is palindrome or not
293-
x=str(x) #explicitly convert into string data type so as to iterate through each character
294-
r=''
295-
for i in range(len(x)-1,-1,-1):
296-
r=r+x[i]
297-
if x==r: # if the parameter get matched with its reverse then returns true othewise false
298-
return True
299-
else:
300-
return False
98+
return p
30199

302-
303-
304100
def even_or_odd(data:'int'):
305101
try :
306102
if data%2==0:
@@ -310,34 +106,3 @@ def even_or_odd(data:'int'):
310106

311107
except:
312108
print("\nError occured, parameter passed should be purely numeric")
313-
314-
315-
316-
def remove_punctuation(my_str:'str')->'str':
317-
punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
318-
# remove punctuations from the string
319-
no_punct = ""
320-
#run a for loop and traverse every element in a string and check ,if char is not match with punctuations char then it will add in no_punct
321-
for char in my_str:
322-
if char not in punctuations:
323-
no_punct = no_punct + char
324-
#return no_punct
325-
return no_punct
326-
327-
def count_vowels(ip_str:'str')->'int':
328-
# string of vowels
329-
vowels = 'aeiou'
330-
# make it suitable for comparisions
331-
ip_str = ip_str.casefold()
332-
333-
# make a dictionary with each vowel a key and value 0
334-
count = {}.fromkeys(vowels,0)
335-
336-
# count the vowels
337-
for char in ip_str:
338-
if char in count:
339-
count[char] += 1
340-
341-
#return the count dictionary
342-
return count
343-

0 commit comments

Comments
 (0)