diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-07-25 18:08:13 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-07-25 18:08:13 +0100 |
| commit | 410695fa864dea7e420f68cbfdcba4ede9fdd020 (patch) | |
| tree | be8d39b5c3d3b972e33ec403f58003d168e9b9e6 | |
| parent | 30a7af76075d2842697896b8e070367a55b50608 (diff) | |
| parent | ce22dc3c534e9335d4311f5832d0223a4a1e7706 (diff) | |
| download | perlweeklychallenge-club-410695fa864dea7e420f68cbfdcba4ede9fdd020.tar.gz perlweeklychallenge-club-410695fa864dea7e420f68cbfdcba4ede9fdd020.tar.bz2 perlweeklychallenge-club-410695fa864dea7e420f68cbfdcba4ede9fdd020.zip | |
Merge pull request #12407 from packy/master
Challenge 331 solutions by Packy Anderson
| -rw-r--r-- | challenge-331/packy-anderson/README.md | 2 | ||||
| -rw-r--r-- | challenge-331/packy-anderson/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-331/packy-anderson/elixir/ch-1.exs | 21 | ||||
| -rwxr-xr-x | challenge-331/packy-anderson/elixir/ch-2.exs | 43 | ||||
| -rwxr-xr-x | challenge-331/packy-anderson/perl/ch-1.pl | 20 | ||||
| -rwxr-xr-x | challenge-331/packy-anderson/perl/ch-2.pl | 36 | ||||
| -rwxr-xr-x | challenge-331/packy-anderson/python/ch-1.py | 20 | ||||
| -rwxr-xr-x | challenge-331/packy-anderson/python/ch-2.py | 33 | ||||
| -rwxr-xr-x | challenge-331/packy-anderson/raku/ch-1.raku | 20 | ||||
| -rwxr-xr-x | challenge-331/packy-anderson/raku/ch-2.raku | 36 |
10 files changed, 231 insertions, 1 deletions
diff --git a/challenge-331/packy-anderson/README.md b/challenge-331/packy-anderson/README.md index c3bfc77311..3bc9462275 100644 --- a/challenge-331/packy-anderson/README.md +++ b/challenge-331/packy-anderson/README.md @@ -23,4 +23,4 @@ ## Blog Post -[Perl Weekly Challenge: [TITLE OF THE POST]](https://packy.dardan.com/b/Yf) +[Perl Weekly Challenge: You have the last word, Buddy...](https://packy.dardan.com/b/Yw) diff --git a/challenge-331/packy-anderson/blog.txt b/challenge-331/packy-anderson/blog.txt new file mode 100644 index 0000000000..f468595399 --- /dev/null +++ b/challenge-331/packy-anderson/blog.txt @@ -0,0 +1 @@ +https://packy.dardan.com/b/Yw
\ No newline at end of file diff --git a/challenge-331/packy-anderson/elixir/ch-1.exs b/challenge-331/packy-anderson/elixir/ch-1.exs new file mode 100755 index 0000000000..722ab69799 --- /dev/null +++ b/challenge-331/packy-anderson/elixir/ch-1.exs @@ -0,0 +1,21 @@ +#!/usr/bin/env elixir + +defmodule PWC do + def last_word(str) do + str |> String.split |> List.last |> String.length + end + + def solution(str) do + IO.puts("Input: $str = \"#{str}\"") + IO.puts("Output: #{last_word(str)}") + end +end + +IO.puts("Example 1:") +PWC.solution("The Weekly Challenge") + +IO.puts("\nExample 2:") +PWC.solution(" Hello World ") + +IO.puts("\nExample 3:") +PWC.solution("Let's begin the fun") diff --git a/challenge-331/packy-anderson/elixir/ch-2.exs b/challenge-331/packy-anderson/elixir/ch-2.exs new file mode 100755 index 0000000000..010f395bec --- /dev/null +++ b/challenge-331/packy-anderson/elixir/ch-2.exs @@ -0,0 +1,43 @@ +#!/usr/bin/env elixir + +defmodule PWC do + # womp-womp! nothing matched! + def buddy_string(source, _, i) when i >= length(source)-1, + do: "false" + + def buddy_string(source, target, i) do + # swap the i-th and following positions + # and re-join the list into a string, then + # test to see if it matches the target! + if target == Enum.slide(source, i, i+1) |> Enum.join do + "true" + else + # look starting with the next character + buddy_string(source, target, i+1) + end + end + + def buddy_string(source, target) do + # put the source characters in a list, and + # process from the beginning of the list + buddy_string(String.graphemes(source), target, 0) + end + + def solution(source, target) do + IO.puts("Input: $source = \"#{source}\"") + IO.puts(" $target = \"#{target}\"") + IO.puts("Output: #{buddy_string(source, target)}") + end +end + +IO.puts("Example 1:") +PWC.solution("fuck", "fcuk") + +IO.puts("\nExample 2:") +PWC.solution("love", "love") + +IO.puts("\nExample 3:") +PWC.solution("fodo", "food") + +IO.puts("\nExample 4:") +PWC.solution("feed", "feed") diff --git a/challenge-331/packy-anderson/perl/ch-1.pl b/challenge-331/packy-anderson/perl/ch-1.pl new file mode 100755 index 0000000000..7a78299d35 --- /dev/null +++ b/challenge-331/packy-anderson/perl/ch-1.pl @@ -0,0 +1,20 @@ +#!/usr/bin/env perl +use v5.40; + +sub lastWord($str) { + return length(+(split q{ }, $str)[-1]); +} + +sub solution($str) { + say qq/Input: \$str = "$str"/; + say qq/Output: @{[lastWord($str)]}/; +} + +say "Example 1:"; +solution("The Weekly Challenge"); + +say "\nExample 2:"; +solution(" Hello World "); + +say "\nExample 3:"; +solution("Let's begin the fun"); diff --git a/challenge-331/packy-anderson/perl/ch-2.pl b/challenge-331/packy-anderson/perl/ch-2.pl new file mode 100755 index 0000000000..583b52ce77 --- /dev/null +++ b/challenge-331/packy-anderson/perl/ch-2.pl @@ -0,0 +1,36 @@ +#!/usr/bin/env perl +use v5.40; + +sub buddyString($source, $target) { + # put the source characters in an array + my @source = split //, $source; + # loop over the first to all but last characters + foreach my $i (0 .. $#source - 1) { + # generate a list of character positions + my @slice = (0 .. $#source); + # swap the $i-th and following positions + ($slice[$i], $slice[$i+1]) = ($slice[$i+1], $slice[$i]); + # test to see if it matches the target! + return 'true' if join('', @source[@slice]) eq $target; + } + # womp-womp! nothing matched! + return 'false'; +} + +sub solution($source, $target) { + say qq{Input: $source = "$source"}; + say qq{ $target = "$target"}; + say qq{Output: } . buddyString($source, $target); +} + +say "Example 1:"; +solution("fuck", "fcuk"); + +say "\nExample 2:"; +solution("love", "love"); + +say "\nExample 3:"; +solution("fodo", "food"); + +say "\nExample 4:"; +solution("feed", "feed"); diff --git a/challenge-331/packy-anderson/python/ch-1.py b/challenge-331/packy-anderson/python/ch-1.py new file mode 100755 index 0000000000..c37a04e768 --- /dev/null +++ b/challenge-331/packy-anderson/python/ch-1.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python + +def distinctAverages(nums): + pass + +def last_word(strVal): + return len(strVal.split()[-1]) + +def solution(strVal): + print(f'Input: $str = "{strVal}"') + print(f'Output: {last_word(strVal)}') + +print('Example 1:') +solution("The Weekly Challenge") + +print('\nExample 2:') +solution(" Hello World ") + +print('\nExample 3:') +solution("Let's begin the fun") diff --git a/challenge-331/packy-anderson/python/ch-2.py b/challenge-331/packy-anderson/python/ch-2.py new file mode 100755 index 0000000000..fb0049aae5 --- /dev/null +++ b/challenge-331/packy-anderson/python/ch-2.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python + +def buddy_string(source, target): + # put the source characters in an array + src = list(source) + # loop over the first to all but last characters + for i in range(len(src) - 1): + # generate a list of character positions + slice = list(range(len(src))) + # swap the $i-th and following positions + (slice[i], slice[i+1]) = (slice[i+1], slice[i]) + # test to see if it matches the target! + if ''.join([ src[c] for c in slice ]) == target: + return True + # womp-womp! nothing matched! + return False + +def solution(source, target): + print(f'Input: $source = "{source}"') + print(f' $target = "{target}"') + print(f'Output: {buddy_string(source, target)}') + +print('Example 1:') +solution("fuck", "fcuk") + +print('\nExample 2:') +solution("love", "love") + +print('\nExample 3:') +solution("fodo", "food") + +print('\nExample 4:') +solution("feed", "feed") diff --git a/challenge-331/packy-anderson/raku/ch-1.raku b/challenge-331/packy-anderson/raku/ch-1.raku new file mode 100755 index 0000000000..d01db74aa2 --- /dev/null +++ b/challenge-331/packy-anderson/raku/ch-1.raku @@ -0,0 +1,20 @@ +#!/usr/bin/env raku +use v6; + +sub lastWord($str) { + return $str.split(q{ }, :skip-empty)[*-1].chars; +} + +sub solution($str) { + say qq/Input: \$str = "$str"/; + say qq/Output: {lastWord($str)}/; +} + +say "Example 1:"; +solution("The Weekly Challenge"); + +say "\nExample 2:"; +solution(" Hello World "); + +say "\nExample 3:"; +solution("Let's begin the fun"); diff --git a/challenge-331/packy-anderson/raku/ch-2.raku b/challenge-331/packy-anderson/raku/ch-2.raku new file mode 100755 index 0000000000..fda4b96a5f --- /dev/null +++ b/challenge-331/packy-anderson/raku/ch-2.raku @@ -0,0 +1,36 @@ +#!/usr/bin/env raku +use v6; + +sub buddyString($source, $target) { + # put the source characters in an array + my @source = $source.comb; + # loop over the first to all but last characters + for 0 .. @source.elems - 2 -> $i { + # generate a list of character positions + my @slice = (0 .. @source.elems - 1); + # swap the $i-th and following positions + (@slice[$i], @slice[$i+1]) = (@slice[$i+1], @slice[$i]); + # test to see if it matches the target! + return True if @source[@slice].join('') eq $target; + } + # womp-womp! nothing matched! + return False; +} + +sub solution($source, $target) { + say qq{Input: $source = "$source"}; + say qq{ $target = "$target"}; + say qq{Output: } ~ buddyString($source, $target); +} + +say "Example 1:"; +solution("fuck", "fcuk"); + +say "\nExample 2:"; +solution("love", "love"); + +say "\nExample 3:"; +solution("fodo", "food"); + +say "\nExample 4:"; +solution("feed", "feed");
\ No newline at end of file |
