diff options
| author | andrezgz <andrezgz@gmail.com> | 2020-03-21 14:09:09 -0300 |
|---|---|---|
| committer | andrezgz <andrezgz@gmail.com> | 2020-03-21 14:09:09 -0300 |
| commit | a6f7cf35d7ec5f09773840e41e5de110ff02b665 (patch) | |
| tree | fc7de55d06b7a566b2896d8ff14d5f34784eb4f6 | |
| parent | 310282c9866d9d001f3c60f56094d46915f80684 (diff) | |
| download | perlweeklychallenge-club-a6f7cf35d7ec5f09773840e41e5de110ff02b665.tar.gz perlweeklychallenge-club-a6f7cf35d7ec5f09773840e41e5de110ff02b665.tar.bz2 perlweeklychallenge-club-a6f7cf35d7ec5f09773840e41e5de110ff02b665.zip | |
challenge-052 andrezgz solution
| -rw-r--r-- | challenge-052/andrezgz/perl/ch-1.pl | 34 | ||||
| -rw-r--r-- | challenge-052/andrezgz/perl/ch-2.pl | 75 |
2 files changed, 109 insertions, 0 deletions
diff --git a/challenge-052/andrezgz/perl/ch-1.pl b/challenge-052/andrezgz/perl/ch-1.pl new file mode 100644 index 0000000000..60e7bcfa40 --- /dev/null +++ b/challenge-052/andrezgz/perl/ch-1.pl @@ -0,0 +1,34 @@ +#!/usr/bin/perl + +# https://perlweeklychallenge.org/blog/perl-weekly-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 strict; +use warnings; +use v5.10; + +die "Usage: $0 <number> <number>\nEnter numbers between 100 and 999" + unless @ARGV == 2; + +for (@ARGV) { + die "$_ is not allowed. Enter numbers between 100 and 999" + unless $_ =~ /^\d+$/ && $_ >= 100 && $_ <= 999; +} + +my ( $begin, $end ) = @ARGV; +($begin, $end) = ($end, $begin) if $end < $begin; + +for ($begin .. $end) { + my @d = split //; + next if abs($d[0] - $d[1]) != 1 || + abs($d[1] - $d[2]) != 1; + say $_; +} diff --git a/challenge-052/andrezgz/perl/ch-2.pl b/challenge-052/andrezgz/perl/ch-2.pl new file mode 100644 index 0000000000..993657c8a2 --- /dev/null +++ b/challenge-052/andrezgz/perl/ch-2.pl @@ -0,0 +1,75 @@ +#!/usr/bin/perl + +# https://perlweeklychallenge.org/blog/perl-weekly-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 strict; +use warnings; +use v5.10; + +my %coin_value = ( + 1 => '1p', + 2 => '2p', + 5 => '5p', + 10 => '10p', + 20 => '20p', + 50 => '50p', + 100 => '£1', + 200 => '£2', +); + +# Schwartzian transform to randomize order +# although some randomness could be obtained by a simple: keys %coin_value +my @coins = map { $_->[0] } + sort { $a->[1] <=> $b->[1] } + map { [$_, int rand 8] } + keys %coin_value; + +my (@user, @computer); + +while (@coins) { + + # USER + list_coins('Coins on the table',@coins); + say "> Please choose L (for $coin_value{$coins[0]}), R (for $coin_value{$coins[-1]}) or Q to quit"; + + my $letter = uc <STDIN>; + chomp $letter; + redo unless $letter =~ /^[RLQ]$/; + exit 0 if $letter eq 'Q'; + + my $chosen = $letter eq 'L' ? shift @coins : pop @coins; + push @user, $chosen; + + # COMPUTER + list_coins('Coins on the table',@coins); + + $chosen = $coins[0] > $coins[-1] ? shift @coins : pop @coins; + push @computer, $chosen; + say "Computer chooses: $coin_value{$chosen}"; + +} + +say $/ . 'Final Result' . $/ . '-' x 12; +list_coins('User',@user); +list_coins('Computer',@computer); + +my ($u,$c) = (0,0); +$u += $_ for (@user); +$c += $_ for (@computer); +say $/, $u > $c ? 'User' : 'Computer', ' is the lucky winner!'; + +sub list_coins { + my ($name,@coins) = @_; + say $/ . $name. ': ' . join ',', map {$coin_value{$_}} @coins; +} |
