Skip to content

Commit a027af4

Browse files
committed
initial commit
0 parents  commit a027af4

File tree

7 files changed

+138
-0
lines changed

7 files changed

+138
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__pycache__

Driver.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from math_example import get_sum_factorial_of_digits
2+
from recursive_digits_example import single_digit_of_all_numbers
3+
from input_validation import validate
4+
5+
def iterative_function_calling():
6+
"""
7+
Example that calls other functions
8+
"""
9+
get_sum_factorial_of_digits(123456)
10+
11+
12+
def recursive_function_calling():
13+
"""
14+
Recursive example of the logger being used when the function calls itself
15+
"""
16+
single_digit_of_all_numbers(123456)
17+
18+
19+
def exception_function_calling():
20+
"""
21+
Example with an exception handled
22+
"""
23+
validate("Please input a number between 0 and 5: ", 0, 5)
24+
25+
def main():
26+
# iterative_function_calling()
27+
# recursive_function_calling()
28+
exception_function_calling()
29+
main()

Logger.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from typing import Callable
2+
def log(func: Callable) -> Callable:
3+
def wrapper(*args, **kwargs) -> Callable:
4+
print(f"Calling: {func.__name__}")
5+
print(f"Documentation: {func.__doc__}")
6+
return func(*args, **kwargs)
7+
return wrapper
8+
9+
10+
def output(func: Callable) -> Callable:
11+
def wrapper(*args, **kwargs) -> Callable:
12+
print(f"Calling: {func.__name__}")
13+
print(f"Documentation: {func.__doc__}")
14+
call = func(*args, **kwargs)
15+
if(type(call) != type(None)):
16+
print(f"The output of {func.__name__} is {call}")
17+
18+
return wrapper

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Logger
2+
3+
This repository has a logger included. This also provides an example of how to Logger works. This logger may or may not be useful for all the projects.

input_validation.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from Logger import output
2+
3+
4+
@output
5+
def validate(prompt: str, min: int = 0, max: int = float('inf')) -> int:
6+
"""
7+
Validate the user input and ensure the user input a number between the minimum and maximum values. Will keep trying if the user inputs an invalid character or a number out of the range.
8+
:param prompt – the prompt to ask the user in the user input
9+
:param min – the minimum value – defaults to 0
10+
:param max – the maximum value – defaults to infinity
11+
:return int of choice
12+
"""
13+
try:
14+
val = int(input(prompt))
15+
if(val > max or val < min):
16+
raise TypeError
17+
return val
18+
except TypeError:
19+
"""
20+
out of range failure caught
21+
"""
22+
print(f"Value not between {min} and {max}")
23+
return validate(prompt, min, max)
24+
except ValueError:
25+
"""
26+
Invalid type caught
27+
"""
28+
print("Value inputted was not a number! Try again!")
29+
return validate(prompt, min, max)

math_example.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from Logger import log, output
2+
3+
@log
4+
def factorial(n: int) -> int:
5+
"""
6+
Solve the factorial of some number
7+
:param n – the number to get the factorial of
8+
:return the factorial of n
9+
"""
10+
f = 1
11+
for i in range(1, n + 1):
12+
f *= i
13+
return f
14+
15+
@log
16+
def getDigits(n: int) -> list:
17+
"""
18+
Return a list of the digits of a number
19+
:param n – the number to find the digits of
20+
:return a list of the digits
21+
"""
22+
digits = []
23+
while(n > 0):
24+
digits.append(n % 10)
25+
n //= 10
26+
digits = digits[::-1]
27+
return digits
28+
29+
30+
@output
31+
def get_sum_factorial_of_digits(n: int) -> int:
32+
"""
33+
Get the sum of the factorial of each digit in a given number
34+
:param n – the number to find the value of
35+
:return the sum of the factorial of all the digits
36+
"""
37+
return sum(map(factorial, getDigits(n)))
38+
39+
40+
41+

recursive_digits_example.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from Logger import log, output
2+
3+
@output
4+
def single_digit_of_all_numbers(n: int, s: int = 0) -> int:
5+
"""
6+
Calculate the single digit sum of all the digits
7+
:param n – current number
8+
:param s – sum
9+
:return Integer of total sum of all digits down to single digit
10+
"""
11+
if(n == 0):
12+
if(s > 10):
13+
return single_digit_of_all_numbers(s, 0)
14+
return s
15+
else:
16+
return single_digit_of_all_numbers(n // 10, s + n % 10)
17+

0 commit comments

Comments
 (0)