diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-07-22 00:28:49 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-07-22 00:28:49 +0100 |
| commit | 8bc1d93bc38667ec4e317b24cebf2f34002ff189 (patch) | |
| tree | d323c4c7c9fdfd08af951dd14754c7e08443fc20 | |
| parent | d8154dfc33ce0c554d75fe5fdedb5337d32742e5 (diff) | |
| parent | 84e37b6dc9a2ca9d1ccf281a499304895280e9b6 (diff) | |
| download | perlweeklychallenge-club-8bc1d93bc38667ec4e317b24cebf2f34002ff189.tar.gz perlweeklychallenge-club-8bc1d93bc38667ec4e317b24cebf2f34002ff189.tar.bz2 perlweeklychallenge-club-8bc1d93bc38667ec4e317b24cebf2f34002ff189.zip | |
Merge pull request #12392 from jeanluc2020/jeanluc2020-331
Add solution 331.
| -rw-r--r-- | challenge-331/jeanluc2020/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-331/jeanluc2020/blog-2.txt | 1 | ||||
| -rwxr-xr-x | challenge-331/jeanluc2020/perl/ch-1.pl | 50 | ||||
| -rwxr-xr-x | challenge-331/jeanluc2020/perl/ch-2.pl | 79 |
4 files changed, 131 insertions, 0 deletions
diff --git a/challenge-331/jeanluc2020/blog-1.txt b/challenge-331/jeanluc2020/blog-1.txt new file mode 100644 index 0000000000..38398edfef --- /dev/null +++ b/challenge-331/jeanluc2020/blog-1.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-331-1.html diff --git a/challenge-331/jeanluc2020/blog-2.txt b/challenge-331/jeanluc2020/blog-2.txt new file mode 100644 index 0000000000..e47153305f --- /dev/null +++ b/challenge-331/jeanluc2020/blog-2.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-331-2.html diff --git a/challenge-331/jeanluc2020/perl/ch-1.pl b/challenge-331/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..2c913f7b94 --- /dev/null +++ b/challenge-331/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,50 @@ +#!/usr/bin/env perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-331/#TASK1 +# +# Task 1: Last Word +# ================= +# +# You are given a string. +# +# Write a script to find the length of last word in the given string. +# +## Example 1 +## +## Input: $str = "The Weekly Challenge" +## Output: 9 +# +# +## Example 2 +## +## Input: $str = " Hello World " +## Output: 5 +# +# +## Example 3 +## +## Input: $str = "Let's begin the fun" +## Output: 3 +# +############################################################ +## +## discussion +## +############################################################ +# +# We begin by removing trailing whitespace, then we split the +# string into its words, of which we then calculate the length +# of the last one. + +use v5.36; + +last_word("The Weekly Challenge"); +last_word(" Hello World "); +last_word("Let's begin the fun"); + +sub last_word($str) { + say "Input: \"$str\""; + $str =~ s/ +$//; + my @words = split/\s+/,$str; + say "Output: " . length($words[-1]); +} + diff --git a/challenge-331/jeanluc2020/perl/ch-2.pl b/challenge-331/jeanluc2020/perl/ch-2.pl new file mode 100755 index 0000000000..0c93a31a86 --- /dev/null +++ b/challenge-331/jeanluc2020/perl/ch-2.pl @@ -0,0 +1,79 @@ +#!/usr/bin/env perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-331/#TASK2 +# +# Task 2: Buddy Strings +# ===================== +# +# You are given two strings, source and target. +# +# Write a script to find out if the given strings are Buddy Strings. +# +## If swapping of a letter in one string make them same as the other then they +## are `Buddy Strings`. +# +# +## Example 1 +## +## Input: $source = "fuck" +## $target = "fcuk" +## Output: true +## +## The swapping of 'u' with 'c' makes it buddy strings. +# +# +## Example 2 +## +## Input: $source = "love" +## $target = "love" +## Output: false +# +# +## Example 3 +## +## Input: $source = "fodo" +## $target = "food" +## Output: true +# +# +## Example 4 +## +## Input: $source = "feed" +## $target = "feed" +## Output: true +# +############################################################ +## +## discussion +## +############################################################ +# +# We split one of the words (let's decide for the target, but +# we could equally use the source here) into individual characters. +# Then we just try to swap each character against each other, and if +# the resulting string matches the source, we return true. Then we +# swap back of course, jumping to the next pair of characters to +# swap. If we didn't find a true solution at the end, we can +# return false. + +use v5.36; + +buddy_strings("fuck", "fcuk"); +buddy_strings("love", "love"); +buddy_strings("fodo", "food"); +buddy_strings("feed", "feed"); + +sub buddy_strings($source, $target) { + say "Input: \"$source\", \"$target\""; + my @t_chars = split //, $target; + foreach my $i (0..$#t_chars) { + foreach my $j ($i+1..$#t_chars) { + ($t_chars[$i], $t_chars[$j]) = ($t_chars[$j], $t_chars[$i]); + my $tmp = join("", @t_chars); + if($source eq $tmp) { + return say "Output: true"; + } + ($t_chars[$i], $t_chars[$j]) = ($t_chars[$j], $t_chars[$i]); + } + } + say "Output: false"; +} |
