diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-11-17 16:40:39 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-11-17 16:40:39 +0000 |
| commit | e9a204c20ca0ce35486c10b6ffed93a7ff371e22 (patch) | |
| tree | fc8f2e5e5aecd102055ac99ed89844edd5f8ff73 | |
| parent | b3dbd5d952b6251da75bfe1b5e744981be7f88c8 (diff) | |
| parent | 1dc7c094e0b15c0c06dbb8bc1da67497e3b70a53 (diff) | |
| download | perlweeklychallenge-club-e9a204c20ca0ce35486c10b6ffed93a7ff371e22.tar.gz perlweeklychallenge-club-e9a204c20ca0ce35486c10b6ffed93a7ff371e22.tar.bz2 perlweeklychallenge-club-e9a204c20ca0ce35486c10b6ffed93a7ff371e22.zip | |
Merge pull request #11170 from pme/challenge-295
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; |
