Skip to content

Commit a2236cf

Browse files
PatOnTheBackAnupKumarPanwar
authored andcommitted
Improve Formatting and Code Quality (#934)
* Improved Formatting of basic_maths.py - Added docstrings. - Improved whitespace formatting. - Renamed functions to match snake_case. * Improved Formatting of factorial_python.py - Added docstrings. - Improved whitespace formatting. - Renamed constants to match UPPER_CASE. * Improved Formatting of factorial_recursive.py - Improved whitespace formatting to meet PyLint standards. * Improved Code to Conform to PyLint - Renamed `max` to `max_num` to avoid redefining built-in 'max' [pylint] - Removed unnecessary parens after 'while' keyword [pylint] * Improved Formatting of factorial_recursive.py - Added docstrings. - Improved whitespace formatting.
1 parent bd40179 commit a2236cf

File tree

5 files changed

+71
-57
lines changed

5 files changed

+71
-57
lines changed

Diff for: maths/basic_maths.py

+38-28
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,84 @@
1+
"""Implementation of Basic Math in Python."""
12
import math
23

3-
def primeFactors(n):
4+
5+
def prime_factors(n):
6+
"""Find Prime Factors."""
47
pf = []
58
while n % 2 == 0:
69
pf.append(2)
710
n = int(n / 2)
8-
9-
for i in range(3, int(math.sqrt(n))+1, 2):
11+
12+
for i in range(3, int(math.sqrt(n)) + 1, 2):
1013
while n % i == 0:
1114
pf.append(i)
1215
n = int(n / i)
13-
16+
1417
if n > 2:
1518
pf.append(n)
16-
19+
1720
return pf
1821

19-
def numberOfDivisors(n):
22+
23+
def number_of_divisors(n):
24+
"""Calculate Number of Divisors of an Integer."""
2025
div = 1
21-
26+
2227
temp = 1
2328
while n % 2 == 0:
2429
temp += 1
2530
n = int(n / 2)
26-
div = div * (temp)
27-
28-
for i in range(3, int(math.sqrt(n))+1, 2):
31+
div = div * (temp)
32+
33+
for i in range(3, int(math.sqrt(n)) + 1, 2):
2934
temp = 1
3035
while n % i == 0:
3136
temp += 1
3237
n = int(n / i)
3338
div = div * (temp)
34-
39+
3540
return div
3641

37-
def sumOfDivisors(n):
42+
43+
def sum_of_divisors(n):
44+
"""Calculate Sum of Divisors."""
3845
s = 1
39-
46+
4047
temp = 1
4148
while n % 2 == 0:
4249
temp += 1
4350
n = int(n / 2)
4451
if temp > 1:
45-
s *= (2**temp - 1) / (2 - 1)
46-
47-
for i in range(3, int(math.sqrt(n))+1, 2):
52+
s *= (2**temp - 1) / (2 - 1)
53+
54+
for i in range(3, int(math.sqrt(n)) + 1, 2):
4855
temp = 1
4956
while n % i == 0:
5057
temp += 1
5158
n = int(n / i)
5259
if temp > 1:
5360
s *= (i**temp - 1) / (i - 1)
54-
61+
5562
return s
5663

57-
def eulerPhi(n):
58-
l = primeFactors(n)
64+
65+
def euler_phi(n):
66+
"""Calculte Euler's Phi Function."""
67+
l = prime_factors(n)
5968
l = set(l)
6069
s = n
6170
for x in l:
62-
s *= (x - 1)/x
63-
return s
71+
s *= (x - 1) / x
72+
return s
73+
6474

6575
def main():
66-
print(primeFactors(100))
67-
print(numberOfDivisors(100))
68-
print(sumOfDivisors(100))
69-
print(eulerPhi(100))
70-
76+
"""Print the Results of Basic Math Operations."""
77+
print(prime_factors(100))
78+
print(number_of_divisors(100))
79+
print(sum_of_divisors(100))
80+
print(euler_phi(100))
81+
82+
7183
if __name__ == '__main__':
7284
main()
73-
74-

Diff for: maths/factorial_python.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
# Python program to find the factorial of a number provided by the user.
1+
"""Python program to find the factorial of a number provided by the user."""
22

33
# change the value for a different result
4-
num = 10
4+
NUM = 10
55

66
# uncomment to take input from the user
7-
#num = int(input("Enter a number: "))
7+
# num = int(input("Enter a number: "))
88

9-
factorial = 1
9+
FACTORIAL = 1
1010

1111
# check if the number is negative, positive or zero
12-
if num < 0:
13-
print("Sorry, factorial does not exist for negative numbers")
14-
elif num == 0:
15-
print("The factorial of 0 is 1")
12+
if NUM < 0:
13+
print("Sorry, factorial does not exist for negative numbers")
14+
elif NUM == 0:
15+
print("The factorial of 0 is 1")
1616
else:
17-
for i in range(1,num + 1):
18-
factorial = factorial*i
19-
print("The factorial of",num,"is",factorial)
17+
for i in range(1, NUM + 1):
18+
FACTORIAL = FACTORIAL * i
19+
print("The factorial of", NUM, "is", FACTORIAL)

Diff for: maths/factorial_recursive.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
def fact(n):
2-
"""
3-
Return 1, if n is 1 or below,
4-
otherwise, return n * fact(n-1).
5-
"""
6-
return 1 if n <= 1 else n * fact(n-1)
2+
"""
3+
Return 1, if n is 1 or below,
4+
otherwise, return n * fact(n-1).
5+
"""
6+
return 1 if n <= 1 else n * fact(n - 1)
7+
78

89
"""
9-
Shown factorial for i,
10+
Show factorial for i,
1011
where i ranges from 1 to 20.
1112
"""
12-
for i in range(1,21):
13-
print(i, ": ", fact(i), sep='')
13+
for i in range(1, 21):
14+
print(i, ": ", fact(i), sep='')

Diff for: maths/find_lcm.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55

66
def find_lcm(num_1, num_2):
77
"""Find the LCM of two numbers."""
8-
max = num_1 if num_1 > num_2 else num_2
9-
lcm = max
10-
while (True):
8+
max_num = num_1 if num_1 > num_2 else num_2
9+
lcm = max_num
10+
while True:
1111
if ((lcm % num_1 == 0) and (lcm % num_2 == 0)):
1212
break
13-
lcm += max
13+
lcm += max_num
1414
return lcm
1515

1616

Diff for: sorts/pancake_sort.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1-
# Pancake sort algorithm
1+
"""Pancake Sort Algorithm."""
22
# Only can reverse array from 0 to i
33

4+
45
def pancake_sort(arr):
6+
"""Sort Array with Pancake Sort."""
57
cur = len(arr)
68
while cur > 1:
79
# Find the maximum number in arr
810
mi = arr.index(max(arr[0:cur]))
9-
# Reverse from 0 to mi
10-
arr = arr[mi::-1] + arr[mi+1:len(arr)]
11-
# Reverse whole list
12-
arr = arr[cur-1::-1] + arr[cur:len(arr)]
11+
# Reverse from 0 to mi
12+
arr = arr[mi::-1] + arr[mi + 1:len(arr)]
13+
# Reverse whole list
14+
arr = arr[cur - 1::-1] + arr[cur:len(arr)]
1315
cur -= 1
1416
return arr
1517

18+
1619
if __name__ == '__main__':
17-
print(pancake_sort([0,10,15,3,2,9,14,13]))
20+
print(pancake_sort([0, 10, 15, 3, 2, 9, 14, 13]))

0 commit comments

Comments
 (0)