diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2021-01-17 02:44:59 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2021-01-17 02:44:59 +0000 |
| commit | c155a69465870cea064c5aefcf57ccdaaea5a49c (patch) | |
| tree | 7c2288036fa4155a60e56f1b2cd8a5adc893a4e6 /challenge-095 | |
| parent | 42921ce97196797ad92601b49933cb56f82f172f (diff) | |
| download | perlweeklychallenge-club-c155a69465870cea064c5aefcf57ccdaaea5a49c.tar.gz perlweeklychallenge-club-c155a69465870cea064c5aefcf57ccdaaea5a49c.tar.bz2 perlweeklychallenge-club-c155a69465870cea064c5aefcf57ccdaaea5a49c.zip | |
- Added solutions by Colin Crain.
Diffstat (limited to 'challenge-095')
| -rw-r--r-- | challenge-095/colin-crain/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-095/colin-crain/perl/ch-1.pl | 49 | ||||
| -rw-r--r-- | challenge-095/colin-crain/perl/ch-2.pl | 94 | ||||
| -rw-r--r-- | challenge-095/colin-crain/raku/ch-1.raku | 19 | ||||
| -rw-r--r-- | challenge-095/colin-crain/raku/ch-2.raku | 101 |
5 files changed, 264 insertions, 0 deletions
diff --git a/challenge-095/colin-crain/blog.txt b/challenge-095/colin-crain/blog.txt new file mode 100644 index 0000000000..ecc88aa3d8 --- /dev/null +++ b/challenge-095/colin-crain/blog.txt @@ -0,0 +1 @@ +https://colincrain.com/2021/01/16/judging-jenga-symmetries/ diff --git a/challenge-095/colin-crain/perl/ch-1.pl b/challenge-095/colin-crain/perl/ch-1.pl new file mode 100644 index 0000000000..d2d7d2c54a --- /dev/null +++ b/challenge-095/colin-crain/perl/ch-1.pl @@ -0,0 +1,49 @@ +#! /opt/local/bin/perl
+#
+# palindrome.pl
+#
+# TASK #1 › Palindrome Number
+# Submitted by: Mohammad S Anwar
+#
+# You are given a number $N.
+#
+# Write a script to figure out if the given number is Palindrome. Print 1
+# if true otherwise 0.
+#
+# Example 1:
+#
+# Input: 1221
+# Output: 1
+#
+# Example 2:
+#
+# Input: -101
+# Output: 0, since -101 and 101- are not the same.
+#
+# Example 3:
+#
+# Input: 90
+# Output: 0
+#
+# © 2021 colin crain
+#
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+
+
+use warnings;
+use strict;
+use feature ":5.26";
+
+## ## ## ## ## MAIN:
+exit unless scalar @ARGV;
+my $num = shift @ARGV;
+$num += 0;
+
+say 1 and exit if $num =~ m/^(.*).?(??{reverse($1)})$/;
+say 0;
+
+
+
+
+
diff --git a/challenge-095/colin-crain/perl/ch-2.pl b/challenge-095/colin-crain/perl/ch-2.pl new file mode 100644 index 0000000000..3884e8b26d --- /dev/null +++ b/challenge-095/colin-crain/perl/ch-2.pl @@ -0,0 +1,94 @@ +#! /opt/local/bin/perl
+#
+# jenga.pl
+#
+# TASK #2 › Demo Stack
+# Submitted by: Mohammad S Anwar
+# Write a script to demonstrate Stack operations like below:
+#
+# push($n) - add $n to the stack
+# pop() - remove the top element
+# top() - get the top element
+# min() - return the minimum element
+#
+# Example:
+#
+# my $stack = Stack->new;
+# $stack->push(2);
+# $stack->push(-1);
+# $stack->push(0);
+# $stack->pop; # removes 0
+# print $stack->top; # prints -1
+# $stack->push(0);
+# print $stack->min; # prints -1
+#
+#
+# © 2021 colin crain
+#
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+
+
+use warnings;
+use strict;
+use feature ":5.26";
+
+
+package Node;
+use Moo;
+
+ has value => ( is => 'rw' );
+ has down => ( is => 'rw' );
+
+package Stack;
+use Moo;
+
+ has last => ( is => 'rw' );
+
+ sub push {
+ my ($self, $value) = @_;
+ my $node = Node->new( value => $value ,
+ down => $self->last );
+ $self->last( $node );
+ };
+
+ sub pop {
+ my $self = shift;
+ my $node = $self->last;
+ $self->last( $node->down );
+ return $node->value;
+ }
+
+ sub top {
+ my $self = shift;
+ return $self->last->value;
+ }
+
+ sub min {
+ my $self = shift;
+ my $node = $self->last;
+ my $min = $node->value;
+ while ( defined $node->down ) {
+ $min = $node->down->value if $node->down->value < $min;
+ $node = $node->down;
+ }
+ return $min;
+ }
+
+package main;
+
+my $stack = Stack->new;
+$stack->push( 2 );
+$stack->push( -1 );
+$stack->push( 0 );
+$stack->pop; # removes 0
+say $stack->top; # prints -1
+$stack->push( 0 );
+say $stack->min; # prints -1
+
+
+
+
+
+
+
diff --git a/challenge-095/colin-crain/raku/ch-1.raku b/challenge-095/colin-crain/raku/ch-1.raku new file mode 100644 index 0000000000..a8f591b0bb --- /dev/null +++ b/challenge-095/colin-crain/raku/ch-1.raku @@ -0,0 +1,19 @@ +#!/usr/bin/env perl6 +# +# +# pal-n-done.raku +# +# +# +# © 2021 colin crain +## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## + + + +unit sub MAIN (Int:D $num is copy) ; + +$num += 0; + +say +$num ~~ m:ex/^ (.*) {} .? "{$0.flip}" $/ ?? 1 + !! 0 ; diff --git a/challenge-095/colin-crain/raku/ch-2.raku b/challenge-095/colin-crain/raku/ch-2.raku new file mode 100644 index 0000000000..8adb97983e --- /dev/null +++ b/challenge-095/colin-crain/raku/ch-2.raku @@ -0,0 +1,101 @@ +#!/usr/bin/env perl6 +# +# +# jenga.raku +# +# +# +# © 2021 colin crain +# +## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## + + +unit sub MAIN () ; + +class Node { + has Any $.value is rw; + has Node $.down is rw; +} + +class Stack { + has Node $.last is rw; + + method push ( $value ) { + my $node = Node.new( value => $value, + down => $.last ); + $.last = $node; + $.show_stack("method 'push $value'"); + } + + method pop () { + return Nil if not defined $.last; + my $value = $.last.value; + $.last = $.last.down; + $.show_stack("method 'pop'"); + return $value; + } + + method top () { + $.show_stack("method 'top'"); + return $.last.value; + } + + method min () { + return Nil if not defined $.last; + my $node = $.last; + my $min = $node.value; + while defined $node.down { + $min = min( $min, $node.down.value ); + $node = $node.down; + } + $.show_stack("method 'min'"); + return $min; + } + + method max () { + return Nil if not defined $.last; + my $node = $.last; + my $max = $node.value; + while defined $node.down { + $max = max( $max, $node.down.value ); + $node = $node.down; + } + $.show_stack("method 'max'"); + return $max; + } + + + method show_stack ($note?) { + return Nil if not defined $.last; + my $node = $.last; + my @s = $node.value if defined $node; + while defined $node.down { + @s.append: $node.down.value; + $node = $node.down; + } + say ''; + say $note; + say "stack now: ", @s.fmt("%3d", ' → '); + } +} + +my $stack = Stack.new; + +$stack.push( 2 ); +$stack.push( -1 ); +$stack.push( 0 ); + +say "returns: ", $stack.pop; # removes 0 +say "returns: ", $stack.top; # returns -1 + +$stack.push( 0 ); + +say "returns: ", $stack.min; # returns -1 +say "returns: ", $stack.pop; # removes 0 +say "returns: ", $stack.pop; # removes -1 +say "returns: ", $stack.min; # returns 2 + +$stack.push( 5 ); + +say "returns: ", $stack.max; # returns 5 + |
