diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-08-13 12:19:09 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-08-13 12:19:09 +0100 |
| commit | 49bbf78f2b3513183b7f9722ec1793cd186ba9d4 (patch) | |
| tree | cd893c8efe8d1b14b0be0de16a7c3d2fc30034eb | |
| parent | 88a73a05f5d42e415fb4c0bba6ca35f68322242f (diff) | |
| parent | e1cbecb3b1eb4c06c9ea29f09d71a471d88b21e9 (diff) | |
| download | perlweeklychallenge-club-49bbf78f2b3513183b7f9722ec1793cd186ba9d4.tar.gz perlweeklychallenge-club-49bbf78f2b3513183b7f9722ec1793cd186ba9d4.tar.bz2 perlweeklychallenge-club-49bbf78f2b3513183b7f9722ec1793cd186ba9d4.zip | |
Merge pull request #10608 from packy/master
Challenge 282 solutions by Packy Anderson
| -rw-r--r-- | challenge-282/packy-anderson/README.md | 2 | ||||
| -rw-r--r-- | challenge-282/packy-anderson/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-282/packy-anderson/elixir/ch-1.exs | 49 | ||||
| -rwxr-xr-x | challenge-282/packy-anderson/elixir/ch-2.exs | 33 | ||||
| -rwxr-xr-x | challenge-282/packy-anderson/perl/ch-1.pl | 42 | ||||
| -rwxr-xr-x | challenge-282/packy-anderson/perl/ch-2.pl | 28 | ||||
| -rwxr-xr-x | challenge-282/packy-anderson/python/ch-1.py | 33 | ||||
| -rwxr-xr-x | challenge-282/packy-anderson/python/ch-2.py | 24 | ||||
| -rwxr-xr-x | challenge-282/packy-anderson/raku/ch-1.raku | 41 | ||||
| -rwxr-xr-x | challenge-282/packy-anderson/raku/ch-2.raku | 28 |
10 files changed, 280 insertions, 1 deletions
diff --git a/challenge-282/packy-anderson/README.md b/challenge-282/packy-anderson/README.md index 69be440f4b..014753ac41 100644 --- a/challenge-282/packy-anderson/README.md +++ b/challenge-282/packy-anderson/README.md @@ -22,4 +22,4 @@ ## Blog Post -[Perl Weekly Challenge: The Ultimate Test of Cerebral Fitness](https://packy.dardan.com/b/Pd) +[Perl Weekly Challenge: Ch-ch-ch-changes!](https://packy.dardan.com/b/Q8) diff --git a/challenge-282/packy-anderson/blog.txt b/challenge-282/packy-anderson/blog.txt new file mode 100644 index 0000000000..cefdbead5a --- /dev/null +++ b/challenge-282/packy-anderson/blog.txt @@ -0,0 +1 @@ +https://packy.dardan.com/b/Q8
\ No newline at end of file diff --git a/challenge-282/packy-anderson/elixir/ch-1.exs b/challenge-282/packy-anderson/elixir/ch-1.exs new file mode 100755 index 0000000000..8bb5d759d8 --- /dev/null +++ b/challenge-282/packy-anderson/elixir/ch-1.exs @@ -0,0 +1,49 @@ +#!/usr/bin/env elixir + +defmodule PWC do + def goodInteger(list, count, char \\ "") + + def goodInteger([], count, char) do + if count == 3 and char != "" do + "\"" <> String.duplicate(char, count) <> "\"" + else + "-1" + end + end + + def goodInteger([char | rest], count, _) do + next = List.first(rest) + if char != next do + # the character changed! + if count == 3 do + "\"" <> String.duplicate(char, count) <> "\"" + else + # restart the count with new character + goodInteger(rest, 1) + end + else + # the character is the same + goodInteger(rest, count + 1, char) + end + end + + def goodInteger(int) do + Integer.to_string(int) + |> String.graphemes + |> goodInteger(1) + end + + def solution(int) do + IO.puts("Input: $int = #{to_string(int)}") + IO.puts("Output: " <> goodInteger(int) ) + end +end + +IO.puts("Example 1:") +PWC.solution(12344456) + +IO.puts("\nExample 2:") +PWC.solution(1233334) + +IO.puts("\nExample 3:") +PWC.solution(10020003) diff --git a/challenge-282/packy-anderson/elixir/ch-2.exs b/challenge-282/packy-anderson/elixir/ch-2.exs new file mode 100755 index 0000000000..0b9243df34 --- /dev/null +++ b/challenge-282/packy-anderson/elixir/ch-2.exs @@ -0,0 +1,33 @@ +#!/usr/bin/env elixir + +defmodule PWC do + def keyChanges([], _, count), do: count + + def keyChanges([char | rest], last, count) do + char = String.downcase(char) + last = String.downcase(last) + cond do + char == last -> keyChanges(rest, char, count) + true -> keyChanges(rest, char, count + 1) + end + end + + def keyChanges(str) do + [first | rest] = String.graphemes(str) + keyChanges(rest, first, 0) + end + + def solution(str) do + IO.puts("Input: $str = '#{str}'") + IO.puts("Output: " <> to_string(keyChanges(str)) ) + end +end + +IO.puts("Example 1:") +PWC.solution("pPeERrLl") + +IO.puts("\nExample 2:") +PWC.solution("rRr") + +IO.puts("\nExample 3:") +PWC.solution("GoO") diff --git a/challenge-282/packy-anderson/perl/ch-1.pl b/challenge-282/packy-anderson/perl/ch-1.pl new file mode 100755 index 0000000000..7ae5f9af80 --- /dev/null +++ b/challenge-282/packy-anderson/perl/ch-1.pl @@ -0,0 +1,42 @@ +#!/usr/bin/env perl +use v5.40; + +sub goodInteger($int) { + my @chars = split //, "$int"; + my $good = shift @chars; + my $count = 1; + foreach my $c ( @chars ) { + if ($c ne $good) { + # the character changed! + if ($count == 3) { + last; + } + # restart the count with new character + $good = $c; + $count = 1; + } + else { + # the character is the same + $count++; + } + } + if ($count == 3) { + return '"' . ($good x $count) . '"'; + } + return -1; +} + +sub solution($int) { + say qq/Input: \$int = $int/; + say qq/Output: / . goodInteger($int); +} + +say "Example 1:"; +solution(12344456); + +say "\nExample 2:"; +solution(1233334); + +say "\nExample 3:"; +solution(10020003); + diff --git a/challenge-282/packy-anderson/perl/ch-2.pl b/challenge-282/packy-anderson/perl/ch-2.pl new file mode 100755 index 0000000000..01c35411d1 --- /dev/null +++ b/challenge-282/packy-anderson/perl/ch-2.pl @@ -0,0 +1,28 @@ +#!/usr/bin/env perl +use v5.40; + +sub keyChanges($str) { + my @chars = split //, $str; + my $char = shift @chars; + my $changes = 0; + while (my $next = shift @chars) { + next if lc($char) eq lc($next); + $changes++; + $char = $next; + } + return $changes; +} + +sub solution($str) { + say "Input: \$str = '$str'"; + say 'Output: ' . keyChanges($str); +} + +say "Example 1:"; +solution("pPeERrLl"); + +say "\nExample 2:"; +solution("rRr"); + +say "\nExample 3:"; +solution("GoO"); diff --git a/challenge-282/packy-anderson/python/ch-1.py b/challenge-282/packy-anderson/python/ch-1.py new file mode 100755 index 0000000000..9c7a56daf7 --- /dev/null +++ b/challenge-282/packy-anderson/python/ch-1.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python + +def goodInteger(intVal): + chars = list(str(intVal)) + good = chars.pop(0) + count = 1 + for c in chars: + if c != good: + # the character changed! + if count == 3: + break + # restart the count with new character + good = c + count = 1 + else: + # the character is the same + count += 1 + if count == 3: + return '"' + (good * count) + '"' + return -1 + +def solution(intVal): + print(f'Input: $int = ({intVal})') + print(f'Output: {goodInteger(intVal)}') + +print('Example 1:') +solution(12344456) + +print('\nExample 2:') +solution(1233334) + +print('\nExample 3:') +solution(10020003) diff --git a/challenge-282/packy-anderson/python/ch-2.py b/challenge-282/packy-anderson/python/ch-2.py new file mode 100755 index 0000000000..fc7936433c --- /dev/null +++ b/challenge-282/packy-anderson/python/ch-2.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python + +def keyChanges(strVal): + chars = list(strVal) + char = chars.pop(0) + changes = 0 + for next in chars: + if char.lower() != next.lower(): + changes += 1 + char = next + return changes + +def solution(strVal): + print(f"Input: $str = '{strVal}'") + print(f"Output: {keyChanges(strVal)}") + +print('Example 1:') +solution("pPeERrLl") + +print('\nExample 2:') +solution("rRr") + +print('\nExample 3:') +solution("GoO") diff --git a/challenge-282/packy-anderson/raku/ch-1.raku b/challenge-282/packy-anderson/raku/ch-1.raku new file mode 100755 index 0000000000..7b29a98261 --- /dev/null +++ b/challenge-282/packy-anderson/raku/ch-1.raku @@ -0,0 +1,41 @@ +#!/usr/bin/env raku +use v6; + +sub goodInteger($int) { + my @chars = $int.Str.comb; + my $good = @chars.shift; + my $count = 1; + for @chars -> $c { + if ($c ne $good) { + # the character changed! + if ($count == 3) { + last; + } + # restart the count with new character + $good = $c; + $count = 1; + } + else { + # the character is the same + $count++; + } + } + if ($count == 3) { + return '"' ~ ($good x $count) ~ '"'; + } + return -1; +} + +sub solution($int) { + say qq/Input: \$int = $int/; + say qq/Output: / ~ goodInteger($int); +} + +say "Example 1:"; +solution(12344456); + +say "\nExample 2:"; +solution(1233334); + +say "\nExample 3:"; +solution(10020003); diff --git a/challenge-282/packy-anderson/raku/ch-2.raku b/challenge-282/packy-anderson/raku/ch-2.raku new file mode 100755 index 0000000000..7385395381 --- /dev/null +++ b/challenge-282/packy-anderson/raku/ch-2.raku @@ -0,0 +1,28 @@ +#!/usr/bin/env raku +use v6; + +sub keyChanges($str) { + my @chars = $str.comb; + my $char = @chars.shift; + my $changes = 0; + while (my $next = @chars.shift) { + next if lc($char) eq lc($next); + $changes++; + $char = $next; + } + return $changes; +} + +sub solution($str) { + say "Input: \$str = '$str'"; + say 'Output: ' ~ keyChanges($str); +} + +say "Example 1:"; +solution("pPeERrLl"); + +say "\nExample 2:"; +solution("rRr"); + +say "\nExample 3:"; +solution("GoO"); |
