aboutsummaryrefslogtreecommitdiff
path: root/challenge-095
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-01-12 00:38:29 +0000
committerGitHub <noreply@github.com>2021-01-12 00:38:29 +0000
commit1ecd5023b2cb20b8d54f498a6cee7e92e0782736 (patch)
tree0d975103fe829877a7ddc14b52ae8ff156212c73 /challenge-095
parentf2b58aa7a91f035c023644e70ae79c38952aae0d (diff)
parentfdc17edcca734b67b6d36f852f54b12bef697380 (diff)
downloadperlweeklychallenge-club-1ecd5023b2cb20b8d54f498a6cee7e92e0782736.tar.gz
perlweeklychallenge-club-1ecd5023b2cb20b8d54f498a6cee7e92e0782736.tar.bz2
perlweeklychallenge-club-1ecd5023b2cb20b8d54f498a6cee7e92e0782736.zip
Merge pull request #3220 from poizon/branch-for-challenge-095
Perl solution by Pavel Kuptsov
Diffstat (limited to 'challenge-095')
-rw-r--r--challenge-095/pavel_kuptsov/perl/ch-1.pl48
-rw-r--r--challenge-095/pavel_kuptsov/perl/ch-2.pl93
2 files changed, 141 insertions, 0 deletions
diff --git a/challenge-095/pavel_kuptsov/perl/ch-1.pl b/challenge-095/pavel_kuptsov/perl/ch-1.pl
new file mode 100644
index 0000000000..2929681dc3
--- /dev/null
+++ b/challenge-095/pavel_kuptsov/perl/ch-1.pl
@@ -0,0 +1,48 @@
+#!/usr/bin/perl -w
+
+use strict;
+use warnings;
+use Test::More;
+=head2
+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
+
+=cut
+
+=head2 is_palindrome_number($N)
+
+
+
+=cut
+sub is_palindrome_number
+{
+ my $n = shift;
+ return 0 if $n < 0;
+ return 1 if ($n == reverse $n);
+ return 0;
+}
+
+for my $p( qw(0 1221 3443 5665) )
+{
+ ok(is_palindrome_number($p) == 1 );
+}
+
+for my $p( qw(-101 1234 5678 90) )
+{
+ ok(is_palindrome_number($p) == 0 );
+}
+
+done_testing(8); \ No newline at end of file
diff --git a/challenge-095/pavel_kuptsov/perl/ch-2.pl b/challenge-095/pavel_kuptsov/perl/ch-2.pl
new file mode 100644
index 0000000000..cfd9e7067b
--- /dev/null
+++ b/challenge-095/pavel_kuptsov/perl/ch-2.pl
@@ -0,0 +1,93 @@
+#!/usr/bin/perl -w
+
+use strict;
+use warnings;
+use feature qw(say);
+use List::Util qw();
+use Test::More;
+
+=head2
+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
+
+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
+
+=cut
+
+
+my @stack = ();
+
+=head2 push($N)
+
+push($n) - add $n to the stack
+
+=cut
+sub push
+{
+ CORE::push @stack, $_[0];
+}
+
+
+=head2 pop($N)
+
+pop() - remove the top element
+
+=cut
+sub pop
+{
+ return CORE::pop @stack;
+}
+
+
+=head2 top($N)
+
+top() - get the top element
+
+=cut
+sub top
+{
+ return $stack[-1];
+}
+
+
+=head2 min($N)
+
+min() - return the minimum element
+
+=cut
+sub min
+{
+ return List::Util::min @stack;
+}
+
+main::push 10;
+main::push -1;
+main::push 0;
+
+ok( scalar @stack == 3 );
+
+main::pop;
+
+ok( scalar @stack == 2 );
+ok( $stack[-1] == -1 );
+ok( main::top == -1 );
+
+main::push 0;
+
+ok( main::min == -1 );
+
+
+done_testing(5); \ No newline at end of file