diff options
| author | Packy Anderson <packy@cpan.org> | 2024-01-01 18:44:09 -0500 |
|---|---|---|
| committer | Packy Anderson <packy@cpan.org> | 2024-01-01 18:44:09 -0500 |
| commit | 81ef15374a3796bc9585e25b4e059dfb34c6304e (patch) | |
| tree | 2f41cdcb831680a1109e2a854a2ca0a6c20a94a3 | |
| parent | 5f38c976cae9103ec02e413224d047d8b149956d (diff) | |
| download | perlweeklychallenge-club-81ef15374a3796bc9585e25b4e059dfb34c6304e.tar.gz perlweeklychallenge-club-81ef15374a3796bc9585e25b4e059dfb34c6304e.tar.bz2 perlweeklychallenge-club-81ef15374a3796bc9585e25b4e059dfb34c6304e.zip | |
Challenge 250 solutions by Packy Anderson
* Raku
* Perl
* Python
1 Blog post
| -rw-r--r-- | challenge-250/packy-anderson/README.md | 68 | ||||
| -rw-r--r-- | challenge-250/packy-anderson/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-250/packy-anderson/perl/ch-1.pl | 24 | ||||
| -rwxr-xr-x | challenge-250/packy-anderson/perl/ch-2.pl | 28 | ||||
| -rwxr-xr-x | challenge-250/packy-anderson/python/ch-1.py | 24 | ||||
| -rwxr-xr-x | challenge-250/packy-anderson/python/ch-2.py | 28 | ||||
| -rwxr-xr-x | challenge-250/packy-anderson/raku/ch-1.raku | 24 | ||||
| -rwxr-xr-x | challenge-250/packy-anderson/raku/ch-2.raku | 26 |
8 files changed, 156 insertions, 67 deletions
diff --git a/challenge-250/packy-anderson/README.md b/challenge-250/packy-anderson/README.md index 6682f66637..36982b3432 100644 --- a/challenge-250/packy-anderson/README.md +++ b/challenge-250/packy-anderson/README.md @@ -3,83 +3,17 @@ ## Raku * [Task 1](raku/ch-1.raku) - -Sample output -``` -$ raku/ch-1.raku -Example 1: -Input: @ints = (3, 2, 3, 2, 2, 2) -Output: (3, 3), (2, 2), (2, 2) - -Example 2: -Input: @ints = (1, 2, 3, 4) -Output: () - -Example 3: -Input: @ints = (1, 2, 3, 4, 4, 3, 2, 1) -Output: (1, 1), (4, 4), (3, 3), (2, 2) -``` - * [Task 2](raku/ch-2.raku) -Sample output -``` -$ raku/ch-2.raku -Example 1: -Input: $str = 'IDID' -Output: (0, 4, 1, 3, 2) - -Example 2: -Input: $str = 'III' -Output: (0, 1, 2, 3) - -Example 3: -Input: $str = 'DDI' -Output: (3, 2, 0, 1) -``` - ## Perl * [Task 1](perl/ch-1.pl) - -Sample output -``` -$ perl/ch-1.pl -Example 1: -Input: @ints = (3, 2, 3, 2, 2, 2) -Output: (2, 2), (2, 2), (3, 3) - -Example 2: -Input: @ints = (1, 2, 3, 4) -Output: () - -Example 3: -Input: @ints = (1, 2, 3, 4, 4, 3, 2, 1) -Output: (4, 4), (3, 3), (2, 2), (1, 1) -``` - * [Task 2](perl/ch-2.pl) -Sample output -``` -$ perl/ch-2.pl -Example 1: -Input: $str = 'IDID' -Output: (0, 4, 1, 3, 2) - -Example 2: -Input: $str = 'III' -Output: (0, 1, 2, 3) - -Example 3: -Input: $str = 'DDI' -Output: (3, 2, 0, 1) -``` - ## Guest Language: Python * [Task 1](python/ch-1.py) * [Task 2](python/ch-2.py) ## Blog Post -[Perl Weekly Challenge: Stringy DI and Paired Equals](https://packy.dardan.com/b/Fv) +[Two-Hundred Fifty Perl Weekly Challenges! Two-Hundred Fifty problems so clear...](https://packy.dardan.com/b/G4) diff --git a/challenge-250/packy-anderson/blog.txt b/challenge-250/packy-anderson/blog.txt new file mode 100644 index 0000000000..df3e0f5f05 --- /dev/null +++ b/challenge-250/packy-anderson/blog.txt @@ -0,0 +1 @@ +https://packy.dardan.com/b/G4
\ No newline at end of file diff --git a/challenge-250/packy-anderson/perl/ch-1.pl b/challenge-250/packy-anderson/perl/ch-1.pl new file mode 100755 index 0000000000..6a96d08c32 --- /dev/null +++ b/challenge-250/packy-anderson/perl/ch-1.pl @@ -0,0 +1,24 @@ +#!/usr/bin/env perl +use v5.38; + +sub smallestIndex(@ints) { + foreach my $i ( 0 .. $#ints ) { + return $i if ($i % 10) == $ints[$i]; + } + return -1; +} + +sub solution(@ints) { + say 'Input: @ints = (' . join(', ', @ints) . ')'; + my $smallest = smallestIndex(@ints); + say "Output: $smallest"; +} + +say "Example 1:"; +solution(0, 1, 2); + +say "\nExample 2:"; +solution(4, 3, 2, 1); + +say "\nExample 3:"; +solution(1, 2, 3, 4, 5, 6, 7, 8, 9, 0);
\ No newline at end of file diff --git a/challenge-250/packy-anderson/perl/ch-2.pl b/challenge-250/packy-anderson/perl/ch-2.pl new file mode 100755 index 0000000000..a809e47886 --- /dev/null +++ b/challenge-250/packy-anderson/perl/ch-2.pl @@ -0,0 +1,28 @@ +#!/usr/bin/env perl +use v5.38; + +use List::Util qw( max ); + +sub alphanumValue($str) { + if ($str =~ /^\d+$/) { + return $str + 0; + } + return length($str); +} + +sub maxAlphanumValue(@alphanumstr) { + my @values = map { alphanumValue($_) } @alphanumstr; + return max(@values); +} + +sub solution(@alphanumstr) { + say 'Input: @alphanumstr = ("' . join('", "', @alphanumstr) . '")'; + my $maxValue = maxAlphanumValue(@alphanumstr); + say "Output: $maxValue"; +} + +say "Example 1:"; +solution("perl", "2", "000", "python", "r4ku"); + +say "\nExample 2:"; +solution("001", "1", "000", "0001");
\ No newline at end of file diff --git a/challenge-250/packy-anderson/python/ch-1.py b/challenge-250/packy-anderson/python/ch-1.py new file mode 100755 index 0000000000..4db4b59bd5 --- /dev/null +++ b/challenge-250/packy-anderson/python/ch-1.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python + +def smallestIndex(ints): + for i in range(len(ints)): + if (i % 10) == ints[i]: + return i + return -1 + +def comma_join(arr): + return ', '.join(map(lambda i: str(i), arr)) + +def solution(ints): + print(f'Input: @ints = ({comma_join(ints)})') + smallest = smallestIndex(ints) + print(f'Output: {smallest}') + +print('Example 1:') +solution([0, 1, 2]) + +print('\nExample 2:') +solution([4, 3, 2, 1]) + +print('\nExample 3:') +solution([1, 2, 3, 4, 5, 6, 7, 8, 9, 0])
\ No newline at end of file diff --git a/challenge-250/packy-anderson/python/ch-2.py b/challenge-250/packy-anderson/python/ch-2.py new file mode 100755 index 0000000000..b54a2c24d3 --- /dev/null +++ b/challenge-250/packy-anderson/python/ch-2.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python + +import re + +is_numeric = re.compile('^\d+$') + +def alphanumValue(strval): + if (is_numeric.match(strval)): + return int(strval) + return len(strval) + +def maxAlphanumValue(alphanumstr): + values = [ alphanumValue(x) for x in alphanumstr ] + return max(values) + +def comma_join(arr): + return '"' + '", "'.join(arr) + '"' + +def solution(alphanumstr): + print(f'Input: @alphanumstr = ({comma_join(alphanumstr)})') + maxValue = maxAlphanumValue(alphanumstr) + print(f'Output: {maxValue}') + +print('Example 1:') +solution(["perl", "2", "000", "python", "r4ku"]) + +print('\nExample 2:') +solution(["001", "1", "000", "0001"])
\ No newline at end of file diff --git a/challenge-250/packy-anderson/raku/ch-1.raku b/challenge-250/packy-anderson/raku/ch-1.raku new file mode 100755 index 0000000000..2f57207b1b --- /dev/null +++ b/challenge-250/packy-anderson/raku/ch-1.raku @@ -0,0 +1,24 @@ +#!/usr/bin/env raku +use v6; + +sub smallestIndex(@ints) { + for 0 .. @ints.end -> $i { + return $i if ($i mod 10) == @ints[$i]; + } + return -1; +} + +sub solution(@ints) { + say 'Input: @ints = (' ~ @ints.join(', ') ~ ')'; + my $smallest = smallestIndex(@ints); + say "Output: $smallest"; +} + +say "Example 1:"; +solution([0, 1, 2]); + +say "\nExample 2:"; +solution([4, 3, 2, 1]); + +say "\nExample 3:"; +solution([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]);
\ No newline at end of file diff --git a/challenge-250/packy-anderson/raku/ch-2.raku b/challenge-250/packy-anderson/raku/ch-2.raku new file mode 100755 index 0000000000..60fe1e3a34 --- /dev/null +++ b/challenge-250/packy-anderson/raku/ch-2.raku @@ -0,0 +1,26 @@ +#!/usr/bin/env raku +use v6; + +sub alphanumValue($str) { + if ($str ~~ /^\d+$/) { + return Int($str); + } + return $str.chars; +} + +sub maxAlphanumValue(@alphanumstr) { + my @values = @alphanumstr.map({ alphanumValue($_) }); + return @values.max; +} + +sub solution(@alphanumstr) { + say 'Input: @alphanumstr = ("' ~ @alphanumstr.join('", "') ~ '")'; + my $maxValue = maxAlphanumValue(@alphanumstr); + say "Output: $maxValue"; +} + +say "Example 1:"; +solution(["perl", "2", "000", "python", "r4ku"]); + +say "\nExample 2:"; +solution(["001", "1", "000", "0001"]);
\ No newline at end of file |
