diff options
| author | Mohammad Sajid Anwar <mohammad.anwar@yahoo.com> | 2025-07-31 10:53:41 +0100 |
|---|---|---|
| committer | Mohammad Sajid Anwar <mohammad.anwar@yahoo.com> | 2025-07-31 10:53:41 +0100 |
| commit | 7880d7e0670bf9b8cdf60cf717d4e5fbb621f76c (patch) | |
| tree | 14b9aeb62de724ba48f8864653622a2e187ce78b | |
| parent | 3c483fd97a9083cfa97e13c34368c8610bdd25be (diff) | |
| download | perlweeklychallenge-club-7880d7e0670bf9b8cdf60cf717d4e5fbb621f76c.tar.gz perlweeklychallenge-club-7880d7e0670bf9b8cdf60cf717d4e5fbb621f76c.tar.bz2 perlweeklychallenge-club-7880d7e0670bf9b8cdf60cf717d4e5fbb621f76c.zip | |
- Added solutions by Fabio Valeri.
25 files changed, 214 insertions, 31 deletions
diff --git a/challenge-331/fabio-valeri/perl/ch-1.pl b/challenge-331/fabio-valeri/perl/ch-1.pl new file mode 100644 index 0000000000..74e5680134 --- /dev/null +++ b/challenge-331/fabio-valeri/perl/ch-1.pl @@ -0,0 +1,57 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use feature 'say'; + +# Task 1: Last Word +# Submitted by: Mohammad Sajid Anwar +# +# 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 + + +# Function to count the number of letters of the last word +sub last_word { + my $input = shift; + + # Read last word considering empty spaces + $input =~ /(\S+)\s*$/; + # compute length + return length($1); + +} + +# Test cases +my @test_strings = ( + "The Weekly Challenge" + , "Hello World " + , "Let's begin the fun" +); + +# Test the function with all test cases +say "Testing last_word function:\n"; + +foreach my $test (@test_strings) { + my $result = last_word($test); + printf "Input: %-25s => Output: %s\n", "\"$test\"", "\"$result\""; +} + +# Also allow command line testing +if (@ARGV) { + say "\nCommand line input:"; + my $result = last_word($ARGV[0]); + say "Input: \"$ARGV[0]\" => Output: \"$result\""; + } diff --git a/challenge-331/fabio-valeri/perl/ch-2.pl b/challenge-331/fabio-valeri/perl/ch-2.pl new file mode 100644 index 0000000000..e7d389f33b --- /dev/null +++ b/challenge-331/fabio-valeri/perl/ch-2.pl @@ -0,0 +1,111 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use feature 'say'; + + +# Task 2: Buddy Strings +# Submitted by: Mohammad Sajid Anwar +# +# 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 + + +# Function to check if two strings are buddy strings +sub are_buddy_strings { + my ($source, $target) = @_; + + # Must be same length + return 0 if length($source) != length($target); + + # Find differences + my @diff_positions = (); + for my $i (0 .. length($source) - 1) { + if (substr($source, $i, 1) ne substr($target, $i, 1)) { + push @diff_positions, $i; + } + } + + # Case 1: No differences - strings are identical + if (@diff_positions == 0) { + # Check if source has duplicate characters (can swap identical chars) + my %char_count = (); + for my $char (split //, $source) { + $char_count{$char}++; + return 1 if $char_count{$char} >= 2; # Found duplicate + } + return 0; # No duplicates, can't swap + } + + # Case 2: Exactly 2 differences + elsif (@diff_positions == 2) { + my ($pos1, $pos2) = @diff_positions; + # Check if swapping these positions makes strings equal + return (substr($source, $pos1, 1) eq substr($target, $pos2, 1) && + substr($source, $pos2, 1) eq substr($target, $pos1, 1)); + } + + # Case 3: More than 2 differences - impossible with single swap + else { + return 0; + } +} + +# Test cases from the problem +my @test_cases = ( + ["fuck", "fcuk", 1, "Swapping 'u' with 'c'"] + ,["love", "love", 0, "Identical with no duplicates"] + ,["fodo", "food", 1, "Swapping 'd' with 'o'"] + ,["feed", "feed", 1, "Identical with duplicate 'e'"] + ,["abc" , "def" , 0, "Completely different"] + ,["abcd", "abc" , 0, "Different lengths"] + ,["ab" , "ba" , 1, "Simple swap"] + ,["aa" , "aa" , 1, "Identical with duplicates"] + ,["abc" , "acb" , 1, "Swap b and c"] + ,["abcd", "badc", 0, "Too many differences"] +); + +say "Testing Buddy Strings:\n"; + +foreach my $test (@test_cases) { + my ($source, $target, $expected, $description) = @$test; + my $result = are_buddy_strings($source, $target); + my $status = ($result == $expected) ? "OK" : "ERROR"; + + printf "%s Source: %-6s Target: %-6s => %s (Expected: %s) %s\n" + , $status, "\"$source\"", "\"$target\"" + , $result ? "true" : "false" + , $expected ? "true" : "false" + , $description + f; +} + +# Command line testing +if (@ARGV >= 2) { + say "\nCommand line test:"; + my $result = are_buddy_strings($ARGV[0], $ARGV[1]); + say "Source: \"$ARGV[0]\", Target: \"$ARGV[1]\" => " . ($result ? "true" : "false"); +} diff --git a/stats/pwc-current.json b/stats/pwc-current.json index d4f61aac0d..001fe36430 100644 --- a/stats/pwc-current.json +++ b/stats/pwc-current.json @@ -119,6 +119,16 @@ { "data" : [ [ + "Perl", + 2 + ] + ], + "id" : "Fabio Valeri", + "name" : "Fabio Valeri" + }, + { + "data" : [ + [ "Raku", 2 ] @@ -485,6 +495,11 @@ "y" : 2 }, { + "drilldown" : "Fabio Valeri", + "name" : "Fabio Valeri", + "y" : 2 + }, + { "drilldown" : "Feng Chang", "name" : "Feng Chang", "y" : 2 @@ -609,7 +624,7 @@ } ], "subtitle" : { - "text" : "[Champions: 34] Last updated at 2025-07-31 09:37:05 GMT" + "text" : "[Champions: 35] Last updated at 2025-07-31 09:53:03 GMT" }, "title" : { "text" : "The Weekly Challenge - 331" diff --git a/stats/pwc-language-breakdown-2019.json b/stats/pwc-language-breakdown-2019.json index f92d7f2b5e..5c868efb3b 100644 --- a/stats/pwc-language-breakdown-2019.json +++ b/stats/pwc-language-breakdown-2019.json @@ -970,7 +970,7 @@ } ], "subtitle" : { - "text" : "Click the columns to drilldown the language breakdown. Last updated at 2025-07-31 09:37:05 GMT" + "text" : "Click the columns to drilldown the language breakdown. Last updated at 2025-07-31 09:53:03 GMT" }, "title" : { "text" : "The Weekly Challenge Language" diff --git a/stats/pwc-language-breakdown-2020.json b/stats/pwc-language-breakdown-2020.json index 7488e00a86..56c53696bf 100644 --- a/stats/pwc-language-breakdown-2020.json +++ b/stats/pwc-language-breakdown-2020.json @@ -1223,7 +1223,7 @@ } ], "subtitle" : { - "text" : "Click the columns to drilldown the language breakdown. Last updated at 2025-07-31 09:37:05 GMT" + "text" : "Click the columns to drilldown the language breakdown. Last updated at 2025-07-31 09:53:03 GMT" }, "title" : { "text" : "The Weekly Challenge Language" diff --git a/stats/pwc-language-breakdown-2021.json b/stats/pwc-language-breakdown-2021.json index b16afe756b..540e3158c2 100644 --- a/stats/pwc-language-breakdown-2021.json +++ b/stats/pwc-language-breakdown-2021.json @@ -1223,7 +1223,7 @@ } ], "subtitle" : { - "text" : "Click the columns to drilldown the language breakdown. Last updated at 2025-07-31 09:37:05 GMT" + "text" : "Click the columns to drilldown the language breakdown. Last updated at 2025-07-31 09:53:03 GMT" }, "title" : { "text" : "The Weekly Challenge Language" diff --git a/stats/pwc-language-breakdown-2022.json b/stats/pwc-language-breakdown-2022.json index 3643368d38..bd9b0e10bd 100644 --- a/stats/pwc-language-breakdown-2022.json +++ b/stats/pwc-language-breakdown-2022.json @@ -1223,7 +1223,7 @@ } ], "subtitle" : { - "text" : "Click the columns to drilldown the language breakdown. Last updated at 2025-07-31 09:37:05 GMT" + "text" : "Click the columns to drilldown the language breakdown. Last updated at 2025-07-31 09:53:03 GMT" }, "title" : { "text" : "The Weekly Challenge Language" diff --git a/stats/pwc-language-breakdown-2023.json b/stats/pwc-language-breakdown-2023.json index fb36c98ed0..c7c4a7d704 100644 --- a/stats/pwc-language-breakdown-2023.json +++ b/stats/pwc-language-breakdown-2023.json @@ -1200,7 +1200,7 @@ } ], "subtitle" : { - "text" : "Click the columns to drilldown the language breakdown. Last updated at 2025-07-31 09:37:05 GMT" + "text" : "Click the columns to drilldown the language breakdown. Last updated at 2025-07-31 09:53:03 GMT" }, "title" : { "text" : "The Weekly Challenge Language" diff --git a/stats/pwc-language-breakdown-2024.json b/stats/pwc-language-breakdown-2024.json index e1fbbc5124..7221e2c3e2 100644 --- a/stats/pwc-language-breakdown-2024.json +++ b/stats/pwc-language-breakdown-2024.json @@ -1246,7 +1246,7 @@ } ], "subtitle" : { - "text" : "Click the columns to drilldown the language breakdown. Last updated at 2025-07-31 09:37:05 GMT" + "text" : "Click the columns to drilldown the language breakdown. Last updated at 2025-07-31 09:53:03 GMT" }, "title" : { "text" : "The Weekly Challenge Language" diff --git a/stats/pwc-language-breakdown-2025.json b/stats/pwc-language-breakdown-2025.json index 481bca6794..1df3292cf6 100644 --- a/stats/pwc-language-breakdown-2025.json +++ b/stats/pwc-language-breakdown-2025.json @@ -8,7 +8,7 @@ "data" : [ [ "Perl", - 50 + 52 ], [ "Raku", @@ -547,7 +547,7 @@ { "drilldown" : "331", "name" : "331", - "y" : 100 + "y" : 102 }, { "drilldown" : "330", @@ -694,7 +694,7 @@ } ], "subtitle" : { - "text" : "Click the columns to drilldown the language breakdown. Last updated at 2025-07-31 09:37:05 GMT" + "text" : "Click the columns to drilldown the language breakdown. Last updated at 2025-07-31 09:53:03 GMT" }, "title" : { "text" : "The Weekly Challenge Language" diff --git a/stats/pwc-language-breakdown-summary.json b/stats/pwc-language-breakdown-summary.json index 7f784bde26..3d54487678 100644 --- a/stats/pwc-language-breakdown-summary.json +++ b/stats/pwc-language-breakdown-summary.json @@ -10,7 +10,7 @@ "data" : [ [ "Perl", - 17104 + 17106 ], [ "Raku", @@ -37,7 +37,7 @@ } ], "subtitle" : { - "text" : "Last updated at 2025-07-31 09:37:05 GMT" + "text" : "Last updated at 2025-07-31 09:53:03 GMT" }, "title" : { "text" : "The Weekly Challenge Contributions [2019 - 2025]" diff --git a/stats/pwc-leaders.json b/stats/pwc-leaders.json index 69cb4363a6..ee344908b3 100644 --- a/stats/pwc-leaders.json +++ b/stats/pwc-leaders.json @@ -1049,7 +1049,7 @@ } ], "subtitle" : { - "text" : "Click the columns to drilldown the score breakdown. Last updated at 2025-07-31 09:37:05 GMT" + "text" : "Click the columns to drilldown the score breakdown. Last updated at 2025-07-31 09:53:03 GMT" }, "title" : { "text" : "Team Leaders (TOP 50)" diff --git a/stats/pwc-summary-1-30.json b/stats/pwc-summary-1-30.json index dd3bf0295a..0c78f615f6 100644 --- a/stats/pwc-summary-1-30.json +++ b/stats/pwc-summary-1-30.json @@ -115,7 +115,7 @@ } ], "subtitle" : { - "text" : "[Champions: 30] Last updated at 2025-07-31 09:37:05 GMT" + "text" : "[Champions: 30] Last updated at 2025-07-31 09:53:03 GMT" }, "title" : { "text" : "The Weekly Challenge [2019 - 2025]" diff --git a/stats/pwc-summary-121-150.json b/stats/pwc-summary-121-150.json index aea9a2bf7a..252e9da71c 100644 --- a/stats/pwc-summary-121-150.json +++ b/stats/pwc-summary-121-150.json @@ -115,7 +115,7 @@ } ], "subtitle" : { - "text" : "[Champions: 30] Last updated at 2025-07-31 09:37:05 GMT" + "text" : "[Champions: 30] Last updated at 2025-07-31 09:53:03 GMT" }, "title" : { "text" : "The Weekly Challenge [2019 - 2025]" diff --git a/stats/pwc-summary-151-180.json b/stats/pwc-summary-151-180.json index f34f105539..a7ff809091 100644 --- a/stats/pwc-summary-151-180.json +++ b/stats/pwc-summary-151-180.json @@ -115,7 +115,7 @@ } ], "subtitle" : { - "text" : "[Champions: 30] Last updated at 2025-07-31 09:37:05 GMT" + "text" : "[Champions: 30] Last updated at 2025-07-31 09:53:03 GMT" }, "title" : { "text" : "The Weekly Challenge [2019 - 2025]" diff --git a/stats/pwc-summary-181-210.json b/stats/pwc-summary-181-210.json index 753d68436b..fc174a7256 100644 --- a/stats/pwc-summary-181-210.json +++ b/stats/pwc-summary-181-210.json @@ -115,7 +115,7 @@ } ], "subtitle" : { - "text" : "[Champions: 30] Last updated at 2025-07-31 09:37:05 GMT" + "text" : "[Champions: 30] Last updated at 2025-07-31 09:53:03 GMT" }, "title" : { "text" : "The Weekly Challenge [2019 - 2025]" diff --git a/stats/pwc-summary-211-240.json b/stats/pwc-summary-211-240.json index c8e3789687..9a16eb15dc 100644 --- a/stats/pwc-summary-211-240.json +++ b/stats/pwc-summary-211-240.json @@ -115,7 +115,7 @@ } ], "subtitle" : { - "text" : "[Champions: 30] Last updated at 2025-07-31 09:37:05 GMT" + "text" : "[Champions: 30] Last updated at 2025-07-31 09:53:03 GMT" }, "title" : { "text" : "The Weekly Challenge [2019 - 2025]" diff --git a/stats/pwc-summary-241-270.json b/stats/pwc-summary-241-270.json index d6200e01ba..6c20e5f6ed 100644 --- a/stats/pwc-summary-241-270.json +++ b/stats/pwc-summary-241-270.json @@ -115,7 +115,7 @@ } ], "subtitle" : { - "text" : "[Champions: 30] Last updated at 2025-07-31 09:37:05 GMT" + "text" : "[Champions: 30] Last updated at 2025-07-31 09:53:03 GMT" }, "title" : { "text" : "The Weekly Challenge [2019 - 2025]" diff --git a/stats/pwc-summary-271-300.json b/stats/pwc-summary-271-300.json index 9dab42172a..03103bba92 100644 --- a/stats/pwc-summary-271-300.json +++ b/stats/pwc-summary-271-300.json @@ -115,7 +115,7 @@ } ], "subtitle" : { - "text" : "[Champions: 30] Last updated at 2025-07-31 09:37:05 GMT" + "text" : "[Champions: 30] Last updated at 2025-07-31 09:53:03 GMT" }, "title" : { "text" : "The Weekly Challenge [2019 - 2025]" diff --git a/stats/pwc-summary-301-330.json b/stats/pwc-summary-301-330.json index 88ac4d4e58..f147b6f40d 100644 --- a/stats/pwc-summary-301-330.json +++ b/stats/pwc-summary-301-330.json @@ -106,7 +106,7 @@ } ], "subtitle" : { - "text" : "[Champions: 27] Last updated at 2025-07-31 09:37:05 GMT" + "text" : "[Champions: 27] Last updated at 2025-07-31 09:53:03 GMT" }, "title" : { "text" : "The Weekly Challenge [2019 - 2025]" diff --git a/stats/pwc-summary-31-60.json b/stats/pwc-summary-31-60.json index bdcf22e8f3..3aec9d8078 100644 --- a/stats/pwc-summary-31-60.json +++ b/stats/pwc-summary-31-60.json @@ -115,7 +115,7 @@ } ], "subtitle" : { - "text" : "[Champions: 30] Last updated at 2025-07-31 09:37:05 GMT" + "text" : "[Champions: 30] Last updated at 2025-07-31 09:53:03 GMT" }, "title" : { "text" : "The Weekly Challenge [2019 - 2025]" diff --git a/stats/pwc-summary-61-90.json b/stats/pwc-summary-61-90.json index 4bd124758b..d4699715aa 100644 --- a/stats/pwc-summary-61-90.json +++ b/stats/pwc-summary-61-90.json @@ -35,7 +35,7 @@ 396, 651, 2, - 3, + 5, 8, 22, 0, @@ -115,7 +115,7 @@ } ], "subtitle" : { - "text" : "[Champions: 30] Last updated at 2025-07-31 09:37:05 GMT" + "text" : "[Champions: 30] Last updated at 2025-07-31 09:53:03 GMT" }, "title" : { "text" : "The Weekly Challenge [2019 - 2025]" diff --git a/stats/pwc-summary-91-120.json b/stats/pwc-summary-91-120.json index a15ad71aa4..c62fe8c451 100644 --- a/stats/pwc-summary-91-120.json +++ b/stats/pwc-summary-91-120.json @@ -115,7 +115,7 @@ } ], "subtitle" : { - "text" : "[Champions: 30] Last updated at 2025-07-31 09:37:05 GMT" + "text" : "[Champions: 30] Last updated at 2025-07-31 09:53:03 GMT" }, "title" : { "text" : "The Weekly Challenge [2019 - 2025]" diff --git a/stats/pwc-summary.json b/stats/pwc-summary.json index f31f5fac33..34376f1dc9 100644 --- a/stats/pwc-summary.json +++ b/stats/pwc-summary.json @@ -95,7 +95,7 @@ 202, 326, 1, - 2, + 3, 4, 12, 0, @@ -396,8 +396,8 @@ 6, 0, 0, - 9, 1, + 9, 0, 104, 0, @@ -1006,7 +1006,7 @@ } ], "subtitle" : { - "text" : "[Champions: 327] Last updated at 2025-07-31 09:37:05 GMT" + "text" : "[Champions: 327] Last updated at 2025-07-31 09:53:03 GMT" }, "title" : { "text" : "The Weekly Challenge [2019 - 2025]" diff --git a/stats/pwc-yearly-language-summary.json b/stats/pwc-yearly-language-summary.json index 58064d7d03..918cc0f29b 100644 --- a/stats/pwc-yearly-language-summary.json +++ b/stats/pwc-yearly-language-summary.json @@ -8,7 +8,7 @@ "data" : [ [ "Perl", - 1333 + 1335 ], [ "Raku", @@ -151,7 +151,7 @@ { "drilldown" : "2025", "name" : "2025", - "y" : 2537 + "y" : 2539 }, { "drilldown" : "2024", @@ -188,7 +188,7 @@ } ], "subtitle" : { - "text" : "Click the columns to drilldown the language breakdown. Last updated at 2025-07-31 09:37:05 GMT" + "text" : "Click the columns to drilldown the language breakdown. Last updated at 2025-07-31 09:53:03 GMT" }, "title" : { "text" : "The Weekly Challenge Language" |
