diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-03-23 00:20:03 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-03-23 00:20:03 +0000 |
| commit | 0643d0ab174c982d5167ab41a82282d6c78ee03c (patch) | |
| tree | 4a7cf7bd32a06f7b15c5c25aa66561a45cfc8826 /challenge-052 | |
| parent | 486f4ddac15dea4a5ccde00999c54736e9780d8c (diff) | |
| parent | 0238e3a95ef3f7d4f3738638495b9979a7d5a513 (diff) | |
| download | perlweeklychallenge-club-0643d0ab174c982d5167ab41a82282d6c78ee03c.tar.gz perlweeklychallenge-club-0643d0ab174c982d5167ab41a82282d6c78ee03c.tar.bz2 perlweeklychallenge-club-0643d0ab174c982d5167ab41a82282d6c78ee03c.zip | |
Merge pull request #1448 from dcw803/master
imported my solutions for challenge 52..
Diffstat (limited to 'challenge-052')
| -rw-r--r-- | challenge-052/duncan-c-white/README | 55 | ||||
| -rwxr-xr-x | challenge-052/duncan-c-white/perl/ch-1.pl | 47 | ||||
| -rwxr-xr-x | challenge-052/duncan-c-white/perl/ch-2.pl | 139 |
3 files changed, 223 insertions, 18 deletions
diff --git a/challenge-052/duncan-c-white/README b/challenge-052/duncan-c-white/README index 1c6e36e8ae..557a59563d 100644 --- a/challenge-052/duncan-c-white/README +++ b/challenge-052/duncan-c-white/README @@ -1,30 +1,49 @@ -Task 1: "3 Sum +Task 1: "Stepping Numbers -Given an array @L of integers, and a target T. Write a script to find -all unique triplets such that a + b + c is same as the target T. Also -make sure a <= b <= c. +Write a script to accept two numbers between 100 and 999. It should then +print all Stepping Numbers between them. -Example: - - @L = (-25, -10, -7, -3, 2, 4, 8, 10); - -One such triplet for target 0 i.e. -10 + 2 + 8 = 0. + 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. " -My notes: Fine. +My notes: Sounds simple. -Task #2: "Colourful Number +Task #2: "Lucky Winner -Write a script to display all Colorful Number with 3 digits. +Suppose there are following coins arranged on a table in a line in +random order. - A Colorful Number is an integer where all the products of consecutive - subsets of digit are different. + £1, 50p, 1p, 10p, 5p, 20p, £2, 2p -For example, 263 is a Colorful Number since 2, 6, 3, 2x6, 6x3, 2x6x3 are unique. +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? " -My notes: Interesting. +My notes: umm.. with this wording, it wasn't very clear what exactly we're +being asked to do, so the wording I've chosen is: + + "Suppose the following coins are arranged in a line, but in + a different random order each game: + + £1, 50p, 1p, 10p, 5p, 20p, £2, 2p + + A human is playing against the computer. Pick a random player to go + first, after that players play alternative moves. Each move, either pick + the FIRST coin or the LAST coin. The winner is the player with the greatest + amount of money. Write a program to play the whole game, with a computer + move choice method with the greatest choice of winning. + " + +Given this understanding, and those specific coin values, then whoever picks +up the £2 coin will win. obviously if the £2 coin is initially at one end, +picking it is the correct move, otherwise both players should ensure they do +not give the other player the opportunity to pick the £2 coin next move. +ie. avoid leaving the £2 coin at either end.. -Having written this, assuming that it's correct, I find 328 Colourful 3-digit -numbers. +In practice: the first player alway wins, so rather a dull game. +If playing second, the best the computer can do is pounce on a human +mistake. +# diff --git a/challenge-052/duncan-c-white/perl/ch-1.pl b/challenge-052/duncan-c-white/perl/ch-1.pl new file mode 100755 index 0000000000..7918222184 --- /dev/null +++ b/challenge-052/duncan-c-white/perl/ch-1.pl @@ -0,0 +1,47 @@ +#!/usr/bin/perl +# +# 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. +# " +# +# My notes: Sounds simple. +# + +use feature 'say'; +use strict; +use warnings; +use Function::Parameters; +#use Data::Dumper; + +die "Usage: stepping-nos [from [to]]\n" if @ARGV > 2; +my $from = shift // 100; +my $to = shift // 999; + + +# +# my $isstepping = stepping( $x ); +# Return true iff $x is a stepping number in the sense of the question, +# ie. one where the adjacent digits have a difference of 1. eg 456. +# +fun stepping( $x ) +{ + my @digits = split(//,$x); + my $prev = shift @digits; + foreach my $next (@digits) + { + return 0 unless $next == $prev+1; + $prev = $next; + } + return 1; +} + + +foreach my $n ($from..$to) +{ + say $n if stepping($n); +} diff --git a/challenge-052/duncan-c-white/perl/ch-2.pl b/challenge-052/duncan-c-white/perl/ch-2.pl new file mode 100755 index 0000000000..bd03820f26 --- /dev/null +++ b/challenge-052/duncan-c-white/perl/ch-2.pl @@ -0,0 +1,139 @@ +#!/usr/bin/perl +# +# Task #2: "Lucky Winner +# +# Suppose the following coins are arranged in a line, but in +# a different random order each game: +# +# £1, 50p, 1p, 10p, 5p, 20p, £2, 2p +# +# A human is playing against the computer. Pick a random player to go +# first, after that players play alternative moves. Each move, either pick +# the FIRST coin or the LAST coin. The winner is the player with the greatest +# amount of money. Write a program to play the whole game, with a computer +# move choice method with the greatest choice of winning. +# " +# +# My notes: umm.. with the original wording, it wasn't very clear what exactly +# we're being asked to do, so I've changed the wording above to clarify what +# I think was meant. +# Given those coin values, whoever picks up the £2 coin will win. obviously +# if the £2 coin is initially at one end,. picking it immediately is the +# correct move, otherwise both players should ensure they do not give the +# other player the opportunity to pick the £2 coin next move. ie. avoid +# leaving the £2 coin at either end.. +# In practice: the first player alway wins, so rather a dull game. +# If playing second, the best the computer can do is pounce on a human +# mistake. +# + +use strict; +use warnings; +use feature 'say'; +use Function::Parameters; +use Data::Dumper; +use List::Util qw(max); + +my @coins = ( 100, 50, 1, 10, 5, 20, 200, 2 ); + +my $biggest = max(@coins); + +srand( $$ ^ time() ); + +# randomise their order.. +@coins = sort { int rand(3) - 1 } @coins; + +#die Dumper \@coins; + +# +# my $choice = pick_first_or_last( @coins ); +# Given an array of coin values @coins (expressed in pennies), +# pick either the "first" coin (return 'first') or the "last" coin +# (return 'last'), depending on which is a better move to make. +# +# The $biggest coin (with these coins the £2 coin) is critical: pick +# it if it's at either end, otherwise prevent it from getting to either +# end.. if it's already been picked, calculate the biggest remaining +# and apply the same strategy to that value.. +# +fun pick_first_or_last( @coins ) +{ + my $firstc = $coins[0]; + return 'first' if $firstc == $biggest; + my $lastc = $coins[$#coins]; + return 'last' if $lastc == $biggest; + + # find position of biggest (if it's still here) + my @bigpos = grep { $coins[$_] == $biggest } 0..$#coins; + # if not here.. change biggest to the biggest that is still here + if( @bigpos == 0 ) + { + $biggest = max(@coins); + # find the position of that new biggest + @bigpos = grep { $coins[$_] == $biggest } 0..$#coins; + } + # now: @bigpos==1, $bigpos[0] is the position of that biggest. + my $nbp = @bigpos; + die "logic error, bigpos array has $nbp elements, should be 1\n" + unless $nbp==1; + my $bigpos = shift @bigpos; + + return 'last' if $bigpos == 1; # biggest very close to front + return 'first' if $bigpos == $#coins-1; # biggest very close to back + + # pick bigger + return 'last' if $coins[$#coins] > $coins[0]; + return 'first'; +} + +my $humtot = 0; +my $comptot = 0; + +# randomise who plays first +my $player = int(rand(2)); # 0 is human, 1 is computer +my @who = qw(You I); + +say "$who[$player] play first"; + +while( @coins ) +{ + say "coins: ", join(',',@coins); + if( $player == 0 ) + { + my $choice = 'f'; + if( @coins > 1 ) + { + print " pick first coin (f) or last coin (l)? "; + $choice = <STDIN>; + chomp $choice; + $choice = lc($choice); + } + + my $coin = ($choice eq 'f')? shift @coins : pop @coins; + $humtot += $coin; + say " you picked $coin, your total is now $humtot"; + } else + { + my $choice = pick_first_or_last( @coins ); + say " I pick $choice coin"; + + my $coin = ($choice eq 'first')? shift @coins : pop @coins; + $comptot += $coin; + say " I picked $coin, my total is now $comptot"; + } + + # switch players + $player = 1-$player; +} + +say "you scored $humtot, I scored $comptot"; +if( $humtot > $comptot ) +{ + say "well done, you win"; +} elsif( $humtot < $comptot ) +{ + say "yippee, I win"; +} else +{ + say "it's a draw!"; +} |
