diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-01-24 20:38:19 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-01-24 20:38:19 +0000 |
| commit | cbe10caf39868976a5207052594fbb0ccfb23767 (patch) | |
| tree | 50030902af956dc837d94b0ff1b0a1f504df1b77 /challenge-044 | |
| parent | 2508e86d6ff23e6e0ddc4d5c070b9377eea18630 (diff) | |
| parent | dcb5f4323785b8938fdecfa088929cfb414ee387 (diff) | |
| download | perlweeklychallenge-club-cbe10caf39868976a5207052594fbb0ccfb23767.tar.gz perlweeklychallenge-club-cbe10caf39868976a5207052594fbb0ccfb23767.tar.bz2 perlweeklychallenge-club-cbe10caf39868976a5207052594fbb0ccfb23767.zip | |
Merge pull request #1164 from saiftynet/branch-044
Blending Numbers and operations together
Diffstat (limited to 'challenge-044')
| -rw-r--r-- | challenge-044/saiftynet/perl/ch-1.pl | 48 | ||||
| -rw-r--r-- | challenge-044/saiftynet/perl/ch-2.pl | 64 |
2 files changed, 112 insertions, 0 deletions
diff --git a/challenge-044/saiftynet/perl/ch-1.pl b/challenge-044/saiftynet/perl/ch-1.pl new file mode 100644 index 0000000000..7512fdfd77 --- /dev/null +++ b/challenge-044/saiftynet/perl/ch-1.pl @@ -0,0 +1,48 @@ +#!/usr/env perl +# Perl Weekly Challenge Challenge 044 Task 1 +# 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. +# +# This soloution extends to allow any start string and any target# +# anf prints all possible answers + +use strict; use warnings; + +tryInsert("123456789",100); # main call: provide intial string and target. + +# This subroutine splits a string into two at each possible position +# in the string, and tries a calulation with a + or a - in beteen. +# It then does the same with the second half of the string. +# at each insert it evaluates the result, and prints the string +# as a possible solution; All discovered answers are displayed. + +sub tryInsert{ + my ($str,$target,$marker)=@_; + $marker //=1; # $marker identifies the first insertion point + # further attempts ar from this point to the + # postion before the last character in the string + foreach my $pos ($marker..((length $str)-1)){ + foreach my $operator(qw{+ -}){ + my $temp=$str; # put in a temporary string + substr $temp,$pos,0,$operator; # insert the operator + print $temp."\n" # print the reulting string + if eval ("$temp")==$target; # only if the result is our $target + tryInsert($temp,$target,$pos+2); # recurse with new insertion point + } + } +} + + +## Output +# 1+2+3-4+5+6+78+9 +# 1+2+34-5+67-8+9 +# 1+23-4+5+6+78-9 +# 1+23-4+56+7+8+9 +# 12+3+4+5-6-7+89 +# 12+3-4+5+67+8+9 +# 12-3-4+5-6+7+89 +# 123+4-5+67-89 +# 123+45-67+8-9 +# 123-4-5-6-7+8-9 +# 123-45-67+89 diff --git a/challenge-044/saiftynet/perl/ch-2.pl b/challenge-044/saiftynet/perl/ch-2.pl new file mode 100644 index 0000000000..41f8c2bf30 --- /dev/null +++ b/challenge-044/saiftynet/perl/ch-2.pl @@ -0,0 +1,64 @@ +#!/usr/env perl +# Perl Weekly Challenge Challenge 044 Task 2 +# 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. +# +# This soloution extends to allow any start and any target + +use strict; use warnings; + +doubleOrAdd(1,200); # The main call: Give a start and the target, + # returns the results to console + +sub doubleOrAdd{ # this routine takes starting value and final target + my ($start,$target)=@_; + + return print "Number out of bounds\n" # too big takes too long and + if $target>100000 or $target<$start; # can't be smaller than $target + + my @options=("$start"); # initialise list of operations, + my $found=($start==$target); # do we already have goal? + + while (! $found){ + @options = map {("($_+1)","($_*2)")} @options; # add the two possible ops + foreach my $answer ( @options ) { # test each of the sequences + if ( eval("$answer")==$target) { # against our target + + # if found, announce it + # the answer will look like (((((((((1+1)+1)*2)*2)*2)+1)*2)*2)*2) + # to make it easier to read, we will translate it to English + + # number of moves is count of open brackets,remove these. + print $answer=~s/\(//g," moves required\n"; + + $answer=~s/^(\d+)/Start with \$$1,\n/; # the starting number is 1st number + $answer=~s/\+1\)/ add 1,\n/g; # make the result human + $answer=~s/\*2\)/ double it,\n/g; # readable: + + print $answer; + + + $found=1;last; # stop looking + }; + }; + } + print " Now you have \$$target!"; # declare discovery +} + +## Output +# 9 moves required +# Start with $1, +# add 1, +# add 1, +# double it, +# double it, +# double it, +# add 1, +# double it, +# double it, +# double it, +# gives you $200 + + |
