aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-234/packy-anderson/README.md106
-rw-r--r--challenge-234/packy-anderson/blog.txt1
-rwxr-xr-xchallenge-234/packy-anderson/perl/ch-1.pl75
-rwxr-xr-xchallenge-234/packy-anderson/perl/ch-2.pl42
-rwxr-xr-xchallenge-234/packy-anderson/python/ch-1.py66
-rwxr-xr-xchallenge-234/packy-anderson/python/ch-2.py35
-rwxr-xr-xchallenge-234/packy-anderson/raku/ch-1.raku71
-rwxr-xr-xchallenge-234/packy-anderson/raku/ch-2.raku41
8 files changed, 437 insertions, 0 deletions
diff --git a/challenge-234/packy-anderson/README.md b/challenge-234/packy-anderson/README.md
new file mode 100644
index 0000000000..496619a8dc
--- /dev/null
+++ b/challenge-234/packy-anderson/README.md
@@ -0,0 +1,106 @@
+# Solutions by Packy Anderson
+
+## Perl
+
+* [Task 1](perl/ch-1.pl)
+
+Sample output
+```
+$ perl/ch-1.pl
+Example 1:
+Input: @words = ("java", "javascript", "julia")
+Output: ("j", "a")
+
+Example 2:
+Input: @words = ("bella", "label", "roller")
+Output: ("e", "l", "l")
+
+Example 3:
+Input: @words = ("cool", "lock", "cook")
+Output: ("c", "o")
+```
+
+* [Task 2](perl/ch-2.pl)
+
+Sample output
+```
+$ perl/ch-2.pl
+Example 1:
+Input: @ints = (4, 4, 2, 4, 3)
+Output: 3
+
+(0, 2, 4) because 4 != 2 != 3
+(1, 2, 4) because 4 != 2 != 3
+(2, 3, 4) because 2 != 4 != 3
+
+Example 2:
+Input: @ints = (1, 1, 1, 1, 1)
+Output: 0
+
+Example 3:
+Input: @ints = (4, 7, 1, 10, 7, 4, 1, 1)
+Output: 28
+
+(0, 1, 2) because 4 != 7 != 1
+(0, 1, 3) because 4 != 7 != 10
+(0, 1, 6) because 4 != 7 != 1
+(0, 1, 7) because 4 != 7 != 1
+(0, 2, 3) because 4 != 1 != 10
+(0, 2, 4) because 4 != 1 != 7
+(0, 3, 4) because 4 != 10 != 7
+(0, 3, 6) because 4 != 10 != 1
+(0, 3, 7) because 4 != 10 != 1
+(0, 4, 6) because 4 != 7 != 1
+(0, 4, 7) because 4 != 7 != 1
+(1, 2, 3) because 7 != 1 != 10
+(1, 2, 5) because 7 != 1 != 4
+(1, 3, 5) because 7 != 10 != 4
+(1, 3, 6) because 7 != 10 != 1
+(1, 3, 7) because 7 != 10 != 1
+(1, 5, 6) because 7 != 4 != 1
+(1, 5, 7) because 7 != 4 != 1
+(2, 3, 4) because 1 != 10 != 7
+(2, 3, 5) because 1 != 10 != 4
+(2, 4, 5) because 1 != 7 != 4
+(3, 4, 5) because 10 != 7 != 4
+(3, 4, 6) because 10 != 7 != 1
+(3, 4, 7) because 10 != 7 != 1
+(3, 5, 6) because 10 != 4 != 1
+(3, 5, 7) because 10 != 4 != 1
+(4, 5, 6) because 7 != 4 != 1
+(4, 5, 7) because 7 != 4 != 1
+```
+
+## Raku
+
+* [Task 1](raku/ch-1.raku)
+
+Sample output
+```
+$ raku/ch-1.raku
+Example 1:
+Input: @words = ("java", "javascript", "julia")
+Output: ("j", "a")
+
+Example 2:
+Input: @words = ("bella", "label", "roller")
+Output: ("e", "l", "l")
+
+Example 3:
+Input: @words = ("cool", "lock", "cook")
+Output: ("c", "o")
+```
+
+* [Task 2](raku/ch-2.raku)
+
+Sample output
+```
+```
+
+## Guest Language: Python
+* [Task 1](python/ch-1.py)
+* [Task 2](python/ch-1.py)
+
+## Blog Post
+
+[Perl Weekly Challenge: Common, but Unequal, Triplet Characters](https://packy.dardan.com/2023/09/11/perl-weekly-challenge-common-but-unequal-triplet-characters/) \ No newline at end of file
diff --git a/challenge-234/packy-anderson/blog.txt b/challenge-234/packy-anderson/blog.txt
new file mode 100644
index 0000000000..0475d41383
--- /dev/null
+++ b/challenge-234/packy-anderson/blog.txt
@@ -0,0 +1 @@
+https://packy.dardan.com/2023/09/11/perl-weekly-challenge-common-but-unequal-triplet-characters/ \ No newline at end of file
diff --git a/challenge-234/packy-anderson/perl/ch-1.pl b/challenge-234/packy-anderson/perl/ch-1.pl
new file mode 100755
index 0000000000..11f455501b
--- /dev/null
+++ b/challenge-234/packy-anderson/perl/ch-1.pl
@@ -0,0 +1,75 @@
+#!/usr/bin/env perl
+
+use v5.38;
+
+use List::Util qw( min );
+
+sub charFrequency {
+ my $word = shift;
+ my %freq;
+ foreach my $c ( split //, $word ) {
+ $freq{$c}++;
+ }
+ return \%freq; # return a hash REFERENCE
+}
+
+sub commonCharacters {
+ my @words = @_;
+ my @freq = map { charFrequency($_) } @words;
+ # grab the character frequency map for the first word
+ my $first = shift @freq;
+ # now check the characters in the first word against
+ # the characters in all the subsequent words
+ foreach my $subsequent ( @freq ) {
+ foreach my $c ( keys %$first ) {
+ if (! exists $subsequent->{$c}) {
+ # this character isn't in subsequent words,
+ # so let's remove it from the frequency map
+ # of the first word
+ delete $first->{$c};
+ }
+ else {
+ # the character IS in subsequent words,
+ # so let's set the frequency count to be
+ # the minimum count found in those words
+ $first->{$c} = min($first->{$c}, $subsequent->{$c});
+ }
+ }
+ }
+
+ # now we generate a list of characters in the order they
+ # appear in the first word
+ my @output;
+ # once again, loop over the characters in the first word
+ foreach my $c ( split //, $words[0] ) {
+ next unless exists $first->{$c};
+ if ($first->{$c} > 1) {
+ # there's more than one occurence, so let's decrement
+ # the count for the next time through the loop
+ $first->{$c}--;
+ }
+ else {
+ # there is only one occurence left, so remove the
+ # character
+ delete $first->{$c};
+ }
+ push @output, $c;
+ }
+ return @output;
+}
+
+sub solution {
+ my @words = @_;
+ say 'Input: @words = ("' . join('", "', @words) . '")';
+ my @output = commonCharacters(@words);
+ say 'Output: ("' . join('", "', @output) . '")';
+}
+
+say "Example 1:";
+solution("java", "javascript", "julia");
+
+say "\nExample 2:";
+solution("bella", "label", "roller");
+
+say "\nExample 3:";
+solution("cool", "lock", "cook");
diff --git a/challenge-234/packy-anderson/perl/ch-2.pl b/challenge-234/packy-anderson/perl/ch-2.pl
new file mode 100755
index 0000000000..15cdc2cdd0
--- /dev/null
+++ b/challenge-234/packy-anderson/perl/ch-2.pl
@@ -0,0 +1,42 @@
+#!/usr/bin/env perl
+
+use v5.38;
+
+sub findTriplets {
+ my @ints = @_;
+ my @solutions;
+ foreach my $i ( 0 .. $#ints - 2 ) {
+ foreach my $j ( $i+1 .. $#ints - 1 ) {
+ foreach my $k ( $j+1 .. $#ints ) {
+ if ($ints[$i] != $ints[$j] &&
+ $ints[$j] != $ints[$k] &&
+ $ints[$i] != $ints[$k]) {
+ push @solutions, [$i, $j, $k];
+ }
+ }
+ }
+ }
+ return @solutions;
+}
+
+sub solution {
+ my @ints = @_;
+ say 'Input: @ints = (' . join(', ', @ints) . ')';
+ my @solutions = findTriplets(@ints);
+ say 'Output: ' . scalar(@solutions);
+ say "" if @solutions;
+ foreach my $triplet ( @solutions ) {
+ my($i, $j, $k) = @$triplet;
+ say "($i, $j, $k) because "
+ . "$ints[$i] != $ints[$j] != $ints[$k]";
+ }
+}
+
+say "Example 1:";
+solution(4, 4, 2, 4, 3);
+
+say "\nExample 2:";
+solution(1, 1, 1, 1, 1);
+
+say "\nExample 3:";
+solution(4, 7, 1, 10, 7, 4, 1, 1); \ No newline at end of file
diff --git a/challenge-234/packy-anderson/python/ch-1.py b/challenge-234/packy-anderson/python/ch-1.py
new file mode 100755
index 0000000000..ddfc986b12
--- /dev/null
+++ b/challenge-234/packy-anderson/python/ch-1.py
@@ -0,0 +1,66 @@
+#!/usr/bin/env python
+
+from collections import Counter
+
+def charFrequency(word):
+ # https://docs.python.org/3/library/collections.html#counter-objects
+ freq = Counter()
+ for c in word:
+ freq[c] += 1
+ return freq
+
+def commonCharacters(words):
+ # get the character freqencies for each word
+ freq = list(map(charFrequency, words))
+
+ # grab the character frequency map for the first word
+ first = freq.pop(0)
+
+ # make a copy of the dictionary since we'll
+ # be modifying it in the loop
+ first_orig = dict(first)
+
+ # now check the characters in the first word against
+ # the characters in all the subsequent words
+ for subsequent in freq:
+ for c in first_orig:
+ if c not in subsequent:
+ # this character isn't in subsequent words,
+ # so let's remove it from the frequency map
+ # of the first word
+ first.pop(c)
+ else:
+ # the character IS in subsequent words,
+ # so let's set the frequency count to be
+ # the minimum count found in those words
+ first[c] = min(first[c], subsequent[c])
+
+ # now we generate a list of characters in the order they
+ # appear in the first word
+ output = []
+ # once again, loop over the characters in the first word
+ for c in words[0]:
+ if c not in first:
+ continue
+ if first[c] > 1:
+ first[c] -= 1
+ else:
+ first.pop(c)
+ output.append(c)
+ return output
+
+def solution(words):
+ quoted = '"' + '", "'.join(words) + '"'
+ print(f'Input: @words = ({quoted})')
+ output = commonCharacters(words)
+ quoted = '"' + '", "'.join(output) + '"'
+ print(f'Output: ({quoted})')
+
+print("Example 1:")
+solution(["java", "javascript", "julia"])
+
+print("\nExample 2:")
+solution(["bella", "label", "roller"])
+
+print("\nExample 3:")
+solution(["cool", "lock", "cook"]) \ No newline at end of file
diff --git a/challenge-234/packy-anderson/python/ch-2.py b/challenge-234/packy-anderson/python/ch-2.py
new file mode 100755
index 0000000000..9024561157
--- /dev/null
+++ b/challenge-234/packy-anderson/python/ch-2.py
@@ -0,0 +1,35 @@
+#!/usr/bin/env python
+
+def findTriplets(ints):
+ solutions = []
+ for i in range(0, len(ints) - 3 ):
+ for j in range(i + 1, len(ints) - 2):
+ for k in range(j + 1, len(ints) - 1):
+ if (ints[i] != ints[j] and
+ ints[j] != ints[k] and
+ ints[i] != ints[k]):
+ solutions.append([i, j, k])
+ return solutions
+
+def solution(ints):
+ intlist = ", ".join([ str(i) for i in ints ])
+ print(f'Input: @ints = ({intlist})')
+ solutions = findTriplets(ints)
+ print(f'Output: {len(solutions)}')
+ if solutions:
+ print("")
+ for triplet in solutions:
+ i, j, k = triplet
+ print(
+ f"({i}, {j}, {k}) because " +
+ f"{ints[i]} != {ints[j]} != {ints[k]}"
+ )
+
+print("Example 1:")
+solution([4, 4, 2, 4, 3])
+
+print("\nExample 2:")
+solution([1, 1, 1, 1, 1])
+
+print("\nExample 3:")
+solution([4, 7, 1, 10, 7, 4, 1, 1]) \ No newline at end of file
diff --git a/challenge-234/packy-anderson/raku/ch-1.raku b/challenge-234/packy-anderson/raku/ch-1.raku
new file mode 100755
index 0000000000..5afdb22773
--- /dev/null
+++ b/challenge-234/packy-anderson/raku/ch-1.raku
@@ -0,0 +1,71 @@
+#!/usr/bin/env raku
+
+use v6;
+
+sub charFrequency(Str $word) {
+ my %freq;
+ for $word.split('', :skip-empty) -> $c {
+ %freq{$c}++;
+ }
+ return %freq;
+}
+
+sub commonCharacters(*@words where ($_.all ~~ Str)) {
+ my @freq = @words.map({ charFrequency($_) });
+ # grab the character frequency map for the first word
+ my $first = shift @freq;
+ # now check the characters in the first word against
+ # the characters in all the subsequent words
+ for @freq -> $subsequent {
+ for $first.keys() -> $c {
+ if ($subsequent{$c}:!exists) {
+ # this character isn't in subsequent words,
+ # so let's remove it from the frequency map
+ # of the first word
+ $first{$c}:delete;
+ }
+ else {
+ # the character IS in subsequent words,
+ # so let's set the frequency count to be
+ # the minimum count found in those words
+ $first{$c} = min($first{$c}, $subsequent{$c});
+ }
+ }
+ }
+
+ # now we generate a list of characters in the order they
+ # appear in the first word
+ my @output;
+ # once again, loop over the characters in the first word
+ for @words[0].split('', :skip-empty) -> $c {
+ next unless $first{$c}:exists;
+ if ($first{$c} > 1) {
+ # there's more than one occurence, so let's decrement
+ # the count for the next time through the loop
+ $first{$c}--;
+ }
+ else {
+ # there is only one occurence left, so remove the
+ # character
+ $first{$c}:delete;
+ }
+ push @output, $c;
+ }
+ return @output;
+}
+
+sub solution {
+ my @words = @_;
+ say 'Input: @words = ("' ~ @words.join('", "') ~ '")';
+ my @output = commonCharacters(@words);
+ say 'Output: ("' ~ @output.join('", "') ~ '")';
+}
+
+say "Example 1:";
+solution("java", "javascript", "julia");
+
+say "\nExample 2:";
+solution("bella", "label", "roller");
+
+say "\nExample 3:";
+solution("cool", "lock", "cook"); \ No newline at end of file
diff --git a/challenge-234/packy-anderson/raku/ch-2.raku b/challenge-234/packy-anderson/raku/ch-2.raku
new file mode 100755
index 0000000000..613b9d57d3
--- /dev/null
+++ b/challenge-234/packy-anderson/raku/ch-2.raku
@@ -0,0 +1,41 @@
+#!/usr/bin/env raku
+
+use v6;
+
+sub findTriplets(@ints where ($_.all ~~ Int)) {
+ my @solutions;
+ for 0 .. @ints.elems - 3 -> $i {
+ for $i + 1 .. @ints.elems - 2 -> $j {
+ for $j + 1 .. @ints.elems - 1 -> $k {
+ if (@ints[$i] != @ints[$j] &&
+ @ints[$j] != @ints[$k] &&
+ @ints[$i] != @ints[$k]) {
+ push @solutions, [$i, $j, $k];
+ }
+ }
+ }
+ }
+ return @solutions;
+}
+
+sub solution {
+ my @ints = @_;
+ say 'Input: @ints = (' ~ @ints.join(', ') ~ ')';
+ my @solutions = findTriplets(@ints);
+ say 'Output: ' ~ @solutions.elems;
+ say "" if @solutions;
+ for @solutions -> @triplet {
+ my ($i, $j, $k) = @triplet;
+ say "($i, $j, $k) because "
+ ~ "@ints[$i] != @ints[$j] != @ints[$k]";
+ }
+}
+
+say "Example 1:";
+solution(4, 4, 2, 4, 3);
+
+say "\nExample 2:";
+solution(1, 1, 1, 1, 1);
+
+say "\nExample 3:";
+solution(4, 7, 1, 10, 7, 4, 1, 1); \ No newline at end of file