From fd680e83ac92321f57666fb29ffebf8efc367004 Mon Sep 17 00:00:00 2001 From: Packy Anderson Date: Thu, 12 Jun 2025 20:45:17 -0400 Subject: Challenge 325 solutions by Packy Anderson * Raku that maybe looks like Raku, but mostly like Perl * Perl * Python that definitely looks like Perl * Elixir that looks kinda like Elixir 1 blog post --- challenge-325/packy-anderson/README.md | 2 +- challenge-325/packy-anderson/blog.txt | 1 + challenge-325/packy-anderson/elixir/ch-1.exs | 41 ++++++++++++++++++++++++++++ challenge-325/packy-anderson/elixir/ch-2.exs | 31 +++++++++++++++++++++ challenge-325/packy-anderson/perl/ch-1.pl | 37 +++++++++++++++++++++++++ challenge-325/packy-anderson/perl/ch-2.pl | 30 ++++++++++++++++++++ challenge-325/packy-anderson/python/ch-1.py | 35 ++++++++++++++++++++++++ challenge-325/packy-anderson/python/ch-2.py | 28 +++++++++++++++++++ challenge-325/packy-anderson/raku/ch-1.raku | 37 +++++++++++++++++++++++++ challenge-325/packy-anderson/raku/ch-2.raku | 28 +++++++++++++++++++ 10 files changed, 269 insertions(+), 1 deletion(-) create mode 100644 challenge-325/packy-anderson/blog.txt create mode 100755 challenge-325/packy-anderson/elixir/ch-1.exs create mode 100755 challenge-325/packy-anderson/elixir/ch-2.exs create mode 100755 challenge-325/packy-anderson/perl/ch-1.pl create mode 100755 challenge-325/packy-anderson/perl/ch-2.pl create mode 100755 challenge-325/packy-anderson/python/ch-1.py create mode 100755 challenge-325/packy-anderson/python/ch-2.py create mode 100755 challenge-325/packy-anderson/raku/ch-1.raku create mode 100755 challenge-325/packy-anderson/raku/ch-2.raku diff --git a/challenge-325/packy-anderson/README.md b/challenge-325/packy-anderson/README.md index e0b74a44c1..f8e5961b41 100644 --- a/challenge-325/packy-anderson/README.md +++ b/challenge-325/packy-anderson/README.md @@ -23,4 +23,4 @@ ## Blog Post -[Perl Weekly Challenge: Totally 2bular!](https://packy.dardan.com/b/W3) +[Perl Weekly Challenge: The Final Count One!](https://packy.dardan.com/b/WY) diff --git a/challenge-325/packy-anderson/blog.txt b/challenge-325/packy-anderson/blog.txt new file mode 100644 index 0000000000..9923411bab --- /dev/null +++ b/challenge-325/packy-anderson/blog.txt @@ -0,0 +1 @@ +https://packy.dardan.com/b/WY \ No newline at end of file diff --git a/challenge-325/packy-anderson/elixir/ch-1.exs b/challenge-325/packy-anderson/elixir/ch-1.exs new file mode 100755 index 0000000000..a48fce8dbb --- /dev/null +++ b/challenge-325/packy-anderson/elixir/ch-1.exs @@ -0,0 +1,41 @@ +#!/usr/bin/env elixir + +defmodule PWC do + def consecutiveOne([], _, max_c), do: max_c + + def consecutiveOne([current | remaining], consecutive, max_c) do + cond do + current == 1 -> + consecutiveOne( + remaining, consecutive + 1, + max(max_c, consecutive + 1) + ) + true -> + consecutiveOne(remaining, 0, max_c) + end + end + + def consecutiveOne(binary) do + consecutiveOne(binary, 0, 0) + end + + def solution(binary) do + IO.puts("Input: @ints = (" <> Enum.join(binary, ", ") <> ")") + IO.puts("Output: #{consecutiveOne(binary)}") + end +end + +IO.puts("Example 1:") +PWC.solution([0, 1, 1, 0, 1, 1, 1]) + +IO.puts("\nExample 2:") +PWC.solution([0, 0, 0, 0]) + +IO.puts("\nExample 3:") +PWC.solution([1, 0, 1, 0, 1, 1]) + +IO.puts("\nExample 4:") +PWC.solution([1, 1, 1, 0, 1, 1, 0]) + +IO.puts("\nExample 5:") +PWC.solution([1, 0, 1, 1, 0, 1, 0]) diff --git a/challenge-325/packy-anderson/elixir/ch-2.exs b/challenge-325/packy-anderson/elixir/ch-2.exs new file mode 100755 index 0000000000..0a912b9695 --- /dev/null +++ b/challenge-325/packy-anderson/elixir/ch-2.exs @@ -0,0 +1,31 @@ +#!/usr/bin/env elixir + +defmodule PWC do + def finalPrice([], discounts), do: discounts + + def finalPrice([current | prices], discounts) do + lower = Enum.find(prices, 0, fn p -> p <= current end) + finalPrice(prices, discounts ++ [current - lower]) + end + + def finalPrice(prices) do + finalPrice(prices, []) + end + + def solution(prices) do + price_list = "(" <> Enum.join(prices, ", ") <> ")" + IO.puts("Input: @prices = #{price_list}") + discounts = finalPrice(prices) + discount_list = "(" <> Enum.join(discounts, ", ") <> ")" + IO.puts("Output: #{discount_list}") + end +end + +IO.puts("Example 1:") +PWC.solution([8, 4, 6, 2, 3]) + +IO.puts("\nExample 2:") +PWC.solution([1, 2, 3, 4, 5]) + +IO.puts("\nExample 3:") +PWC.solution([7, 1, 1, 5]) diff --git a/challenge-325/packy-anderson/perl/ch-1.pl b/challenge-325/packy-anderson/perl/ch-1.pl new file mode 100755 index 0000000000..0a1c132ba0 --- /dev/null +++ b/challenge-325/packy-anderson/perl/ch-1.pl @@ -0,0 +1,37 @@ +#!/usr/bin/env perl +use v5.40; + +sub consecutiveOne(@binary) { + my $max = 0; + my $consecutive = 0; + for my $bit (@binary) { + if ($bit) { + $consecutive++; + $max = $consecutive if $consecutive > $max; + } + else { + $consecutive = 0; + } + } + return $max; +} + +sub solution($binary) { + say 'Input: @binary = (' . join(', ', @$binary) . ')'; + say 'Output: ' . consecutiveOne(@$binary); +} + +say "Example 1:"; +solution([0, 1, 1, 0, 1, 1, 1]); + +say "\nExample 2:"; +solution([0, 0, 0, 0]); + +say "\nExample 3:"; +solution([1, 0, 1, 0, 1, 1]); + +say "\nExample 4:"; +solution([1, 1, 1, 0, 1, 1, 0]); + +say "\nExample 5:"; +solution([1, 0, 1, 1, 0, 1, 0]); diff --git a/challenge-325/packy-anderson/perl/ch-2.pl b/challenge-325/packy-anderson/perl/ch-2.pl new file mode 100755 index 0000000000..6ddc4cb7a6 --- /dev/null +++ b/challenge-325/packy-anderson/perl/ch-2.pl @@ -0,0 +1,30 @@ +#!/usr/bin/env perl +use v5.40; + +use List::AllUtils qw( first ); + +sub finalPrice(@prices) { + my @discounts; + while (my $current = shift @prices) { + if (my $lower = first { $_ <= $current} @prices) { + $current -= $lower; + } + push @discounts, $current; + } + return @discounts; +} + +sub solution($prices) { + say 'Input: @prices = (' . join(', ', @$prices) . ')'; + my @discounts = finalPrice(@$prices); + say 'Output: (' . join(', ', @discounts) . ')'; +} + +say "Example 1:"; +solution([8, 4, 6, 2, 3]); + +say "\nExample 2:"; +solution([1, 2, 3, 4, 5]); + +say "\nExample 3:"; +solution([7, 1, 1, 5]); diff --git a/challenge-325/packy-anderson/python/ch-1.py b/challenge-325/packy-anderson/python/ch-1.py new file mode 100755 index 0000000000..788c6bb951 --- /dev/null +++ b/challenge-325/packy-anderson/python/ch-1.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python + +def consecutiveOne(binary): + max_c = 0 + consecutive = 0 + for bit in binary: + if bit == 1: + consecutive += 1 + if consecutive > max_c: max_c = consecutive + else: + consecutive = 0 + return max_c + +def int_join(joiner, arr): + return joiner.join(map(lambda i: str(i), arr)) + +def solution(binary): + print(f'Input: @binary = ({int_join(", ", binary)})') + print(f'Output: {consecutiveOne(binary)}') + + +print('Example 1:') +solution([0, 1, 1, 0, 1, 1, 1]) + +print('\nExample 2:') +solution([0, 0, 0, 0]) + +print('\nExample 3:') +solution([1, 0, 1, 0, 1, 1]) + +print('\nExample 4:') +solution([1, 1, 1, 0, 1, 1, 0]) + +print('\nExample 5:') +solution([1, 0, 1, 1, 0, 1, 0]) diff --git a/challenge-325/packy-anderson/python/ch-2.py b/challenge-325/packy-anderson/python/ch-2.py new file mode 100755 index 0000000000..e10d6230fe --- /dev/null +++ b/challenge-325/packy-anderson/python/ch-2.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python + +def finalPrice(prices): + discounts = [] + while prices: + current = prices.pop(0) + lower = next((p for p in prices if p <= current), 0) + current -= lower + discounts.append(current) + return discounts + +def int_join(joiner, arr): + return joiner.join(map(lambda i: str(i), arr)) + +def solution(prices): + print(f'Input: @nums = ({int_join(", ", prices)})') + discounts = finalPrice(prices) + print(f'Output: ({int_join(", ", discounts)})') + + +print('Example 1:') +solution([8, 4, 6, 2, 3]) + +print('\nExample 2:') +solution([1, 2, 3, 4, 5]) + +print('\nExample 3:') +solution([7, 1, 1, 5]) diff --git a/challenge-325/packy-anderson/raku/ch-1.raku b/challenge-325/packy-anderson/raku/ch-1.raku new file mode 100755 index 0000000000..ee6569548e --- /dev/null +++ b/challenge-325/packy-anderson/raku/ch-1.raku @@ -0,0 +1,37 @@ +#!/usr/bin/env raku +use v6; + +sub consecutiveOne(@binary) { + my $max = 0; + my $consecutive = 0; + for @binary -> $bit { + if ($bit) { + $consecutive++; + $max = $consecutive if $consecutive > $max; + } + else { + $consecutive = 0; + } + } + return $max; +} + +sub solution(@binary) { + say 'Input: @binary = (' ~ @binary.join(', ') ~ ')'; + say 'Output: ' ~ consecutiveOne(@binary); +} + +say "Example 1:"; +solution([0, 1, 1, 0, 1, 1, 1]); + +say "\nExample 2:"; +solution([0, 0, 0, 0]); + +say "\nExample 3:"; +solution([1, 0, 1, 0, 1, 1]); + +say "\nExample 4:"; +solution([1, 1, 1, 0, 1, 1, 0]); + +say "\nExample 5:"; +solution([1, 0, 1, 1, 0, 1, 0]); diff --git a/challenge-325/packy-anderson/raku/ch-2.raku b/challenge-325/packy-anderson/raku/ch-2.raku new file mode 100755 index 0000000000..213457138c --- /dev/null +++ b/challenge-325/packy-anderson/raku/ch-2.raku @@ -0,0 +1,28 @@ +#!/usr/bin/env raku +use v6; + +sub finalPrice(@prices) { + my @discounts; + while (my $current = @prices.shift) { + if (my $lower = @prices.first: * <= $current) { + $current -= $lower; + } + @discounts.push($current); + } + return @discounts; +} + +sub solution(@prices) { + say 'Input: @prices = (' ~ @prices.join(', ') ~ ')'; + my @discounts = finalPrice(@prices); + say 'Output: (' ~ @discounts.join(', ') ~ ')'; +} + +say "Example 1:"; +solution([8, 4, 6, 2, 3]); + +say "\nExample 2:"; +solution([1, 2, 3, 4, 5]); + +say "\nExample 3:"; +solution([7, 1, 1, 5]); -- cgit