mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-04-19 04:07:36 +00:00
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.
This commit is contained in:
parent
bd4017928e
commit
a2236cfb97
@ -1,6 +1,9 @@
|
|||||||
|
"""Implementation of Basic Math in Python."""
|
||||||
import math
|
import math
|
||||||
|
|
||||||
def primeFactors(n):
|
|
||||||
|
def prime_factors(n):
|
||||||
|
"""Find Prime Factors."""
|
||||||
pf = []
|
pf = []
|
||||||
while n % 2 == 0:
|
while n % 2 == 0:
|
||||||
pf.append(2)
|
pf.append(2)
|
||||||
@ -16,7 +19,9 @@ def primeFactors(n):
|
|||||||
|
|
||||||
return pf
|
return pf
|
||||||
|
|
||||||
def numberOfDivisors(n):
|
|
||||||
|
def number_of_divisors(n):
|
||||||
|
"""Calculate Number of Divisors of an Integer."""
|
||||||
div = 1
|
div = 1
|
||||||
|
|
||||||
temp = 1
|
temp = 1
|
||||||
@ -34,7 +39,9 @@ def numberOfDivisors(n):
|
|||||||
|
|
||||||
return div
|
return div
|
||||||
|
|
||||||
def sumOfDivisors(n):
|
|
||||||
|
def sum_of_divisors(n):
|
||||||
|
"""Calculate Sum of Divisors."""
|
||||||
s = 1
|
s = 1
|
||||||
|
|
||||||
temp = 1
|
temp = 1
|
||||||
@ -54,21 +61,24 @@ def sumOfDivisors(n):
|
|||||||
|
|
||||||
return s
|
return s
|
||||||
|
|
||||||
def eulerPhi(n):
|
|
||||||
l = primeFactors(n)
|
def euler_phi(n):
|
||||||
|
"""Calculte Euler's Phi Function."""
|
||||||
|
l = prime_factors(n)
|
||||||
l = set(l)
|
l = set(l)
|
||||||
s = n
|
s = n
|
||||||
for x in l:
|
for x in l:
|
||||||
s *= (x - 1) / x
|
s *= (x - 1) / x
|
||||||
return s
|
return s
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
print(primeFactors(100))
|
"""Print the Results of Basic Math Operations."""
|
||||||
print(numberOfDivisors(100))
|
print(prime_factors(100))
|
||||||
print(sumOfDivisors(100))
|
print(number_of_divisors(100))
|
||||||
print(eulerPhi(100))
|
print(sum_of_divisors(100))
|
||||||
|
print(euler_phi(100))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|
||||||
|
|
@ -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
|
# change the value for a different result
|
||||||
num = 10
|
NUM = 10
|
||||||
|
|
||||||
# uncomment to take input from the user
|
# 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
|
# check if the number is negative, positive or zero
|
||||||
if num < 0:
|
if NUM < 0:
|
||||||
print("Sorry, factorial does not exist for negative numbers")
|
print("Sorry, factorial does not exist for negative numbers")
|
||||||
elif num == 0:
|
elif NUM == 0:
|
||||||
print("The factorial of 0 is 1")
|
print("The factorial of 0 is 1")
|
||||||
else:
|
else:
|
||||||
for i in range(1,num + 1):
|
for i in range(1, NUM + 1):
|
||||||
factorial = factorial*i
|
FACTORIAL = FACTORIAL * i
|
||||||
print("The factorial of",num,"is",factorial)
|
print("The factorial of", NUM, "is", FACTORIAL)
|
||||||
|
@ -5,8 +5,9 @@ def fact(n):
|
|||||||
"""
|
"""
|
||||||
return 1 if n <= 1 else 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.
|
where i ranges from 1 to 20.
|
||||||
"""
|
"""
|
||||||
for i in range(1, 21):
|
for i in range(1, 21):
|
||||||
|
@ -5,12 +5,12 @@
|
|||||||
|
|
||||||
def find_lcm(num_1, num_2):
|
def find_lcm(num_1, num_2):
|
||||||
"""Find the LCM of two numbers."""
|
"""Find the LCM of two numbers."""
|
||||||
max = num_1 if num_1 > num_2 else num_2
|
max_num = num_1 if num_1 > num_2 else num_2
|
||||||
lcm = max
|
lcm = max_num
|
||||||
while (True):
|
while True:
|
||||||
if ((lcm % num_1 == 0) and (lcm % num_2 == 0)):
|
if ((lcm % num_1 == 0) and (lcm % num_2 == 0)):
|
||||||
break
|
break
|
||||||
lcm += max
|
lcm += max_num
|
||||||
return lcm
|
return lcm
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
# Pancake sort algorithm
|
"""Pancake Sort Algorithm."""
|
||||||
# Only can reverse array from 0 to i
|
# Only can reverse array from 0 to i
|
||||||
|
|
||||||
|
|
||||||
def pancake_sort(arr):
|
def pancake_sort(arr):
|
||||||
|
"""Sort Array with Pancake Sort."""
|
||||||
cur = len(arr)
|
cur = len(arr)
|
||||||
while cur > 1:
|
while cur > 1:
|
||||||
# Find the maximum number in arr
|
# Find the maximum number in arr
|
||||||
@ -13,5 +15,6 @@ def pancake_sort(arr):
|
|||||||
cur -= 1
|
cur -= 1
|
||||||
return arr
|
return arr
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
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]))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user