diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-12-24 17:50:45 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-12-24 17:50:45 +0000 |
| commit | da96f382ee18109d6e94de35a30f66233be49457 (patch) | |
| tree | cbd40d3674a0ff13ae29bb307864a01d2579f2e1 /challenge-091 | |
| parent | 30d79d30b0f7f22f11437d0a798a59a55dd34be5 (diff) | |
| download | perlweeklychallenge-club-da96f382ee18109d6e94de35a30f66233be49457.tar.gz perlweeklychallenge-club-da96f382ee18109d6e94de35a30f66233be49457.tar.bz2 perlweeklychallenge-club-da96f382ee18109d6e94de35a30f66233be49457.zip | |
- Added solutions to week 91 by Colin Crain.
Diffstat (limited to 'challenge-091')
| -rw-r--r-- | challenge-091/colin-crain/perl/ch-1.pl | 121 | ||||
| -rw-r--r-- | challenge-091/colin-crain/perl/ch-2.pl | 130 | ||||
| -rw-r--r-- | challenge-091/colin-crain/raku/ch-1.raku | 24 | ||||
| -rw-r--r-- | challenge-091/colin-crain/raku/ch-2.raku | 30 |
4 files changed, 305 insertions, 0 deletions
diff --git a/challenge-091/colin-crain/perl/ch-1.pl b/challenge-091/colin-crain/perl/ch-1.pl new file mode 100644 index 0000000000..bcaa8fbc37 --- /dev/null +++ b/challenge-091/colin-crain/perl/ch-1.pl @@ -0,0 +1,121 @@ +#! /opt/local/bin/perl5.26
+#
+# number-speak.pl
+#
+# TASK #1 › Count Number
+# Submitted by: Mohammad S Anwar
+# You are given a positive number $N.
+#
+# Write a script to count number and display as you read it.
+#
+# Example 1:
+# Input: $N = 1122234
+# Output: 21321314
+# (as we read "two 1 three 2 one 3 one 4")
+#
+# Example 2:
+# Input: $N = 2333445
+# Output: 12332415
+#
+# (as we read "one 2 three 3 two 4 one 5")
+#
+# Example 3:
+# Input: $N = 12345
+# Output: 1112131415
+#
+# (as we read "one 1 one 2 one 3 one 4 one 5")
+#
+# method:
+# the puzzle here is to count the instances of a given
+# digit as we read the number. The count starts at 1, and
+# if the next number is the same as the previous, the
+# count is incremented.
+#
+# 2020 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+
+
+use warnings;
+use strict;
+use feature ":5.26";
+
+## ## ## ## ## MAIN:
+
+
+my $num = shift @ARGV // '12227786622222222222222222222222222222';
+
+## mention our input
+say "input: ",$num;
+
+## split and count...
+say "numerically ", speak_number( $num );
+
+## ...using regex...
+say "now using a regex: ",
+ join '', map { length($_), substr($_,0,1) } $num =~ m/1+|2+|3+|4+|5+|6+|7+|8+|9+|0+/g;
+
+## ...and spoken
+say "now as she is spoke: ", speak_english( $num );
+
+
+## ## ## ## ## SUBS:
+
+sub speak_number {
+ my ($current, @digits) = split //, shift;
+ my $count = 1;
+ my $output = '';
+
+ for (@digits) {
+ ($count++, next) if ($_ == $current);
+ $output .= $count . $current;
+ ($current, $count) = ($_, 1);
+ }
+
+ return $output . $count . $current;
+
+}
+
+sub speak_english {
+ use Lingua::EN::Inflexion;
+ my ($current, @digits) = split //, shift;
+ my $count = 1;
+ my @output;
+ my $mult = 0;
+ my %cardinal = ( 1 => 'ones',
+ 2 => 'twos',
+ 3 => 'threes',
+ 4 => 'fours',
+ 5 => 'fives',
+ 6 => 'sixes',
+ 7 => 'sevens',
+ 8 => 'eights',
+ 9 => 'nines',
+ 0 => 'zeros' );
+
+ for (@digits) {
+ ($count++, next) if ($_ == $current);
+ my $exp = inflect("<#nfw300:$count> <N:$cardinal{$current}>");
+ push @output, $exp;
+ ($current, $count) = ($_, 1);
+ $mult = 1;
+ }
+
+ my $str = (join ', ', @output) . ($mult? " and " : "");
+ return q(") . "\u$str" . inflect("<#nw300:$count> <N:$cardinal{$current}>") . q(.");
+
+}
+
+
+
+# say "\n-----------------------------------\n";
+#
+# use Test::More;
+#
+# is speak_number(1122234), 21321314, 'Ex 1';
+# is speak_number(2333445), 12332415, 'Ex 2';
+# is speak_number(12345), 1112131415, 'Ex 3';
+# is speak_number(99968882222), 39163842, 'Ex I just made up ';
+#
+#
+# done_testing();
diff --git a/challenge-091/colin-crain/perl/ch-2.pl b/challenge-091/colin-crain/perl/ch-2.pl new file mode 100644 index 0000000000..85bd99a2aa --- /dev/null +++ b/challenge-091/colin-crain/perl/ch-2.pl @@ -0,0 +1,130 @@ +#! /opt/local/bin/perl
+#
+# jump_street.pl
+#
+# TASK #2 › Jump Game
+# Submitted by: Mohammad S Anwar
+# You are given an array of positive numbers @N, where
+# value at each index determines how far you are allowed
+# to jump further.
+
+# Write a script to decide if you can jump to the last
+# index. Print 1 if you are able to reach the last index
+# otherwise 0.
+#
+# Example 1:
+# Input: @N = (1, 2, 1, 2)
+# Output: 1
+#
+# as we jump one place from index 0 and then twoe places
+# from index 1 to reach the last index.
+#
+# Example 2:
+# Input: @N = (2,1,1,0,2)
+# Output: 0
+#
+# it is impossible to reach the last index. as we jump two
+# places from index 0 to reach index 2, followed by one
+# place jump from index 2 to reach the index 3. once you
+# reached the index 3, you can't go any further because
+# you can only jump 0 position further.
+#
+# method:
+#
+# a silly little game, but we'll need to establish some
+# groundrules. First, do we need to land exactly on the
+# last element or can we overshoot? Should we wait and see
+# if Mohammad goes back and clarifies this before solving?
+# I think from the language it implies we should be
+# jumping _to_ the last index _exactly_. So if we
+# overshoot, then there's no way to get back and we fail.
+#
+# If that's the case then there are two ways to fail: to
+# either land on a zero and be unable to continue, or to
+# overshoot the mark.
+#
+# I would also point out that 0 is not positive, but is
+# used in an example. Thus we will rephrase the input as
+# an "array of non-negative numbers" which will include
+# the number 0.
+#
+# So we keep tabs on an index value, advancing it by the
+# value of the array at that index. If that value is
+# either 0 or undef, then we fail.
+#
+# If the index is the end of the array we win and stop
+# there.
+#
+# Continue until we win or lose.
+#
+# I like the idea of allowing negative values to move
+# backwards. In this case the failure modes would be the
+# same, with the addition of a third: if we ever visited
+# an element twice it would indicate we have entered a
+# loop, and will repeat without reaching the end. A little
+# more complicated in the possible pathways but still will
+# always resolve sooner or later.
+#
+# 2020 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+
+
+use warnings;
+use strict;
+use feature ":5.26";
+
+
+## ## ## ## ## SUBS:
+
+sub jump_forward {
+## minimal version as described.
+## Array -> 1|0
+## returns 1 on success, 0 on failure
+ my $idx = 0;
+ while ( my $jump = $_[$idx] ) {
+ $idx += $jump;
+ return 1 if $idx == @_ - 1;
+ }
+ return 0;
+}
+
+sub jump_around {
+## a more robust game allowing negative values.
+## fails on
+## exceeding array bounds,
+## landing on 0 (cannot jump further)
+## landing on position twice (signifying a closed loop)
+## wins
+## by landing on last element
+## returns on determination
+ my @array = @_;
+ my $idx = 0;
+ my $last = scalar @array - 1;
+ my %visited;
+ while (1) {
+ my $next = $idx + $array[$idx];
+ return 1 if $next == $last; ## win
+ return 0 if $next == $idx; ## stuck
+ return 0 if $next < 0 or $next > $last; ## out of bounds
+ return 0 if exists $visited{$next}; ## looping
+ $idx = $next;
+ $visited{$idx} = 1;
+ }
+}
+
+use Test::More;
+
+is jump_forward(1, 2, 1, 2), 1, 'forward ex 1: success!';
+is jump_forward(2,1,1,0,2), 0, 'forward ex 2: stuck on 0';
+is jump_forward(2,1,1,1,0), 1, 'forward: ok, last ele 0';
+
+is jump_around(1, 2, 1, 2), 1, 'around ex 1: success!';
+is jump_around(2,1,1,0,2), 0, 'around ex 2: stuck on 0';
+is jump_around(2,1,1,1,0), 1, 'around: ok, last ele 0';
+is jump_around(2,3,1,-2,5), 1, 'around: ok: back and forth and home';
+is jump_around(2,3,1,-12,5), 0, 'around: fail: back too far';
+is jump_around(2,13,1,-2,5), 0, 'around: fail: forward too far';
+
+
+done_testing();
diff --git a/challenge-091/colin-crain/raku/ch-1.raku b/challenge-091/colin-crain/raku/ch-1.raku new file mode 100644 index 0000000000..16350d2b8c --- /dev/null +++ b/challenge-091/colin-crain/raku/ch-1.raku @@ -0,0 +1,24 @@ +#!/usr/bin/env perl6
+#
+#
+# .raku
+#
+#
+#
+# 2020 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+
+
+unit sub MAIN (Int $num = 2244444431111111) ;
+
+say $num;
+
+my $regex = /1+|2+|3+|4+|5+|6+|7+|8+|9+|0+/;
+
+($num ~~ m:g/$regex/)
+ .map({|($_.chars, substr($_,0,1))}) ## need slip to slip in
+ .join
+ .say;
+
+
diff --git a/challenge-091/colin-crain/raku/ch-2.raku b/challenge-091/colin-crain/raku/ch-2.raku new file mode 100644 index 0000000000..d212d22eee --- /dev/null +++ b/challenge-091/colin-crain/raku/ch-2.raku @@ -0,0 +1,30 @@ +#!/usr/bin/env perl6
+#
+#
+# .raku
+#
+#
+#
+# 2020 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+
+unit sub MAIN (*@array) ;
+
+@array.elems == 0 and @array = 1,2,15,2;
+@array.elems == 1 and do { say 1; exit };
+
+my $idx = 0;
+while my $jump = @array[$idx] {
+ $idx += $jump;
+ $idx == @array.end and do { say 1; exit };
+}
+say 0;
+
+
+
+
+
+
+
+
|
