diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-07-22 00:29:29 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-07-22 00:29:29 +0100 |
| commit | 1131559ab4a8d9a87b099304a8629deea0b4b0a1 (patch) | |
| tree | 2f335399d06edb6762eda1a9fb944207e4f49b63 | |
| parent | 7dd94f5a7a9bbf1feb1176e5dba65ed9741b79da (diff) | |
| parent | 25b3aa135dfc49b4f3551acdf73ac5399cc882c6 (diff) | |
| download | perlweeklychallenge-club-1131559ab4a8d9a87b099304a8629deea0b4b0a1.tar.gz perlweeklychallenge-club-1131559ab4a8d9a87b099304a8629deea0b4b0a1.tar.bz2 perlweeklychallenge-club-1131559ab4a8d9a87b099304a8629deea0b4b0a1.zip | |
Merge pull request #12394 from pme/challenge-331
challenge-331
| -rwxr-xr-x | challenge-331/peter-meszaros/perl/ch-1.pl | 53 | ||||
| -rwxr-xr-x | challenge-331/peter-meszaros/perl/ch-2.pl | 83 | ||||
| -rwxr-xr-x | challenge-331/peter-meszaros/tcl/ch-1.tcl | 56 | ||||
| -rwxr-xr-x | challenge-331/peter-meszaros/tcl/ch-2.tcl | 81 |
4 files changed, 273 insertions, 0 deletions
diff --git a/challenge-331/peter-meszaros/perl/ch-1.pl b/challenge-331/peter-meszaros/perl/ch-1.pl new file mode 100755 index 0000000000..ad15f7428c --- /dev/null +++ b/challenge-331/peter-meszaros/perl/ch-1.pl @@ -0,0 +1,53 @@ +#!/usr/bin/env perl +# +=head1 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. + +=head2 Example 1 + + Input: $str = "The Weekly Challenge" + Output: 9 + +=head2 Example 2 + + Input: $str = " Hello World " + Output: 5 + +=head2 Example 3 + + Input: $str = "Let's begin the fun" + Output: 3 + +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + ["The Weekly Challenge", 9, "Example 1"], + [" Hello World ", 5, "Example 2"], + [ "Let's begin the fun", 3, "Example 3"], + [ " ", 0, "Example 4"], +]; + +sub last_word +{ + my $str = shift; + + my @words = split /\s+/, $str; + return @words ? length $words[-1] : 0; +} + +for (@$cases) { + is(last_word($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; diff --git a/challenge-331/peter-meszaros/perl/ch-2.pl b/challenge-331/peter-meszaros/perl/ch-2.pl new file mode 100755 index 0000000000..c4853609eb --- /dev/null +++ b/challenge-331/peter-meszaros/perl/ch-2.pl @@ -0,0 +1,83 @@ +#!/usr/bin/env perl +# +=head1 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`. + +=head2 Example 1 + + Input: $source = "fuck" + $target = "fcuk" + Output: true + + The swapping of 'u' with 'c' makes it buddy strings. + +=head2 Example 2 + + Input: $source = "love" + $target = "love" + Output: false + +=head2 Example 3 + + Input: $source = "fodo" + $target = "food" + Output: true + +=head2 Example 4 + + Input: $source = "feed" + $target = "feed" + Output: true + +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + [["fuck", "fcuk"], 1, "Example 1"], + [["love", "love"], 0, "Example 2"], + [["fodo", "food"], 1, "Example 3"], + [["feed", "feed"], 0, "Example 4"], +]; + +sub buddy_strings +{ + my $source = $_[0]->[0]; + my $target = $_[0]->[1]; + + my $buddy = 0; + if (length($source) == length($target) and $source ne $target) { + for my $i (0 .. length($source) - 1) { + if (substr($source, $i, 1) ne substr($target, $i, 1)) { + my $j = index($target, substr($source, $i, 1), $i + 1); + if ($j != -1 and substr($source, $j, 1) eq substr($target, $i, 1)) { + if ($buddy) { + $buddy = 0; + last; + } + $buddy = 1; + } + } + } + } + + return $buddy; +} + +for (@$cases) { + is(buddy_strings($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; diff --git a/challenge-331/peter-meszaros/tcl/ch-1.tcl b/challenge-331/peter-meszaros/tcl/ch-1.tcl new file mode 100755 index 0000000000..757ce03ed0 --- /dev/null +++ b/challenge-331/peter-meszaros/tcl/ch-1.tcl @@ -0,0 +1,56 @@ +#!/usr/bin/env tclsh +# +# 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 +# + +package require tcltest + +set cases { + {"The Weekly Challenge" 9 "Example 1"} + {" Hello World " 5 "Example 2"} + { "Let's begin the fun" 3 "Example 3"} + { " " 0 "Example 4"} +} + +proc last_word {str} { + set str [string trim $str] + + set words [split $str " "] + + if {[llength $words] == 0} { + return 0 + } else { + return [string length [lindex $words end]] + } +} + +tcltest::configure -verbose {pass} +foreach case $cases { + tcltest::test [lindex $case 2] {} { + last_word [lindex $case 0] + } [lindex $case 1] +} + +exit 0 + diff --git a/challenge-331/peter-meszaros/tcl/ch-2.tcl b/challenge-331/peter-meszaros/tcl/ch-2.tcl new file mode 100755 index 0000000000..d486b2b3ae --- /dev/null +++ b/challenge-331/peter-meszaros/tcl/ch-2.tcl @@ -0,0 +1,81 @@ +#!/usr/bin/env tclsh +# +# 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 +# + +package require tcltest + +set cases { + {{"fuck" "fcuk"} 1 "Example 1"} + {{"love" "love"} 0 "Example 2"} + {{"fodo" "food"} 1 "Example 3"} + {{"feed" "feed"} 0 "Example 4"} +} + +proc buddy_strings {strs} { + set source [lindex $strs 0] + set target [lindex $strs 1] + + set buddy 0 + if {[string length $source] == [string length $target] && $source ne $target} { + for {set i 0} {$i < [string length $source]} {incr i} { + if {[string index $source $i] ne [string index $target $i]} { + set j [string first [string index $source $i] $target [expr $i + 1]] + if {$j != -1 && [string index $source $j] eq [string index $target $i]} { + if {$buddy} { + set buddy 0 + break + } + set buddy 1 + } + } + } + } + + return $buddy +} + +tcltest::configure -verbose {pass} +foreach case $cases { + tcltest::test [lindex $case 2] {} { + buddy_strings [lindex $case 0] + } [lindex $case 1] +} + +exit 0 + |
