diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-05-16 11:00:30 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-05-16 11:00:30 +0100 |
| commit | 6e2fa7dbd4128b06645e20e01417b452ea3de250 (patch) | |
| tree | 6c48a4414921aac5671e9f9ab19b7e04effdf4c2 | |
| parent | ad40e526ac68de0e68f6c151623eed5d50350de1 (diff) | |
| parent | 82c0eae234775425fe7e2f8829e37bcf83b4a4c5 (diff) | |
| download | perlweeklychallenge-club-6e2fa7dbd4128b06645e20e01417b452ea3de250.tar.gz perlweeklychallenge-club-6e2fa7dbd4128b06645e20e01417b452ea3de250.tar.bz2 perlweeklychallenge-club-6e2fa7dbd4128b06645e20e01417b452ea3de250.zip | |
Merge pull request #10100 from packy/master
Challenge 269 solutions by Packy Anderson
| -rw-r--r-- | challenge-269/packy-anderson/README.md | 2 | ||||
| -rw-r--r-- | challenge-269/packy-anderson/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-269/packy-anderson/elixir/ch-1.exs | 37 | ||||
| -rwxr-xr-x | challenge-269/packy-anderson/elixir/ch-2.exs | 45 | ||||
| -rwxr-xr-x | challenge-269/packy-anderson/perl/ch-1.pl | 34 | ||||
| -rwxr-xr-x | challenge-269/packy-anderson/perl/ch-2.pl | 36 | ||||
| -rwxr-xr-x | challenge-269/packy-anderson/python/ch-1.py | 33 | ||||
| -rwxr-xr-x | challenge-269/packy-anderson/python/ch-2.py | 34 | ||||
| -rwxr-xr-x | challenge-269/packy-anderson/raku/ch-1.raku | 34 | ||||
| -rwxr-xr-x | challenge-269/packy-anderson/raku/ch-2.raku | 36 |
10 files changed, 291 insertions, 1 deletions
diff --git a/challenge-269/packy-anderson/README.md b/challenge-269/packy-anderson/README.md index ef082e98a3..3f65972d0f 100644 --- a/challenge-269/packy-anderson/README.md +++ b/challenge-269/packy-anderson/README.md @@ -20,4 +20,4 @@ ## Blog Post -[Let's do the Numbers!](https://packy.dardan.com/b/L6) +[Bitwise Distribution](https://packy.dardan.com/b/LS) diff --git a/challenge-269/packy-anderson/blog.txt b/challenge-269/packy-anderson/blog.txt new file mode 100644 index 0000000000..4c9876db7e --- /dev/null +++ b/challenge-269/packy-anderson/blog.txt @@ -0,0 +1 @@ +https://packy.dardan.com/b/LS
\ No newline at end of file diff --git a/challenge-269/packy-anderson/elixir/ch-1.exs b/challenge-269/packy-anderson/elixir/ch-1.exs new file mode 100755 index 0000000000..a4bb4c9d60 --- /dev/null +++ b/challenge-269/packy-anderson/elixir/ch-1.exs @@ -0,0 +1,37 @@ +#!/usr/bin/env elixir + +defmodule PWC do + # if we only have one element, return false + def bitwisePair([_first | []]), do: false + + def bitwisePair([first | ints]) do + # bitwise OR the first element with + # each of the remaining elements and + # check if the result is even + found_pair = for next <- ints, + Bitwise.band(Bitwise.bor(first, next), 1) == 0, do: true + + # found_pair is either a list of true values + # or an empty list + if List.first(found_pair) do + List.first(found_pair) + else + # search the remaining list for pairs + bitwisePair(ints) + end + end + + def solution(ints) do + IO.puts("Input: @ints = (" <> Enum.join(ints, ", ") <> ")") + IO.puts("Output: " <> to_string(bitwisePair(ints))) + end +end + +IO.puts("Example 1:") +PWC.solution([1, 2, 3, 4, 5]) + +IO.puts("\nExample 2:") +PWC.solution([2, 3, 8, 16]) + +IO.puts("\nExample 3:") +PWC.solution([1, 2, 5, 7, 9]) diff --git a/challenge-269/packy-anderson/elixir/ch-2.exs b/challenge-269/packy-anderson/elixir/ch-2.exs new file mode 100755 index 0000000000..f450191f4d --- /dev/null +++ b/challenge-269/packy-anderson/elixir/ch-2.exs @@ -0,0 +1,45 @@ +#!/usr/bin/env elixir + +defmodule PWC do + # when the list is empty, return the two distribution arrays + def distributeElements([], arr1, arr2) do + {arr1, arr2} + end + + def distributeElements([i | ints], arr1, arr2) do + {arr1, arr2} = case List.last(arr1) > List.last(arr2) do + true -> { arr1 ++ [i], arr2 } + false -> { arr1, arr2 ++ [i] } + end + # recursively call distributeElements/3 + distributeElements(ints, arr1, arr2) + end + + def distributeElements(ints) do + # grab the first two elements off the given array + # and put them in the two distribution arrays + {arr1, ints} = Enum.split(ints, 1) + {arr2, ints} = Enum.split(ints, 1) + + # call distributeElements/3 to distribute the rest + {arr1, arr2} = distributeElements(ints, arr1, arr2) + + # concatenate the arrays together + List.flatten(arr1, arr2) + end + + def solution(ints) do + IO.puts("Input: @ints = (" <> Enum.join(ints, ", ") <> ")") + out = distributeElements(ints) + IO.puts("Output: (" <> Enum.join(out, ", ") <> ")" ) + end +end + +IO.puts("Example 1:") +PWC.solution([2, 1, 3, 4, 5]) + +IO.puts("\nExample 2:") +PWC.solution([3, 2, 4]) + +IO.puts("\nExample 3:") +PWC.solution([5, 4, 3 ,8]) diff --git a/challenge-269/packy-anderson/perl/ch-1.pl b/challenge-269/packy-anderson/perl/ch-1.pl new file mode 100755 index 0000000000..f1e296b488 --- /dev/null +++ b/challenge-269/packy-anderson/perl/ch-1.pl @@ -0,0 +1,34 @@ +#!/usr/bin/env perl +use v5.38; + +sub bitwisePair(@ints) { + my $first = shift @ints; + + # if we have no more elements to + # compare it to, return false + return 'False' if scalar(@ints) == 0; + + # bitwise OR the first element with + # each of the remaining elements and + # check if the result is even + foreach my $next ( @ints ) { + return 'True' if (($first | $next) & 1) == 0; + } + + # search the remaining list for pairs + return bitwisePair(@ints); +} + +sub solution($ints) { + say 'Input: @ints = (' . join(', ', @$ints) . ')'; + say 'Output: ' . bitwisePair(@$ints); +} + +say "Example 1:"; +solution([1, 2, 3, 4, 5]); + +say "\nExample 2:"; +solution([2, 3, 8, 16]); + +say "\nExample 3:"; +solution([1, 2, 5, 7, 9]); diff --git a/challenge-269/packy-anderson/perl/ch-2.pl b/challenge-269/packy-anderson/perl/ch-2.pl new file mode 100755 index 0000000000..2168667ae3 --- /dev/null +++ b/challenge-269/packy-anderson/perl/ch-2.pl @@ -0,0 +1,36 @@ +#!/usr/bin/env perl +use v5.38; + +sub distributeElements(@ints) { + # grab the first two elements off the given array + # and put them in the two distribution arrays + my @arr1 = ( shift @ints ); + my @arr2 = ( shift @ints ); + + # loop through the remaining elements of the given array + foreach my $i ( @ints ) { + if ($arr1[-1] > $arr2[-1]) { + push @arr1, $i; + } + else { + push @arr2, $i; + } + } + # concatenate the arrays together + return @arr1, @arr2; +} + +sub solution($ints) { + say 'Input: @ints = (' . join(', ', @$ints) . ')'; + my @out = distributeElements(@$ints); + say 'Output: (' . join(', ', @out) . ')'; +} + +say "Example 1:"; +solution([2, 1, 3, 4, 5]); + +say "\nExample 2:"; +solution([3, 2, 4]); + +say "\nExample 3:"; +solution([5, 4, 3 ,8]); diff --git a/challenge-269/packy-anderson/python/ch-1.py b/challenge-269/packy-anderson/python/ch-1.py new file mode 100755 index 0000000000..1ca354ee8c --- /dev/null +++ b/challenge-269/packy-anderson/python/ch-1.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python + +def bitwisePair(ints): + first = ints.pop(0) + + # if we have no more elements to + # compare it to, return false + if len(ints) == 0: return False + + # bitwise OR the first element with + # each of the remaining elements and + # check if the result is even + for next in ints: + if ((first | next) & 1) == 0: return True + + # search the remaining list for pairs + return bitwisePair(ints) + +def comma_join(arr): + return ', '.join(map(lambda i: str(i), arr)) + +def solution(ints): + print(f'Input: @ints = ({comma_join(ints)})') + print(f'Output: {bitwisePair(ints)}') + +print('Example 1:') +solution([1, 2, 3, 4, 5]) + +print('\nExample 2:') +solution([2, 3, 8, 16]) + +print('\nExample 3:') +solution([1, 2, 5, 7, 9]) diff --git a/challenge-269/packy-anderson/python/ch-2.py b/challenge-269/packy-anderson/python/ch-2.py new file mode 100755 index 0000000000..223eddc749 --- /dev/null +++ b/challenge-269/packy-anderson/python/ch-2.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python + +def distributeElements(ints): + # grab the first two elements off the given array + # and put them in the two distribution arrays + arr1 = [ ints.pop(0) ] + arr2 = [ ints.pop(0) ] + + # loop through the remaining elements of the given array + for i in ints: + if arr1[-1] > arr2[-1]: + arr1.append(i) + else: + arr2.append(i) + + # concatenate the arrays together + return arr1 + arr2 + +def comma_join(arr): + return ', '.join(map(lambda i: str(i), arr)) + +def solution(ints): + print(f'Input: @ints = ({comma_join(ints)})') + out = distributeElements(ints) + print(f'Output: ({comma_join(out)})') + +print('Example 1:') +solution([2, 1, 3, 4, 5]) + +print('\nExample 2:') +solution([3, 2, 4]) + +print('\nExample 3:') +solution([5, 4, 3 ,8])
\ No newline at end of file diff --git a/challenge-269/packy-anderson/raku/ch-1.raku b/challenge-269/packy-anderson/raku/ch-1.raku new file mode 100755 index 0000000000..96acb78ac1 --- /dev/null +++ b/challenge-269/packy-anderson/raku/ch-1.raku @@ -0,0 +1,34 @@ +#!/usr/bin/env raku +use v6; + +sub bitwisePair(@ints) { + my $first = @ints.shift; + + # if we have no more elements to + # compare it to, return false + return False if @ints.elems == 0; + + # bitwise OR the first element with + # each of the remaining elements and + # check if the result is even + for @ints -> $next { + return True if (($first +| $next) +& 1) == 0; + } + + # search the remaining list for pairs + return bitwisePair(@ints); +} + +sub solution(@ints) { + say 'Input: @ints = (' ~ @ints.join(', ') ~ ')'; + say 'Output: ' ~ bitwisePair(@ints); +} + +say "Example 1:"; +solution([1, 2, 3, 4, 5]); + +say "\nExample 2:"; +solution([2, 3, 8, 16]); + +say "\nExample 3:"; +solution([1, 2, 5, 7, 9]); diff --git a/challenge-269/packy-anderson/raku/ch-2.raku b/challenge-269/packy-anderson/raku/ch-2.raku new file mode 100755 index 0000000000..c8ab5ca9ee --- /dev/null +++ b/challenge-269/packy-anderson/raku/ch-2.raku @@ -0,0 +1,36 @@ +#!/usr/bin/env raku +use v6; + +sub distributeElements(@ints) { + # grab the first two elements off the given array + # and put them in the two distribution arrays + my @arr1 = ( @ints.shift ); + my @arr2 = ( @ints.shift ); + + # loop through the remaining elements of the given array + for @ints -> $i { + if (@arr1[*-1] > @arr2[*-1]) { + @arr1.push($i); + } + else { + @arr2.push($i); + } + } + # concatenate the arrays together + return flat(@arr1, @arr2); +} + +sub solution(@ints) { + say 'Input: @ints = (' ~ @ints.join(', ') ~ ')'; + my @out = distributeElements(@ints); + say 'Output: (' ~ @out.join(', ') ~ ')'; +} + +say "Example 1:"; +solution([2, 1, 3, 4, 5]); + +say "\nExample 2:"; +solution([3, 2, 4]); + +say "\nExample 3:"; +solution([5, 4, 3 ,8]); |
