From 1711da4f548d3134248cd5e02a13d71762d5e4cb Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Tue, 12 Apr 2022 17:54:42 +0100 Subject: Add Perl solution to challenge 052 --- challenge-052/paulo-custodio/Makefile | 2 ++ challenge-052/paulo-custodio/README | 1 + challenge-052/paulo-custodio/perl/ch-1.pl | 18 ++++++++++ challenge-052/paulo-custodio/perl/ch-2.pl | 53 ++++++++++++++++++++++++++++++ challenge-052/paulo-custodio/t/test-1.yaml | 7 ++++ 5 files changed, 81 insertions(+) create mode 100644 challenge-052/paulo-custodio/Makefile create mode 100644 challenge-052/paulo-custodio/README create mode 100644 challenge-052/paulo-custodio/perl/ch-1.pl create mode 100644 challenge-052/paulo-custodio/perl/ch-2.pl create mode 100644 challenge-052/paulo-custodio/t/test-1.yaml diff --git a/challenge-052/paulo-custodio/Makefile b/challenge-052/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-052/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-052/paulo-custodio/README b/challenge-052/paulo-custodio/README new file mode 100644 index 0000000000..87dc0b2fbd --- /dev/null +++ b/challenge-052/paulo-custodio/README @@ -0,0 +1 @@ +Solution by Paulo Custodio diff --git a/challenge-052/paulo-custodio/perl/ch-1.pl b/challenge-052/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..dd441a30b2 --- /dev/null +++ b/challenge-052/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,18 @@ +#!/usr/bin/env perl + +# Challenge 052 +# +# TASK #1 +# Stepping Numbers +# Write a script to accept two numbers between 100 and 999. It should then print +# all Stepping Numbers between them. +# +# A number is called a stepping number if the adjacent digits have a difference +# of 1. For example, 456 is a stepping number but 129 is not. + +use Modern::Perl; + +my($start, $end) = @ARGV; +for my $n (123, 234, 345, 456, 567, 678, 789) { + say $n if $n >= $start && $n <= $end; +} diff --git a/challenge-052/paulo-custodio/perl/ch-2.pl b/challenge-052/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..b3a3127ce9 --- /dev/null +++ b/challenge-052/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,53 @@ +#!/usr/bin/env perl + +# Challenge 052 +# +# TASK #2 +# Lucky Winner +# Suppose there are following coins arranged on a table in a line in random +# order. +# +# £1, 50p, 1p, 10p, 5p, 20p, £2, 2p +# +# Suppose you are playing against the computer. Player can only pick one coin +# at a time from either ends. Find out the lucky winner, who has the larger +# amounts in total? + +use Modern::Perl; +use List::Util qw( shuffle ); +local $|=1; + +my @coins = shuffle(100, 50, 1, 10, 5, 20, 200, 2); + +my $human = 0; +my $computer = 0; + +while (@coins) { + # human + print "Coins: @coins. draw (b/e)? "; + my $draw = ""; + while ($draw ne 'b' && $draw ne 'e') { + chomp($draw = <>); + } + if ($draw eq 'b') { + $human += shift(@coins); + } + else { + $human += pop(@coins); + } + + # computer + if ($coins[0] >= $coins[-1]) { + $computer += shift(@coins); + } + else { + $computer += pop(@coins); + } +} + +if ($human > $computer) { + say "You WIN ($human/$computer)"; +} +else { + say "You LOOSE ($human/$computer)"; +} diff --git a/challenge-052/paulo-custodio/t/test-1.yaml b/challenge-052/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..45b116b82c --- /dev/null +++ b/challenge-052/paulo-custodio/t/test-1.yaml @@ -0,0 +1,7 @@ +- setup: + cleanup: + args: 250 550 + input: + output: | + |345 + |456 -- cgit