aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-11-18 09:53:04 +0000
committerGitHub <noreply@github.com>2025-11-18 09:53:04 +0000
commit43278fd210df61e0bdaa63f05b3108c02cba7227 (patch)
tree175d292d9a305be60e0117f7ebc9b2bb76088e0c
parente7fe20f766cdb80ee6a8534595b4dee3888adc76 (diff)
parent5e91a09ab773ea96a0e277456e872f49833a33aa (diff)
downloadperlweeklychallenge-club-43278fd210df61e0bdaa63f05b3108c02cba7227.tar.gz
perlweeklychallenge-club-43278fd210df61e0bdaa63f05b3108c02cba7227.tar.bz2
perlweeklychallenge-club-43278fd210df61e0bdaa63f05b3108c02cba7227.zip
Merge pull request #13042 from pme/challenge-348
challenge-348
-rwxr-xr-xchallenge-348/peter-meszaros/perl/ch-1.pl93
-rwxr-xr-xchallenge-348/peter-meszaros/perl/ch-2.pl102
-rwxr-xr-xchallenge-348/peter-meszaros/tcl/ch-1.tcl104
-rwxr-xr-xchallenge-348/peter-meszaros/tcl/ch-2.tcl107
4 files changed, 406 insertions, 0 deletions
diff --git a/challenge-348/peter-meszaros/perl/ch-1.pl b/challenge-348/peter-meszaros/perl/ch-1.pl
new file mode 100755
index 0000000000..f6e48a83ef
--- /dev/null
+++ b/challenge-348/peter-meszaros/perl/ch-1.pl
@@ -0,0 +1,93 @@
+#!/usr/bin/env perl
+#
+=head1 Task 1: String Alike
+
+Submitted by: Mohammad Sajid Anwar
+
+You are given a string of even length.
+
+Write a script to find if the given string split into two halves of equal
+lengths and they both have same number of vowels.
+
+=head2 Example 1
+
+ Input: $str = "textbook"
+ Output: false
+
+ 1st half: "text" (1 vowel)
+ 2nd half: "book" (2 vowels)
+
+=head2 Example 2
+
+ Input: $str = "book"
+ Output: true
+
+ 1st half: "bo" (1 vowel)
+ 2nd half: "ok" (1 vowel)
+
+=head2 Example 3
+
+ Input: $str = "AbCdEfGh"
+ Output: true
+
+ 1st half: "AbCd" (1 vowel)
+ 2nd half: "EfGh" (1 vowel)
+
+=head2 Example 4
+
+ Input: $str = "rhythmmyth"
+ Output: false
+
+ 1st half: "rhyth" (0 vowel)
+ 2nd half: "mmyth" (0 vowel)
+
+=head2 Example 5
+
+ Input: $str = "UmpireeAudio"
+ Output: false
+
+ 1st half: "Umpire" (3 vowels)
+ 2nd half: "eAudio" (5 vowels)
+
+=cut
+
+use strict;
+use warnings;
+use Test2::V0 -no_srand => 1;
+use Data::Dumper;
+use constant { true => 1, false => 0 };
+
+my $cases = [
+ ["textbook", false, "Example 1"],
+ ["book", true, "Example 2"],
+ ["AbCdEfGh", true, "Example 3"],
+ ["rhythmmyth", false, "Example 4"],
+ ["UmpireeAudio", false, "Example 5"],
+];
+
+sub string_alike
+{
+ sub _vowel_count {
+ my $s = shift;
+ my $count = 0;
+ $count++ while ($s =~ /[aeiou]/gi);
+ return $count;
+ };
+
+ my $str = shift;
+
+ my $len = length($str);
+ return false if $len % 2 != 0;
+ my $half = $len / 2;
+ my $first_half_cnt = _vowel_count(substr($str, 0, $half));
+ my $second_half_cnt = _vowel_count(substr($str, $half));
+ return true if $first_half_cnt > 0 && $first_half_cnt == $second_half_cnt;
+ return false;
+}
+
+for (@$cases) {
+ is(string_alike($_->[0]), $_->[1], $_->[2]);
+}
+done_testing();
+
+exit 0;
diff --git a/challenge-348/peter-meszaros/perl/ch-2.pl b/challenge-348/peter-meszaros/perl/ch-2.pl
new file mode 100755
index 0000000000..fa964444d0
--- /dev/null
+++ b/challenge-348/peter-meszaros/perl/ch-2.pl
@@ -0,0 +1,102 @@
+#!/usr/bin/env perl
+#
+=head1 Task 2: Convert Time
+
+Submitted by: Mohammad Sajid Anwar
+
+You are given two strings, $source and $target, containing time in 24-hour time
+form.
+
+Write a script to convert the source into target by performing one of the
+following operations:
+
+ 1. Add 1 minute
+ 2. Add 5 minutes
+ 3. Add 15 minutes
+ 4. Add 60 minutes
+
+Find the total operations needed to get to the target.
+
+=head2 Example 1
+
+ Input: $source = "02:30"
+ $target = "02:45"
+ Output: 1
+
+ Just one operation i.e. "Add 15 minutes".
+
+=head2 Example 2
+
+ Input: $source = "11:55"
+ $target = "12:15"
+ Output: 2
+
+ Two operations i.e. "Add 15 minutes" followed by "Add 5 minutes".
+
+=head2 Example 3
+
+ Input: $source = "09:00"
+ $target = "13:00"
+ Output: 4
+
+ Four operations of "Add 60 minutes".
+
+=head2 Example 4
+
+ Input: $source = "23:45"
+ $target = "00:30"
+ Output: 3
+
+ Three operations of "Add 15 minutes".
+
+=head2 Example 5
+
+ Input: $source = "14:20"
+ $target = "15:25"
+ Output: 2
+
+ Two operations, one "Add 60 minutes" and one "Add 5 minutes"
+
+=cut
+
+use strict;
+use warnings;
+use Test2::V0 -no_srand => 1;
+use Data::Dumper;
+use constant { true => 1, false => 0 };
+
+my $cases = [
+ [["02:30", "02:45"], 1, "Example 1"],
+ [["11:55", "12:15"], 2, "Example 2"],
+ [["09:00", "13:00"], 4, "Example 3"],
+ [["23:45", "00:30"], 3, "Example 4"],
+ [["14:20", "15:25"], 2, "Example 5"],
+];
+
+sub convert_time
+{
+ my $source = $_[0]->[0];
+ my $target = $_[0]->[1];
+
+ my ($sh, $sm) = split(':', $source);
+ my ($th, $tm) = split(':', $target);
+ my $source_mins = $sh * 60 + $sm;
+ my $target_mins = $th * 60 + $tm;
+ $target_mins += 24 * 60 if $target_mins < $source_mins;
+ my $diff = $target_mins - $source_mins;
+ my $ops = 0;
+ for my $add (60, 15, 5, 1) {
+ while ($diff >= $add) {
+ $diff -= $add;
+ $ops++;
+ }
+ }
+ return $ops;
+}
+
+for (@$cases) {
+ is(convert_time($_->[0]), $_->[1], $_->[2]);
+}
+done_testing();
+
+exit 0;
diff --git a/challenge-348/peter-meszaros/tcl/ch-1.tcl b/challenge-348/peter-meszaros/tcl/ch-1.tcl
new file mode 100755
index 0000000000..e020689aef
--- /dev/null
+++ b/challenge-348/peter-meszaros/tcl/ch-1.tcl
@@ -0,0 +1,104 @@
+#!/usr/bin/env tclsh
+#
+# Task 1: String Alike
+#
+# Submitted by: Mohammad Sajid Anwar
+#
+# You are given a string of even length.
+#
+# Write a script to find if the given string split into two halves of equal
+# lengths and they both have same number of vowels.
+#
+# Example 1
+#
+# Input: $str = "textbook"
+# Output: false
+#
+# 1st half: "text" (1 vowel)
+# 2nd half: "book" (2 vowels)
+#
+# Example 2
+#
+# Input: $str = "book"
+# Output: true
+#
+# 1st half: "bo" (1 vowel)
+# 2nd half: "ok" (1 vowel)
+#
+# Example 3
+#
+# Input: $str = "AbCdEfGh"
+# Output: true
+#
+# 1st half: "AbCd" (1 vowel)
+# 2nd half: "EfGh" (1 vowel)
+#
+# Example 4
+#
+# Input: $str = "rhythmmyth"
+# Output: false
+#
+# 1st half: "rhyth" (0 vowel)
+# 2nd half: "mmyth" (0 vowel)
+#
+# Example 5
+#
+# Input: $str = "UmpireeAudio"
+# Output: false
+#
+# 1st half: "Umpire" (3 vowels)
+# 2nd half: "eAudio" (5 vowels)
+#
+
+package require Tcl 8.6
+package require tcltest
+
+set cases {
+ {"textbook" 0 "Example 1"}
+ {"book" 1 "Example 2"}
+ {"AbCdEfGh" 1 "Example 3"}
+ {"rhythmmyth" 0 "Example 4"}
+ {"UmpireeAudio" 0 "Example 5"}
+}
+
+proc string_alike {str} {
+ set len [string length $str]
+
+ if {$len % 2 != 0} {
+ return 0
+ }
+ set half [expr $len / 2]
+ set first_half [string range $str 0 [expr $half - 1]]
+ set second_half [string range $str $half end]
+
+ set vowel_count_first 0
+ set vowel_count_second 0
+
+ foreach char [split $first_half ""] {
+ if {[string match -nocase {[aeiou]} $char]} {
+ incr vowel_count_first
+ }
+ }
+
+ foreach char [split $second_half ""] {
+ if {[string match -nocase {[aeiou]} $char]} {
+ incr vowel_count_second
+ }
+ }
+
+ if {$vowel_count_first > 0 && $vowel_count_first == $vowel_count_second} {
+ return 1
+ }
+
+ return 0
+}
+
+tcltest::configure -verbose {pass}
+foreach case $cases {
+ tcltest::test [lindex $case 2] {} {
+ string_alike [lindex $case 0]
+ } [lindex $case 1]
+}
+
+exit 0
+
diff --git a/challenge-348/peter-meszaros/tcl/ch-2.tcl b/challenge-348/peter-meszaros/tcl/ch-2.tcl
new file mode 100755
index 0000000000..bfb18852f5
--- /dev/null
+++ b/challenge-348/peter-meszaros/tcl/ch-2.tcl
@@ -0,0 +1,107 @@
+#!/usr/bin/env tclsh
+#
+# Task 2: Convert Time
+#
+# Submitted by: Mohammad Sajid Anwar
+#
+# You are given two strings, $source and $target, containing time in 24-hour time
+# form.
+#
+# Write a script to convert the source into target by performing one of the
+# following operations:
+#
+# 1. Add 1 minute
+# 2. Add 5 minutes
+# 3. Add 15 minutes
+# 4. Add 60 minutes
+#
+# Find the total operations needed to get to the target.
+#
+# Example 1
+#
+# Input: $source = "02:30"
+# $target = "02:45"
+# Output: 1
+#
+# Just one operation i.e. "Add 15 minutes".
+#
+# Example 2
+#
+# Input: $source = "11:55"
+# $target = "12:15"
+# Output: 2
+#
+# Two operations i.e. "Add 15 minutes" followed by "Add 5 minutes".
+#
+# Example 3
+#
+# Input: $source = "09:00"
+# $target = "13:00"
+# Output: 4
+#
+# Four operations of "Add 60 minutes".
+#
+# Example 4
+#
+# Input: $source = "23:45"
+# $target = "00:30"
+# Output: 3
+#
+# Three operations of "Add 15 minutes".
+#
+# Example 5
+#
+# Input: $source = "14:20"
+# $target = "15:25"
+# Output: 2
+#
+# Two operations, one "Add 60 minutes" and one "Add 5 minutes"
+#
+
+package require Tcl 8.6
+package require tcltest
+
+set cases {
+ {{"02:30" "02:45"} 1 "Example 1"}
+ {{"11:55" "12:15"} 2 "Example 2"}
+ {{"09:00" "13:00"} 4 "Example 3"}
+ {{"23:45" "00:30"} 3 "Example 4"}
+ {{"14:20" "15:25"} 2 "Example 5"}
+}
+
+proc convert_time {times} {
+ set source [lindex $times 0]
+ set target [lindex $times 1]
+
+ set sl [split $source ":"]
+ set sh [scan [lindex $sl 0] %d]
+ set sm [scan [lindex $sl 1] %d]
+ set tl [split $target ":"]
+ set th [scan [lindex $tl 0] %d]
+ set tm [scan [lindex $tl 1] %d]
+
+ set source_mins [expr $sh * 60 + $sm]
+ set target_mins [expr $th * 60 + $tm]
+ if {$target_mins < $source_mins} {
+ set target_mins [expr $target_mins + 24 * 60]
+ }
+ set diff [expr $target_mins - $source_mins]
+ set ops 0
+ foreach add {60 15 5 1} {
+ while {$diff >= $add} {
+ set diff [expr $diff - $add]
+ incr ops
+ }
+ }
+ return $ops
+}
+
+tcltest::configure -verbose {pass}
+foreach case $cases {
+ tcltest::test [lindex $case 2] {} {
+ convert_time [lindex $case 0]
+ } [lindex $case 1]
+}
+
+exit 0
+