aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-06-23 16:59:00 +0100
committerGitHub <noreply@github.com>2024-06-23 16:59:00 +0100
commit6cfc29a9990d7e73fdbae6091bb05084556f2712 (patch)
treeb8a701d7167db73728e29b1937668ad077cda62f
parent199682fe258bf09032b4fe1e94c1005201113900 (diff)
parent82ec0395303f0d55fbffc346063a19e8566dde59 (diff)
downloadperlweeklychallenge-club-6cfc29a9990d7e73fdbae6091bb05084556f2712.tar.gz
perlweeklychallenge-club-6cfc29a9990d7e73fdbae6091bb05084556f2712.tar.bz2
perlweeklychallenge-club-6cfc29a9990d7e73fdbae6091bb05084556f2712.zip
Merge pull request #10291 from packy/master
Challenge 274 solutions by Packy Anderson
-rw-r--r--challenge-274/packy-anderson/README.md2
-rw-r--r--challenge-274/packy-anderson/blog.txt1
-rwxr-xr-xchallenge-274/packy-anderson/elixir/ch-1.exs46
-rwxr-xr-xchallenge-274/packy-anderson/elixir/ch-2.exs20
-rwxr-xr-xchallenge-274/packy-anderson/perl/ch-1.pl39
-rwxr-xr-xchallenge-274/packy-anderson/perl/ch-2.pl83
-rwxr-xr-xchallenge-274/packy-anderson/python/ch-1.py36
-rwxr-xr-xchallenge-274/packy-anderson/python/ch-2.py66
-rwxr-xr-xchallenge-274/packy-anderson/raku/ch-1.raku38
-rwxr-xr-xchallenge-274/packy-anderson/raku/ch-2.raku80
10 files changed, 410 insertions, 1 deletions
diff --git a/challenge-274/packy-anderson/README.md b/challenge-274/packy-anderson/README.md
index 5daf97b693..30f565cde1 100644
--- a/challenge-274/packy-anderson/README.md
+++ b/challenge-274/packy-anderson/README.md
@@ -22,4 +22,4 @@
## Blog Post
-[Time to count B chars](https://packy.dardan.com/b/Md)
+[Bus Route, Bus Goat, Under My Umbrellamaaaaaaaaaaaa](https://packy.dardan.com/b/Mq)
diff --git a/challenge-274/packy-anderson/blog.txt b/challenge-274/packy-anderson/blog.txt
new file mode 100644
index 0000000000..f39d82abfc
--- /dev/null
+++ b/challenge-274/packy-anderson/blog.txt
@@ -0,0 +1 @@
+https://packy.dardan.com/b/Mq \ No newline at end of file
diff --git a/challenge-274/packy-anderson/elixir/ch-1.exs b/challenge-274/packy-anderson/elixir/ch-1.exs
new file mode 100755
index 0000000000..112774c2d5
--- /dev/null
+++ b/challenge-274/packy-anderson/elixir/ch-1.exs
@@ -0,0 +1,46 @@
+#!/usr/bin/env elixir
+
+defmodule PWC do
+ @startsWithVowel Regex.compile!("^[aeiou]", "i")
+
+ def addAs([], list, _), do: list
+
+ def addAs([first | rest], list, a_count) do
+ list = list ++ [ first <> String.duplicate("a", a_count) ]
+ addAs(rest, list, a_count + 1)
+ end
+
+ def goatWord(word) do
+ if Regex.run(@startsWithVowel, word) do
+ word <> "ma"
+ else
+ first = String.first(word)
+ rest = String.slice(word, 1 .. -1//1)
+ rest <> first <> "ma"
+ end
+ end
+
+ def goatLatin(sentence) do
+ String.split(sentence)
+ |> Enum.map(&goatWord/1)
+ |> addAs([], 1)
+ |> Enum.join(" ")
+ end
+
+ def solution(sentence) do
+ IO.puts("Input: $sentence = \"#{ sentence }\"")
+ IO.puts("Output: \"#{ goatLatin(sentence) }\"")
+ end
+end
+
+IO.puts("Example 1:")
+PWC.solution("I love Perl")
+
+IO.puts("\nExample 2:")
+PWC.solution("Perl and Raku are friends")
+
+IO.puts("\nExample 3:")
+PWC.solution("The Weekly Challenge")
+
+IO.puts("\nExample 4:")
+PWC.solution("Bus stop Bus goes She stays Love grows Under my umbrella")
diff --git a/challenge-274/packy-anderson/elixir/ch-2.exs b/challenge-274/packy-anderson/elixir/ch-2.exs
new file mode 100755
index 0000000000..04b5c8529b
--- /dev/null
+++ b/challenge-274/packy-anderson/elixir/ch-2.exs
@@ -0,0 +1,20 @@
+#!/usr/bin/env elixir
+
+defmodule PWC do
+
+ def solution(ints) do
+ IO.puts("Input: @ints = (" <> Enum.join(ints, ", ") <> ")")
+ {sign, explain} = PWC.productSign(ints)
+ IO.puts("Output: " <> to_string(sign) )
+ IO.puts("\n" <> explain)
+ end
+end
+
+IO.puts("Example 1:")
+PWC.solution()
+
+IO.puts("\nExample 2:")
+PWC.solution()
+
+IO.puts("\nExample 3:")
+PWC.solution()
diff --git a/challenge-274/packy-anderson/perl/ch-1.pl b/challenge-274/packy-anderson/perl/ch-1.pl
new file mode 100755
index 0000000000..bfe808d86d
--- /dev/null
+++ b/challenge-274/packy-anderson/perl/ch-1.pl
@@ -0,0 +1,39 @@
+#!/usr/bin/env perl
+use v5.40;
+
+my $startsWithVowel = qr/ ^[aeiou] /ix;
+
+sub goatLatin($sentence) {
+ my @words = split /\s+/, $sentence;
+ my $a_count = 1;
+ foreach my $word ( @words ) {
+ if ($word =~ /$startsWithVowel/) {
+ $word .= 'ma';
+ }
+ else {
+ my $first = substr($word, 0, 1);
+ my $rest = substr($word, 1);
+ $word = $rest . $first . 'ma';
+ }
+ $word .= 'a' x $a_count++;
+ }
+ return join(' ', @words);
+}
+
+sub solution($sentence) {
+ say 'Input: $sentence = "' . $sentence . '"';
+ say 'Output: "' . goatLatin($sentence) . '"';
+}
+
+say "Example 1:";
+solution("I love Perl");
+
+say "\nExample 2:";
+solution("Perl and Raku are friends");
+
+say "\nExample 3:";
+solution("The Weekly Challenge");
+
+say "\nExample 4:";
+solution("Bus stop Bus goes She stays Love grows Under my umbrella");
+
diff --git a/challenge-274/packy-anderson/perl/ch-2.pl b/challenge-274/packy-anderson/perl/ch-2.pl
new file mode 100755
index 0000000000..a5e5983deb
--- /dev/null
+++ b/challenge-274/packy-anderson/perl/ch-2.pl
@@ -0,0 +1,83 @@
+#!/usr/bin/env perl
+use v5.40;
+
+use List::Util qw( mesh uniq );
+
+sub routeTimes($route) {
+ my ($interval, $start, $duration) = @$route;
+ my %times;
+ while ($start < 60 + $interval) {
+ $times{$start} = $start + $duration;
+ $start += $interval;
+ }
+ return %times;
+}
+
+sub faster(@routes) {
+ # find the arrival times for each route's departures
+ my %times;
+ foreach my $route ( @routes ) {
+ foreach my($start, $arrive) ( routeTimes($route) ) {
+ if (exists $times{$start} && $times{$start} > $arrive) {
+ # this departure for this route will arrive sooner than
+ # the departure for the earlier route, keep this one!
+ $times{$start} = $arrive;
+ }
+ elsif (! exists $times{$start}) {
+ # no route we've seen has a departure at this time
+ $times{$start} = $arrive;
+ }
+ }
+ }
+
+ # now look at all the departures and see if there are
+ # later departures with earlier arrivals
+ my @bad_starts;
+ my @starts = sort { $a <=> $b } keys %times;
+ foreach my($pos, $start) ( mesh [0 .. $#starts], \@starts ) {
+ foreach my $i (@starts[$pos+1 .. $#starts]) {
+ if ($times{$i} < $times{$start} ) {
+ # we found a later departure with an earlier arrival!
+ push @bad_starts, $start;
+ }
+ }
+ }
+ @bad_starts = uniq @bad_starts;
+
+ # now determine which minutes of the hour we should say
+ # "skip the next bus and take the later one"
+ my @skip;
+ foreach my $min ( 0 .. 59 ) {
+ if (@starts && @bad_starts && $starts[0] == $bad_starts[0]) {
+ # we're in a bad window!
+ push @skip, $min;
+ }
+ if (@starts && $starts[0] == $min) {
+ shift @starts; # remove the start time
+ }
+ if (@bad_starts && $bad_starts[0] == $min) {
+ shift @bad_starts; # remove the bad start time
+ }
+ }
+ return @skip;
+}
+
+sub routeJoin(@routes) {
+ my @out;
+ foreach my $route ( @routes ) {
+ push @out, '[' . join(', ', @$route) . ']';
+ }
+ return '[ ' . join(', ', @out) . ' ]'
+}
+
+sub solution(@routes) {
+ say 'Input: ' . routeJoin(@routes);
+ say 'Output: [' . join(', ', faster(@routes)) . ']';
+}
+
+say "Example 1:";
+solution([12, 11, 41], [15, 5, 35]);
+
+say "\nExample 2:";
+solution([12, 3, 41], [15, 9, 35], [30, 5, 25]);
+
diff --git a/challenge-274/packy-anderson/python/ch-1.py b/challenge-274/packy-anderson/python/ch-1.py
new file mode 100755
index 0000000000..3a1c513295
--- /dev/null
+++ b/challenge-274/packy-anderson/python/ch-1.py
@@ -0,0 +1,36 @@
+#!/usr/bin/env python
+
+import re
+
+startsWithVowel = re.compile(r'^[aeiou]', re.I)
+
+def goatLatin(sentence):
+ words = sentence.split()
+ new_words = []
+ a_count = 1
+ for word in words:
+ if re.search(startsWithVowel, word):
+ new_words.append(word + 'ma')
+ else:
+ first = word[0:1]
+ rest = word[1:]
+ new_words.append(rest + first + 'ma')
+ new_words[-1] += 'a' * a_count
+ a_count += 1
+ return ' '.join(new_words)
+
+def solution(sentence):
+ print(f'Input: $sentence = "{ sentence }"')
+ print(f'Output: "{ goatLatin(sentence) }"')
+
+print('Example 1:')
+solution("I love Perl")
+
+print('\nExample 2:')
+solution("Perl and Raku are friends")
+
+print('\nExample 3:')
+solution("The Weekly Challenge")
+
+print('\nExample 4:')
+solution("Bus stop Bus goes She stays Love grows Under my umbrella")
diff --git a/challenge-274/packy-anderson/python/ch-2.py b/challenge-274/packy-anderson/python/ch-2.py
new file mode 100755
index 0000000000..24bd26ed4e
--- /dev/null
+++ b/challenge-274/packy-anderson/python/ch-2.py
@@ -0,0 +1,66 @@
+#!/usr/bin/env python
+
+def routeTimes(route):
+ (interval, start, duration) = route
+ times = {}
+ while (start < 60 + interval):
+ times[start] = start + duration
+ start += interval
+ return times
+
+def faster(routes):
+ # find the arrival times for each route's departures
+ times = {}
+ for route in routes:
+ for start, arrive in routeTimes(route).items():
+ if (start in times) and (times[start] > arrive):
+ # this departure for this route will arrive sooner than
+ # the departure for the earlier route, keep this one!
+ times[start] = arrive
+ elif (start not in times):
+ # no route we've seen has a departure at this time
+ times[start] = arrive
+
+ # now look at all the departures and see if there are
+ # later departures with earlier arrivals
+ bad_starts = []
+ starts = sorted(times.keys(), key=int)
+ for pos, start in enumerate(starts):
+ for i in starts[pos+1:]:
+ if (times[i] < times[start]):
+ # we found a later departure with an earlier arrival!
+ if start not in bad_starts:
+ bad_starts.append(start)
+
+ # now determine which minutes of the hour we should say
+ # "skip the next bus and take the later one"
+ skip = []
+ for min in range(60):
+ if (starts and bad_starts and starts[0] == bad_starts[0]):
+ # we're in a bad window!
+ skip.append(min)
+ if (starts and starts[0] == min):
+ starts.pop(0) # remove the start time
+ if (bad_starts and bad_starts[0] == min):
+ bad_starts.pop(0) # remove the bad start time
+
+ return skip
+
+def comma_join(arr):
+ return ', '.join(map(lambda i: str(i), arr))
+
+def routeJoin(routes):
+ out = []
+ for route in routes:
+ out.append('[' + comma_join(route) + ']')
+ return '[ ' + ', '.join(out) + ' ]'
+
+def solution(routes):
+ print(f'Input: {routeJoin(routes)}')
+ print(f'Output: [{comma_join(faster(routes))}]')
+
+print('Example 1:')
+solution([ [12, 11, 41], [15, 5, 35] ])
+
+print('\nExample 2:')
+solution([ [12, 3, 41], [15, 9, 35], [30, 5, 25] ])
diff --git a/challenge-274/packy-anderson/raku/ch-1.raku b/challenge-274/packy-anderson/raku/ch-1.raku
new file mode 100755
index 0000000000..5b7a60e3cb
--- /dev/null
+++ b/challenge-274/packy-anderson/raku/ch-1.raku
@@ -0,0 +1,38 @@
+#!/usr/bin/env raku
+use v6;
+
+my $startsWithVowel = rx/ :i ^<[aeiou]> /;
+
+sub goatLatin($sentence) {
+ my @words = $sentence.comb(/\S+/);
+ my $a_count = 1;
+ for @words <-> $word {
+ if ($word ~~ $startsWithVowel) {
+ $word ~= 'ma';
+ }
+ else {
+ my $first = $word.substr(0,1);
+ my $rest = $word.substr(1);
+ $word = $rest ~ $first ~ 'ma';
+ }
+ $word ~= 'a' x $a_count++;
+ }
+ return @words.join(' ');
+}
+
+sub solution($sentence) {
+ say 'Input: $sentence = "' ~ $sentence ~ '"';
+ say 'Output: "' ~ goatLatin($sentence) ~ '"';
+}
+
+say "Example 1:";
+solution("I love Perl");
+
+say "\nExample 2:";
+solution("Perl and Raku are friends");
+
+say "\nExample 3:";
+solution("The Weekly Challenge");
+
+say "\nExample 4:";
+solution("Bus stop Bus goes She stays Love grows Under my umbrella");
diff --git a/challenge-274/packy-anderson/raku/ch-2.raku b/challenge-274/packy-anderson/raku/ch-2.raku
new file mode 100755
index 0000000000..793126b74a
--- /dev/null
+++ b/challenge-274/packy-anderson/raku/ch-2.raku
@@ -0,0 +1,80 @@
+#!/usr/bin/env raku
+use v6;
+
+sub routeTimes(@route) {
+ my ($interval, $start, $duration) = @route;
+ my %times;
+ while ($start < 60 + $interval) {
+ %times{$start} = $start + $duration;
+ $start += $interval;
+ }
+ return %times;
+}
+
+sub faster(@routes) {
+ # find the arrival times for each route's departures
+ my %times;
+ for @routes -> @route {
+ for routeTimes(@route).kv -> $start, $arrive {
+ if (%times{$start}:exists && %times{$start} > $arrive) {
+ # this departure for this route will arrive sooner than
+ # the departure for the earlier route, keep this one!
+ %times{$start} = $arrive;
+ }
+ elsif (%times{$start}:!exists) {
+ # no route we've seen has a departure at this time
+ %times{$start} = $arrive;
+ }
+ }
+ }
+
+ # now look at all the departures and see if there are
+ # later departures with earlier arrivals
+ my @bad_starts;
+ my @starts = %times.keys.sort: *.Int;
+ for @starts.kv -> $pos, $start {
+ for @starts[$pos+1 .. *] -> $i {
+ if (%times{$i} < %times{$start} ) {
+ # we found a later departure with an earlier arrival!
+ @bad_starts.push($start);
+ }
+ }
+ }
+ @bad_starts = @bad_starts.unique;
+
+ # now determine which minutes of the hour we should say
+ # "skip the next bus and take the later one"
+ my @skip;
+ for 0 .. 59 -> $min {
+ if (@starts && @bad_starts && @starts[0] == @bad_starts[0]) {
+ # we're in a bad window!
+ @skip.push($min);
+ }
+ if (@starts && @starts[0] == $min) {
+ @starts.shift; # remove the start time
+ }
+ if (@bad_starts && @bad_starts[0] == $min) {
+ @bad_starts.shift; # remove the bad start time
+ }
+ }
+ return @skip;
+}
+
+sub routeJoin(@routes) {
+ my @out;
+ for @routes -> @route {
+ @out.push('[' ~ @route.join(', ') ~ ']')
+ }
+ return '[ ' ~ @out.join(', ') ~ ' ]'
+}
+
+sub solution(@routes) {
+ say 'Input: ' ~ routeJoin(@routes);
+ say 'Output: [' ~ faster(@routes).join(', ') ~ ']';
+}
+
+say "Example 1:";
+solution([ [12, 11, 41], [15, 5, 35] ]);
+
+say "\nExample 2:";
+solution([ [12, 3, 41], [15, 9, 35], [30, 5, 25] ]);