mirror of
https://github.com/TheAlgorithms/Python.git
synced 2026-03-13 09:50:19 +08:00
Added Whitespace and Docstring (#924)
* Added Whitespace and Docstring I modified the file to make Pylint happier and make the code more readable. * Beautified Code and Added Docstring I modified the file to make Pylint happier and make the code more readable. * Added DOCSTRINGS, Wikipedia link, and whitespace I added DOCSTRINGS and whitespace to make the code more readable and understandable. * Improved Formatting * Wrapped comments * Fixed spelling error for `movement` variable * Added DOCSTRINGs * Improved Formatting * Corrected whitespace to improve readability. * Added docstrings. * Made comments fit inside an 80 column layout.
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
"""Tower of Hanoi."""
|
||||
|
||||
# @author willx75
|
||||
# Tower of Hanoi recursion game algorithm is a game, it consists of three rods and a number of disks of different sizes, which can slide onto any rod
|
||||
# Tower of Hanoi recursion game algorithm is a game, it consists of three rods
|
||||
# and a number of disks of different sizes, which can slide onto any rod
|
||||
|
||||
import logging
|
||||
|
||||
@@ -7,18 +10,20 @@ log = logging.getLogger()
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
|
||||
|
||||
def Tower_Of_Hanoi(n, source, dest, by, mouvement):
|
||||
def Tower_Of_Hanoi(n, source, dest, by, movement):
|
||||
"""Tower of Hanoi - Move plates to different rods."""
|
||||
if n == 0:
|
||||
return n
|
||||
elif n == 1:
|
||||
mouvement += 1
|
||||
# no print statement (you could make it an optional flag for printing logs)
|
||||
movement += 1
|
||||
# no print statement
|
||||
# (you could make it an optional flag for printing logs)
|
||||
logging.debug('Move the plate from', source, 'to', dest)
|
||||
return mouvement
|
||||
return movement
|
||||
else:
|
||||
|
||||
mouvement = mouvement + Tower_Of_Hanoi(n-1, source, by, dest, 0)
|
||||
movement = movement + Tower_Of_Hanoi(n - 1, source, by, dest, 0)
|
||||
logging.debug('Move the plate from', source, 'to', dest)
|
||||
|
||||
mouvement = mouvement + 1 + Tower_Of_Hanoi(n-1, by, dest, source, 0)
|
||||
return mouvement
|
||||
movement = movement + 1 + Tower_Of_Hanoi(n - 1, by, dest, source, 0)
|
||||
return movement
|
||||
|
||||
11
maths/abs.py
11
maths/abs.py
@@ -1,6 +1,10 @@
|
||||
"""Absolute Value."""
|
||||
|
||||
|
||||
def absVal(num):
|
||||
"""
|
||||
Function to fins absolute value of numbers.
|
||||
Find the absolute value of a number.
|
||||
|
||||
>>absVal(-5)
|
||||
5
|
||||
>>absVal(0)
|
||||
@@ -11,8 +15,11 @@ def absVal(num):
|
||||
else:
|
||||
return num
|
||||
|
||||
|
||||
def main():
|
||||
print(absVal(-34)) # = 34
|
||||
"""Print absolute value of -34."""
|
||||
print(absVal(-34)) # = 34
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
||||
@@ -1,13 +1,20 @@
|
||||
"""Find mean of a list of numbers."""
|
||||
|
||||
|
||||
def average(nums):
|
||||
"""Find mean of a list of numbers."""
|
||||
sum = 0
|
||||
for x in nums:
|
||||
sum += x
|
||||
sum += x
|
||||
avg = sum / len(nums)
|
||||
print(avg)
|
||||
return avg
|
||||
|
||||
|
||||
def main():
|
||||
average([2, 4, 6, 8, 20, 50, 70])
|
||||
"""Call average module to find mean of a specific list of numbers."""
|
||||
average([2, 4, 6, 8, 20, 50, 70])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
main()
|
||||
|
||||
@@ -1,4 +1,10 @@
|
||||
"""Find Least Common Multiple."""
|
||||
|
||||
# https://en.wikipedia.org/wiki/Least_common_multiple
|
||||
|
||||
|
||||
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):
|
||||
@@ -9,6 +15,7 @@ def find_lcm(num_1, num_2):
|
||||
|
||||
|
||||
def main():
|
||||
"""Use test numbers to run the find_lcm algorithm."""
|
||||
num_1 = 12
|
||||
num_2 = 76
|
||||
print(find_lcm(num_1, num_2))
|
||||
|
||||
Reference in New Issue
Block a user