CY's take on Perl Weekly Challenge #059

linked_item.png

This is a part of Perl Weekly Challenge(PWC) #059 and the followings are related to my solution. If you want to challenge yourself on Perl, go to https://perlweeklychallenge.org, code the latest challenges, submit codes on-time (by GitHub or email) if possible, before reading my blog post.

(Finally, I installed a Linux distribution in my laptop -- my choice is Linux Mint (a distribution forked from earlier Ubuntu) .) (In Hong Kong, there are no shops selling Linux-installed/Linux-Windows-dual-boot laptop. People[1] are too rich and just buy Windows pre-installed laptops or MacBooks.(???) ) (This is not my first time to own a laptop with Linux but this time I am more serious about the system setting.)

The COVID-19 virus pandemic is under control in Hong Kong these two week. What a piece of good news.

Perl Weekly Challenge #058

Last week I just did the second task because after reading the problem statement, some while later, I realize it is just a problem of inversion in discrete mathematics. Afterwards I searched the related keywords, and quickly implemented the algorithm stated.

There was a little twist, though. I just implemented the algorithm but not the "problem" -- I had missed "the problematic cases of the problem". Okay, simply speaking, there had been no error messages for invalid constraints. (Lazily Fixed.)

A problem of understanding problems and its consequences is going to be presented.


Perl Weekly Challenge #059

Task 1 Linked List

Back for this week now. The first problem LOOK LIKE straight-forward if we handle the item in the ARRAY one-by-one, in a way of ... I have SECRETLY been reading about LISP and code a Common Lisp script; it was coded before my little Perl script:
;(setf *line* '(1 4 3 2 5 2))
;(setf *K* 3)
(setf *line* '(5 6 3 2 7 9))
(setf *K* 6)

(setf *small* nil)
(setf *large* nil)

(defun newas (R)
      (append *small* (cons R nil))
)

(defun newal (R)
      (append *large* (cons R nil))
)

(defun biway (R)
   (if (> *K* R)
      (setf *small* (newas R))
      (setf *large* (newal R))
   )
)

(loop for R in *line*
   do (biway R)    
)

(print (append *small* *large*))
Here is the main part of my little Perl script
sub newlist {
    my @small = ();
    my @large = ();
    my ($K@line) = @_;
    foreach(@line) {
       if ($_ < $K) {
            push @small$_;
        }
        else {
            push @large$_;
        }
    }

    return (@small@large);
}

Well, which one do you like more? :) ...

WAIT!!! BUT THEY ARE NOT IMPLEMENTATION OF LINKED LISTS!

I become alert on this constraint after looking PWC members' codes and blogs yesterday. Many really do implement linked lists. Should not be TOO lazy. Then I have coded a linked list version today, starting at noon.

Maybe on applications, some programmers need a linked-list in-place program.

Revise my basic OO Perl.

Here are some critical parts of my perl script:

package item;

    sub new
    {
        my ($class) = @_;
        bless {
            _value => $_[1],
            _next => $_[2],
        }, $class;
    }

    sub value { $_[0]->{_value}}

    sub inext {
        my ($self$newnext) = @_;
        $self->{_next} = $newnext if defined($newnext);
        return $self->{_next};
    }

    sub unlink {
        my ($self) = @_;
        $self->{_next} = undef;
        return $self->{_next};
    }
}

...

my $HELPER = -1;
make_linked_list($HELPER@input);

$smallpivot = $head;
$inusepivot = ${$head}->inext;

...

#inside a while loop

    if ($$inusepivot->value < $K) {
        $$previous->unlink;
        $$previous->inext$$inusepivot->inext );

        $$inusepivot->unlink;
        $$inusepivot->inext($$smallpivot->inext);

        $$smallpivot->unlink;
        $$smallpivot->inext($inusepivot);
        $smallpivot = $inusepivot;

    }
#...

The unlink class method is not essential here; but when I code better, I would like to[2] code a "safe" inext.

Task 2 Flip Binary

It is at ease with bitwise operations, though I wanted to produce a subroutine adding leading zeros to a binary string before I found bitwise operations "KO" the task. Well, we also need %b in sprintf.

(sprintf and printf in Perl have the %b option; printf in C standard does not support %b -- mentioned because I had been confused by a C reference book for somewhat half an hour .)

P.S.: Have started use Test::Simple this task.


My code can be found in github.


Life Besides Code

24-hr is not enough! The difficulty mainly lies on the obstacles in self-study. Grab a close example: the Common Lisp code above is my first trial of a LISP program, and it spent me 2.5 hr. The linked list code spend my whole day of dayoff. On the other hand, I do believe many valuable and fascinating human endowments have steep learning curves -- the steep slope makes them valuable and extraordinary; with more knowledge on the fundamentals, the speed of study might be improved sometimes -- my simple belief, maybe my placebo of recent self-study on many stuff in math or now computing .

Okay, I am happy with coding and especially happy with the Unicode support on Linux Mint. I can type Chinese characters on terminal and Vim without clumsy settings. Woo, Larry Wall also has worked under Linux Mint, according his 2016 Interview on the Slashdot. It is a comfortable coding environment.

By the way, as an ad for PWC, my data-scientist friend said that my study is more structured after joining the PWC. :) Previously I would be distracted by reading novels, playing contract bridge, barfoobarfeebar... PWC has effectively pushed me write one or two complete codes once a week.

No one knows the future. Okay, stay optimistic for the future. Stay healthy! □

[1] Hong Kong has a high Gini coefficient among the of developed regions/countries.
[2] Don't be annoyed by my aggression.

1 Comment

It was pleasant to know that Perl Weekly Challenge made a positive change in your study. Keep it up the great work.

Leave a comment

About C.-Y. Fung

user-pic This blog is inactive and replaced by https://e7-87-83.github.io/coding/blog.html ; but I post highly Perl-related posts here.