From 2294c901bff2c9794cd4dffd3ff4e09c423ef2f6 Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Sat, 28 Nov 2020 13:58:26 +0100 Subject: Solutions Challenge 088 Perl LK --- challenge-088/lubos-kolouch/perl/ch-1.pl | 38 +++++++++++++++ challenge-088/lubos-kolouch/perl/ch-2.pl | 84 ++++++++++++++++++++++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 challenge-088/lubos-kolouch/perl/ch-1.pl create mode 100644 challenge-088/lubos-kolouch/perl/ch-2.pl diff --git a/challenge-088/lubos-kolouch/perl/ch-1.pl b/challenge-088/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..34ac6c9a8a --- /dev/null +++ b/challenge-088/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,38 @@ +#!/usr/bin/perl +#=============================================================================== +# +# FILE: ch-1.pl +# +# USAGE: ./ch-1.pl +# +# DESCRIPTION: https://perlweeklychallenge.org/blog/perl-weekly-challenge-088/ +# Task 1 +# Array of Product +# +# AUTHOR: Lubos Kolouch +# VERSION: 1.0 +# CREATED: 11/28/2020 01:02:17 PM +#=============================================================================== + +use strict; +use warnings; +use List::Util qw/product/; + +sub get_product{ + my $in_arr = shift; + + my $product = product(@$in_arr); + + my @out_arr; + + push @out_arr, int($product / $_) for (@$in_arr); + + return \@out_arr; +} + +use Test::More; + +is_deeply(get_product([5, 2, 1, 4, 3]), [24, 60, 120, 30, 40]); +is_deeply(get_product([2, 1, 4, 3]), [12, 24, 6, 8]); + +done_testing(); diff --git a/challenge-088/lubos-kolouch/perl/ch-2.pl b/challenge-088/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..19694d424c --- /dev/null +++ b/challenge-088/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,84 @@ +#!/usr/bin/perl +#=============================================================================== +# +# FILE: ch-2.pl +# +# USAGE: ./ch-2.pl +# +# DESCRIPTION: https://perlweeklychallenge.org/blog/perl-weekly-challenge-088/ +# Task 2 +# Spiral Matrix +# +# AUTHOR: Lubos Kolouch +# VERSION: 1.0 +# CREATED: 11/28/2020 01:02:17 PM +#=============================================================================== + +use strict; +use warnings; + +sub get_spiral{ + my $in_arr = shift; + + my $x_max = scalar @$in_arr; + my $y_max = scalar @{$in_arr->[0]}; + + my @pos = (0, 0); + my $direction = ">"; + + my %seen_cells; + my @out_arr; + + while (1) { + + return \@out_arr if scalar @out_arr == $x_max * $y_max; + + push @out_arr, $in_arr->[$pos[0]][$pos[1]] unless $seen_cells{$pos[0]}{$pos[1]}; + $seen_cells{$pos[0]}{$pos[1]} = 1; + + # can move in the direction we are walking? + if ($direction eq ">") { + if (($pos[1] + 1 < $x_max) and not ($seen_cells{$pos[0]}{$pos[1]+1})) { + $pos[1]++; + } else { + $direction = "v"; + } + } + + + elsif ($direction eq "v") { + if (($pos[0] + 1 < $y_max) and not ($seen_cells{$pos[0]+1}{$pos[1]})) { + $pos[0]++; + } else { + $direction = "<"; + } + } + + elsif ($direction eq "<") { + if (($pos[0] - 1 >= 0) and not ($seen_cells{$pos[0]-1}{$pos[1]})) { + $pos[0]--; + next; + } else { + $direction = "^"; + } + } + + elsif ($direction eq "^") { + if (($pos[1] - 1 >= 0) and not ($seen_cells{$pos[0]}{$pos[1]-1})) { + $pos[1]--; + next; + } else { + $direction = ">"; + } + } + + } + +} + +use Test::More; + +is_deeply(get_spiral([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), [1, 2, 3, 6, 9, 8, 7, 4, 5]); +is_deeply(get_spiral([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]), [1, 2, 3, 4, 8, 12, 16, 15, 14, 13, 9, 5, 6, 7, 11, 10 ]); + +done_testing(); -- cgit