From f50d577c273c460cafc1ebf53d5ab117a44341a4 Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Sat, 26 Feb 2022 12:05:49 +0100 Subject: feat(challenge-153/lubos-kolouch/python/ch-1.py): Challenge 153 Task 1 Python LK --- challenge-153/lubos-kolouch/python/ch-1.py | 68 ++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 challenge-153/lubos-kolouch/python/ch-1.py (limited to 'challenge-153/lubos-kolouch/python') diff --git a/challenge-153/lubos-kolouch/python/ch-1.py b/challenge-153/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..cdb8b0a586 --- /dev/null +++ b/challenge-153/lubos-kolouch/python/ch-1.py @@ -0,0 +1,68 @@ +""" Challenge 153 Task 1""" + + +class LeftFactCalculator: + """Challenge 153 Task 1""" + + def __init__(self): + # keep seen factorials in a cache to speed things up + self.fact_cache = {0: 1} + self.left_fact_cache = {0: 0} + + def calculate_factorial(self, what: int): + """Calculate factorial on one number""" + + try: + return self.fact_cache[what] + except KeyError: + pass + + # let's utilize the fact that we are processing the numbers in sequence + self.fact_cache[what] = what * self.fact_cache[what - 1] + + return self.fact_cache[what] + + def calculate_left_fact(self, what: int): + """Calculate the left factorial of one number""" + + try: + return self.left_fact_cache[what] + except KeyError: + pass + + # let's utilize the fact that we are processing the numbers in sequence + self.left_fact_cache[what] = ( + self.calculate_factorial(what - 1) + self.left_fact_cache[what - 1] + ) + + return self.left_fact_cache[what] + + def get_left_factorial(self, what: int): + """Get the solution for the task""" + + output = [] + + for i in range(1, what + 1): + output.append(self.calculate_left_fact(i)) + + return output + + +def main(): + calculator = LeftFactCalculator() + assert calculator.get_left_factorial(10) == [ + 1, + 2, + 4, + 10, + 34, + 154, + 874, + 5914, + 46234, + 409114, + ] + + +if __name__ == "__main__": + main() -- cgit From decb68c5a9c212ae099534722c360da5644ba498 Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Sat, 26 Feb 2022 12:27:17 +0100 Subject: feat(challenge-153/lubos-kolouch/python/ch-2.py): Challenge 153 Task 2 Python LK --- challenge-153/lubos-kolouch/python/ch-2.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 challenge-153/lubos-kolouch/python/ch-2.py (limited to 'challenge-153/lubos-kolouch/python') diff --git a/challenge-153/lubos-kolouch/python/ch-2.py b/challenge-153/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..4f4090917e --- /dev/null +++ b/challenge-153/lubos-kolouch/python/ch-2.py @@ -0,0 +1,29 @@ +""" Challenge 153 Task 2""" + + +def calculate_factorial(what: int): + """Calculate the factorial""" + + fact = 1 + + for i in range(1, what + 1): + fact *= i + + return fact + + +def get_factorions_sum(what: int): + """Get the factorions sum""" + + my_sum = sum(calculate_factorial(int(i)) for i in str(what)) + + return my_sum + + +def is_equal(what: int): + """Test the equality""" + return what == get_factorions_sum(what) + + +assert is_equal(145) == 1 +assert is_equal(123) == 0 -- cgit