diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-01-17 11:08:20 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-01-17 11:08:20 +0000 |
| commit | 21815bbd8bbf24cce8c5104f68a4a09bbb2e917f (patch) | |
| tree | c4ddc098119b4b9001ff53a8bf02f48bcd9dfec5 | |
| parent | ca8e82c03043dedb7da95a98fd1b93eaa03f57eb (diff) | |
| parent | e72873350cddfabd95d9dee7f6e39677c30c46f5 (diff) | |
| download | perlweeklychallenge-club-21815bbd8bbf24cce8c5104f68a4a09bbb2e917f.tar.gz perlweeklychallenge-club-21815bbd8bbf24cce8c5104f68a4a09bbb2e917f.tar.bz2 perlweeklychallenge-club-21815bbd8bbf24cce8c5104f68a4a09bbb2e917f.zip | |
Merge pull request #3295 from wanderdoc/master
Solutions to challenge-095.
| -rw-r--r-- | challenge-095/wanderdoc/perl/ch-1.pl | 34 | ||||
| -rw-r--r-- | challenge-095/wanderdoc/perl/ch-2.pl | 95 |
2 files changed, 129 insertions, 0 deletions
diff --git a/challenge-095/wanderdoc/perl/ch-1.pl b/challenge-095/wanderdoc/perl/ch-1.pl new file mode 100644 index 0000000000..5811e231a5 --- /dev/null +++ b/challenge-095/wanderdoc/perl/ch-1.pl @@ -0,0 +1,34 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +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 + + + + + + +use Regexp::Common; +use Test::More; + +sub is_palindrom_number +{ + my $num = $_[0]; + die "Not a number!$/" unless $num =~ $RE{num}{real}; + + my $rev = reverse $num; + + return ($num eq $rev ) ? 1 : 0; +} + +is(is_palindrom_number(1221), 1, 'Example 1'); +is(is_palindrom_number(-101), 0, 'Example 2'); +is(is_palindrom_number(90), 0, 'Example 3'); + +done_testing();
\ No newline at end of file diff --git a/challenge-095/wanderdoc/perl/ch-2.pl b/challenge-095/wanderdoc/perl/ch-2.pl new file mode 100644 index 0000000000..37221217ba --- /dev/null +++ b/challenge-095/wanderdoc/perl/ch-2.pl @@ -0,0 +1,95 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +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 + +=cut + + + +package MyStack +{ + use strict; + use warnings FATAL => qw(all); + + sub new + { + my ($class,@data) = @_; + bless [@data], $class; + } + + + + + + sub push { push @{$_[0]}, $_[1] } + sub pop { pop @{$_[0]} } + + sub top { return ${$_[0]}[-1] } + sub min + { + my $min = ${$_[0]}[0]; + + for (@{$_[0]}) + { + $min = $_ if $_ < $min; + } + return $min; + } + sub empty { @{$_[0]} = () } + sub size { return scalar @{$_[0]} } + + sub print_me { print join("", "<<'", join("' '", @{$_[0]}), "'<<"), $/; } + 1; +} + + + +my $stack = MyStack->new; + + +$stack->push(2); +$stack->push(-1); +$stack->push(0); + +$stack->print_me; + +$stack->pop; # removes 0 +$stack->print_me; + + +print "Top: ", $stack->top, $/; # prints -1 + + +$stack->push(0); +print "Min: ", $stack->min, $/; # prints -1 +$stack->print_me; + + +print "Size: ", $stack->size, $/; +$stack->empty; +print "Size: ", $stack->size, $/; + +$stack->push(2); +$stack->push(-1); +$stack->push(0); + + +$stack->print_me;
\ No newline at end of file |
