diff options
| author | Fung Cheok Yin <61836418+E7-87-83@users.noreply.github.com> | 2020-03-21 17:50:43 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-03-21 17:50:43 +0800 |
| commit | 962a9979af911f73612b25670d00b35cfa12e183 (patch) | |
| tree | e1c21e1c0ea10b527c806bf6aee6b2dcda6262be | |
| parent | deac46321462d42d4ff439f3b04ff12b964b3b57 (diff) | |
| download | perlweeklychallenge-club-962a9979af911f73612b25670d00b35cfa12e183.tar.gz perlweeklychallenge-club-962a9979af911f73612b25670d00b35cfa12e183.tar.bz2 perlweeklychallenge-club-962a9979af911f73612b25670d00b35cfa12e183.zip | |
Add files via upload
| -rw-r--r-- | challenge-052/cheok-yin-fung/perl/ch-2.pl | 55 |
1 files changed, 47 insertions, 8 deletions
diff --git a/challenge-052/cheok-yin-fung/perl/ch-2.pl b/challenge-052/cheok-yin-fung/perl/ch-2.pl index 4dc49784bd..e37b06a330 100644 --- a/challenge-052/cheok-yin-fung/perl/ch-2.pl +++ b/challenge-052/cheok-yin-fung/perl/ch-2.pl @@ -1,9 +1,9 @@ #!/usr/bin/perl
use strict;
-# I will use array implementation for the binary tree,
-# see https://en.wikipedia.org/wiki/Binary_tree#Arrays
-# Usage: ch-2.pl LIST,
-# example: "ch-2.pl 2 5 3 1" returns first player wins
+use List::Util qw(any);
+
+# I use array implementation for the binary tree,
+# ref: https://en.wikipedia.org/wiki/Binary_tree#Arrays
sub jumptoLc {
return $_ * 2 + 1;
@@ -14,9 +14,31 @@ sub jumptoRc { }
+#Input Stage
+print "Enter the coins for the game, ".
+ "splitted by comma and with their units. \n";
+
+chomp(my $enter = <STDIN>);
+
+my @coin = split /[\s]*,[\s]*/ , $enter;
+
+my @allinpence;
+
+my $poundsign = chr(156); #or directly £ ...
+#chr(156) on my Windows Command Prompt,
+#chr(163) for some character sets (??), e.g. Latin-1
+foreach (@coin) {
+ if ($_ =~ /.p$/) {
+ push @allinpence, substr($_, 0, -1);
+ } elsif ($_ =~ /^($poundsign)./) {
+ push @allinpence, 100*substr($_, 1);
+ }
+}
+
my $Plist;
-$Plist->[0] = \@ARGV;
+$Plist->[0] = \@allinpence;
+#initialization
my $size = $#{$Plist->[0]}+1;
my @Pvaluef = (0);
@@ -96,11 +118,28 @@ foreach (reverse 0..2**($size-1)-1 ) { }
}
-print "Optimal Play Diff: ".($Pvaluef[0]-$Pvalues[0])."\n";
-if ($Pvaluef[0]-$Pvalues[0] > 0) {
+my $opdiff = $Pvaluef[0]-$Pvalues[0];
+
+
+
+#Output Stage
+sub pize {
+ my $a = int($_[0] / (100));
+ my $b = $_[0] % 100;
+ if ($a == 0) {return $b."p";}
+ if ($b == 0) {return $poundsign.$a;}
+ if ($a != 0 && $b != 0) {
+ return ($poundsign.($_[0]/100));
+ }
+}
+
+
+print "Optimal Play Diff: ".pize($opdiff)."\n";
+
+if ($opdiff > 0) {
print "First player wins."
-} elsif ($Pvaluef[0]==$Pvalues[0]) {
+} elsif ($opdiff==0) {
print "Draw.";
} else {
print "Second player wins.";
|
