diff options
| author | pme <hauptadler@gmail.com> | 2024-11-17 14:18:27 +0100 |
|---|---|---|
| committer | pme <hauptadler@gmail.com> | 2024-11-17 14:18:27 +0100 |
| commit | 1dc7c094e0b15c0c06dbb8bc1da67497e3b70a53 (patch) | |
| tree | 6b602e4817fd29b46ee2a6e87c66674d551756e9 | |
| parent | 3737dfb86ce746a5c07101409360a2ff01c2e9e1 (diff) | |
| download | perlweeklychallenge-club-1dc7c094e0b15c0c06dbb8bc1da67497e3b70a53.tar.gz perlweeklychallenge-club-1dc7c094e0b15c0c06dbb8bc1da67497e3b70a53.tar.bz2 perlweeklychallenge-club-1dc7c094e0b15c0c06dbb8bc1da67497e3b70a53.zip | |
challenge-295
| -rwxr-xr-x | challenge-295/peter-meszaros/perl/ch-1.pl | 56 | ||||
| -rwxr-xr-x | challenge-295/peter-meszaros/perl/ch-2.pl | 65 |
2 files changed, 121 insertions, 0 deletions
diff --git a/challenge-295/peter-meszaros/perl/ch-1.pl b/challenge-295/peter-meszaros/perl/ch-1.pl new file mode 100755 index 0000000000..acb5a9c410 --- /dev/null +++ b/challenge-295/peter-meszaros/perl/ch-1.pl @@ -0,0 +1,56 @@ +#!/usr/bin/env perl +# +=head1 Task 1: Word Break + +Submitted by: Mohammad Sajid Anwar + +You are given a string, $str, and list of words, @words. + +Write a script to return true or false whether the given string can be +segmented into a space separated sequnce of one or more words from the given +list. + +=head2 Example 1 + +Input: $str = 'weeklychallenge', @words = ("challenge", "weekly") +Output: true + +=head2 Example 2 + +Input: $str = "perlrakuperl", @words = ("raku", "perl") +Output: true + +=head2 Example 3 + +Input: $str = "sonsanddaughters", @words = ("sons", "sand", "daughters") +Output: false + +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + [['weeklychallenge', ['challenge', 'weekly']], 1, 'Example 1'], + [['perlrakuperl', ['raku', 'perl']], 1, 'Example 2'], + [['sonsanddaughters', ['sons', 'sand', 'daughters']], 0, 'Example 3'], +]; + +sub word_break +{ + my $w = $_[0]->[0]; + my $l = $_[0]->[1]; + + $w =~ s/$_//g for @$l; + + return length($w) ? 0 : 1; +} + +for (@$cases) { + is(word_break($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; diff --git a/challenge-295/peter-meszaros/perl/ch-2.pl b/challenge-295/peter-meszaros/perl/ch-2.pl new file mode 100755 index 0000000000..b90915b207 --- /dev/null +++ b/challenge-295/peter-meszaros/perl/ch-2.pl @@ -0,0 +1,65 @@ +#!/usr/bin/env perl +# +=head1 Task 2: Jump Game + +Submitted by: Mohammad Sajid Anwar + +You are given an array of integers, @ints. + +Write a script to find the minimum number of jumps to reach the last element. +$ints[$i] represents the maximum length of a forward jump from the index $i. In +case last element is unreachable then return -1. + +=head2 Example 1 + + Input: @ints = (2, 3, 1, 1, 4) + Output: 2 + + Jump 1 step from index 0 then 3 steps from index 1 to the last element. + +=head2 Example 2 + + Input: @ints = (2, 3, 0, 4) + Output: 2 + +=head2 Example 3 + + Input: @ints = (2, 0, 0, 4) + Output: -1 + +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + [[2, 3, 1, 1, 4], 2, 'Example 1'], + [[2, 3, 0, 4], 2, 'Example 2'], + [[2, 0, 0, 4], -1, 'Example 3'], +]; + +sub jump_game +{ + my $l = shift; + + my $jumps = {0 => 0}; + for my $i (0 .. $#$l-1) { + next unless exists $jumps->{$i}; + + for my $j (1 .. $l->[$i]) { + return $jumps->{$i}+1 if ($i+$j) >= $#$l; + $jumps->{$i+$j} //= $jumps->{$i}+1; + } + } + + return -1; +} + +for (@$cases) { + is(jump_game($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; |
