Skip to content

Commit f384f62

Browse files
authored
Merge pull request #11680 from pme/challenge-311
challenge-311
2 parents 9097b5f + 7af56e3 commit f384f62

File tree

4 files changed

+258
-0
lines changed

4 files changed

+258
-0
lines changed
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env perl
2+
#
3+
=head1 Task 1: Upper Lower
4+
5+
Submitted by: Mohammad Sajid Anwar
6+
7+
You are given a string consists of english letters only.
8+
9+
Write a script to convert lower case to upper and upper case to lower in the
10+
given string.
11+
12+
=head2 Example 1
13+
14+
Input: $str = "pERl"
15+
Output: "PerL"
16+
17+
=head2 Example 2
18+
19+
Input: $str = "rakU"
20+
Output: "RAKu"
21+
22+
=head2 Example 3
23+
24+
Input: $str = "PyThOn"
25+
Output: "pYtHoN"
26+
27+
=cut
28+
29+
use strict;
30+
use warnings;
31+
use Test2::V0 -no_srand => 1;
32+
use Data::Dumper;
33+
34+
my $cases = [
35+
["pERl", "PerL", "Example 1"],
36+
["rakU", "RAKu", "Example 2"],
37+
["PyThOn", "pYtHoN", "Example 3"],
38+
];
39+
40+
sub upper_lower
41+
{
42+
my $str = shift;
43+
$str =~ tr/A-Za-z/a-zA-Z/;
44+
return $str;
45+
}
46+
47+
for (@$cases) {
48+
is(upper_lower($_->[0]), $_->[1], $_->[2]);
49+
}
50+
done_testing();
51+
52+
exit 0;
+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/usr/bin/env perl
2+
#
3+
=head1 Task 2: Group Digit Sum
4+
5+
Submitted by: Mohammad Sajid Anwar
6+
7+
You are given a string, $str, made up of digits, and an integer, $int, which is
8+
less than the length of the given string.
9+
10+
Write a script to divide the given string into consecutive groups of size $int
11+
(plus one for leftovers if any). Then sum the digits of each group, and
12+
concatenate all group sums to create a new string. If the length of the new
13+
string is less than or equal to the given integer then return the new string,
14+
otherwise continue the process.
15+
16+
=head2 Example 1
17+
18+
Input: $str = "111122333", $int = 3
19+
Output: "359"
20+
21+
Step 1: "111", "122", "333" => "359"
22+
23+
=head2 Example 2
24+
25+
Input: $str = "1222312", $int = 2
26+
Output: "76"
27+
28+
Step 1: "12", "22", "31", "2" => "3442"
29+
Step 2: "34", "42" => "76"
30+
31+
=head2 Example 3
32+
33+
Input: $str = "100012121001", $int = 4
34+
Output: "162"
35+
36+
Step 1: "1000", "1212", "1001" => "162"
37+
38+
=cut
39+
40+
use strict;
41+
use warnings;
42+
use Test2::V0 -no_srand => 1;
43+
use Data::Dumper;
44+
use List::Util qw(sum0);
45+
46+
my $cases = [
47+
[["111122333", 3], 359, "Example 1"],
48+
[["1222312", 2], 76, "Example 2"],
49+
[["100012121001", 4], 162, "Example 3"],
50+
];
51+
52+
sub group_digit_sum
53+
{
54+
my $str = $_[0]->[0];
55+
my $int = $_[0]->[1];
56+
57+
my $new_str;
58+
while (length($str) > $int) {
59+
$new_str = '';
60+
for (my $i = 0; $i < length($str); $i += $int) {
61+
my $s = substr($str, $i, $int);
62+
$new_str .= sum0 split(//, $s);
63+
}
64+
$str = $new_str;
65+
}
66+
return $new_str;
67+
}
68+
69+
for (@$cases) {
70+
is(group_digit_sum($_->[0]), $_->[1], $_->[2]);
71+
}
72+
done_testing();
73+
74+
exit 0;
+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/env tclsh
2+
#
3+
# Task 1: Upper Lower
4+
#
5+
# Submitted by: Mohammad Sajid Anwar
6+
#
7+
# You are given a string consists of english letters only.
8+
#
9+
# Write a script to convert lower case to upper and upper case to lower in the
10+
# given string.
11+
#
12+
# Example 1
13+
#
14+
# Input: $str = "pERl"
15+
# Output: "PerL"
16+
#
17+
# Example 2
18+
#
19+
# Input: $str = "rakU"
20+
# Output: "RAKu"
21+
#
22+
# Example 3
23+
#
24+
# Input: $str = "PyThOn"
25+
# Output: "pYtHoN"
26+
#
27+
28+
package require tcltest
29+
30+
set cases {
31+
{"pERl" "PerL" "Example 1"}
32+
{"rakU" "RAKu" "Example 2"}
33+
{"PyThOn" "pYtHoN" "Example 3"}
34+
}
35+
36+
proc upper_lower {p} {
37+
set l [split $p ""]
38+
foreach c $l {
39+
if {[string is upper $c]} {
40+
append r [string tolower $c]
41+
} else {
42+
append r [string toupper $c]
43+
}
44+
}
45+
return $r
46+
}
47+
48+
#
49+
tcltest::configure -verbose {pass}
50+
foreach case $cases {
51+
tcltest::test [lindex $case 2] {} {
52+
upper_lower [lindex $case 0]
53+
} [lindex $case 1]
54+
}
55+
56+
exit 0
57+
+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/usr/bin/env tclsh
2+
#
3+
# Task 2: Group Digit Sum
4+
#
5+
# Submitted by: Mohammad Sajid Anwar
6+
#
7+
# You are given a string, $str, made up of digits, and an integer, $int, which is
8+
# less than the length of the given string.
9+
#
10+
# Write a script to divide the given string into consecutive groups of size $int
11+
# (plus one for leftovers if any). Then sum the digits of each group, and
12+
# concatenate all group sums to create a new string. If the length of the new
13+
# string is less than or equal to the given integer then return the new string,
14+
# otherwise continue the process.
15+
#
16+
# Example 1
17+
#
18+
# Input: $str = "111122333", $int = 3
19+
# Output: "359"
20+
#
21+
# Step 1: "111", "122", "333" => "359"
22+
#
23+
# Example 2
24+
#
25+
# Input: $str = "1222312", $int = 2
26+
# Output: "76"
27+
#
28+
# Step 1: "12", "22", "31", "2" => "3442"
29+
# Step 2: "34", "42" => "76"
30+
#
31+
# Example 3
32+
#
33+
# Input: $str = "100012121001", $int = 4
34+
# Output: "162"
35+
#
36+
# Step 1: "1000", "1212", "1001" => "162"
37+
#
38+
39+
package require tcltest
40+
41+
set cases {
42+
{{"111122333" 3} 359 "Example 1"}
43+
{{"1222312" 2} 76 "Example 2"}
44+
{{"100012121001" 4} 162 "Example 3"}
45+
}
46+
47+
proc ladd l {
48+
if {![llength $l]} {return 0}
49+
return [expr [join $l +]]
50+
}
51+
52+
proc group_digit_sum {p} {
53+
set str [lindex $p 0]
54+
set int [lindex $p 1]
55+
56+
set new_str {}
57+
while {[string length $str] > $int} {
58+
set new_str {}
59+
for {set i 0} {$i < [string length $str]} {incr i $int} {
60+
set s [string range $str $i [expr $i + $int - 1]]
61+
append new_str [ladd [split $s {}]]
62+
}
63+
set str $new_str
64+
}
65+
return $new_str
66+
}
67+
68+
tcltest::configure -verbose {pass}
69+
foreach case $cases {
70+
tcltest::test [lindex $case 2] {} {
71+
group_digit_sum [lindex $case 0]
72+
} [lindex $case 1]
73+
}
74+
75+
exit 0

0 commit comments

Comments
 (0)