Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion challenge-353/packy-anderson/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@


## Blog Post
[Perl Weekly Challenge: Doodling with matches and prefixes](https://packy.dardan.com/b/fi)
[Perl Weekly Challenge: Ok, swing code... SWING!](https://packy.dardan.com/b/g4)
1 change: 1 addition & 0 deletions challenge-353/packy-anderson/blog.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://packy.dardan.com/b/g4
37 changes: 37 additions & 0 deletions challenge-353/packy-anderson/elixir/ch-1.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env elixir

defmodule PWC do
def word_count(sentence) do
sentence |> String.split |> length
end

def max_words(sentences) do
sentences |> Enum.map(&word_count/1) |> Enum.max
end

def solution(sentences) do
joined = sentences
|> Enum.map(fn s -> "\"#{s}\"" end)
|> Enum.join(", ")
IO.puts("Input: @sentences = (#{joined})")
IO.puts("Output: #{max_words(sentences)}")
end
end

IO.puts("Example 1:")
PWC.solution(["Hello world", "This is a test", "Perl is great"])

IO.puts("\nExample 2:")
PWC.solution(["Single"])

IO.puts("\nExample 3:")
PWC.solution(["Short", "This song's just six words long",
"A B C", "Just four words here"])

IO.puts("\nExample 4:")
PWC.solution(["One", "Two parts", "Three part phrase", ""])

IO.puts("\nExample 5:")
PWC.solution(["The quick brown fox jumps over the lazy dog", "A",
"She sells seashells by the seashore",
"To be or not to be that is the question"])
71 changes: 71 additions & 0 deletions challenge-353/packy-anderson/elixir/ch-2.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/usr/bin/env elixir

defmodule PWC do
@good_names ["electronics", "grocery", "pharmacy", "restaurant"]

def check_coupon([code, name, status]) do
cond do
status == "true" and
Enum.any?(@good_names, fn n -> n == name end) and
Regex.match?(~r/^[a-zA-Z0-9_]+$/, code) -> "true"
true -> "false"
end
end

def validate_coupon(%{codes: codes, names: names,
status: status}) do
Enum.zip_with([codes, names, status], &check_coupon/1)
end

def quoted_list(items) do
items |> Enum.map(fn s -> "\"#{s}\"" end) |> Enum.join(", ")
end

def solution(%{codes: codes, names: names, status: status}) do
IO.puts("Input: @codes = (#{quoted_list(codes)})")
IO.puts(" @names = (#{quoted_list(names)})")
IO.puts(" @status = (#{quoted_list(status)})")
output = validate_coupon(%{codes: codes, names: names, status: status})
|> Enum.join(", ")
IO.puts("Output: (#{output})")
end
end

IO.puts("Example 1:")
PWC.solution(%{
codes: ["A123", "B_456", "C789", "D@1", "E123"],
names: ["electronics", "restaurant", "electronics",
"pharmacy", "grocery"],
status: ["true", "false", "true", "true", "true"]
})

IO.puts("\nExample 2:")
PWC.solution(%{
codes: ["Z_9", "AB_12", "G01", "X99", "test"],
names: ["pharmacy", "electronics", "grocery",
"electronics", "unknown"],
status: ["true", "true", "false", "true", "true"]
})

IO.puts("\nExample 3:")
PWC.solution(%{
codes: ["_123", "123", "", "Coupon_A", "Alpha"],
names: ["restaurant", "electronics", "electronics",
"pharmacy", "grocery"],
status: ["true", "true", "false", "true", "true"]
})

IO.puts("\nExample 4:")
PWC.solution(%{
codes: ["ITEM_1", "ITEM_2", "ITEM_3", "ITEM_4"],
names: ["electronics", "electronics", "grocery", "grocery"],
status: ["true", "true", "true", "true"]
})

IO.puts("\nExample 5:")
PWC.solution(%{
codes: ["CAFE_X", "ELEC_100", "FOOD_1", "DRUG_A", "ELEC_99"],
names: ["restaurant", "electronics", "grocery", "pharmacy",
"electronics"],
status: ["true", "true", "true", "true", "false"]
})
37 changes: 37 additions & 0 deletions challenge-353/packy-anderson/perl/ch-1.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env perl
use v5.40;

use List::AllUtils qw( max );

sub wordCount($sentence) {
scalar(split(/\s+/, $sentence));
}

sub maxWords(@sentences) {
max map { wordCount($_) } @sentences;
}

sub solution($sentences) {
my $joined = join(', ', map { qq{"$_"} } @$sentences);
say 'Input: @sentences = (' . $joined . ')';
say 'Output: ' . maxWords(@$sentences);
}

say "Example 1:";
solution(["Hello world", "This is a test", "Perl is great"]);

say "\nExample 2:";
solution(["Single"]);

say "\nExample 3:";
solution(["Short", "This song's just six words long",
"A B C", "Just four words here"]);

say "\nExample 4:";
solution(["One", "Two parts", "Three part phrase", ""]);

say "\nExample 5:";
solution(["The quick brown fox jumps over the lazy dog", "A",
"She sells seashells by the seashore",
"To be or not to be that is the question"]);

71 changes: 71 additions & 0 deletions challenge-353/packy-anderson/perl/ch-2.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/usr/bin/env perl
use v5.40;

use List::AllUtils qw( any zip_by );

my @good_names =
("electronics", "grocery", "pharmacy", "restaurant");

sub checkCoupon($code, $name, $status) {
return 'true' if $status eq 'true'
&& (any { $name eq $_ } @good_names)
&& $code =~ /^[a-zA-Z0-9_]+$/;
return 'false';
}

sub validateCoupon($params) {
zip_by {
checkCoupon(@_)
} $params->{codes}, $params->{names}, $params->{status};
}

sub quotedList($list) {
join(", ", map { qq{"$_"} } @$list);
}

sub solution($params) {
say "Input: \@codes = (@{[quotedList($params->{codes})]})";
say " \@names = (@{[quotedList($params->{names})]})";
say " \@status = (@{[quotedList($params->{status})]})";
my $output = join ", ", validateCoupon($params);
say "Output: ($output)";
}

say "Example 1:";
solution({
codes => ["A123", "B_456", "C789", 'D@1', "E123"],
names => ["electronics", "restaurant", "electronics",
"pharmacy", "grocery"],
status => ["true", "false", "true", "true", "true"]
});

say "\nExample 2:";
solution({
codes => ["Z_9", "AB_12", "G01", "X99", "test"],
names => ["pharmacy", "electronics", "grocery",
"electronics", "unknown"],
status => ["true", "true", "false", "true", "true"]
});

say "\nExample 3:";
solution({
codes => ["_123", "123", "", "Coupon_A", "Alpha"],
names => ["restaurant", "electronics", "electronics",
"pharmacy", "grocery"],
status => ["true", "true", "false", "true", "true"]
});

say "\nExample 4:";
solution({
codes => ["ITEM_1", "ITEM_2", "ITEM_3", "ITEM_4"],
names => ["electronics", "electronics", "grocery", "grocery"],
status => ["true", "true", "true", "true"]
});

say "\nExample 5:";
solution({
codes => ["CAFE_X", "ELEC_100", "FOOD_1", "DRUG_A", "ELEC_99"],
names => ["restaurant", "electronics", "grocery", "pharmacy",
"electronics"],
status => ["true", "true", "true", "true", "false"]
});
30 changes: 30 additions & 0 deletions challenge-353/packy-anderson/python/ch-1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env python

def word_count(sentences):
return len(sentences.split())

def max_words(sentences):
return max([word_count(s) for s in sentences])

def solution(sentences):
joined = ", ".join([f'"{s}"' for s in sentences])
print(f'Input: @sentences = ({joined})')
print(f'Output: {max_words(sentences)}')

print('Example 1:')
solution(["Hello world", "This is a test", "Perl is great"])

print('\nExample 2:')
solution(["Single"])

print('\nExample 3:')
solution(["Short", "This song's just six words long",
"A B C", "Just four words here"])

print('\nExample 4:')
solution(["One", "Two parts", "Three part phrase", ""])

print('\nExample 5:')
solution(["The quick brown fox jumps over the lazy dog", "A",
"She sells seashells by the seashore",
"To be or not to be that is the question"])
74 changes: 74 additions & 0 deletions challenge-353/packy-anderson/python/ch-2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/usr/bin/env python

import re

good_names = [
"electronics", "grocery", "pharmacy", "restaurant"
]

def check_coupon(code, name, status):
if (
status == 'true' and
name in good_names and
re.fullmatch(r'^[a-zA-Z0-9_]+$', code)
):
return 'true'
else:
return 'false'

def validate_coupon(params):
coupons = []
for t in zip(params["codes"],
params["names"],
params["status"]):
coupons.append(check_coupon(*t))
return coupons

def quoted_list(arr):
return ", ".join([f'"{s}"' for s in arr])

def solution(params):
print(f'Input: @codes = ({quoted_list(params["codes"])})')
print(f' @names = ({quoted_list(params["names"])})')
print(f' @status = ({quoted_list(params["status"])})')
output = ", ".join(validate_coupon(params))
print(f'Output: {output}')

print('Example 1:')
solution({
'codes': ["A123", "B_456", "C789", 'D@1', "E123"],
'names': ["electronics", "restaurant", "electronics",
"pharmacy", "grocery"],
'status': ["true", "false", "true", "true", "true"]
})

print('\nExample 2:')
solution({
'codes': ["Z_9", "AB_12", "G01", "X99", "test"],
'names': ["pharmacy", "electronics", "grocery",
"electronics", "unknown"],
'status': ["true", "true", "false", "true", "true"]
})

print('\nExample 3:')
solution({
'codes': ["_123", "123", "", "Coupon_A", "Alpha"],
'names': ["restaurant", "electronics", "electronics",
"pharmacy", "grocery"],
'status': ["true", "true", "false", "true", "true"]
})

print('\nExample 4:')
solution({
'codes': ["ITEM_1", "ITEM_2", "ITEM_3", "ITEM_4"],
'names': ["electronics", "electronics", "grocery", "grocery"],
'status': ["true", "true", "true", "true"]
})

print('\nExample 5:')
solution({
'codes': ["CAFE_X", "ELEC_100", "FOOD_1", "DRUG_A", "ELEC_99"],
'names': ["restaurant", "electronics", "grocery", "pharmacy",
"electronics"],
'status': ["true", "true", "true", "true", "false"]
})
34 changes: 34 additions & 0 deletions challenge-353/packy-anderson/raku/ch-1.raku
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env raku
use v6;

sub wordCount($sentence) {
$sentence.split(/\s+/).elems;
}

sub maxWords(@sentences) {
@sentences.map({ wordCount($_) }).max;
}

sub solution(@sentences) {
my $joined = @sentences.map({ qq{"$_"}}).join(', ');
say 'Input: @sentences = (' ~ $joined ~ ')';
say 'Output: ' ~ maxWords(@sentences);
}

say "Example 1:";
solution(["Hello world", "This is a test", "Perl is great"]);

say "\nExample 2:";
solution(["Single"]);

say "\nExample 3:";
solution(["Short", "This song's just six words long",
"A B C", "Just four words here"]);

say "\nExample 4:";
solution(["One", "Two parts", "Three part phrase", ""]);

say "\nExample 5:";
solution(["The quick brown fox jumps over the lazy dog", "A",
"She sells seashells by the seashore",
"To be or not to be that is the question"]);
Loading