From a2236cfb97ce96b13c03be9761a3d053997aace9 Mon Sep 17 00:00:00 2001 From: PatOnTheBack <51241310+PatOnTheBack@users.noreply.github.com> Date: Tue, 2 Jul 2019 00:05:43 -0400 Subject: [PATCH] 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. --- maths/basic_maths.py | 66 +++++++++++++++++++++--------------- maths/factorial_python.py | 22 ++++++------ maths/factorial_recursive.py | 17 +++++----- maths/find_lcm.py | 8 ++--- sorts/pancake_sort.py | 15 ++++---- 5 files changed, 71 insertions(+), 57 deletions(-) diff --git a/maths/basic_maths.py b/maths/basic_maths.py index 6e8c919a0..cd7bac011 100644 --- a/maths/basic_maths.py +++ b/maths/basic_maths.py @@ -1,74 +1,84 @@ +"""Implementation of Basic Math in Python.""" import math -def primeFactors(n): + +def prime_factors(n): + """Find Prime Factors.""" pf = [] while n % 2 == 0: pf.append(2) n = int(n / 2) - - for i in range(3, int(math.sqrt(n))+1, 2): + + for i in range(3, int(math.sqrt(n)) + 1, 2): while n % i == 0: pf.append(i) n = int(n / i) - + if n > 2: pf.append(n) - + return pf -def numberOfDivisors(n): + +def number_of_divisors(n): + """Calculate Number of Divisors of an Integer.""" div = 1 - + temp = 1 while n % 2 == 0: temp += 1 n = int(n / 2) - div = div * (temp) - - for i in range(3, int(math.sqrt(n))+1, 2): + div = div * (temp) + + for i in range(3, int(math.sqrt(n)) + 1, 2): temp = 1 while n % i == 0: temp += 1 n = int(n / i) div = div * (temp) - + return div -def sumOfDivisors(n): + +def sum_of_divisors(n): + """Calculate Sum of Divisors.""" s = 1 - + temp = 1 while n % 2 == 0: temp += 1 n = int(n / 2) if temp > 1: - s *= (2**temp - 1) / (2 - 1) - - for i in range(3, int(math.sqrt(n))+1, 2): + s *= (2**temp - 1) / (2 - 1) + + for i in range(3, int(math.sqrt(n)) + 1, 2): temp = 1 while n % i == 0: temp += 1 n = int(n / i) if temp > 1: s *= (i**temp - 1) / (i - 1) - + return s -def eulerPhi(n): - l = primeFactors(n) + +def euler_phi(n): + """Calculte Euler's Phi Function.""" + l = prime_factors(n) l = set(l) s = n for x in l: - s *= (x - 1)/x - return s + s *= (x - 1) / x + return s + def main(): - print(primeFactors(100)) - print(numberOfDivisors(100)) - print(sumOfDivisors(100)) - print(eulerPhi(100)) - + """Print the Results of Basic Math Operations.""" + print(prime_factors(100)) + print(number_of_divisors(100)) + print(sum_of_divisors(100)) + print(euler_phi(100)) + + if __name__ == '__main__': main() - - \ No newline at end of file diff --git a/maths/factorial_python.py b/maths/factorial_python.py index 376983e08..6c1349fd5 100644 --- a/maths/factorial_python.py +++ b/maths/factorial_python.py @@ -1,19 +1,19 @@ -# Python program to find the factorial of a number provided by the user. +"""Python program to find the factorial of a number provided by the user.""" # change the value for a different result -num = 10 +NUM = 10 # uncomment to take input from the user -#num = int(input("Enter a number: ")) +# num = int(input("Enter a number: ")) -factorial = 1 +FACTORIAL = 1 # check if the number is negative, positive or zero -if num < 0: - print("Sorry, factorial does not exist for negative numbers") -elif num == 0: - print("The factorial of 0 is 1") +if NUM < 0: + print("Sorry, factorial does not exist for negative numbers") +elif NUM == 0: + print("The factorial of 0 is 1") else: - for i in range(1,num + 1): - factorial = factorial*i - print("The factorial of",num,"is",factorial) + for i in range(1, NUM + 1): + FACTORIAL = FACTORIAL * i + print("The factorial of", NUM, "is", FACTORIAL) diff --git a/maths/factorial_recursive.py b/maths/factorial_recursive.py index 41391a271..06173dcbc 100644 --- a/maths/factorial_recursive.py +++ b/maths/factorial_recursive.py @@ -1,13 +1,14 @@ def fact(n): - """ - Return 1, if n is 1 or below, - otherwise, return n * fact(n-1). - """ - return 1 if n <= 1 else n * fact(n-1) + """ + Return 1, if n is 1 or below, + otherwise, return n * fact(n-1). + """ + return 1 if n <= 1 else n * fact(n - 1) + """ -Shown factorial for i, +Show factorial for i, where i ranges from 1 to 20. """ -for i in range(1,21): - print(i, ": ", fact(i), sep='') +for i in range(1, 21): + print(i, ": ", fact(i), sep='') diff --git a/maths/find_lcm.py b/maths/find_lcm.py index 779cb1288..9062d462b 100644 --- a/maths/find_lcm.py +++ b/maths/find_lcm.py @@ -5,12 +5,12 @@ def find_lcm(num_1, num_2): """Find the LCM of two numbers.""" - max = num_1 if num_1 > num_2 else num_2 - lcm = max - while (True): + max_num = num_1 if num_1 > num_2 else num_2 + lcm = max_num + while True: if ((lcm % num_1 == 0) and (lcm % num_2 == 0)): break - lcm += max + lcm += max_num return lcm diff --git a/sorts/pancake_sort.py b/sorts/pancake_sort.py index 478a9a967..3b48bc6e4 100644 --- a/sorts/pancake_sort.py +++ b/sorts/pancake_sort.py @@ -1,17 +1,20 @@ -# Pancake sort algorithm +"""Pancake Sort Algorithm.""" # Only can reverse array from 0 to i + def pancake_sort(arr): + """Sort Array with Pancake Sort.""" cur = len(arr) while cur > 1: # Find the maximum number in arr mi = arr.index(max(arr[0:cur])) - # Reverse from 0 to mi - arr = arr[mi::-1] + arr[mi+1:len(arr)] - # Reverse whole list - arr = arr[cur-1::-1] + arr[cur:len(arr)] + # Reverse from 0 to mi + arr = arr[mi::-1] + arr[mi + 1:len(arr)] + # Reverse whole list + arr = arr[cur - 1::-1] + arr[cur:len(arr)] cur -= 1 return arr + if __name__ == '__main__': - print(pancake_sort([0,10,15,3,2,9,14,13])) + print(pancake_sort([0, 10, 15, 3, 2, 9, 14, 13]))