[Personal Review] Codes from The Weekly Challenge Week 095-105

If you want to challenge yourself on programming, especially on Perl and/or Raku, go to https://perlweeklychallenge.org, code the latest challenges, submit codes on-time (by GitHub or email).

-------------------------------------

My coding momentum is a bit low.

Reflections on the codes I have written:

#095

Task 1: Palindrome Number
TMTOWTDI. On this seemly and actually simple task, I chose to compare the digit one by one.

Task 2: Demo Stack
A bit smell of laziness. I did not provide functions when stack is empty and pop() or min() is called.

#096

Task 1: Reverse Words
A lesson on extra-white space. Oppositely but as lack of caution as a sin, this morning (GMT+8) I found I have forgotten a newline for my code for #105 Task 1.

Task 2: Edit Distance
That was a standard computer science exercise. I was astounded by reading Mr Abigail's blog on the approach on saving memory space.

#097

Task 1: Caesar Cipher
Trivial.

Task 2: Binary Substrings
Seems to be weird at the first sight, but much simpler after "fourth" thought.
I wish I would have a head for calm analysis when it comes to a more time-limited situation.

#098

Task 1: Read N-characters
Trivial for Perl long-term user, but I had not known read before.

Task 2: Search Insert Position
Put a binary search tree as solution. Actually it was modified from some codes for rosalind.info .

#099

Task 1: Pattern Match
KOed by regex.

Task 2: Unique Subsequence
Quite a loaded task for me. idea->input(letter by letter);

Output: a procedural script.

#100

Task 1: Fun Time
When I was in secondary school, one of my class teachers is a strict English teacher. He said we should say "Good noon Mr Chan" if the class begins at 12:00 nn. Yeah, 12:00 nn.

Task 2: Triangle Sum
Ignoring instructions, people took the fastest way: from bottom to top traversal.

I think I am over-unpredictable on how strict I follow the examples or task instructions.

#101
My laziest week recently.

Task 1: Pack a Spiral
Back to a few years ago, I read a similar task on a competitive programming guide book. At that time I have no clues. A mark of improvement.

Task 2: origin-containing triangle
But still thanks Mr S. Little introduce some basic computer graphics task.

#102

Task 1: Rare Numbers
As said, it is faster to generate the numbers, instead of checking the natural numbers one after one... I let go of my previous experience.

Task 2: Hash-counting String
I used recursion for the task. Mr J. Smith's two elegant lines save the recursion, no wonder he gets the team title of Champion of Feb 2021 soon after he enters the team.

#103

Task 1: Chinese Zodiac
Trivial.

Task 2: What’s playing?
I got the "wrong answer" at first. Anyway, I like this task as its nature suggests me getting experience of some of the CPAN modules.

Extra:
Instead of using the Test::XXXXXX module, I wrote a short script for test. (Reinventing or whatever, I know there is a book on Perl Testing, I will check it.) This helps me get on the latest #105 -- I undergo time travel again.

Here is my testing script:

#!/usr/bin/perl
use strict;
use warnings;
# Testing script for
#The Weekly Challenge #103 Task 1

my %data_ret = 
   (2017 => "Fire Rooster",
    1938 => "Earth Tiger",
    1997 => "Fire Ox",
    1990 => "Metal Horse",
    1967 => "Fire Goat",
   );


my $program = "perl ch-1.pl"
#MODIFY FOR DIFFERENT USES

# ============================= #

my $num_of_success = 0;

for my $parameter (keys %data_ret) {
  my $test_return = `$program $parameter`
  chomp($test_return);
  if ($test_return eq $data_ret{$parameter}) {  
  #CHANGE == INTO "eq" for non-numeric
      print "test case parameter $parameter: passed \n";
      $num_of_success++;
  } 
  else {
      print "test case parameter $parameter: failed \n";
  }
}

print "\n";
print "done ",  scalar keys %data_ret" test case(s); PASS: $num_of_success case(s) .\n";

#104

Task 1: FUSC Sequence
Seems easy? I submitted a seriously bugged script, just fix it after reading others' blog in the afternoon on Monday.

Task 2 NIM Game:
The winning strategy is well-known if you like math or have read some math expositions for laymen.

I enjoy programming for traditional games.

#105

Task 1: Nth Root
My interpretation: N is an integer.

I think thtat using log with exp throws out the interesting mathematics. My first thought after seeing the task statement is using Newton's method. However, I was afraid of numerical analysis and delayed coding til early morning of the 29th March.

I tried two approaches. One is Newton's method, one is basic integer for-loop. Then I compare their results making use of a version of my testing script and apply warnings to users.

$ perl ch-1.pl 10 1048578
WARN: Recommend to take the result from Newton's method if two methods dispute WARN: N is large; probably dispute between two methods By lazy method: 4.00 By Newton's method: 4.00

$ cat tester_105-ch-1.pl
...
for (1..100) {
my $temp_N = 2 + int rand(9);
my $temp_k = rand(3000);
$data_ret{"$temp_N $temp_k"} = lazy_method($temp_N, $temp_k);
}

my $program = "perl ch-1_newton.pl"; #MODIFY FOR DIFFERENT USES
...

$ perl tester_105-ch-1.pl
...
test case parameter 10 1708.35583396219: failed
got 2.11 , expect: 2.10
...
done 100 test case(s); PASS: 99 case(s) .

Maybe I need a rest, maybe I need a clearer goal in programming, maybe I should just enjoy the logic challenges. I don't know yet.

Anyway, I discover that I had forgotten a newline character after a sleep. A confession here. □


Leave a comment

About C.-Y. Fung

user-pic a beginner in programming; passionate in mathematics, distance running and gender issues; from Hong Kong. https://twitter.com/e7_87