Permalink
Browse files
Made almost everything in 99-problems run in both niecza and rakudo.
All changes were minor: most were fixes to logic
and a couple to Pod errors.
P08-viklund and P13-viklund:
I couldn't get these to run without completely
altering their code, so I just left them alone.
Loading branch information...
@@ -31,8 +31,8 @@ (@l)
# c. Pop like perl5
# pop the last element off, which also returns it
# say either way
-say <X Y Z >. pop ;
-<X Y Z >. pop . say ;
+say <X Y Z >. list . pop ;
+<X Y Z >. list . pop . say ;
# ORIGINAL LISP SPECIFICATION
@@ -42,7 +42,7 @@ (@l)
# vice versa; as a result, this example does not distinguish between a
# single element and a list containing a single element.
-= begin
+= begin pod
= head1 NAME
@@ -55,5 +55,5 @@ =head1 LISP
* (my-last '(a b c d))
(D)
-=end
+= end pod
@@ -32,7 +32,7 @@ (@l)
# ORIGINAL LISP SPECIFICATION
-= begin
+= begin pod
= head1 NAME
@@ -45,5 +45,5 @@ =head1 LISP
* (my-but-last '(a b c d))
(C D)
-=end
+= end pod
@@ -30,7 +30,7 @@ (@l, $n)
# ORIGINAL LISP SPECIFICATION
-= begin
+= begin pod
= head1 NAME
@@ -44,5 +44,5 @@ =head1 LISP
* (element-at '(a b c d e) 3)
C
-=end
+= end pod
@@ -17,11 +17,11 @@
# ORIGINAL LISP SPECIFICATION
-= begin
+= begin pod
= head1 NAME
P04 - Find the number of elements of a list
-= end
+= end pod
@@ -8,17 +8,17 @@
# .reverse on the object to reverse the order
# .join called to present the data with a " "
# say displays the result
-say <A B C D >. reverse . join (' ' );
+say <A B C D >. list . reverse . join (' ' );
# b. Perl representation
# .perl serialises the data as perl representation (like Data::Dumper in perl5)
# .say to display the result (print with a new line)
-<A B C D >. reverse . perl . say ;
+<A B C D >. list . reverse . perl . say ;
-= begin
+= begin pod
= head1 NAME
P05 - Reverse a list
-= end
+= end pod
@@ -20,7 +20,7 @@
# ORIGINAL LISP SPECIFICATION
-= begin
+= begin pod
= head1 NAME
@@ -31,5 +31,5 @@ =head1 LISP
P06 (*) Find out whether a list is a palindrome.
A palindrome can be read forward or backward; e.g. (x a m a x).
-= end
+= end pod
@@ -10,7 +10,7 @@
sub splat (@t) {
- my @return ;
+ my @return = [] ;
for @t -> $i {
if ($i. isa (Array )) {
@return . push ( splat($i) );
@@ -15,6 +15,10 @@ (@in)
my @return ;
my $last ;
for @in -> $i {
+# The FIRST { } will remove the 'use of uninitialised value of type Any in
+# String context' warning in rakudo, but niecza hasn't implemented it as of
+# Aug 15 2012 so I'll leave it here, but commented out.
+# FIRST { $last = '' }
if ($i ne $last ) {
@return . push ($i);
$last = $i;
@@ -31,8 +31,8 @@ sub p09
for @in -> $elem
{
- push @out, [] if $elem ne @out[-1 ][0 ];
- push @out[-1 ], $elem;
+ push @out, [] if $elem ne @out[* - 1 ][0 ];
+ push @out[* - 1 ], $elem;
}
return @out;
}
@@ -13,7 +13,7 @@
sub prob09 (@in) {
return gather while @in. elems {
my $val = @in[0 ];
- take [gather while @in[0 ] ~~ $val { take shift @in }];
+ take [gather while @in. elems and @in [0 ] ~~ $val { take shift @in }];
}
}
say ~ @l;
@@ -16,7 +16,14 @@ (@in)
return gather loop {
last if ! @in. elems ;
my $val = @in[0 ];
- take [ gather { loop { @in[0 ] ~~ $val ?? take shift @in !! last }; }. elems , $val ];
+ take [
+ gather {
+ loop {
+ last if ! @in. elems ;
+ @in[0 ] ~~ $val ?? take (shift @in) !! last
+ };
+ }. elems , $val
+ ];
};
}
say ~ @l;
@@ -15,7 +15,10 @@ (@in)
return gather loop {
last if ! @in. elems ;
my $val = @in[0 ];
- my @a = gather { loop { @in[0 ] ~~ $val ?? take shift @in !! last }; };
+ my @a = gather loop {
+ last if ! @in. elems ;
+ @in[0 ] ~~ $val ?? take (shift @in) !! last
+ };
take @a. end ?? [@a. elems , $val] !! $val;
};
}
@@ -10,7 +10,7 @@
sub prob12 (@in) {
my @out;
for 0 ... (@in. end ) -> $x {
- if @in[$x ]. WHAT eq ' Array() ' {
+ if @in[$x ] ~~ Array {
loop (my $i= 0 ; $i < @in[$x ][0 ]; $i++ ) {
push @out, @in[$x ][1 ];
};
@@ -15,7 +15,9 @@
# iterations
sub runlength (@a) {
- gather while @a. elems {
+ gather {
+ last if ! @a. elems ;
+
my $val = @a. shift ;
my $num = 1 ;
while @a[0 ] ~~ $val { @a. shift ; $num ++ ; }
@@ -10,7 +10,7 @@
sub prime_factors (Int $n) {
my $residue = $n;
- gather for (2 ,3 ,* +2 ... $n) -> $k {
+ gather for (2 ,3 ,* +2 ... * > $n) -> $k {
while $residue %% $k {
# try 'take 0+$k' to work around a known rakudo issue (2010-09-05)
take $k;
@@ -27,7 +27,7 @@ (Int $n)
}
}
-say ~ prime_factors $_ for 2 .. 20 ;
+say ~ prime_factors( $_ ) for 2 .. 20 ;
say ~ prime_factors(315 );
say ~ prime_factors(1723 );
@@ -100,7 +100,7 @@
while $number % $prime-number == 0 {
% factors{$prime-number} // = 0 ;
% factors{$prime-number}++ ;
- $number / = $prime-number;
+ $number div = $prime-number. Int ;
}
}
% factors{$number}++ if $number != 1 ; # we have a prime left over
@@ -12,7 +12,7 @@
sub prime_factors_mult (Int $n) {
my $residue = $n;
- gather for (2 ,3 ,* +2 ... $n) -> $k {
+ gather for (2 ,3 ,* +2 ... * > $n) -> $k {
my $mult= 0 ;
while $residue %% $k {
$mult++ ;
@@ -16,7 +16,7 @@
# Straight from P36-rhebus.pl
sub prime_factors_mult (Int $n) {
my $residue = $n;
- gather for (2 ,3 ,* +2 ... $n) -> $k {
+ gather for (2 ,3 ,* +2 ... * > $n) -> $k {
my $mult= 0 ;
while $residue %% $k {
$mult++ ;
@@ -33,8 +33,10 @@ (Int $n)
# 1. One-liner version
+
say " phi($_): " , [* ] prime_factors_mult($_ ). map ({ (. key-1) * . key ** (. value-1) })
for 1 .. 20 ;
+
say [* ] prime_factors_mult(315 ). map : { (. key-1) * . key ** (. value-1) };
@@ -49,7 +51,8 @@ (Int $n)
}
}
-say " phi2($_): " , totient($_ ) for 1 .. 20 ;
+# This hangs too! \o/
+# say "phi2($_): ", totient($_) for 1..20;
say " phi2(315): " , totient(315 );
# vim:ft=perl6
@@ -29,6 +29,6 @@ (*@range)
say ~ primes(3 ,5 ,17 ,257 ,65537 );
# or a series...
-say ~ primes(1 ,2 ,*+* ... 100 );
+say ~ primes(1 ,2 ,* +1 ... 100 );
# vim:ft=perl6
@@ -57,6 +57,6 @@ (Int $n where {$^a > 2 && $^a %% 2})
}
goldbach-list 9 ,20 ;
-goldbach-list 2 ,3000 ,10 ;
+goldbach-list 2 ,1000 ,10 ;
# vim:ft=perl6
Toggle all file notes
0 comments on commit
d692b61