|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "Orders Matter and Order Matters: Weekly Challenge #323" |
| 4 | +author: "Dave Jacoby" |
| 5 | +date: "2025-05-29 15:42:41 -0400" |
| 6 | +categories: "" |
| 7 | +--- |
| 8 | + |
| 9 | +Top |
| 10 | +logo |
| 11 | +Home |
| 12 | +About |
| 13 | +Chart |
| 14 | +Champions |
| 15 | +Team |
| 16 | +Challenges |
| 17 | +Perl/Review |
| 18 | +Raku/Review |
| 19 | +Recaps |
| 20 | +Blogs |
| 21 | +FAQ |
| 22 | +Contact |
| 23 | +The Weekly Challenge - 323 |
| 24 | +Friday, May 16, 2025| Tags: Perl, Raku |
| 25 | + |
| 26 | +TABLE OF CONTENTS |
| 27 | + |
| 28 | +1. HEADLINES |
| 29 | +2. SPONSOR |
| 30 | +3. RECAP |
| 31 | +4. PERL REVIEW |
| 32 | +5. RAKU REVIEW |
| 33 | +6. CHART |
| 34 | +7. NEW MEMBERS |
| 35 | +8. GUESTS |
| 36 | +9. TASK #1: Increment Decrement |
| 37 | +10. TASK #2: Tax Amount |
| 38 | + |
| 39 | +HEADLINES |
| 40 | +Welcome to the Week #323 of The Weekly Challenge. |
| 41 | + |
| 42 | +Welcome aboard, Yitzchak Scott-Thoennes and thanks for your first contributions in Perl and Python. |
| 43 | + |
| 44 | +We now have 324 members and 28 guests in Team PWC. |
| 45 | + |
| 46 | +With some late contributions, Week #322, became the best week of the year 2025 so far. |
| 47 | + |
| 48 | +Happy Hacking!! |
| 49 | + |
| 50 | +Last 5 weeks mainstream contribution stats. Thank you Team PWC for your support and encouragements. |
| 51 | + |
| 52 | +Week Perl Raku Blog |
| 53 | + 318 44 23 19 |
| 54 | + 319 38 20 16 |
| 55 | + 320 46 26 18 |
| 56 | + 321 44 26 27 |
| 57 | + 322 44 24 25 |
| 58 | + |
| 59 | +Last 5 weeks guest contribution stats. Thank you each and every guest contributors for your time and efforts. |
| 60 | + |
| 61 | +Week Guests Contributions Languages |
| 62 | + 318 10 48 16 |
| 63 | + 319 14 58 22 |
| 64 | + 320 14 62 23 |
| 65 | + 321 13 61 23 |
| 66 | + 322 12 52 18 |
| 67 | + |
| 68 | +TOP 10 Guest Languages |
| 69 | +Do you see your favourite language in the Top #10? If not then why not contribute regularly and make it to the top. |
| 70 | + |
| 71 | +1. Python (3386) |
| 72 | +2. Rust (910) |
| 73 | +3. Ruby (799) |
| 74 | +4. Haskell (777) |
| 75 | +5. Lua (724) |
| 76 | +6. C++ (617) |
| 77 | +7. C (590) |
| 78 | +8. JavaScript (562) |
| 79 | +9. Go (486) |
| 80 | +10. BQN (440) |
| 81 | + |
| 82 | +Blogs with Creative Title |
| 83 | + |
| 84 | +1. Ordered Format Array by Adam Russell. |
| 85 | +2. Stringed Array by Arne Sommer. |
| 86 | +3. String Format by Bob Lied. |
| 87 | +4. Dashing This Off by Dave Jacoby. |
| 88 | +5. Group Ranking by Jorg Sommrey. |
| 89 | +6. Splitting and Sorting by Luca Ferrari. |
| 90 | +7. Ranking Code, Ranking Numbers by Matthias Muth. |
| 91 | +8. The array is rank, but the string is formatted by Packy Anderson. |
| 92 | +9. More strings and arrays by Peter Campbell Smith. |
| 93 | +10. The Strings Are Rank by Roger Bell_West. |
| 94 | +11. Strings and Arrays by Simon Green. |
| 95 | + |
| 96 | +GitHub Repository Stats |
| 97 | + |
| 98 | +1. Commits: 43,653 (+106) |
| 99 | +2. Pull Requests: 12,064 (+38) |
| 100 | +3. Contributors: 260 (+1) |
| 101 | +4. Fork: 327 (+1) |
| 102 | +5. Stars: 191 (+2) |
| 103 | + |
| 104 | +SPONSOR |
| 105 | +With start of Week #268, we have a new sponsor Lance Wicks until the end of year 2025. Having said we are looking for more sponsors so that we can go back to weekly winner. If anyone interested please get in touch with us at perlweeklychallenge@yahoo.com. Thanks for your support in advance. |
| 106 | + |
| 107 | +RECAP |
| 108 | +Quick recap of The Weekly Challenge - 322 by Mohammad Sajid Anwar. |
| 109 | + |
| 110 | +PERL REVIEW |
| 111 | +If you missed any past reviews then please check out the collection. |
| 112 | + |
| 113 | +RAKU REVIEW |
| 114 | +If you missed any past reviews then please check out the collection. |
| 115 | + |
| 116 | +CHART |
| 117 | +Please take a look at the charts showing interesting data. |
| 118 | + |
| 119 | +I would like to THANK every member of the team for their valuable suggestions. Please do share your experience with us. |
| 120 | + |
| 121 | +NEW MEMBERS |
| 122 | +Yitzchak Scott-Thoennes, an expert Perl hacker joined the Team PWC. |
| 123 | + |
| 124 | +Please find out How to contribute?, if you have any doubts. |
| 125 | + |
| 126 | +Please try the excellent tool EZPWC created by respected member Saif Ahmed of Team PWC. |
| 127 | + |
| 128 | +GUESTS |
| 129 | +Please check out the guest contributions for the Week #322. |
| 130 | + |
| 131 | +Please find past solutions by respected guests. Please share your creative solutions in other languages. |
| 132 | + |
| 133 | +### Task 1: Increment Decrement |
| 134 | + |
| 135 | +> Submitted by: Mohammad Sajid Anwar |
| 136 | +> You are given a list of operations. |
| 137 | +> |
| 138 | +> Write a script to return the final value after performing the given operations in order. The initial value is always `0`. |
| 139 | +> |
| 140 | +> Possible Operations: |
| 141 | +> |
| 142 | +> - **`++x` or `x++`:** increment by 1 |
| 143 | +> - **`--x` or `x--`:** decrement by 1 |
| 144 | +
|
| 145 | +#### Let's Talk About It |
| 146 | + |
| 147 | +An aside before we go in: increment and decrement as prefix differ whether it comes before or after the number. Consider the following code block: |
| 148 | + |
| 149 | +```perl |
| 150 | +my $i = 1; |
| 151 | +my $j = $i++; |
| 152 | +my $k = ++$i; |
| 153 | +say join ' ', $i,$j,$k; # 3 1 3 |
| 154 | +``` |
| 155 | + |
| 156 | +`$i` equals `3`, because it gets incremented twice. `$j` gets assigned before the increment, because `$i` comes before `++`. `$k` gets assigned after the increment, so `$i` becomes `3`, then `$k` becomes `$i`. There's *no* assignment like that going on in in the midst of the operations, so it isn't germane to the problem, but it's an interesting piece of syntax that is easy to forget. |
| 157 | + |
| 158 | +Anyway, I loop through the operations and increment and decrement based on whether the operation contains `++` or `--`, then return the result. |
| 159 | + |
| 160 | +The *cool* way would be to use `scalar grep` to get a count of the plus and minuses, then `return 0 + $plus - $minus`. It's only slightly shorter, and probably similarly fast. |
| 161 | + |
| 162 | +#### Show Me The Code! |
| 163 | + |
| 164 | +```perl |
| 165 | +#!/usr/bin/env perl |
| 166 | + |
| 167 | +use strict; |
| 168 | +use warnings; |
| 169 | +use experimental qw{ say state postderef signatures }; |
| 170 | + |
| 171 | +use List::Util qw{ sum0 }; |
| 172 | + |
| 173 | +my @examples = ( |
| 174 | + |
| 175 | + [ "--x", "x++", "x++" ], |
| 176 | + [ "x++", "++x", "x++" ], |
| 177 | + [ "x++", "++x", "--x", "x--" ], |
| 178 | +); |
| 179 | + |
| 180 | +for my $example (@examples) { |
| 181 | + my $operations = join ', ', map { qq{"$_"} } $example->@*; |
| 182 | + my $output = increment_decrement($example->@*); |
| 183 | + say <<"END"; |
| 184 | + Input: \@operations = ($operations) |
| 185 | + Output: $output |
| 186 | +END |
| 187 | +} |
| 188 | + |
| 189 | +sub increment_decrement (@operations) { |
| 190 | + my $value = 0; |
| 191 | + for my $op ( @operations ) { |
| 192 | + $value ++ if $op =~ /\+\+/mx; |
| 193 | + $value -- if $op =~ /\-\-/mx; |
| 194 | + } |
| 195 | + return $value; |
| 196 | +} |
| 197 | +``` |
| 198 | + |
| 199 | +```text |
| 200 | +$ ./ch-1.pl |
| 201 | + Input: @operations = ("--x", "x++", "x++") |
| 202 | + Output: 1 |
| 203 | +
|
| 204 | + Input: @operations = ("x++", "++x", "x++") |
| 205 | + Output: 3 |
| 206 | +
|
| 207 | + Input: @operations = ("x++", "++x", "--x", "x--") |
| 208 | + Output: 0 |
| 209 | +``` |
| 210 | + |
| 211 | +### Task 2: Tax Amount |
| 212 | + |
| 213 | +> Submitted by: Mohammad Sajid Anwar |
| 214 | +> You are given an income amount and tax brackets. |
| 215 | +> |
| 216 | +> Write a script to calculate the total tax amount. |
| 217 | +
|
| 218 | +#### Let's Talk About It |
| 219 | + |
| 220 | +We are taxed at the rate of a bracket for the income within a bracket. In the first example, the income is `10`. |
| 221 | + |
| 222 | +* The first bracket is between `0` and `3`, and the tax rate applies to the first `3` whatevers from the income of 10. |
| 223 | +* The second bracket is between `3` and `7`, and the tax rate applies to everything above `7` and above `3`, which, again, is totally covered by the income, so that's `4`. |
| 224 | +* The third bracket is between `7` and `12`, and that' just between `7` and `10` for us, and that's `3`. |
| 225 | + |
| 226 | +Everything else is just multiplication and addition. |
| 227 | + |
| 228 | +#### Show Me The Code! |
| 229 | + |
| 230 | +```perl |
| 231 | +#!/usr/bin/env perl |
| 232 | + |
| 233 | +use strict; |
| 234 | +use warnings; |
| 235 | +use experimental qw{ say state postderef signatures }; |
| 236 | +use List::Util qw{ uniq }; |
| 237 | + |
| 238 | +my @examples = ( |
| 239 | + |
| 240 | + { income => 10, tax => [ [ 3, 50 ], [ 7, 10 ], [ 12, 25 ] ] }, |
| 241 | + { income => 2, tax => [ [ 1, 0 ], [ 4, 25 ], [ 5, 50 ] ] }, |
| 242 | + { income => 0, tax => [ [ 2, 50 ] ] }, |
| 243 | +); |
| 244 | + |
| 245 | +for my $example (@examples) { |
| 246 | + my $income = $example->{income}; |
| 247 | + my @tax = $example->{tax}->@*; |
| 248 | + my $tax = join ', ', map { qq{[ $_ ]} } |
| 249 | + map { join ', ', $_->@* } @tax; |
| 250 | + my $output = tax_amount($example); |
| 251 | + say <<"END"; |
| 252 | + Input: \@income = $income, |
| 253 | + \@tax = ($tax) |
| 254 | + Output: $output |
| 255 | +END |
| 256 | +} |
| 257 | + |
| 258 | +sub tax_amount($example) { |
| 259 | + my $total = 0; |
| 260 | + my $income = $example->{income}; |
| 261 | + my @tax = $example->{tax}->@*; |
| 262 | + for my $i ( 0 .. $#tax ) { |
| 263 | + my $bracket = $tax[$i]; |
| 264 | + my ( $upto, $rate ) = $bracket->@*; |
| 265 | + my $prev = 0; |
| 266 | + $prev = $i - 1 >= 0 ? $tax[ $i - 1 ][0] : 0; |
| 267 | + my $subset = 0; |
| 268 | + if ( $income >= $upto ) { $subset = $upto - $prev; } |
| 269 | + elsif ( $income >= $prev ) { $subset = $income - $prev; } |
| 270 | + my $subtax = $subset * ( $rate / 100 ); |
| 271 | + $total += $subtax; |
| 272 | + } |
| 273 | + return sprintf '%.02f', $total; |
| 274 | +} |
| 275 | +``` |
| 276 | + |
| 277 | +```text |
| 278 | +$ ./ch-2.pl |
| 279 | + Input: @income = 10, |
| 280 | + @tax = ([ 3, 50 ], [ 7, 10 ], [ 12, 25 ]) |
| 281 | + Output: 2.65 |
| 282 | +
|
| 283 | + Input: @income = 2, |
| 284 | + @tax = ([ 1, 0 ], [ 4, 25 ], [ 5, 50 ]) |
| 285 | + Output: 0.25 |
| 286 | +
|
| 287 | + Input: @income = 0, |
| 288 | + @tax = ([ 2, 50 ]) |
| 289 | + Output: 0.00 |
| 290 | +``` |
| 291 | + |
| 292 | +#### If you have any questions or comments, I would be glad to hear it. Ask me on [Mastodon](https://mastodon.xyz/@jacobydave) or [make an issue on my blog repo.](https://github.com/jacoby/jacoby.github.io) |
0 commit comments