diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-03-21 11:44:39 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-03-21 11:44:39 +0000 |
| commit | eec3a0b7cd99b74a802627c52bbc513c757dca34 (patch) | |
| tree | cf80a637bde1c357dac6f7cdbce2a3582deb488d | |
| parent | d4bcbbbadbabfa4e954135447697f88ffa381f85 (diff) | |
| parent | 82f7c612b098da64552875a18799aad6282dc430 (diff) | |
| download | perlweeklychallenge-club-eec3a0b7cd99b74a802627c52bbc513c757dca34.tar.gz perlweeklychallenge-club-eec3a0b7cd99b74a802627c52bbc513c757dca34.tar.bz2 perlweeklychallenge-club-eec3a0b7cd99b74a802627c52bbc513c757dca34.zip | |
Merge pull request #1433 from E7-87-83/master
an improved version of my code
| -rw-r--r-- | challenge-052/cheok-yin-fung/perl/ch-2.pl | 54 |
1 files changed, 46 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..4849a9653c 100644 --- a/challenge-052/cheok-yin-fung/perl/ch-2.pl +++ b/challenge-052/cheok-yin-fung/perl/ch-2.pl @@ -1,9 +1,8 @@ #!/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
+
+# I use array implementation for the binary tree,
+# ref: https://en.wikipedia.org/wiki/Binary_tree#Arrays
sub jumptoLc {
return $_ * 2 + 1;
@@ -14,9 +13,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 +117,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.";
|
