aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-077/lubos-kolouch/perl/ch-1.pl74
-rw-r--r--challenge-077/lubos-kolouch/python/ch-1.py61
2 files changed, 135 insertions, 0 deletions
diff --git a/challenge-077/lubos-kolouch/perl/ch-1.pl b/challenge-077/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..fdf2a9f1da
--- /dev/null
+++ b/challenge-077/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,74 @@
+#!/bin/env perl
+#""" Perl Weekly challenge 077 Task 1 """
+#""" https://perlweeklychallenge.org/blog/perl-weekly-challenge-077/ """
+#""" Solution Lubos Kolouch """
+use strict;
+use warnings;
+use Data::Dumper;
+use List::Util qw/sum/;
+
+my @all_fibs;
+my @solution_arr;
+
+sub get_all_fibs {
+ #""" Generate all fibonacci numbers """
+ my $max_n = shift;
+
+ unshift @all_fibs, 1;
+ unshift @all_fibs, 2;
+
+ my $fib_nr = 2;
+
+ while ($fib_nr < $max_n) {
+ $fib_nr = $all_fibs[0] + $all_fibs[1];
+ unshift @all_fibs, $fib_nr;
+ }
+}
+
+sub find_solutions {
+ #""" Print all solutions """
+ my $max_n = shift;
+
+ @all_fibs = ();
+ @solution_arr = ();
+ get_all_fibs($max_n);
+
+ partition({ max_n => $max_n });
+ @solution_arr = [0] unless @solution_arr;
+
+ warn Dumper \@solution_arr;
+ return \@solution_arr;
+}
+
+
+sub partition {
+ #""" Recursive method to get the partitions """
+
+ my ($args) = @_;
+
+ my $idx = $args->{idx} // 0;
+ my $solution = $args->{solution} // [];
+ my $max_n = $args->{max_n} // die 'No max value specified';
+
+
+ my $rem_value = @$solution? $max_n - sum(@$solution) : $max_n;
+
+ if ($rem_value == 0) {
+ push @solution_arr, [@$solution];
+ return 0;
+ }
+
+ for my $i ($idx..scalar @all_fibs-1) {
+ next if $all_fibs[$i] > $rem_value;
+
+ my @new_arr = (@$solution, $all_fibs[$i]);
+ partition({ idx => $i+1, max_n => $max_n,solution => \@new_arr });
+ }
+ return 1;
+}
+
+use Test::More;
+
+is_deeply( find_solutions(6), [[5,1],[3,2,1]]);
+is_deeply( find_solutions(9), [[8,1],[5,3,1]]);
+is_deeply( find_solutions(-19), [[0]]);
diff --git a/challenge-077/lubos-kolouch/python/ch-1.py b/challenge-077/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..d181c21487
--- /dev/null
+++ b/challenge-077/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,61 @@
+#!/bin/env python
+""" Perl Weekly challenge 077 Task 1 """
+""" https://perlweeklychallenge.org/blog/perl-weekly-challenge-077/ """
+""" Solution Lubos Kolouch """
+
+
+class FibSolver:
+ """ Class for solving the challenge """
+
+ def __init__(self, n: int):
+ """ init the solution """
+ self.max_n = n
+ self.solution_arr = list()
+ self.all_fibs = list()
+
+ def get_all_fibs(self):
+ """ Generate all fibonacci numbers """
+
+ self.all_fibs.append(2)
+ self.all_fibs.append(1)
+
+ fib_nr = 2
+
+ while fib_nr < self.max_n:
+ fib_nr = self.all_fibs[0] + self.all_fibs[1]
+ self.all_fibs.insert(0, fib_nr)
+
+ def find_solutions(self):
+ """ Print all solutions """
+
+ self.get_all_fibs()
+ self.partition(solution=[])
+
+ if self.solution_arr:
+ print(self.solution_arr)
+ return self.solution_arr
+
+ print("0")
+ return 0
+
+ def partition(self, idx: int = 0, solution: list = None):
+ """ Recursive method to get the partitions """
+
+ rem_value = self.max_n - sum(solution)
+ if rem_value == 0:
+ self.solution_arr.append(solution)
+ return
+
+ for i in range(idx, len(self.all_fibs)):
+ if self.all_fibs[i] > rem_value:
+ continue
+ self.partition(i+1, solution+[self.all_fibs[i]])
+ return
+
+
+fib = FibSolver(-19)
+assert fib.find_solutions() == 0
+fib = FibSolver(6)
+assert fib.find_solutions() == [[5, 1], [3, 2, 1]]
+fib = FibSolver(9)
+assert fib.find_solutions() == [[8, 1], [5, 3, 1]]