diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-07-28 17:09:47 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-07-28 17:09:47 +0100 |
| commit | 9ef6fa4fd3ef943410209e98ba99b84921da987b (patch) | |
| tree | 40443773e805bf167a643e60043e2b52cf7f42af | |
| parent | 2eb2c90d6abbd00adad7e017b9ae4d3812022196 (diff) | |
| parent | bd458c58d0fece7695aadc708f3fda0b7177dd8f (diff) | |
| download | perlweeklychallenge-club-9ef6fa4fd3ef943410209e98ba99b84921da987b.tar.gz perlweeklychallenge-club-9ef6fa4fd3ef943410209e98ba99b84921da987b.tar.bz2 perlweeklychallenge-club-9ef6fa4fd3ef943410209e98ba99b84921da987b.zip | |
Merge pull request #4624 from wlmb/master
Simplify programs
| -rwxr-xr-x | challenge-123/wlmb/perl/ch-1.pl | 17 | ||||
| -rwxr-xr-x | challenge-123/wlmb/perl/ch-2.pl | 6 |
2 files changed, 13 insertions, 10 deletions
diff --git a/challenge-123/wlmb/perl/ch-1.pl b/challenge-123/wlmb/perl/ch-1.pl index da3f90362c..62b334865f 100755 --- a/challenge-123/wlmb/perl/ch-1.pl +++ b/challenge-123/wlmb/perl/ch-1.pl @@ -16,14 +16,17 @@ foreach(@ARGV){ } sub ugly { - my $n=shift @_; # desired ugly number - while($n>@uglies){ - my ($next_id)=sort {$uglies[$u_id[$a]]*$factors[$a]<=>$uglies[$u_id[$b]]*$factors[$b]} - (0..2); - my $next_val=$uglies[$u_id[$next_id]]*$factors[$next_id]; + my $n=shift @_; # desired ugly number. Assume a positive integer + while($n>@uglies){ # generate uglies until the n-th becomes known + my ($next_id)=sort {next_from_id($a)<=>next_from_id($b)} 0..2; + my $next_val=next_from_id($next_id); $u_id[$next_id]++; - next unless $next_val>$uglies[-1]; - push @uglies, $next_val; + push @uglies, $next_val if $next_val>$uglies[-1]; } return $uglies[$n-1]; } + +sub next_from_id { + my $id=shift @_; + return $uglies[$u_id[$id]]*$factors[$id]; +} diff --git a/challenge-123/wlmb/perl/ch-2.pl b/challenge-123/wlmb/perl/ch-2.pl index 01fca9c85a..4130be36f3 100755 --- a/challenge-123/wlmb/perl/ch-2.pl +++ b/challenge-123/wlmb/perl/ch-2.pl @@ -9,18 +9,18 @@ use v5.12; use PDL; foreach(@ARGV){ # assume data as strings "[[x0,y0],[x1,y1],[x2,y2],[x3,y3]]" - my $m=pdl($_); + my $m=pdl($_); # convert to 2d array # Separate into four vectors, translate the origin to the first # and sort by size my $v0=$m->slice(":,(0)"); my (undef, $s1, $s2, $d)=sort {size2($a) <=> size2($b)} map {$_-$v0} $m->dog; # $s1 and $s2 ought to be the sides and $d the diagonal - # check they add up and their sizes. + # check they add up and their sizes. I use 'approx' to accommodate rounding errors. my $ok=approx(size2($s1+$s2-$d),0) && approx(size2($s1),size2($s2)) && approx(size2($d),2*size2($s1)); say "Input: $m Output: $ok" } -sub size2 { #size of vector squared +sub size2 { # squared size of vector my $v=shift @_; return ($v*$v)->sumover; } |
