in reply to General style advice requested

Thanks for your replies everyone. Here is what I now have:

#!/usr/bin/perl use warnings; use strict; use 5.010; # The 3n + 1 problem (http://www.streamtech.nl/problemset/100.html) my @pairs; say "Input:"; while (<STDIN>) { if (/^[1-9]+\d*\s[1-9]+\d*$/) { # check for and prevent illegal + entries chomp; push( @pairs, $_ ); } else { say "invalid input!" } } say "Output:"; foreach my $pair (@pairs) { my ( $first, $second ) = split( / /, $pair ); my $max_cycle_length = 0; if ( $first > $second ) { ( $first, $second ) = ( $second, $first ); } # doesn't matter whether the higher number is entered first o +r second foreach my $num ( $first .. $second ) { my $cycle_length_out = totalizer($num); if ( $cycle_length_out > $max_cycle_length ) { $max_cycle_length = $cycle_length_out; } } say "$first $second $max_cycle_length"; } sub totalizer { my $number = shift; my $current_cycle_length; for ( $current_cycle_length = 1 ; $number != 1 ; ++$current_cycle_ +length ) { if ( $number % 2 != 0 ) { $number = ( $number * 3 + 1 ); } else { $number /= 2; } } return $current_cycle_length; }

I hope this is an improvement.

Replies are listed 'Best First'.
Re^2: General style advice requested
by GrandFather (Saint) on Oct 23, 2010 at 21:51 UTC

    Indeed an improvement. For contemplation here's something closer to what I would write:

    #!/usr/bin/perl use warnings; use strict; use 5.010; # The 3n + 1 problem (http://www.streamtech.nl/problemset/100.html) my @pairs; say "Input:"; while (defined ($_ = <STDIN>) && !/^\.$/) { if (!/^(\d+)\s+(\d+)$/ && $1 > 0 && $2 > 0) { chomp; say "Invalid input >$_<."; say 'Two non-zero positive integers are expected. Input ignore +d'; next; } push @pairs, [$1, $2]; } say "Output:"; for my $pair (@pairs) { my $maxLength = calcCycleLen ($pair->[0]); my $length2 = calcCycleLen ($pair->[1]); $maxLength = $length2 if $maxLength = $length2; say "@$pair $maxLength"; } sub calcCycleLen { my ($number) = @_; my $length = 1; while ($number > 1) { $number = $number % 2 ? $number * 3 + 1 : $number / 2; ++$length; } return $length; }

    The first change is to add a stopping condition for command line input from STDIN. while (<$fooIn>) { is fine if $fooIn is connected to a file, but is less good for devices such as a keyboard where the end of file isn't clear.

    There are a few other changes of note in the while loop:

    1. the regular expression is used to capture the two numbers and the requirement for just one white space character is relaxed.
    2. A range test is performed on the numbers and provides an additional failure mode for the input
    3. The bad input is reported and the valid input criteria are specified
    4. There is no else part to the if statement. Instead the "early exit" technique is used. This very often reduces nested code and can make the main flow of the code much easier to see.
    5. The pairs are stored in their own two element array. @pairs thus contains arrays - it is an AoA (some people like to use lol instead).

    Moving on: as a personal preference I use for everywhere rather than a mixture of for and foreach - probably because I'm lazy.

    The @pairs for loop has changed a lot!

    1. $pair now is a reference to an array containing two numbers instead of a string.
    2. $maxLength is initialised to the first value that will be calculated. This trick is often used where a result is calculated by considering each element of a vector in turn. Initialise the result variable with the calculated value from the first element of the vector, then process the remaining elements in a loop.
    3. Calculate the second value. Note the syntax used to access the array elements from the $pair variable holding a reference to the array btw.
    4. Update $maxLength if it is smaller than the second length. Note the use of if as a statement modifier.

    Now we are in the home stretch.

    1. I use my ($number) = @_; because it scales better than a bunch of shifts. Not needed in this case, but it's what flows from my fingers.
    2. The C style for loop is almost unused in Perl. In this case a while loop better gives the flavour of what is going on anyway.
    3. The ternary operator is easy to abuse. In this case though it reduces some rather cluttered looking code to something that (in my view) is clear and concise.

    The final step is to pass Perl::Tidy over the code to clean up any wayward formatting, then enjoy.

    True laziness is hard work
Re^2: General style advice requested
by choroba (Cardinal) on Oct 29, 2010 at 14:47 UTC
    Thanks for the link :-) I also tried the first assignment yesterday, but with a small speed-up included. See for comparison:
    #!/usr/bin/perl use warnings; use strict; my %cache = (1 => 0); sub _three_n_plus_1 { no warnings qw/recursion/; my ($n,$count) = @_; $count ||= 1; return $count + $cache{$n} if exists $cache{$n}; if($n % 2){ $cache{$n} = _three_n_plus_1(3*$n+1, $count+1) - $count; return $cache{$n} + $count; }else{ $cache{$n} = _three_n_plus_1($n/2, $count+1) - $count; return $cache{$n} + $count; } } sub three_n_plus_1 { my ($i,$j) = @_; my $max = 0; for my $x ($i<$j ? $i..$j : $j..$i){ my $t = _three_n_plus_1($x); $max = $t if $t > $max; } print "$i $j $max\n"; }