diff options
| -rw-r--r-- | challenge-052/alicia-bielsa/perl/ch-1.pl | 48 | ||||
| -rw-r--r-- | challenge-052/alicia-bielsa/perl/ch-2.pl | 118 | ||||
| -rwxr-xr-x | challenge-052/alicia-bielsa/raku/ch-1.p6 | 57 |
3 files changed, 223 insertions, 0 deletions
diff --git a/challenge-052/alicia-bielsa/perl/ch-1.pl b/challenge-052/alicia-bielsa/perl/ch-1.pl new file mode 100644 index 0000000000..7a9cb19421 --- /dev/null +++ b/challenge-052/alicia-bielsa/perl/ch-1.pl @@ -0,0 +1,48 @@ +use strict; +use warnings; + +#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 $numberFrom = $ARGV[0]; +my $numberTo = $ARGV[1]; + +unless ( isValidNumber($numberFrom) && isValidNumber($numberTo) ) { + die "Error: Scripts accepts two numbers between 100 and 999\n"; +} + +#From should be smaller than to, else we swap +if ($numberFrom > $numberTo ){ + my $tmp = $numberFrom; + $numberFrom = $numberTo; + $numberTo = $tmp; +} + +foreach my $number ($numberFrom..$numberTo) { + my @aDigits = split('', $number); + my $isSteppingNumber = 1; + foreach my $i (1..$#aDigits){ + my $diff = $aDigits[$i] - $aDigits[$i-1]; + if ( $diff != 1 && $diff != -1 ){ + $isSteppingNumber = 0; + } + } + if ($isSteppingNumber){ + print "$number\n"; + } +} + + +sub isValidNumber { + my $number = shift; + if ( defined($number) && $number >= 100 && $number <= 999 ) { + return 1; + } + return 0; +}
\ No newline at end of file diff --git a/challenge-052/alicia-bielsa/perl/ch-2.pl b/challenge-052/alicia-bielsa/perl/ch-2.pl new file mode 100644 index 0000000000..46b4003044 --- /dev/null +++ b/challenge-052/alicia-bielsa/perl/ch-2.pl @@ -0,0 +1,118 @@ +use strict; +use warnings; +use Data::Dumper; + + +#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? + +my %hCoinsValue = ( + '£2' => 200, + '£1' => 100, + '50p' => 50, + '20p' => 20, + '10p' => 10, + '5p' => 5, + '2p' => 2, + '1p' => 1 + ); +my @aCoins = keys %hCoinsValue; +my @aPlayerCoins =(); +my @aComputerCoins = (); + +while (scalar(@aCoins)){ + push (@aPlayerCoins, playerChooses()); + print "Player: ".join(',',@aPlayerCoins)."\n"; + push (@aComputerCoins, computerChooses()); + print "Computer: ".join(',',@aComputerCoins)."\n"; +} + +my $totalComputer = sumCoins(\@aComputerCoins); +my $totalPlayer = sumCoins(\@aPlayerCoins); + + +print "----------------------\nEnd of game\n"; +print "Player: ".join(',',@aPlayerCoins)."\n"; +print "Computer: ".join(',',@aComputerCoins)."\n"; + +if ($totalComputer > $totalPlayer){ + print "Computer wins\n"; +} elsif ($totalComputer < $totalPlayer) { + print "Player wins\n"; +} else { + print "Draw\n"; +} + + +sub sumCoins { + my $refCoins = shift; + my $sumCoins = 0; + foreach my $coin (@{$refCoins}){ + $sumCoins += $hCoinsValue{$coin}; + } + return $sumCoins; +} + +sub playerChooses { + + my $response = ''; + while ($response !~ /^(R|L)/){ + drawCoins(); + $response = askPlayer(); + } + if ( $response =~ /^R/){ + return pop(@aCoins); + } else { + return shift(@aCoins); + } + +} + +sub computerChooses { + my $response = ''; + drawCoins(); + if (scalar(@aCoins) == 1){ + return pop(@aCoins); + } + #we dont want the player to get the 2 pound coin + if ($aCoins[1] eq '£2'){ + $response = 'R'; + } elsif($aCoins[$#aCoins-1] eq '£2'){ + $response = 'L'; + } elsif ( $hCoinsValue{$aCoins[0]} > $hCoinsValue{$aCoins[$#aCoins]}) { + $response = 'L'; + } else { + $response = 'R'; + } + + if ( $response =~ /^R/){ + return pop(@aCoins); + } else { + return shift(@aCoins); + } + +} + +sub askPlayer{ + print "Please choose coin from right or left ( R or L )\n"; + my $input = <STDIN>; + chomp($input); + $input =~ s%^\s+%%; + $input =~ s%\s+$%%; + return uc($input); +} + +sub drawCoins { + print "\nL".'--------' x scalar(@aCoins)."R\n"; + foreach my $coin (@aCoins){ + print $coin ."\t"; + } + print "\n".'--------' x scalar(@aCoins)."\n"; +} diff --git a/challenge-052/alicia-bielsa/raku/ch-1.p6 b/challenge-052/alicia-bielsa/raku/ch-1.p6 new file mode 100755 index 0000000000..335e679d1d --- /dev/null +++ b/challenge-052/alicia-bielsa/raku/ch-1.p6 @@ -0,0 +1,57 @@ +use v6; + + +#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. + + +sub MAIN ( Int $firstNumber , Int $secondNumber ) { + unless isValidNumber($firstNumber) && isValidNumber($secondNumber) { + die "Error: Scripts accepts two numbers between 100 and 999\n"; + } + + + my $numberFrom = $firstNumber; + my $numberTo = $secondNumber; + #From should be smaller than To + if $firstNumber > $secondNumber { + $numberFrom = $secondNumber; + $numberTo = $firstNumber; + } + + + for $numberFrom..$numberTo { + printIfSteppingNumber($_); + } +} + +sub isValidNumber (Int $number){ + if $number.defined && $number >= 100 && $number <= 999 { + return 1; + } + say $number ~ " is not valid"; + return 0; +} + +sub printIfSteppingNumber ( Int $number ) { + my @aDigits = split('',$number); + @aDigits.pop; + @aDigits.shift; + my $isSteppingNumber = 1; + + for 1..@aDigits.Int-1 { + my $diff = @aDigits[$_] - @aDigits[$_-1]; + if $diff != 1 && $diff != -1 { + $isSteppingNumber = 0; + } + } + if $isSteppingNumber { + say $number; + } + +}
\ No newline at end of file |
