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 c…

OO linked lists in Perl

After many days, trying to implement linked lists by nested hash (link to Rosetta Code) (link to my code) or Struct::Dumb, I get how to write the (singly) linked list in object-oriented style by Perl. One with bless, another one with Moose. Keep the learning record here.

Updated: See the link in comment section of Tobyink, a showcase of his OO module Zydeco. Thanks Toby!

#reference: “Object Oriented Perl” by Damian Conway 

use strict;
use warnings;

{
package SLL::Node;
use strict;

=pod
sub new {
    my $class = $_[0];
    my $objref = {
        _value => $_[1],
        _nextnode => $_[2],
    };
    bless $objref, $class;
    return $objref;
}

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


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

sub nextnode { $_[0]->{_nextnode} }

}


my $node_a = SLL::Node->new(30undef);
my $node_b = SLL::Node->new(40, \$node_a);
my $node_c = SLL::Node->new(70, \$node_b);

my $node_head = SLL::Node->new(undef, \$node_c);

my $node = $node_head;

do {
    $node = ${$node->nextnode};
    print $node->value ;
while ($node->nextnode);

print \n;

#print 70 40 30


#reference: various resources on Moose

use strict;
use warnings;

package Node {
    use Moose;

    has 'value', is => 'ro', isa => 'Int';
    has 'nextnode', is => 'rw', isa => 'Any';

    sub printvalue { 
        my $self = shift;
        print $self->value , " ";
    }

    sub go_next {
        my $self = shift;
        return $self->nextnode;
    }

}



my $node_a = Node->new(value => 30, nextnode=> undef);
my $node_b = Node->new(value => 40, nextnode => $node_a);
my $node_c = Node->new(value => 70, nextnode => $node_b);

my $node_head = Node->new(value => 0,nextnode=> $node_c);

my $node = $node_head;

while ($node = $node->go_next ) {
     $node->printvalue;
}

print "\n";
#print 70 40 30

Expand one into two - CY's Take on TWC#077

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

I found that I gained unnecessary promotion due to being in a GMT+8.00 timezone - my blogpost appears on the top of http://blogs.perl.org for longer hours.

---

Task 1 Fibonacci Sum

Another dish for math geek!

Really??

Coding Process

I spent a whole day on the Perl script on F…

Sum of Individuals Gives Meaning - CY's Take on PWC#076

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

---

Task 1 Prime Sum

What a dish for math geeks! Goldbach's conjecture is immediately recalled. I have chosen to print the answer first, than give the expression and explanation.

$ perl ch-1.pl 1
0
as 1 is smaller than the smallest prime.

$ perl ch-1.pl 4
2
as 2 + 2 = 4 and 4 is not a prime.

$ perl ch-1.pl 9
2
a…

How and What to do in Programming (CY's Take on PWC#075 Task 2) [Edited]

---

Continued from the discussion of Task 1.

Task 2 Largest Rectangle Histogram

I am not a narcissist; though some of my actions are narcissistic traits, it does not imply that there is yet another narcissist; I am motivating myself for a better self.