aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-02-26 13:39:46 +0000
committerGitHub <noreply@github.com>2022-02-26 13:39:46 +0000
commit97448a848ce786a3f17ac392dae2b3c9d02b36a9 (patch)
treeac6f72d3e69443bb1c07af5829d1c6f5cf891ee4
parent6447555b704e5c75091382d04b4525fb04f59ab3 (diff)
parente95bc80fd0f506326ddef251835d0d5330ed5b2e (diff)
downloadperlweeklychallenge-club-97448a848ce786a3f17ac392dae2b3c9d02b36a9.tar.gz
perlweeklychallenge-club-97448a848ce786a3f17ac392dae2b3c9d02b36a9.tar.bz2
perlweeklychallenge-club-97448a848ce786a3f17ac392dae2b3c9d02b36a9.zip
Merge pull request #5708 from LubosKolouch/master
Challenge 153 LK
-rw-r--r--challenge-153/lubos-kolouch/perl/ch-1.pl46
-rw-r--r--challenge-153/lubos-kolouch/perl/ch-2.pl33
-rw-r--r--challenge-153/lubos-kolouch/php/ch-1.php61
-rw-r--r--challenge-153/lubos-kolouch/php/ch-2.php34
-rw-r--r--challenge-153/lubos-kolouch/python/ch-1.py68
-rw-r--r--challenge-153/lubos-kolouch/python/ch-2.py29
6 files changed, 271 insertions, 0 deletions
diff --git a/challenge-153/lubos-kolouch/perl/ch-1.pl b/challenge-153/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..eec490830a
--- /dev/null
+++ b/challenge-153/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,46 @@
+use strict;
+use warnings;
+
+# keep seen factorials in a cache to speed things up
+my %fact_cache = (0 => 1);
+my %left_fact_cache;
+
+sub calculate_factorial {
+ my $what = shift;
+
+ return $fact_cache{$what} if $fact_cache{$what};
+
+ # let's utilize the fact that we are processing the numbers in sequence
+ $fact_cache{$what} = $what * $fact_cache{$what-1};
+
+ return $fact_cache{$what};
+}
+
+sub calculate_left_fact {
+ my $what = shift;
+
+ return $left_fact_cache{$what} if $left_fact_cache{$what};
+
+ # let's utilize the fact that we are processing the numbers in sequence
+ $left_fact_cache{$what} = calculate_factorial($what-1) + $left_fact_cache{$what-1};
+
+ return $left_fact_cache{$what};
+}
+
+
+sub get_left_factorials {
+ my $what = shift;
+
+ my @output;
+
+ for (1..$what) {
+ push @output, calculate_left_fact($_);
+ }
+
+ return \@output;
+}
+
+use Test::More;
+
+is_deeply(get_left_factorials(10), [1, 2, 4, 10, 34, 154, 874, 5914, 46234, 409114]);
+done_testing;
diff --git a/challenge-153/lubos-kolouch/perl/ch-2.pl b/challenge-153/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..6911fdbe74
--- /dev/null
+++ b/challenge-153/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,33 @@
+use strict;
+use warnings;
+
+
+sub calculate_factorial {
+ my $what = shift;
+
+ my $fact = 1;
+
+ $fact *= $_ for (1..$what);
+
+ return $fact;
+}
+
+sub get_factorions_sum {
+ my $what = shift;
+
+ my $sum = 0;
+
+ $sum += calculate_factorial($_) for (split //, $what);
+ return $sum;
+}
+
+
+sub is_equal {
+ my $what = shift;
+ return $what == get_factorions_sum($what);
+}
+
+use Test::More;
+is(is_equal(145), 1);
+is(is_equal(123), '');
+done_testing;
diff --git a/challenge-153/lubos-kolouch/php/ch-1.php b/challenge-153/lubos-kolouch/php/ch-1.php
new file mode 100644
index 0000000000..5b0656f7b1
--- /dev/null
+++ b/challenge-153/lubos-kolouch/php/ch-1.php
@@ -0,0 +1,61 @@
+<?php
+
+class LeftFactCalculator {
+
+ public function __construct() {
+ # keep seen factorials in a cache to speed things up
+ $this->fact_cache = [0 => 1];
+ $this->left_fact_cache = [0 => 0];
+ }
+
+ public function calculate_factorial(int $what) {
+
+ if (array_key_exists($what, $this->fact_cache)) {
+ return $this->fact_cache[$what];
+ }
+
+ # let's utilize the fact that we are processing the numbers in sequence
+ $this->fact_cache[$what] = $what * $this->fact_cache[$what - 1];
+
+ return $this->fact_cache[$what];
+ }
+
+ public function calculate_left_fact(int $what) {
+
+ if (array_key_exists($what, $this->left_fact_cache)) {
+ return $this->left_fact_cache[$what];
+ }
+
+ # let's utilize the fact that we are processing the numbers in sequence
+ $this->left_fact_cache[$what] = $this->calculate_factorial($what - 1) + $this->left_fact_cache[$what - 1];
+
+ return $this->left_fact_cache[$what];
+ }
+
+ public function get_left_factorial(int $what) {
+
+ $output = [];
+
+ foreach (range(1, $what) as $i) {
+ $output[] = $this->calculate_left_fact($i);
+ }
+
+ return $output;
+ }
+}
+
+$calculator = new LeftFactCalculator();
+array_diff($calculator->get_left_factorial(10), [
+ 1,
+ 2,
+ 4,
+ 10,
+ 34,
+ 154,
+ 874,
+ 5914,
+ 46234,
+ 409114,
+ ]) == [] or throw new Exception("Failed test 1");
+
+?>
diff --git a/challenge-153/lubos-kolouch/php/ch-2.php b/challenge-153/lubos-kolouch/php/ch-2.php
new file mode 100644
index 0000000000..d7ab361c9f
--- /dev/null
+++ b/challenge-153/lubos-kolouch/php/ch-2.php
@@ -0,0 +1,34 @@
+<?php
+
+function calculate_factorial(int $what){
+
+ $fact = 1;
+
+ foreach (range(1, $what) as $i) {
+ $fact *= $i;
+ }
+
+ return $fact;
+}
+
+
+function get_factorions_sum(int $what){
+
+ $my_sum = 0;
+ foreach (str_split($what) as $i) {
+ $my_sum += calculate_factorial($i);
+ }
+
+ return $my_sum;
+}
+
+
+function is_equal(int $what){
+ return $what == get_factorions_sum($what);
+}
+
+
+is_equal(145) == 1 or throw new Exception("Test 1 failed");
+is_equal(123) == 0 or throw new Exception("Test 2 failed");
+
+?>
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