diff options
| author | drbaggy <js5@sanger.ac.uk> | 2021-01-11 06:59:44 +0000 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2021-01-11 06:59:44 +0000 |
| commit | 02b8a32ead85792cdaeebc5a6e30990658c3e1c4 (patch) | |
| tree | 6c1d7b521ab4f81b0e430b2060f0444984b47c13 /challenge-095/james-smith | |
| parent | 5ab993f243ab1a835d8f30aa6c97b2d3b99496af (diff) | |
| download | perlweeklychallenge-club-02b8a32ead85792cdaeebc5a6e30990658c3e1c4.tar.gz perlweeklychallenge-club-02b8a32ead85792cdaeebc5a6e30990658c3e1c4.tar.bz2 perlweeklychallenge-club-02b8a32ead85792cdaeebc5a6e30990658c3e1c4.zip | |
solns for 95
Diffstat (limited to 'challenge-095/james-smith')
| -rw-r--r-- | challenge-095/james-smith/perl/Stack.pm | 48 | ||||
| -rw-r--r-- | challenge-095/james-smith/perl/ch-1.pl | 33 | ||||
| -rw-r--r-- | challenge-095/james-smith/perl/ch-2.pl | 21 |
3 files changed, 102 insertions, 0 deletions
diff --git a/challenge-095/james-smith/perl/Stack.pm b/challenge-095/james-smith/perl/Stack.pm new file mode 100644 index 0000000000..a508b88b75 --- /dev/null +++ b/challenge-095/james-smith/perl/Stack.pm @@ -0,0 +1,48 @@ +package Stack; + +## The tree is stored in an array ref +# The first element is the value of the node +# The remainder of the array are child sub-trees +# +# Methods: +# ->add_child( $child_tree ) +# ->to_ll( $list ) -- convert tree into linked lit ( if list is +# passed then they are added to the end of this list ) +# ->flatten -- flatten list to array. +# + +sub new { + my $class = shift; + my $self = []; + bless $self, $class; +} + +sub push { + my( $self,$val ) = @_; + push @{$self}, $val; + return $self; +} + +sub pop { + my $self = shift; + return unless @{$self}; + return CORE::pop(@{$self}); +} + +sub top { + my $self = shift; + return unless @{$self}; + return $self->[-1]; +} + +sub min { + my $self = shift; + return unless @{$self}; + my $min = $self->[0]; + foreach (@{$self}) { + $min = $_ if $_ < $min; + } + return $min; +} + +1; diff --git a/challenge-095/james-smith/perl/ch-1.pl b/challenge-095/james-smith/perl/ch-1.pl new file mode 100644 index 0000000000..c59d225a6a --- /dev/null +++ b/challenge-095/james-smith/perl/ch-1.pl @@ -0,0 +1,33 @@ +#!/usr/local/bin/perl + +use strict; + +use warnings; +use feature qw(say); +use Test::More; + +is( is_palindrome_rev(1221), 1 ); +is( is_palindrome_rev(-101), 0 ); +is( is_palindrome_rev(90), 0 ); + +is( is_palindrome_array(1221), 1 ); +is( is_palindrome_array(-101), 0 ); +is( is_palindrome_array(90), 0 ); + +done_testing(); + +sub is_palindrome_rev { + return ( $_[0] eq reverse $_[0]) ? 1 : 0; +} + +sub is_palindrome_array { + my $n = shift; + return 0 if $n < 0; + my @digits = $n%10; + push @digits, $n%10 while $n = int ($n/10); + while (@digits>1) { + return 0 if shift @digits != pop @digits; + } + return 1; +} + diff --git a/challenge-095/james-smith/perl/ch-2.pl b/challenge-095/james-smith/perl/ch-2.pl new file mode 100644 index 0000000000..03b71a45d0 --- /dev/null +++ b/challenge-095/james-smith/perl/ch-2.pl @@ -0,0 +1,21 @@ +#!/usr/local/bin/perl + +use strict; + +use warnings; +use feature qw(say); +use Test::More; +use lib q(.); +use Stack; + +my $stack = Stack->new; +$stack->push(2); +$stack->push(-1); +$stack->push(0); +$stack->pop; +is( $stack->top , -1 ); +$stack->push(0); +is( $stack->min, -1 ); + +done_testing(); + |
