diff options
| author | Mark A <andemark@a-iot1t.uch.ad.pvt> | 2021-08-22 22:18:00 -0600 |
|---|---|---|
| committer | Mark A <andemark@a-iot1t.uch.ad.pvt> | 2021-08-22 22:18:00 -0600 |
| commit | 64b474ba7765f147d95b6bd45f981716fa2c8d5c (patch) | |
| tree | e7d406950df9e6d9402feb556f73cccceb5956ea | |
| parent | 7c67b3b4ed52d3aa51c712f1ac42d1d3e2a4db47 (diff) | |
| download | perlweeklychallenge-club-64b474ba7765f147d95b6bd45f981716fa2c8d5c.tar.gz perlweeklychallenge-club-64b474ba7765f147d95b6bd45f981716fa2c8d5c.tar.bz2 perlweeklychallenge-club-64b474ba7765f147d95b6bd45f981716fa2c8d5c.zip | |
Better comments
| -rw-r--r-- | challenge-126/mark-anderson/raku/ch-1.raku | 25 |
1 files changed, 13 insertions, 12 deletions
diff --git a/challenge-126/mark-anderson/raku/ch-1.raku b/challenge-126/mark-anderson/raku/ch-1.raku index 6366bb830c..c83b03f868 100644 --- a/challenge-126/mark-anderson/raku/ch-1.raku +++ b/challenge-126/mark-anderson/raku/ch-1.raku @@ -1,13 +1,17 @@ #!/usr/bin/env raku -# count-numbers uses the following algorithm. -# Example: The result of 9324 = 8*9^3 + 2*9^2 + 1*9^1 + 3*9^0 = 6006 - -# But first, adjust is called. +# The adjust function adjusts the number if it contains a 1 # Examples: 111 is converted to 99 # 901 is converted to 900 # 2731515 is converted to 2730999 -# A number with no 1s is unchanged + +# The count-numbers function performs this equivalent algorithm: +# For all digits, subtract 1 and multiply that by 9 raised to the +# power of the log of that digit's place value. Then sum those +# results. +# Examples: count-numbers(25) = 1*9^1 + 4*9^0 = 13 +# count-numbers(7777) = 6*9^3 + 6*9^2 + 6*9^1 + 6*9^0 = 4920 +# count-numbers(9324) = 8*9^3 + 2*9^2 + 1*9^1 + 3*9^0 = 6006 use Test; plan 9; @@ -30,25 +34,22 @@ multi count-numbers($N is copy) my @digits = $N.comb; my $count; - while @digits + loop { + return $count unless @digits; my $d = @digits.shift; next if $d == 0; $count += ($d-1) * 9 ** @digits.elems; } - - $count; } sub adjust($N is copy) { my @a = $N.split('1', 2); - - return $N if @a.elems == 1; # no 1s + return $N if @a.elems == 1; # $N has no 1s @a.tail = 0 ~ 9 x @a.tail.chars; - return @a.tail.substr(1) unless @a.head; # first digit is 1 - + return +@a.tail unless @a.head; # first digit of $N is 1 return @a.join; } |
