diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-07-08 17:03:07 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-07-08 17:03:07 +0100 |
| commit | b50fa9092617774eef57d6aa176d54f44c68a67c (patch) | |
| tree | 30023c0793c7f8424d462cba9d29ea60dc38f016 | |
| parent | 300c2f7a1732ba0a8bb98f03deaee37b51f2c312 (diff) | |
| parent | 6f763c834495a3e9a2649be47621bbcafada60d5 (diff) | |
| download | perlweeklychallenge-club-b50fa9092617774eef57d6aa176d54f44c68a67c.tar.gz perlweeklychallenge-club-b50fa9092617774eef57d6aa176d54f44c68a67c.tar.bz2 perlweeklychallenge-club-b50fa9092617774eef57d6aa176d54f44c68a67c.zip | |
Merge pull request #12307 from pme/challenge-329
challenge-329
| -rwxr-xr-x | challenge-329/peter-meszaros/perl/ch-1.pl | 61 | ||||
| -rwxr-xr-x | challenge-329/peter-meszaros/perl/ch-2.pl | 83 | ||||
| -rwxr-xr-x | challenge-329/peter-meszaros/tcl/ch-1.tcl | 60 | ||||
| -rwxr-xr-x | challenge-329/peter-meszaros/tcl/ch-2.tcl | 130 |
4 files changed, 334 insertions, 0 deletions
diff --git a/challenge-329/peter-meszaros/perl/ch-1.pl b/challenge-329/peter-meszaros/perl/ch-1.pl new file mode 100755 index 0000000000..1233046a2e --- /dev/null +++ b/challenge-329/peter-meszaros/perl/ch-1.pl @@ -0,0 +1,61 @@ +#!/usr/bin/env perl +# +=head1 Task 1: Counter Integers + +Submitted by: Mohammad Sajid Anwar + +You are given a string containing only lower case English letters and digits. + +Write a script to replace every non-digit character with a space and then +return all the distinct integers left. + +=head2 Example 1 + + Input: $str = "the1weekly2challenge2" + Output: 1, 2 + + 2 is appeared twice, so we count it one only. + +=head2 Example 2 + + Input: $str = "go21od1lu5c7k" + Output: 21, 1, 5, 7 + +=head2 Example 3 + + Input: $str = "4p3e2r1l" + Output: 4, 3, 2, 1 + +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + ["the1weekly2challenge2", [1, 2], "Example 1"], + ["go21od1lu5c7k", [21, 1, 5, 7], "Example 2"], + ["4p3e2r1l", [4, 3, 2, 1], "Example 3"], +]; + +sub counter_integers +{ + my $str = shift; + + my %seen; + my @numbers = grep {/[0-9]/} split /[^0-9]+/, $str; + my @counter_integers; + foreach my $num (@numbers) { + push @counter_integers, $num unless $seen{$num}; + $seen{$num} = 1; + } + return \@counter_integers; +} + +for (@$cases) { + is(counter_integers($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; diff --git a/challenge-329/peter-meszaros/perl/ch-2.pl b/challenge-329/peter-meszaros/perl/ch-2.pl new file mode 100755 index 0000000000..4e5ff324f9 --- /dev/null +++ b/challenge-329/peter-meszaros/perl/ch-2.pl @@ -0,0 +1,83 @@ +#!/usr/bin/env perl +# +=head1 Task 2: Nice String + +Submitted by: Mohammad Sajid Anwar + +You are given a string made up of lower and upper case English letters only. + +Write a script to return the longest substring of the give string which is +nice. A string is nice if, for every letter of the alphabet that the string +contains, it appears both in uppercase and lowercase. + +=head2 Example 1 + + Input: $str = "YaaAho" + Output: "aaA" + +=head2 Example 2 + + Input: $str = "cC" + Output: "cC" + +=head2 Example 3 + + Input: $str = "A" + Output: "" + + No nice string found. + +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + ["YaaAho", "aaA", "Example 1"], + ["cC", "cC", "Example 2"], + ["A", "", "Example 3"], +]; + +sub nice_string +{ + my $str = shift; + + my $max_len = 0; + my $max_substr = ''; + my $len = length($str); + for my $i (0 .. $len - 1) { + for my $j ($i + 1 .. $len) { + my $substr = substr($str, $i, $j - $i); + my %seen; + my $is_nice = 1; + + for my $char (split //, $substr) { + if ($char =~ /[a-zA-Z]/) { + $seen{lc($char)}++; + } + } + + for my $key (keys %seen) { + if ($seen{$key} < 2) { + $is_nice = 0; + last; + } + } + + if ($is_nice && length($substr) > $max_len) { + $max_len = length($substr); + $max_substr = $substr; + } + } + } + return $max_substr; +} + +for (@$cases) { + is(nice_string($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; diff --git a/challenge-329/peter-meszaros/tcl/ch-1.tcl b/challenge-329/peter-meszaros/tcl/ch-1.tcl new file mode 100755 index 0000000000..2fb8114bf2 --- /dev/null +++ b/challenge-329/peter-meszaros/tcl/ch-1.tcl @@ -0,0 +1,60 @@ +#!/usr/bin/env tclsh +# +# Task 1: Counter Integers +# +# Submitted by: Mohammad Sajid Anwar +# +# You are given a string containing only lower case English letters and digits. +# +# Write a script to replace every non-digit character with a space and then +# return all the distinct integers left. +# +# Example 1 +# +# Input: $str = "the1weekly2challenge2" +# Output: 1, 2 +# +# 2 is appeared twice, so we count it one only. +# +# Example 2 +# +# Input: $str = "go21od1lu5c7k" +# Output: 21, 1, 5, 7 +# +# Example 3 +# +# Input: $str = "4p3e2r1l" +# Output: 4, 3, 2, 1 +# + +package require tcltest + +set cases { + {"the1weekly2challenge2" {1 2} "Example 1"} + {"go21od1lu5c7k" {21 1 5 7} "Example 2"} + {"4p3e2r1l" {4 3 2 1} "Example 3"} +} + +proc counter_integers {str} { + set numbers [regexp -all -inline {(\d+)} $str] + set counter_integers {} + + foreach num $numbers { + if {![info exists seen($num)]} { + lappend counter_integers $num + set seen($num) 1 + } + } + + return $counter_integers +} + +tcltest::configure -verbose {pass} +foreach case $cases { + tcltest::test [lindex $case 2] {} { + counter_integers [lindex $case 0] + } [lindex $case 1] +} + +exit 0 + diff --git a/challenge-329/peter-meszaros/tcl/ch-2.tcl b/challenge-329/peter-meszaros/tcl/ch-2.tcl new file mode 100755 index 0000000000..6fe61d639d --- /dev/null +++ b/challenge-329/peter-meszaros/tcl/ch-2.tcl @@ -0,0 +1,130 @@ +#!/usr/bin/env tclsh +# +# Task 2: Nice String +# +# Submitted by: Mohammad Sajid Anwar +# +# You are given a string made up of lower and upper case English letters only. +# +# Write a script to return the longest substring of the give string which is +# nice. A string is nice if, for every letter of the alphabet that the string +# contains, it appears both in uppercase and lowercase. +# +# Example 1 +# +# Input: $str = "YaaAho" +# Output: "aaA" +# +# Example 2 +# +# Input: $str = "cC" +# Output: "cC" +# +# Example 3 +# +# Input: $str = "A" +# Output: "" +# +# No nice string found. +# + +package require tcltest + +set cases { + {"YaaAho" "aaA" "Example 1"} + {"cC" "cC" "Example 2"} + {"A" "" "Example 3"} +} + +proc nice_string {str} { + set max_len 0 + set max_substr "" + + set len [string length $str] + for {set i 0} {$i < $len} {incr i} { + for {set j [expr $i + 1]} {$j <= $len} {incr j} { + set substr [string range $str $i [expr $j - 1]] + set is_nice 1 + array unset seen + + foreach char [split $substr ""] { + if {[regexp {^[a-zA-Z]$} $char]} { + set lower_char [string tolower $char] + if {[info exists seen($lower_char)]} { + incr seen($lower_char) + } else { + set seen($lower_char) 1 + } + } + } + + foreach key [array names seen] { + if {$seen($key) < 2} { + set is_nice 0 + break + } + } + + if {$is_nice && [string length $substr] > $max_len} { + set max_len [string length $substr] + set max_substr $substr + } + } + } + + return $max_substr +} + +tcltest::configure -verbose {pass} +foreach case $cases { + tcltest::test [lindex $case 2] {} { + nice_string [lindex $case 0] + } [lindex $case 1] +} + +exit 0 + + +sub nice_string +{ + my $str = shift; + + my $max_len = 0; + my $max_substr = ''; + my $len = length($str); + for my $i (0 .. $len - 1) { + for my $j ($i + 1 .. $len) { + my $substr = substr($str, $i, $j - $i); + my %seen; + my $is_nice = 1; + + for my $char (split //, $substr) { + if ($char =~ /[a-z]/) { + $seen{lc($char)}++; + } elsif ($char =~ /[A-Z]/) { + $seen{lc($char)}++; + } + } + + for my $key (keys %seen) { + if ($seen{$key} < 2) { + $is_nice = 0; + last; + } + } + + if ($is_nice && length($substr) > $max_len) { + $max_len = length($substr); + $max_substr = $substr; + } + } + } + return $max_substr; +} + +for (@$cases) { + is(nice_string($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; |
