diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-01-26 21:05:38 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-01-26 21:05:38 +0000 |
| commit | 13d20d10ebb79d5f2d6a3b216ea9617e74852c42 (patch) | |
| tree | 4f76144712c3e3f90d7f200165a82c40ee603493 /challenge-044 | |
| parent | cf9e513bac9602971cecfc8067142b4f84609f3a (diff) | |
| parent | 17fe7ecccdb2f879dfe9be59b1f102dee2d27d79 (diff) | |
| download | perlweeklychallenge-club-13d20d10ebb79d5f2d6a3b216ea9617e74852c42.tar.gz perlweeklychallenge-club-13d20d10ebb79d5f2d6a3b216ea9617e74852c42.tar.bz2 perlweeklychallenge-club-13d20d10ebb79d5f2d6a3b216ea9617e74852c42.zip | |
Merge pull request #1170 from aliciabielsa/branch-for-challenge-044
challenge 044
Diffstat (limited to 'challenge-044')
| -rw-r--r-- | challenge-044/alicia-bielsa/perl/ch-1.pl | 88 | ||||
| -rw-r--r-- | challenge-044/alicia-bielsa/perl/ch-2.pl | 33 |
2 files changed, 121 insertions, 0 deletions
diff --git a/challenge-044/alicia-bielsa/perl/ch-1.pl b/challenge-044/alicia-bielsa/perl/ch-1.pl new file mode 100644 index 0000000000..f29178addb --- /dev/null +++ b/challenge-044/alicia-bielsa/perl/ch-1.pl @@ -0,0 +1,88 @@ +#Only 100, please. +#You are given a string “123456789”. +#Write a script that would insert ”+” or ”-” in between digits so that when you evaluate, the result should be 100. + +use strict; +use warnings; +use Data::Dumper; + +my @aNumbers = (1..9); +my @aOperators = ('+', '-'); +my $objective = 100; +my @aNumberDigits = (1,2,3); +my @aResults = (); + + +foreach my $i (0..$#aNumbers){ + addGroupDigits($i); +} + +sub addGroupDigits { + my $index = shift; + + my $previousNumber = $aNumbers[$index -1]; + my @aTmp = @aResults ; + my @aAddResults = (); + foreach my $operator (@aOperators){ + my $groupDigits = ''; + foreach my $numberDigits (@aNumberDigits){ + if (!defined ($aNumbers[$index + $numberDigits -1 ])){ + next; + } + $groupDigits .= $aNumbers[$index + $numberDigits -1 ]; + + if ($index != 0){ + foreach my $resultIndex ( reverse(0..$#aTmp) ){ + if ($aTmp[$resultIndex] =~ /$previousNumber$/ ) { + push (@aAddResults , $aTmp[$resultIndex]."$operator$groupDigits" ); + if (defined($aResults[$resultIndex]) && $aResults[$resultIndex] eq $aTmp[$resultIndex] ){ + splice ( @aResults, $resultIndex, 1 ); + } + } + } + } else { + push (@aResults, "$operator$groupDigits"); + } + } + } + @aResults = (@aAddResults , @aResults); +} + +foreach my $result (@aResults){ + my @aMatches = $result =~ m/([+-]{1})(\d+)/g; + my $currentNumber = 0; + my $previousNumber =0; + my $currentFlagSum = 0; + my $previousFlagSum ; + my $total = 0; + foreach my $matchIndex ( 0..$#aMatches ){ + my $match = $aMatches[$matchIndex]; + if ($match eq '+'){ + $previousFlagSum = $currentFlagSum; + $currentFlagSum = 1; + } elsif ($match eq '-'){ + $previousFlagSum = $currentFlagSum; + $currentFlagSum = 0; + } else { + $currentNumber .= $match ; + unless ($matchIndex == $#aMatches ){ + next; + } + } + $previousNumber = $currentNumber ; + $currentNumber = 0; + + next unless (defined $previousFlagSum ); + if ( $previousFlagSum ){ + $total += $previousNumber; + } else { + $total -= $previousNumber; + } + } + if ($total == 100) { + print "$result = $total\n"; + } + +} + + diff --git a/challenge-044/alicia-bielsa/perl/ch-2.pl b/challenge-044/alicia-bielsa/perl/ch-2.pl new file mode 100644 index 0000000000..38aae4f211 --- /dev/null +++ b/challenge-044/alicia-bielsa/perl/ch-2.pl @@ -0,0 +1,33 @@ +# it $200
+#You have only $1 left at the start of the week. You have been given an opportunity to make it $200.
+#The rule is simple with every move you can either double what you have or add another $1.
+#Write a script to help you get $200 with the smallest number of moves.
+
+use strict;
+use warnings;
+use Data::Dumper;
+
+my $objective = 200;
+my @aMoves =();
+
+while ($objective > 1 ) {
+ if ($objective % 2 == 0){
+ push (@aMoves, 'double');
+ $objective = $objective / 2;
+ } else {
+ push (@aMoves, 'add 1');
+ $objective = $objective - 1;
+ }
+}
+my $amountMoney = 1;
+foreach my $move (reverse(@aMoves)){
+ if ($move eq 'double'){
+ print "Double $amountMoney ";
+ $amountMoney = $amountMoney * 2 ;
+ print "= $amountMoney\n";
+ } else {
+ print "Add 1 to $amountMoney ";
+ $amountMoney = $amountMoney + 1 ;
+ print "= $amountMoney\n";
+ }
+}
\ No newline at end of file |
