diff options
| author | Roger Bell_West <roger@firedrake.org> | 2021-01-12 14:28:49 +0000 |
|---|---|---|
| committer | Roger Bell_West <roger@firedrake.org> | 2021-01-12 14:28:49 +0000 |
| commit | 56b54d2ab2b254a3dcd11bb0c5e0da2df987e783 (patch) | |
| tree | a5f4051d198c6a092bf82e1777d23a06dcfa7e6e | |
| parent | b00c8c4f9ff06c18683a5e5380b03585f3301e43 (diff) | |
| download | perlweeklychallenge-club-56b54d2ab2b254a3dcd11bb0c5e0da2df987e783.tar.gz perlweeklychallenge-club-56b54d2ab2b254a3dcd11bb0c5e0da2df987e783.tar.bz2 perlweeklychallenge-club-56b54d2ab2b254a3dcd11bb0c5e0da2df987e783.zip | |
Solutions for challenge #095
| -rwxr-xr-x | challenge-095/roger-bell-west/perl/ch-1.pl | 15 | ||||
| -rwxr-xr-x | challenge-095/roger-bell-west/perl/ch-2.pl | 54 | ||||
| -rwxr-xr-x | challenge-095/roger-bell-west/python/ch-1.py | 23 | ||||
| -rwxr-xr-x | challenge-095/roger-bell-west/python/ch-2.py | 53 | ||||
| -rwxr-xr-x | challenge-095/roger-bell-west/raku/ch-1.p6 | 17 | ||||
| -rwxr-xr-x | challenge-095/roger-bell-west/raku/ch-2.p6 | 44 | ||||
| -rwxr-xr-x | challenge-095/roger-bell-west/ruby/ch-1.rb | 28 | ||||
| -rwxr-xr-x | challenge-095/roger-bell-west/ruby/ch-2.rb | 67 | ||||
| -rwxr-xr-x | challenge-095/roger-bell-west/rust/ch-1.rs | 27 | ||||
| -rwxr-xr-x | challenge-095/roger-bell-west/rust/ch-2.rs | 72 |
10 files changed, 400 insertions, 0 deletions
diff --git a/challenge-095/roger-bell-west/perl/ch-1.pl b/challenge-095/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..3a0d5bc905 --- /dev/null +++ b/challenge-095/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,15 @@ +#! /usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 3; + +is(pn(1221),1,'example 1'); +is(pn(-101),0,'example 2'); +is(pn(90),0,'example 3'); + +sub pn { + my $n=shift; + return (join('',reverse split '',$n) eq $n)?1:0; +} diff --git a/challenge-095/roger-bell-west/perl/ch-2.pl b/challenge-095/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..e22fd1229c --- /dev/null +++ b/challenge-095/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,54 @@ +#! /usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 3; + +my $stack=Local::Stack->new; +$stack->push(2); +$stack->push(-1); +$stack->push(0); +is($stack->pop,0,'test 1'); # removes 0 +is($stack->top,-1,'test 2'); # prints -1 +$stack->push(0); +is($stack->min,-1,'test 3'); # prints -1 + +package Local::Stack; +use List::Util; + +sub new { + my $class=shift; + my $self=[]; + bless $self,$class; +} + +sub push { + my $self=shift; + my $op=shift; + push @{$self},$op; +} + +sub pop { + my $self=shift; + if (scalar @{$self} == 0) { + die "insufficient elements for pop\n"; + } + return pop @{$self}; +} + +sub top { + my $self=shift; + if (scalar @{$self} == 0) { + die "insufficient elements for top\n"; + } + return $self->[-1]; +} + +sub min { + my $self=shift; + if (scalar @{$self} == 0) { + die "insufficient elements for min\n"; + } + return List::Util::min(@{$self}); +} diff --git a/challenge-095/roger-bell-west/python/ch-1.py b/challenge-095/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..e188b3f325 --- /dev/null +++ b/challenge-095/roger-bell-west/python/ch-1.py @@ -0,0 +1,23 @@ +#! /usr/bin/python3 +import unittest + +def pn(n): + ns='{0}'.format(n) + nr=ns[::-1] + if (nr == ns): + return 1 + else: + return 0 + +class TestPn(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(pn(1221),1,'example 1'); + + def test_ex2(self): + self.assertEqual(pn(-101),0,'example 2'); + + def test_ex3(self): + self.assertEqual(pn(90),0,'example 3'); + +unittest.main() diff --git a/challenge-095/roger-bell-west/python/ch-2.py b/challenge-095/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..8b3b63b97f --- /dev/null +++ b/challenge-095/roger-bell-west/python/ch-2.py @@ -0,0 +1,53 @@ +#! /usr/bin/python3 +import unittest + +class Stack: + + def __init__(self): + self.stack=list() + + def push(self,op): + self.stack.append(op) + + def pop(self): + if len(self.stack)==0: + raise Exception("Insufficient elements for pop") + return self.stack.pop() + + def top(self): + if len(self.stack)==0: + raise Exception("Insufficient elements for top") + return self.stack[len(self.stack)-1] + + def min(self): + if len(self.stack)==0: + raise Exception("Insufficient elements for min") + return min(self.stack) + +class TestStack(unittest.TestCase): + + def test_ex1(self): + stack=Stack() + stack.push(2) + stack.push(-1) + stack.push(0) + self.assertEqual(stack.pop(),0,'example 1') + + def test_ex2(self): + stack=Stack() + stack.push(2) + stack.push(-1) + stack.push(0) + stack.pop() + self.assertEqual(stack.top(),-1,'example 2') + + def test_ex3(self): + stack=Stack() + stack.push(2) + stack.push(-1) + stack.push(0) + stack.pop() + stack.push(0) + self.assertEqual(stack.min(),-1,'example 3') + +unittest.main() diff --git a/challenge-095/roger-bell-west/raku/ch-1.p6 b/challenge-095/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..53d48d4618 --- /dev/null +++ b/challenge-095/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,17 @@ +#! /usr/bin/perl6 + +use Test; + +plan 3; + +is(pn(1221),1,'example 1'); +is(pn(-101),0,'example 2'); +is(pn(90),0,'example 3'); + +sub pn($n) { + if ($n.comb.reverse.join('') eq $n) { + return 1; + } else { + return 0; + } +} diff --git a/challenge-095/roger-bell-west/raku/ch-2.p6 b/challenge-095/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..800184cb46 --- /dev/null +++ b/challenge-095/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,44 @@ +#! /usr/bin/perl6 + +class Local::Stack { + has @!stack; + + method push($op) { + @!stack.push($op); + } + + method pop { + if (@!stack.elems == 0) { + die "insufficient elements for pop\n"; + } + return @!stack.pop; + } + + method top { + if (@!stack.elems == 0) { + die "insufficient elements for top\n"; + } + return @!stack[@!stack.end]; + } + + method min { + if (@!stack.elems == 0) { + die "insufficient elements for min\n"; + } + return min(@!stack); + } + +} + +use Test; + +plan 3; + +my $stack=Local::Stack.new(); +$stack.push(2); +$stack.push(-1); +$stack.push(0); +is($stack.pop,0,'test 1'); # removes 0 +is($stack.top,-1,'test 2'); # prints -1 +$stack.push(0); +is($stack.min,-1,'test 3'); # prints -1 diff --git a/challenge-095/roger-bell-west/ruby/ch-1.rb b/challenge-095/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..f0491781a3 --- /dev/null +++ b/challenge-095/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,28 @@ +#! /usr/bin/ruby + +def pn(n) + ns=n.to_s + if ns==ns.reverse() then + return 1 + else + return 0 + end +end + +require 'test/unit' + +class TestPn < Test::Unit::TestCase + + def test_ex1 + assert_equal(1,pn(1221)) + end + + def test_ex2 + assert_equal(0,pn(-101)) + end + + def test_ex3 + assert_equal(0,pn(90)) + end + +end diff --git a/challenge-095/roger-bell-west/ruby/ch-2.rb b/challenge-095/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..623960752b --- /dev/null +++ b/challenge-095/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,67 @@ +#! /usr/bin/ruby + +class Stack + + def initialize + @stack=Array.new + end + + def push(op) + @stack.push(op) + end + + def pop + if @stack.length()==0 then + raise("insufficient elements for pop") + end + return @stack.pop + end + + def top + if @stack.length()==0 then + raise("insufficient elements for top") + end + return @stack[-1] + end + + def min + if @stack.length()==0 then + raise("insufficient elements for min") + end + return @stack.min + end + +end + +require 'test/unit' + +class TestStack < Test::Unit::TestCase + + def test_ex1 + stack=Stack.new + stack.push(2) + stack.push(-1) + stack.push(0) + assert_equal(0,stack.pop,'example 1') + end + + def test_ex2 + stack=Stack.new + stack.push(2) + stack.push(-1) + stack.push(0) + stack.pop + assert_equal(-1,stack.top,'example 2') + end + + def test_ex3 + stack=Stack.new + stack.push(2) + stack.push(-1) + stack.push(0) + stack.pop + stack.push(0) + assert_equal(-1,stack.min,'example 3') + end + +end diff --git a/challenge-095/roger-bell-west/rust/ch-1.rs b/challenge-095/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..cee35a4e99 --- /dev/null +++ b/challenge-095/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,27 @@ +#! /bin/sh +//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x; rm -f ${0}x ; exit + +#[test] +fn test_ex1() { + assert_eq!(pn(1221),1) +} + +#[test] +fn test_ex2() { + assert_eq!(pn(-101),0) +} + +#[test] +fn test_ex3() { + assert_eq!(pn(90),0) +} + +fn pn(n: i64) -> u8 { + let ns=n.to_string(); + let nr: String=ns.chars().rev().collect(); + if nr == ns { + return 1; + } else { + return 0; + } +} diff --git a/challenge-095/roger-bell-west/rust/ch-2.rs b/challenge-095/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..ce855f7cba --- /dev/null +++ b/challenge-095/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,72 @@ +#! /bin/sh +//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x; rm -f ${0}x ; exit + +#[test] +fn test_ex1() { + let mut stack=Stack::new(); + stack.push(2); + stack.push(-1); + stack.push(0); + assert_eq!(stack.pop(),0); +} + +#[test] +fn test_ex2() { + let mut stack=Stack::new(); + stack.push(2); + stack.push(-1); + stack.push(0); + let _d=stack.pop(); + assert_eq!(stack.top(),-1); +} + +#[test] +fn test_ex3() { + let mut stack=Stack::new(); + stack.push(2); + stack.push(-1); + stack.push(0); + let _d=stack.pop(); + stack.push(0); + assert_eq!(stack.min(),-1); +} + +pub struct Stack { + stack: Vec<i64> +} + +impl Stack { + + pub fn new() -> Stack { + Stack { stack: Vec::new() } + } + + pub fn push(&mut self,op: i64) { + self.stack.push(op); + } + + pub fn pop(&mut self) -> i64 { + if self.stack.len() == 0 { + println!("Insufficient elements for pop"); + std::process::exit(1); + } + return self.stack.pop().unwrap(); + } + + pub fn top(&mut self) -> i64 { + if self.stack.len() == 0 { + println!("Insufficient elements for top"); + std::process::exit(1); + } + return self.stack[self.stack.len()-1]; + } + + pub fn min(&mut self) -> i64 { + if self.stack.len() == 0 { + println!("Insufficient elements for top"); + std::process::exit(1); + } + return *self.stack.iter().min().unwrap(); + } + +} |
