diff options
| author | drbaggy <js5@sanger.ac.uk> | 2021-12-13 17:22:06 +0000 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2021-12-13 17:22:06 +0000 |
| commit | 526c322c070f34841e5a75a25a1d86bd370d88e8 (patch) | |
| tree | 364eda9653df8fae7f7c1ad24436b7eaa453221b /challenge-143/james-smith | |
| parent | 07b1884f7284cfcd9bd72829b5de4c4751b90100 (diff) | |
| download | perlweeklychallenge-club-526c322c070f34841e5a75a25a1d86bd370d88e8.tar.gz perlweeklychallenge-club-526c322c070f34841e5a75a25a1d86bd370d88e8.tar.bz2 perlweeklychallenge-club-526c322c070f34841e5a75a25a1d86bd370d88e8.zip | |
some notes
Diffstat (limited to 'challenge-143/james-smith')
| -rw-r--r-- | challenge-143/james-smith/README.md | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/challenge-143/james-smith/README.md b/challenge-143/james-smith/README.md index d7d0f3e04a..fc9a9a857d 100644 --- a/challenge-143/james-smith/README.md +++ b/challenge-143/james-smith/README.md @@ -18,6 +18,22 @@ https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-143/ja ## The solution +The simple solution is just to "`eval`" the string, but where is the fun in that... We can either go for a tokenizing parser - where we create an array of elements brackets, symbols, numbers, or we can use regular expressions to reduce the equation. + +All students will remember BODMAS - Brackets, order, Division/Multiplication, Addition/Division. (or BIDMAS, PIDMAS, PEMDAS or whatever you used to remember it). So we have to break our prasing down into: + + * Brackets + * Order - we don't have this in our equations + * Division/Multiplication - we only have the latter + * Addition/Subtraction. + +So our logic becomes: + + * Find brackets without brackets within - for these we evaluate the contents using the same algorithm; + * If there are no brackets left - we then looking for multiplications from left to right and evaluate these. + * If there are no brackets or multiplications we look at the addition/subtraction again left to right. + * This gives the following perl function: + ```perl sub evaluate { my $str = shift; @@ -28,12 +44,20 @@ sub evaluate { } ``` +For small strings - this is about the same speed as `eval`, for larger strings not so, but in both cases it is "safer" as string `eval` of "tainted" input is a real security risk. + # Challenge 2 - Stealthy Number ***You are given a positive number, `$n`. Write a script to find out if the given number is Stealthy Number. A positive integer `N` is stealthy, if there exist positive integers `a`, `b`, `c`, `d` such that `a * b = c * d = N` and `a + b = c + d + 1`.*** ## The solution +First we find all the factors of `N` (well the ones for where `N=a*b` and `a<b`) + +We then store the sum of the factors in a hash (as the keys), we also store the sum of the factors + 1 in the hash {using `++` so what is stored is the count of these numbers}. + +So if we have the condition `a+b == c+d+1` is equivalent to seeing which keys in the hash have a value greater than 1. In which case we have our value. For the problem we don't need this value - just whether there is a value. So our function comes down to this. + ```perl sub stealthy_number { my($n,%c) = shift; |
