in reply to Perl linked list

With the material removed, it is now impossible to help the original poster.

Not sure it would be a good idea to help him anyway, since the link provided in the OP (which I copy here to make sure that it does not also disappear from the OP: https://codility.com/honeypot/Perl_is_Perls-Perl-test-1 seems to indicate it is a test for admission to the South Dakota State University.

Je suis Charlie.

Replies are listed 'Best First'.
Re^2: Perl linked list
by Anonymous Monk on Mar 31, 2015 at 17:45 UTC
    Believe or not,I am working at SDSU... and the test link is given public access by me also. It is just used for self-evaluation purpose. You can log in the account using happye321@gmail.com the password is Happye321
      OK, I ran the following code

      # you can use print for debugging purposes, e.g. # print "this is a debug message\n"; use Data::Dumper; sub solution { my ($L)=@_; print Dumper $L; # write your code in Perl 5.14 }

      and got this output, which clarifies that the implementation is done with hashes of hashes

      (sorry indentation lost while copying)

      Running solution... Compilation successful. Example test: [1, 1, 1, 1] Output (stderr): WARNING: producing output may seriously slow down your code! Output:

      $VAR1 = {
      next => {
      next => {
      next => {
      next => undef,
      value => 1
      },
      value => 1
      },
      value => 1
      },
      value => 1
      };

      Reading the confusing text reveals that those lazy people didn't even try to use proper Perl terminology.

      Pointer? Dictionary? Empty? WTF?

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)

      PS: Je suis Charlie!

        Thanks for your help LanX, once Known the data structure, everything make sense now!
      A quick try. My understanding of the data structure is as follows:
      use strict; use warnings; my $pt = { value => "D", next => undef }; my $pt2 = {value => "C", next => $pt}; $pt = {value => "B", next => $pt2}; my $L = { value => "A", next => $pt};
      (It could be much simpler, but I wanted to stay at a relative beginner level.

      It took me less than 5 minutes to write the code to count the nodes, but I'll reveal it only later, once I am sure that this cannot used for other purpose.

      My iterative solution subroutine prints out the node being visited and the node count as follows:

      ABCD 4
      Very easy.

      A recursive solution would probably be even simpler, but I haven't tried so far.

      Je suis Charlie.
        Thanks Laurent, beginner lever is good for me. I actually worked in biology department, and learned Perl just for analyzing data generated from our Next generation sequencing project. I guess linked list will not be something I will use in my data analysis in near future since it seems quite complex to me..... Thanks a lot for your code,though!
        Thanks for your explanation! So basically, it is just a data structure as hash of hashes or array of hash, right? Like in your example code, all scalar on the left are references. If I want to access the last element in the linked list, all I need to do is just keep deference it until the last one is reached, right? Something like $L->{'next'}->{'next'}->{'next'}->{'value'} right? ^_^