diff options
Diffstat (limited to 'challenge-034')
| -rw-r--r-- | challenge-034/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-034/paulo-custodio/README | 1 | ||||
| -rw-r--r-- | challenge-034/paulo-custodio/perl/ch-1.pl | 13 | ||||
| -rw-r--r-- | challenge-034/paulo-custodio/perl/ch-2.pl | 26 | ||||
| -rw-r--r-- | challenge-034/paulo-custodio/python/ch-1.py | 11 | ||||
| -rw-r--r-- | challenge-034/paulo-custodio/python/ch-2.py | 45 | ||||
| -rw-r--r-- | challenge-034/paulo-custodio/t/test-1.yaml | 5 | ||||
| -rw-r--r-- | challenge-034/paulo-custodio/t/test-2.yaml | 20 |
8 files changed, 123 insertions, 0 deletions
diff --git a/challenge-034/paulo-custodio/Makefile b/challenge-034/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-034/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-034/paulo-custodio/README b/challenge-034/paulo-custodio/README new file mode 100644 index 0000000000..87dc0b2fbd --- /dev/null +++ b/challenge-034/paulo-custodio/README @@ -0,0 +1 @@ +Solution by Paulo Custodio diff --git a/challenge-034/paulo-custodio/perl/ch-1.pl b/challenge-034/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..dce4f757f9 --- /dev/null +++ b/challenge-034/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,13 @@ +#!/usr/bin/perl + +# Challenge 034 +# +# Task #1 +# Contributed by Dave Cross +# Write a program that demonstrates using hash slices and/or array slices. + +use Modern::Perl; + +my %data = (oranges => 3, pears => 2, apples => 4); +my @values = @data{qw(oranges apples)}; +say join(", ", @values); diff --git a/challenge-034/paulo-custodio/perl/ch-2.pl b/challenge-034/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..7ce4f31fba --- /dev/null +++ b/challenge-034/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,26 @@ +#!/usr/bin/perl + +# Challenge 034 +# +# Task #2 +# Contributed by Dave Cross +# Write a program that demonstrates a dispatch table. + +use Modern::Perl; + +# simple rpn calculator +my @stack; +my %dispatch = ( + '+' => sub { my $b = pop @stack; my $a = pop @stack; push @stack, $a+$b; }, + '-' => sub { my $b = pop @stack; my $a = pop @stack; push @stack, $a-$b; }, + '*' => sub { my $b = pop @stack; my $a = pop @stack; push @stack, $a*$b; }, + '/' => sub { my $b = pop @stack; my $a = pop @stack; push @stack, $a/$b; }, + '.' => sub { say pop @stack; }, +); + +for (split //, "@ARGV") { + if (/\s/) {} + elsif (/\d/) { push @stack, $_; } + elsif (exists $dispatch{$_}) { $dispatch{$_}->(); } + else { die "invalid operation: $_"; } +} diff --git a/challenge-034/paulo-custodio/python/ch-1.py b/challenge-034/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..d7855da4e7 --- /dev/null +++ b/challenge-034/paulo-custodio/python/ch-1.py @@ -0,0 +1,11 @@ +#!/usr/bin/python3 + +# Challenge 034 +# +# Task #1 +# Contributed by Dave Cross +# Write a program that demonstrates using hash slices and/or array slices. + +data = {'oranges':3, 'pears':2, 'apples':4} +values = [data[x] for x in 'oranges apples'.split()] +print(*values, sep=", ") diff --git a/challenge-034/paulo-custodio/python/ch-2.py b/challenge-034/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..87134c16dc --- /dev/null +++ b/challenge-034/paulo-custodio/python/ch-2.py @@ -0,0 +1,45 @@ +#!/usr/bin/python3 + +# Challenge 034 +# +# Task #2 +# Contributed by Dave Cross +# Write a program that demonstrates a dispatch table. + +import sys + +# simple rpn calculator +stack = [] +def add(): + b = stack.pop() + a = stack.pop() + stack.append(a+b) + +def sub(): + b = stack.pop() + a = stack.pop() + stack.append(a-b) + +def mul(): + b = stack.pop() + a = stack.pop() + stack.append(a*b) + +def div(): + b = stack.pop() + a = stack.pop() + stack.append(a/b) + +def prt(): + print(stack.pop()) + +dispatch = {'+': add, '-':sub, '*':mul, '/':div, '.':prt} + +prog = "".join(sys.argv[1:]) +for c in prog: + if c.isspace(): + pass + elif c.isdigit(): + stack.append(int(c)) + else: + dispatch[c]() diff --git a/challenge-034/paulo-custodio/t/test-1.yaml b/challenge-034/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..764a7a9d39 --- /dev/null +++ b/challenge-034/paulo-custodio/t/test-1.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: + input: + output: 3, 4 diff --git a/challenge-034/paulo-custodio/t/test-2.yaml b/challenge-034/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..746bd8389b --- /dev/null +++ b/challenge-034/paulo-custodio/t/test-2.yaml @@ -0,0 +1,20 @@ +- setup: + cleanup: + args: 1 3 + . + input: + output: 4 +- setup: + cleanup: + args: 1 3 - . + input: + output: -2 +- setup: + cleanup: + args: '"3 2 * ."' + input: + output: 6 +- setup: + cleanup: + args: 3 2 / . + input: + output: 1.5 |
