Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

challenge-311 #11680

Merged
merged 1 commit into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions challenge-311/peter-meszaros/perl/ch-1.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env perl
#
=head1 Task 1: Upper Lower

Submitted by: Mohammad Sajid Anwar

You are given a string consists of english letters only.

Write a script to convert lower case to upper and upper case to lower in the
given string.

=head2 Example 1

Input: $str = "pERl"
Output: "PerL"

=head2 Example 2

Input: $str = "rakU"
Output: "RAKu"

=head2 Example 3

Input: $str = "PyThOn"
Output: "pYtHoN"

=cut

use strict;
use warnings;
use Test2::V0 -no_srand => 1;
use Data::Dumper;

my $cases = [
["pERl", "PerL", "Example 1"],
["rakU", "RAKu", "Example 2"],
["PyThOn", "pYtHoN", "Example 3"],
];

sub upper_lower
{
my $str = shift;
$str =~ tr/A-Za-z/a-zA-Z/;
return $str;
}

for (@$cases) {
is(upper_lower($_->[0]), $_->[1], $_->[2]);
}
done_testing();

exit 0;
74 changes: 74 additions & 0 deletions challenge-311/peter-meszaros/perl/ch-2.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/usr/bin/env perl
#
=head1 Task 2: Group Digit Sum

Submitted by: Mohammad Sajid Anwar

You are given a string, $str, made up of digits, and an integer, $int, which is
less than the length of the given string.

Write a script to divide the given string into consecutive groups of size $int
(plus one for leftovers if any). Then sum the digits of each group, and
concatenate all group sums to create a new string. If the length of the new
string is less than or equal to the given integer then return the new string,
otherwise continue the process.

=head2 Example 1

Input: $str = "111122333", $int = 3
Output: "359"

Step 1: "111", "122", "333" => "359"

=head2 Example 2

Input: $str = "1222312", $int = 2
Output: "76"

Step 1: "12", "22", "31", "2" => "3442"
Step 2: "34", "42" => "76"

=head2 Example 3

Input: $str = "100012121001", $int = 4
Output: "162"

Step 1: "1000", "1212", "1001" => "162"

=cut

use strict;
use warnings;
use Test2::V0 -no_srand => 1;
use Data::Dumper;
use List::Util qw(sum0);

my $cases = [
[["111122333", 3], 359, "Example 1"],
[["1222312", 2], 76, "Example 2"],
[["100012121001", 4], 162, "Example 3"],
];

sub group_digit_sum
{
my $str = $_[0]->[0];
my $int = $_[0]->[1];

my $new_str;
while (length($str) > $int) {
$new_str = '';
for (my $i = 0; $i < length($str); $i += $int) {
my $s = substr($str, $i, $int);
$new_str .= sum0 split(//, $s);
}
$str = $new_str;
}
return $new_str;
}

for (@$cases) {
is(group_digit_sum($_->[0]), $_->[1], $_->[2]);
}
done_testing();

exit 0;
57 changes: 57 additions & 0 deletions challenge-311/peter-meszaros/tcl/ch-1.tcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/usr/bin/env tclsh
#
# Task 1: Upper Lower
#
# Submitted by: Mohammad Sajid Anwar
#
# You are given a string consists of english letters only.
#
# Write a script to convert lower case to upper and upper case to lower in the
# given string.
#
# Example 1
#
# Input: $str = "pERl"
# Output: "PerL"
#
# Example 2
#
# Input: $str = "rakU"
# Output: "RAKu"
#
# Example 3
#
# Input: $str = "PyThOn"
# Output: "pYtHoN"
#

package require tcltest

set cases {
{"pERl" "PerL" "Example 1"}
{"rakU" "RAKu" "Example 2"}
{"PyThOn" "pYtHoN" "Example 3"}
}

proc upper_lower {p} {
set l [split $p ""]
foreach c $l {
if {[string is upper $c]} {
append r [string tolower $c]
} else {
append r [string toupper $c]
}
}
return $r
}

#
tcltest::configure -verbose {pass}
foreach case $cases {
tcltest::test [lindex $case 2] {} {
upper_lower [lindex $case 0]
} [lindex $case 1]
}

exit 0

75 changes: 75 additions & 0 deletions challenge-311/peter-meszaros/tcl/ch-2.tcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/usr/bin/env tclsh
#
# Task 2: Group Digit Sum
#
# Submitted by: Mohammad Sajid Anwar
#
# You are given a string, $str, made up of digits, and an integer, $int, which is
# less than the length of the given string.
#
# Write a script to divide the given string into consecutive groups of size $int
# (plus one for leftovers if any). Then sum the digits of each group, and
# concatenate all group sums to create a new string. If the length of the new
# string is less than or equal to the given integer then return the new string,
# otherwise continue the process.
#
# Example 1
#
# Input: $str = "111122333", $int = 3
# Output: "359"
#
# Step 1: "111", "122", "333" => "359"
#
# Example 2
#
# Input: $str = "1222312", $int = 2
# Output: "76"
#
# Step 1: "12", "22", "31", "2" => "3442"
# Step 2: "34", "42" => "76"
#
# Example 3
#
# Input: $str = "100012121001", $int = 4
# Output: "162"
#
# Step 1: "1000", "1212", "1001" => "162"
#

package require tcltest

set cases {
{{"111122333" 3} 359 "Example 1"}
{{"1222312" 2} 76 "Example 2"}
{{"100012121001" 4} 162 "Example 3"}
}

proc ladd l {
if {![llength $l]} {return 0}
return [expr [join $l +]]
}

proc group_digit_sum {p} {
set str [lindex $p 0]
set int [lindex $p 1]

set new_str {}
while {[string length $str] > $int} {
set new_str {}
for {set i 0} {$i < [string length $str]} {incr i $int} {
set s [string range $str $i [expr $i + $int - 1]]
append new_str [ladd [split $s {}]]
}
set str $new_str
}
return $new_str
}

tcltest::configure -verbose {pass}
foreach case $cases {
tcltest::test [lindex $case 2] {} {
group_digit_sum [lindex $case 0]
} [lindex $case 1]
}

exit 0