CY's Take on PWC#083

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).

... before coding

Long time no blogging for The Weekly Challenge!

I found that I use "and/or" quite frequently in writing. I know, (mathematical-)logically we only need "or". It seems to me to be a language tricky part as the use of gender neutral terms.

I made a logical error for the "Lonely X" task in challenge 077.

$segment =~ s/XI/II/g;
$segment =~ s/IX/II/g;
$segment =~ s/XX/II/g;

should be

$segment =~ s/XX/II/g;
$segment =~ s/XI/II/g;
$segment =~ s/IX/II/g;

Task 1 Words Length

I immediately checked out the "Llama book" and checked the usage of match variables in regular expressions. The result is a 3-line solution! Yo:

$_ = <STDIN>;
/(^\w+\s)((\w+\s*)+)(\s\w+$)/;
print length $2,"\n";


Oooooooooooops, it works for 3-word string.

I discover a big flaw during blogging.
It should be:

$_ = <STDIN>;
/(^\w+\s)([\w\s*]+)(\s\w+$)/;
$_ = $2;
s/\s//g;
print length"\n";


Task 2 Flip Array

I have thought of whether there are some effective algorithms for this task.

One natural thought is -- sorting first! From large to small... Then the following steps are... If the partial sum is negative, there is at least one minus sign in the remaining of the array if we put plus sign in front of the larger element. OK, then we probably should have some recursions; the following steps are... there is at least one minus sign...

Oh, it seems that there are no special ways.

Writing the brute-force solution for this task is maybe the only effective way, but the code is not as simple as I thought. The full code spends approximately 60~100 lines.

For input of n numbers, I use a bit-string of length n to test the sum of the array (with signs):

for my $b (1..(2**scalar @arr- 1) { # I realize that it can be simplified to "(2**scalar @arr) - 2" while blogging
    my $allsign = make_bit_string(scalar @arr,$b); #***#
    my $sum = sum_accord_to_bit_string(\@arr , $allsign); #***#
    if ( $sum <= $recordmin and $sum >= 0 ) {
        $recordflip = scalar @arr if $sum < $recordmin;
        $recordmin = $sum;
        my $num_of_1s = num_of_1s_in_a_bit_string $allsign;
        if ($recordflip > $num_of_1s and $sum == $recordmin) {
            $recordflip = $num_of_1s
            $ans_arr = $allsign;
        }
    }
}

Making the test cases are one of challenging part of this task. For subroutines, make_bit_string and sum_accord_to_bit_string are in my usual styles for enumeration. I initialize $recordflip as scalar @arr ahead of this chosen part of code.

... personal

coding and money

What a woperchild I am # -- I want to be more mature!

Hey, no Perl blogs for a few weeks. (And still cannot get a nicer place for blogging. dev.to looks popular.) Am I getting lazy or busy?

No, I am being crazy. I have put a resignation letter for my restaurant/cafe assistant position in this COVID-19 time in order to study to be a full-time coder. Hence I am busy with settling my daily tasks to other employees. Being a math-related professional is my childhood dream.

Since "upsettingly" Perl has been rare in the coding job market in Hong Kong (probably, not just in Hong Kong... I read the blogposts from an expert participant of The Weekly Challenge this month and in his book he mentions the situation in UK -- difficult to hire Perl coders, codebases change to other languages...), I have to learn the popular languages from scratch and I decide to take Java. I like LISP and my Common Lisp solutions, but LISP positions are more rare than Perl positions. Python is too close to Perl and I have picked up the rough picture fast. C++ has platform-dependency issues to be taken care but I would like to stick with Linux. Golang and Rust are "fresh" and hot but not yet hot enough to serve here. I am not good at art and want to do backend developer, therefore say bye to JavaScript and the latest HTML/CSS specifications. By exclusion, I choose Java.

Luckily I have been a tutor since this month on math and science subjects for a secondary school student (again, partly being a teacher, I have to be more careful in digital life) and I have some savings.

Somehow I dedicated ... I will dedicate to who need to thank (hope I am careful enough to consider all the "cases") when I get stable in a full-time coding career.

CPAN - contract bridge

After doing the champion interview in September, I find that there is all knowledge I need for contributing a CPAN module in the "Alpaca book" ready for me.

I should take action on what I said within the coming few months. (For OO Perl, I guess I will grasp it better after learning a new programming language.)

Last words: reading the "Camel book" from the back recently. (The authors suggest people familiar with "top-down thinking" read the book from the back to front chapters -- not my crazy idea -- , and it works.)

Mental Health

By the way, let all participants of The Weekly Challenge wish good health for Mohammad S Anwar, (I am not a representative but I think everyone read that message feel the same) as he mentioned about the depression and anxiety. :)


Stay alert and healthy! □

Remark:

"Llama book": Learning Perl (RL Schwartz, Tom Phoenix, brian d foy; O'Reilly)
"Alpaca book": Intermediate Perl (RL Schwartz, brian d foy, Tom Phoenix; O'Reilly)
"Camel book": Programming Perl (Tom Christiansen, brian d foy, Larry Wall; O'Reilly)

#: a logical error example taken from the book Java: How to Program, Early Objects (Paul Deitel, Harvey Deitel; Pearson), Chapter 1

link for codes: ch-1.pl, ch-2.pl

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