The Weekly Challenge ‐ Perl and Raku

CY's Take on The Weekly Challenge #119 ‐ Learning New Terms and Languages

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

Do tell me, if I am wrong or you strongly oppose my statements!

It's time for challenges in Week #119 !


Task 1: Swap Nibbles

image info

All coders or IT professionals know the term "byte". Thanks for this task, I have learnt the term "nibble".

I made an one-liner from the knowledge learnt in the recent tasks.


print oct ("0x". scalar reverse sprintf("%02x", $ARGV[0])) ,"\n";
 

The "2" in %02x made an advantage on the "simplicity condition" of the task. #Edited on 7th July

For a bash version script, I made an advantage of the "simplicity condition" of the task ‐ the integers input are between 1 to 255:

 
A=$(($N/16)) B=$(($N%16)) echo $(($B*16+$A))

Task 2: Sequence without 1-on-1

Yes, my coding puzzle creation again!

I intentionally make up a sequence not on The On-Line Encyclopedia of Integer Sequences (OEIS). However, its "prototype" is on OEIS. Before I coded it, actually I took the list of numbers from the A007932 on OEIS; its title is "Numbers that contain only 1's, 2's and 3's". Then I simply used some scripts and text editor to remove terms with "11", wrote a description, and mailed this puzzle out.

This one is easier but it seems that it also receives some positive comments from team members as the puzzled Knight.

Back to the task. My way of coding it can be said as accumulation. I use the terms of length N to produce terms of length N+1. If someone is mindful enough, if the terms in length N has no "11", "11" won't appear in the terms of length N+1 if we do not append those length-N terms ended with "1".

Here is the main part of my code:

 
sub noo { my $p = $_[0]; # sth sth sth while ($#arr < $p) { $y = $#arr; for my $here ($x+1..$y) { push @arr, $arr[$here]."1" if substr($arr[$here], -1, 1) ne "1"; push @arr, $arr[$here]."2"; push @arr, $arr[$here]."3"; } $x = $y; } # sth sth sth }

Not very fancy but does its responsibility.

Awk and Bash

I have learnt the essence of the awk language and played with it on the previous two weeks (Week 117 Task 1 and Week 118 Task 1). It is specific designed for text-file operations and can handle text data by buck with simple scripting. And now shift to learn bash scripting. awk is rather advanced, or say, more modern ‐ compared with bash.

bash is rather harsh on spacing sometimes. I have to get used to its nature. As it was the first encounter with bash, I had fought with it for about 3 hours for porting the Perl codes into bash codes yesterday. For practical use, a coder or sysadmin has to remember some commons file handling parameters.

---

Stay alert and healthy! □


The image of the hex table is from a screenshot of the Wikipedia page 十六进制 (base 16 in zh.wikipedia.org); the image is released under Creative Commons Attribution-ShareAlike 3.0 License.

link for CY's full codes: ch-1.pl, ch-2.pl, ch-1.sh, ch-2.sh


Contact on twitter: @e7_87.

Created Date: 5th July, 2021 (HKT). Last Update: 7th July, 2021 14:12 (HKT)