aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLubos Kolouch <lubos@kolouch.net>2020-09-13 12:09:55 +0200
committerLubos Kolouch <lubos@kolouch.net>2020-09-13 12:09:55 +0200
commit708410c89342ae715feefdb60614da28f69a4e57 (patch)
tree1df50ae896beb93f678462801800cc6ddfa20eeb
parent0c5e0b4b413f0d992c4ef842e02a7ff7535c3232 (diff)
downloadperlweeklychallenge-club-708410c89342ae715feefdb60614da28f69a4e57.tar.gz
perlweeklychallenge-club-708410c89342ae715feefdb60614da28f69a4e57.tar.bz2
perlweeklychallenge-club-708410c89342ae715feefdb60614da28f69a4e57.zip
Refactor the solution 2
-rw-r--r--challenge-077/lubos-kolouch/perl/ch-1.pl5
-rw-r--r--challenge-077/lubos-kolouch/perl/ch-2.pl58
-rw-r--r--challenge-077/lubos-kolouch/python/ch-2.py39
3 files changed, 58 insertions, 44 deletions
diff --git a/challenge-077/lubos-kolouch/perl/ch-1.pl b/challenge-077/lubos-kolouch/perl/ch-1.pl
index c2d506cd0c..68dfafed82 100644
--- a/challenge-077/lubos-kolouch/perl/ch-1.pl
+++ b/challenge-077/lubos-kolouch/perl/ch-1.pl
@@ -50,8 +50,7 @@ sub partition {
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;
+ my $rem_value = @$solution ? $max_n - sum(@$solution) : $max_n;
if ($rem_value == 0) {
push @solution_arr, [@$solution];
@@ -62,7 +61,7 @@ sub partition {
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 });
+ partition({ idx => $i+1, max_n => $max_n, solution => \@new_arr });
}
return 1;
}
diff --git a/challenge-077/lubos-kolouch/perl/ch-2.pl b/challenge-077/lubos-kolouch/perl/ch-2.pl
index 8360ac008d..711ac85dc7 100644
--- a/challenge-077/lubos-kolouch/perl/ch-2.pl
+++ b/challenge-077/lubos-kolouch/perl/ch-2.pl
@@ -6,42 +6,54 @@ use strict;
use warnings;
use feature qw/say/;
+sub is_position_lone {
+ my ($args) = shift;
+
+ my $matrix = $args->{matrix};
+ my $pos_x = $args->{pos_x};
+ my $pos_y = $args->{pos_y};
+
+ for my $d_x ( -1, 0, 1 ) {
+ for my $d_y ( -1, 0, 1 ) {
+ next if $d_x == 0 and $d_y == 0;
+
+ next if ( $pos_x + $d_x < 0 ) or ( $pos_x + $d_x >= scalar @$matrix );
+ next if ( $pos_y + $d_y < 0 ) or ( $pos_y + $d_y >= scalar @$matrix );
+
+ return 0 if $matrix->[ $pos_x + $d_x ][ $pos_y + $d_y ] eq 'X';
+ }
+ }
+
+ return 1;
+}
+
sub count_lone {
- #""" count the occurences of lone X """
-
- my $solution_count = 0;
- my $matrix = shift;
- for my $line_nr ( 0 .. scalar @$matrix - 1 ) {
- for my $col_nr ( 0 .. scalar @$matrix - 1 ) {
+ #""" count the occurences of lone X """
- next if $matrix->[$line_nr][$col_nr] eq "O";
+ my $solution_count = 0;
+ my $matrix = shift;
- my $is_lone = 1;
- for my $d_x ( -1, 0, 1 ) {
- for my $d_y ( -1, 0, 1 ) {
- next if $d_x == 0 and $d_y == 0;
+ for my $pos_x ( 0 .. scalar @$matrix - 1 ) {
+ for my $pos_y ( 0 .. scalar @$matrix - 1 ) {
- next if ( $line_nr + $d_x < 0 ) or ( $line_nr + $d_x >= scalar @$matrix );
- next if ( $col_nr + $d_y < 0 ) or ( $col_nr + $d_y >= scalar @$matrix );
+ next if $matrix->[$pos_x][$pos_y] eq "O";
- if ( $matrix->[ $line_nr + $d_x ][ $col_nr + $d_y ] eq 'X' ) {
- $is_lone = 0;
- last;
- }
- }
- }
- $solution_count += 1 if $is_lone;
+ $solution_count++ if is_position_lone({ matrix => $matrix, pos_x => $pos_x, pos_y => $pos_y } );
}
}
return $solution_count;
}
-
use Test::More;
-is( count_lone([['O', 'O', 'X'], ['X', 'O', 'O'], ['X', 'O', 'O']]), 1);
-is( count_lone([['O', 'O', 'X', 'O'], ['X', 'O', 'O', 'O'], ['X', 'O', 'O', 'X'], ['O', 'X', 'O', 'O']]), 2);
+is( count_lone( [ [ 'O', 'O', 'X' ], [ 'X', 'O', 'O' ], [ 'X', 'O', 'O' ] ] ), 1 );
+is(
+ count_lone(
+ [ [ 'O', 'O', 'X', 'O' ], [ 'X', 'O', 'O', 'O' ], [ 'X', 'O', 'O', 'X' ], [ 'O', 'X', 'O', 'O' ] ]
+ ),
+ 2
+);
done_testing;
diff --git a/challenge-077/lubos-kolouch/python/ch-2.py b/challenge-077/lubos-kolouch/python/ch-2.py
index f77995658d..0e7ffc9c7a 100644
--- a/challenge-077/lubos-kolouch/python/ch-2.py
+++ b/challenge-077/lubos-kolouch/python/ch-2.py
@@ -12,31 +12,34 @@ class LoneX:
self.matrix = matrix
self.solution_count = 0
- def count_lone(self):
- """ count the occurences of lone X """
+ def check_position(self, pos_x: int, pos_y: int):
+ """ check if the position is lone """
- for line_nr, line in enumerate(self.matrix):
- for col_nr, char in enumerate(line):
- if char == "O":
+ for d_x in [-1, 0, 1]:
+ for d_y in [-1, 0, 1]:
+ if d_x == d_y == 0:
continue
- is_lone = True
- for d_x in [-1, 0, 1]:
- for d_y in [-1, 0, 1]:
- if d_x == d_y == 0:
- continue
+ if pos_x + d_x < 0 or pos_x + d_x >= len(self.matrix):
+ continue
+
+ if pos_y + d_y < 0 or pos_y + d_y >= len(self.matrix):
+ continue
- if line_nr + d_x < 0 or line_nr + d_x >= len(self.matrix):
- continue
+ if self.matrix[pos_x + d_x][pos_y + d_y] == 'X':
+ return False
- if col_nr + d_y < 0 or col_nr + d_y >= len(self.matrix):
- continue
+ return True
- if self.matrix[line_nr + d_x][col_nr + d_y] == 'X':
- is_lone = False
- break
+ def count_lone(self):
+ """ count the occurences of lone X """
+
+ for pos_x, line in enumerate(self.matrix):
+ for pos_y, char in enumerate(line):
+ if char == "O":
+ continue
- if is_lone:
+ if self.check_position(pos_x, pos_y):
self.solution_count += 1
def get_count(self):