diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-07-15 18:23:22 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-07-15 18:23:22 +0100 |
| commit | 37f61e983ae9792770a6f7de1e139a02cf752c2f (patch) | |
| tree | b0d0294ef838c17410051150c5b5167d9ef468f4 | |
| parent | 9180a7cac96bbade5e958d93c3a2750997f7792e (diff) | |
| parent | c897c97a884ab2dc8ef30b6e018be3999f3bba6c (diff) | |
| download | perlweeklychallenge-club-37f61e983ae9792770a6f7de1e139a02cf752c2f.tar.gz perlweeklychallenge-club-37f61e983ae9792770a6f7de1e139a02cf752c2f.tar.bz2 perlweeklychallenge-club-37f61e983ae9792770a6f7de1e139a02cf752c2f.zip | |
Merge pull request #12357 from pme/challenge-330
challenge-330
| -rwxr-xr-x | challenge-330/peter-meszaros/perl/ch-1.pl | 61 | ||||
| -rwxr-xr-x | challenge-330/peter-meszaros/perl/ch-2.pl | 62 | ||||
| -rwxr-xr-x | challenge-330/peter-meszaros/tcl/ch-1.tcl | 60 | ||||
| -rwxr-xr-x | challenge-330/peter-meszaros/tcl/ch-2.tcl | 59 |
4 files changed, 242 insertions, 0 deletions
diff --git a/challenge-330/peter-meszaros/perl/ch-1.pl b/challenge-330/peter-meszaros/perl/ch-1.pl new file mode 100755 index 0000000000..ed7546e467 --- /dev/null +++ b/challenge-330/peter-meszaros/perl/ch-1.pl @@ -0,0 +1,61 @@ +#!/usr/bin/env perl +# +=head1 Task 1: Clear Digits + +Submitted by: Mohammad Sajid Anwar + +You are given a string containing only lower case English letters and digits. + +Write a script to remove all digits by removing the first digit and the closest +non-digit character to its left. + +=head2 Example 1 + + Input: $str = "cab12" + Output: "c" + + Round 1: remove "1" then "b" => "ca2" + Round 2: remove "2" then "a" => "c" + +=head2 Example 2 + + Input: $str = "xy99" + Output: "" + + Round 1: remove "9" then "y" => "x9" + Round 2: remove "9" then "x" => "" + +=head2 Example 3 + + Input: $str = "pa1erl" + Output: "perl" + +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + ["cab12", "c", "Example 1"], + ["xy99", "", "Example 2"], + ["pa1erl", "perl", "Example 3"], +]; + +sub clear_digits +{ + my $str = shift; + + while ($str =~ s/[a-z]\d//) { + ; + } + return $str; +} + +for (@$cases) { + is(clear_digits($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; diff --git a/challenge-330/peter-meszaros/perl/ch-2.pl b/challenge-330/peter-meszaros/perl/ch-2.pl new file mode 100755 index 0000000000..0eeba035e9 --- /dev/null +++ b/challenge-330/peter-meszaros/perl/ch-2.pl @@ -0,0 +1,62 @@ +#!/usr/bin/env perl +# +=head1 Task 2: Title Capital + +Submitted by: Mohammad Sajid Anwar + +You are given a string made up of one or more words separated by a single +space. + +Write a script to capitalise the given title. If the word length is 1 or 2 then +convert the word to lowercase otherwise make the first character uppercase and +remaining lowercase. + +=head2 Example 1 + + Input: $str = "PERL IS gREAT" + Output: "Perl is Great" + +=head2 Example 2 + + Input: $str = "THE weekly challenge" + Output: "The Weekly Challenge" + +=head2 Example 3 + + Input: $str = "YoU ARE A stAR" + Output: "You Are a Star" + +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + ["PERL IS gREAT", "Perl is Great", "Example 1"], + ["THE weekly challenge", "The Weekly Challenge", "Example 2"], + ["YoU ARE A stAR", "You Are a Star", "Example 3"], +]; + +sub title_capital +{ + my $str = shift; + my @words = split /\s+/, $str; + + for my $word (@words) { + $word = lc($word); + if (length($word) > 2) { + $word = ucfirst($word); + } + } + + return join(' ', @words); +} + +for (@$cases) { + is(title_capital($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; diff --git a/challenge-330/peter-meszaros/tcl/ch-1.tcl b/challenge-330/peter-meszaros/tcl/ch-1.tcl new file mode 100755 index 0000000000..574bed3003 --- /dev/null +++ b/challenge-330/peter-meszaros/tcl/ch-1.tcl @@ -0,0 +1,60 @@ +#!/usr/bin/env tclsh +# +# Task 1: Clear Digits +# +# Submitted by: Mohammad Sajid Anwar +# +# You are given a string containing only lower case English letters and digits. +# +# Write a script to remove all digits by removing the first digit and the closest +# non-digit character to its left. +# +# Example 1 +# +# Input: $str = "cab12" +# Output: "c" +# +# Round 1: remove "1" then "b" => "ca2" +# Round 2: remove "2" then "a" => "c" +# +# Example 2 +# +# Input: $str = "xy99" +# Output: "" +# +# Round 1: remove "9" then "y" => "x9" +# Round 2: remove "9" then "x" => "" +# +# Example 3 +# +# Input: $str = "pa1erl" +# Output: "perl" +# + +package require tcltest + +set cases { + {"cab12" "c" "Example 1"} + {"xy99" "" "Example 2"} + {"pa1erl" "perl" "Example 3"} +} + +proc clear_digits {str} { + + while {[regsub {[a-z][0-9]} $str {} str]} { + ; + } + + return $str + +} + +tcltest::configure -verbose {pass} +foreach case $cases { + tcltest::test [lindex $case 2] {} { + clear_digits [lindex $case 0] + } [lindex $case 1] +} + +exit 0 + diff --git a/challenge-330/peter-meszaros/tcl/ch-2.tcl b/challenge-330/peter-meszaros/tcl/ch-2.tcl new file mode 100755 index 0000000000..370b84429f --- /dev/null +++ b/challenge-330/peter-meszaros/tcl/ch-2.tcl @@ -0,0 +1,59 @@ +#!/usr/bin/env tclsh +# +# Task 2: Title Capital +# +# Submitted by: Mohammad Sajid Anwar +# +# You are given a string made up of one or more words separated by a single +# space. +# +# Write a script to capitalise the given title. If the word length is 1 or 2 then +# convert the word to lowercase otherwise make the first character uppercase and +# remaining lowercase. +# +# Example 1 +# +# Input: $str = "PERL IS gREAT" +# Output: "Perl is Great" +# +# Example 2 +# +# Input: $str = "THE weekly challenge" +# Output: "The Weekly Challenge" +# +# Example 3 +# +# Input: $str = "YoU ARE A stAR" +# Output: "You Are a Star" +# + +package require tcltest + +set cases { + {"PERL IS gREAT" "Perl is Great" "Example 1"} + {"THE weekly challenge" "The Weekly Challenge" "Example 2"} + {"YoU ARE A stAR" "You Are a Star" "Example 3"} +} + +proc title_capital {str} { + set words [split $str " "] + + foreach word $words { + set word [string tolower $word] + if {[string length $word] > 2} { + set word "[string toupper [string index $word 0]][string range $word 1 end]" + } + lappend w $word + } + return [join $w " "] +} + +tcltest::configure -verbose {pass} +foreach case $cases { + tcltest::test [lindex $case 2] {} { + title_capital [lindex $case 0] + } [lindex $case 1] +} + +exit 0 + |
