aboutsummaryrefslogtreecommitdiff
path: root/challenge-095
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-01-17 21:56:27 +0000
committerGitHub <noreply@github.com>2021-01-17 21:56:27 +0000
commit23b5e26159090db7c5da6ff22b3e4dfe0d98beb7 (patch)
tree106198dcbb802fcb15381daf6de3c2d77466ddd2 /challenge-095
parent2922f62360e256a1fda0281681b892fe60afae07 (diff)
parentd8f4688b2ab6c75ef353468378324ec35e297a23 (diff)
downloadperlweeklychallenge-club-23b5e26159090db7c5da6ff22b3e4dfe0d98beb7.tar.gz
perlweeklychallenge-club-23b5e26159090db7c5da6ff22b3e4dfe0d98beb7.tar.bz2
perlweeklychallenge-club-23b5e26159090db7c5da6ff22b3e4dfe0d98beb7.zip
Merge pull request #3299 from dcw803/master
imported this week's solutions - two easy questions this week
Diffstat (limited to 'challenge-095')
-rw-r--r--challenge-095/duncan-c-white/README65
-rwxr-xr-xchallenge-095/duncan-c-white/perl/ch-1.pl48
-rwxr-xr-xchallenge-095/duncan-c-white/perl/ch-2.pl150
3 files changed, 228 insertions, 35 deletions
diff --git a/challenge-095/duncan-c-white/README b/challenge-095/duncan-c-white/README
index cc13605728..b4f84102f4 100644
--- a/challenge-095/duncan-c-white/README
+++ b/challenge-095/duncan-c-white/README
@@ -1,54 +1,49 @@
-Task 1: "Group Anagrams
+Task 1: "Palindrome Number
-You are given an array of strings @S.
-
-Write a script to group sets of Anagrams (containing the same letters), the order of the anagram-sets doesn't matter.
-
-An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
+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: ("opt", "bat", "saw", "tab", "pot", "top", "was")
- Output: [ ("bat", "tab"),
- ("saw", "was"),
- ("top", "pot", "opt") ]
+ Input: 1221
+ Output: 1
Example 2:
- Input: ("x")
- Output: [ ("x") ]
-"
+ Input: -101
+ Output: 0, since -101 and 101- are not the same.
-My notes: yippee! Good old "Programming Pearls" by Jon Bentley tackled a very similar problem, using SIGNATURES (the sorted bag of letters
-contained in a word) - all anagrams with the same signature form part of the same anagram group. Ideal data structure time:
+Example 3:
-"if only we could have a data structure mapping from a signature to a list of all the words with that signature".
+ Input: 90
+ Output: 0
+"
-Ok, let's build that then: a HoA.
+My notes: sounds trivial. N == join(reverse(split(N)))
-Task 2: "Binary Tree to Linked List
+Task 2: "Demo Stack
-You are given a binary tree.
+Write a script to demonstrate Stack operations like below:
-Write a script to represent the given binary tree as an object and flatten it to a linked list object. Finally print the linked list object.
+push($n) - add $n to the stack
+pop() - remove the top element
+top() - get the top element
+min() - return the minimum element
Example:
- Input:
-
- 1
- / \
- 2 3
- / \
- 4 5
- / \
- 6 7
-
- Output:
-
- 1 -> 2 -> 4 -> 5 -> 6 -> 7 -> 3
+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
"
-My notes: again with the binary trees - no notes on representation, so I'll reuse the one I've used before.
-I think the "flatten to a linked list object" means "perform a pre-order traversal producing a list".
+My notes: ok so by "script" we mean "Perl class". Very easy to do,
+but I wonder what ch-2.pl does - contains the above example I guess.
+On second thought, let's inline the Stack module into ch-2.pl..
diff --git a/challenge-095/duncan-c-white/perl/ch-1.pl b/challenge-095/duncan-c-white/perl/ch-1.pl
new file mode 100755
index 0000000000..5ea3098acf
--- /dev/null
+++ b/challenge-095/duncan-c-white/perl/ch-1.pl
@@ -0,0 +1,48 @@
+#!/usr/bin/perl
+#
+# Task 1: "Palindrome Number
+#
+# 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
+# "
+#
+# My notes: sounds trivial. N == join(reverse(split(N)))
+#
+
+use strict;
+use warnings;
+use feature 'say';
+use Data::Dumper;
+use Function::Parameters;
+
+die "Usage: palindrome N\n" unless @ARGV==1;
+my $n = shift;
+
+#
+# my $ispal = palindrome($n);
+# Return 1 if $n is a palindrome, 0 otherwise.
+#
+fun palindrome( $n )
+{
+ return $n eq join('', reverse( split( //, $n ) ) ) ? 1 : 0;
+}
+
+
+my $ispal = palindrome($n);
+say $ispal;
diff --git a/challenge-095/duncan-c-white/perl/ch-2.pl b/challenge-095/duncan-c-white/perl/ch-2.pl
new file mode 100755
index 0000000000..b124e84b27
--- /dev/null
+++ b/challenge-095/duncan-c-white/perl/ch-2.pl
@@ -0,0 +1,150 @@
+#!/usr/bin/perl
+#
+# Task 2: "Demo Stack
+#
+# 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
+# "
+#
+# My notes: ok so by "script" we mean "Perl class". Very easy to do,
+# but I wonder what ch-2.pl does - contains the above example I guess.
+# On second thought, let's inline the Stack module into here..
+#
+use strict;
+use warnings;
+use feature 'say';
+use Getopt::Long;
+use Function::Parameters;
+use Data::Dumper;
+use List::Util;
+
+my $debug=0;
+die "Usage: ch-2 [--debug]\n" unless GetOptions( "debug" => \$debug ) && @ARGV==0;
+
+
+package Stack;
+
+use overload '""' => \&as_string;
+
+#
+# my $Stack = Stack->new( @n );
+# create a Stack node with the elements in @n on it
+#
+method new($class: @n )
+{
+ my $stack = bless [
+ 'Stack', @n
+ ], $class;
+ say "debug: new stack: $stack" if $debug;
+ return $stack;
+}
+
+
+#
+# $stack->push($n);
+# Push $n onto stack $s.
+#
+method push( $n )
+{
+ CORE::push @$self, $n;
+ say "debug: pushed $n onto stack: $self" if $debug;
+}
+
+
+#
+# my $isempty = $stack->isempty();
+# Return 1 iff $stack is empty, otherwise 0.
+#
+method isempty()
+{
+ my $isempty = @$self==1 ? 1 : 0;
+ say "debug: isempty($self) = $isempty" if $debug;
+ return $isempty;
+}
+
+
+#
+# my $x = $stack->top();
+# Return the top value of $stack (without changing it).
+# Abort if $stack is empty.
+#
+method top( )
+{
+ die "Stack->top: stack is already empty\n" if @$self==1;
+ my $top = $self->[1];
+ say "debug: top($self) = $top" if $debug;
+ return $top;
+}
+
+
+#
+# my $x = $stack->pop();
+# Pop the top value of $stack off - returning it.
+# Abort if $stack is empty.
+#
+method pop( )
+{
+ die "Stack->pop: stack is already empty\n" if @$self==1;
+ my $before = "$self";
+ my $top = splice( @$self, 1, 1 );
+ say "debug: pop($before) = $top, stack now $self" if $debug;
+ return $top;
+}
+
+
+#
+# my $x = $stack->min();
+# Find the minimum value of $stack off - not changing it.
+# Abort if $stack is empty.
+#
+method min( )
+{
+ die "Stack->min: stack is already empty\n" if @$self==1;
+ my @x = @$self;
+ shift @x;
+ my $min = List::Util::min(@x);
+ say "debug: min($self) = $min" if $debug;
+ return $min;
+}
+
+
+#
+# my $str = $Stack->as_string();
+# return the given $Stack as a nice printable string.
+#
+sub as_string($)
+{
+ my( $self ) = @_;
+
+ die "Stack->as_string: bad Stack ", Dumper($self) unless ref($self) eq "Stack";
+ my @x = @$self;
+ my $kind = shift @x;
+ return "[". join(',', map { "$_" } @x ). "]";
+}
+
+
+package main;
+
+my $stack = Stack->new();
+$stack->push(2);
+$stack->push(-1);
+$stack->push(0);
+$stack->pop; # removes 0
+say "top = ", $stack->top; # prints -1
+$stack->push(0);
+say "min = ", $stack->min; # prints -1