mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-19 19:03:02 +08:00
Added doctest and more explanation about Dijkstra execution. (#1014)
* Added doctest and more explanation about Dijkstra execution. * tests were not passing with python2 due to missing __init__.py file at number_theory folder * Removed the dot at the beginning of the imported modules names because 'python3 -m doctest -v data_structures/hashing/*.py' and 'python3 -m doctest -v data_structures/stacks/*.py' were failing not finding hash_table.py and stack.py modules. * Moved global code to main scope and added doctest for project euler problems 1 to 14. * Added test case for negative input. * Changed N variable to do not use end of line scape because in case there is a space after it the script will break making it much more error prone. * Added problems description and doctests to the ones that were missing. Limited line length to 79 and executed python black over all scripts. * Changed the way files are loaded to support pytest call. * Added __init__.py to problems to make them modules and allow pytest execution. * Added project_euler folder to test units execution * Changed 'os.path.split(os.path.realpath(__file__))' to 'os.path.dirname()'
This commit is contained in:

committed by
cclauss

parent
2fb3beeaf1
commit
267b5eff40
@ -1,27 +1,51 @@
|
||||
# Finding the factorial.
|
||||
"""
|
||||
n! means n × (n − 1) × ... × 3 × 2 × 1
|
||||
|
||||
For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800,
|
||||
and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.
|
||||
|
||||
Find the sum of the digits in the number 100!
|
||||
"""
|
||||
|
||||
|
||||
def factorial(n):
|
||||
fact = 1
|
||||
for i in range(1,n+1):
|
||||
for i in range(1, n + 1):
|
||||
fact *= i
|
||||
return fact
|
||||
|
||||
# Spliting the digits and adding it.
|
||||
|
||||
def split_and_add(number):
|
||||
"""Split number digits and add them."""
|
||||
sum_of_digits = 0
|
||||
while(number>0):
|
||||
while number > 0:
|
||||
last_digit = number % 10
|
||||
sum_of_digits += last_digit
|
||||
number = int(number/10) # Removing the last_digit from the given number.
|
||||
number = number // 10 # Removing the last_digit from the given number
|
||||
return sum_of_digits
|
||||
|
||||
# Taking the user input.
|
||||
number = int(input("Enter the Number: "))
|
||||
|
||||
# Assigning the factorial from the factorial function.
|
||||
factorial = factorial(number)
|
||||
def solution(n):
|
||||
"""Returns the sum of the digits in the number 100!
|
||||
>>> solution(100)
|
||||
648
|
||||
>>> solution(50)
|
||||
216
|
||||
>>> solution(10)
|
||||
27
|
||||
>>> solution(5)
|
||||
3
|
||||
>>> solution(3)
|
||||
6
|
||||
>>> solution(2)
|
||||
2
|
||||
>>> solution(1)
|
||||
1
|
||||
"""
|
||||
f = factorial(n)
|
||||
result = split_and_add(f)
|
||||
return result
|
||||
|
||||
# Spliting and adding the factorial into answer.
|
||||
answer = split_and_add(factorial)
|
||||
|
||||
# Printing the answer.
|
||||
print(answer)
|
||||
if __name__ == "__main__":
|
||||
print(solution(int(input("Enter the Number: ").strip())))
|
||||
|
Reference in New Issue
Block a user