aboutsummaryrefslogtreecommitdiff
path: root/challenge-153/lubos-kolouch/python
diff options
context:
space:
mode:
authordrbaggy <js5@sanger.ac.uk>2022-02-28 06:42:52 +0000
committerdrbaggy <js5@sanger.ac.uk>2022-02-28 06:42:52 +0000
commit79466de7ef71946f9ef6acd21d9b80fede2d7ef3 (patch)
treefc7250288695fe29a2fe4294bb6371ccd28c46b4 /challenge-153/lubos-kolouch/python
parentad4031ad944a6ee247c953c7d3e8c98e27fe5f23 (diff)
parent708f0b09a688c48e140d552c4116678099bb0581 (diff)
downloadperlweeklychallenge-club-79466de7ef71946f9ef6acd21d9b80fede2d7ef3.tar.gz
perlweeklychallenge-club-79466de7ef71946f9ef6acd21d9b80fede2d7ef3.tar.bz2
perlweeklychallenge-club-79466de7ef71946f9ef6acd21d9b80fede2d7ef3.zip
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-153/lubos-kolouch/python')
-rw-r--r--challenge-153/lubos-kolouch/python/ch-1.py68
-rw-r--r--challenge-153/lubos-kolouch/python/ch-2.py29
2 files changed, 97 insertions, 0 deletions
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()
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