Saturday, 8 March 2025

Switch Case?

TWC-311

Life Bytes

Before going into the code let me just reflect on the last time I submitted my solution to PWC. That was in 2021. Around 3 years back. That was also the first time I did a submission. I was an avid perl developer at that time. Although I learnt C, python and java programming languages before learning perl, I have a special liking towards perl because of it's versatility, TIMTOWTDI, The three virtues by Larry Wall among other things. Now I have transitioned to a python developer because of reasons not relevant to this discussion but I am still passionate about perl which pulled me back to TWC. I just realised while writing this that PWC has now become TWC!


One interesting observation I had about the EZPWC tool which simplifies submitting these challenges to github is that it can be pronounced as "easy pveesy". Very similar to "easy peasy" i.e how it makes the submission process. I wonder if the creator(s) call it that way. I found that to be really cool. On the same wavelength of coolness is that the REPL(Read Evaluate Print Loop) for perl can be started by just running the "re.pl" command. How cool is that! Anyway, enough of my ramblings, let's get right into code.


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"

Analysis

I first checked if perl has some subroutine to do this. Surprisingly there wasn't a ready to use helper function to do this in perl. So, I finally got the chance to use the tr operator in perl which I only used while learning perl but never after. Since the question explicitly states that the string consists of english letters only, such a simple solution is acceptable.


Code
print("Enter a nice string: "); my $s = <STDIN>; $s =~ tr/a-zA-Z/A-Za-z/; print("Switched Case version of the above string: " . $s);



Sample Output

$ ch-1.pl
Enter a nice string: Happy New Year
Switched Case version of the above string: hAPPY nEW yEAR
$ ch-1.pl
Enter a nice string: REALLY
Switched Case version of the above string: really
$ ch-1.pl
Enter a nice string: omg
Switched Case version of the above string: OMG

Bonus

  • Since the code was small, I also wrote it as a perl one liner
  • perl -e '$ARGV[0] =~ tr/a-zA-Z/A-Za-z/; print($ARGV[0]."\n")' $1
  • Another sidenote is that python happens to have a function called swapcase to do exactly this.
  • The title of this blog is based on this problem. What should be the name of a function which does this kind of functionality? Switch Case immediately came to my mind but the field of programming already has overloaded terms all around us. Another commonly used term being overloaded with a new meaning is the last thing we need right now. So, this can be called swapcase following the pythonic way.


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"
Analysis

I initially misread the question and thought that the sum of the groups of substrings should be done. After a second reading of the challenge, I got to know exactly what is to be done.
There are many interesting parts to this challenge like breaking down a string into equal substrings of a constant length, splitting a string into characters etc. For breaking down a string into chunks, I made use of one other concept that I rarely got to use, the unpack function.
I never ignore the opportunity to use recursion if it makes sense. So here is the final solution combining all the above points.

Code
use strict;
use warnings; use List::Util qw(sum); sub reduce_string { my ($string, $number) = @_; my @strings; print "groups: "; foreach my $chunk (unpack("(A$number)*", $string)) { print "$chunk "; my $local_sum = sum split("", $chunk); push @strings, $local_sum; } print "\ngroups with their sums: @strings\n"; my $new_string = join("", @strings); if (length($new_string) > $number) { print "new string: ".$new_string."\n"; $new_string = reduce_string($new_string, $number); } return $new_string; } print "Enter a random number of random length: "; my $string = <STDIN>; print "Enter any number smaller than the number of digits in previous number: "; my $number = <STDIN>; print("Final reduced string: " . reduce_string($string, $number) . "\n");






















Sample Output

$ ch-2.pl
Enter a random number of random length: 1234567
Enter any number smaller than the number of digits in previous number: 3
groups: 123 456 7
groups with their sums: 6 15 7
new string: 6157
groups: 615 7
groups with their sums: 12 7
Final reduced string: 127

Call for help

Please suggest me some platform for my next blog where markdown can be used for formatting. This will probably be my last blog on this platform which still uses only html. Extremely cumbersome and inconvenient. Happy coding to coders all around the world!

No comments:

Post a Comment