Skip to content

Commit fb325c8

Browse files
committed
Merge branch 'master' of github.com:perl6/perl6-examples
2 parents 5d0d700 + fb32836 commit fb325c8

File tree

10 files changed

+63
-53
lines changed

10 files changed

+63
-53
lines changed

categories/euler/prob004-unobe.pl

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,13 @@
1717

1818
# another case where @array = %*% (100..999) would be nice to have:
1919
# http://use.perl.org/~dpuu/journal/38142
20-
sub diagonal_x (@array) {
21-
my @result = [];
22-
my @copy = @array;
23-
for @copy -> $this {
24-
@copy.shift;
25-
for @copy -> $that { @result.push($this * $that); }
20+
sub diagonal_x (@array is copy) {
21+
22+
gather while @array.shift -> $this {
23+
for @array -> $that { take $this * $that }
2624
}
27-
return @result;
2825
}
2926

30-
diagonal_x(100..999).grep({ $_ eq .flip }).sort.reverse.[0].say;
27+
diagonal_x(100...999).grep({ $_ eq $_.flip }).sort.reverse.[0].say;
3128

3229
# vim: expandtab shiftwidth=4 ft=perl6

categories/euler/prob025-polettix.pl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ (Int :$length = 1000, Bool :$boring = False)
4040
return;
4141
}
4242

43-
my @fibs := 0, 1, *+* ... *;
43+
my @fibs = 0, 1, *+* ... *;
4444
((1..*).grep:{@fibs[$_].chars == $length})[0].say;
4545
return;
4646
}

categories/euler/prob030-andreoss.pl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,18 @@
2323
=end pod
2424

2525
sub get-numbers(:$start = 10, :$depth = 6, *@a) {
26-
return @a.item unless $depth;
27-
do for ^$start -> \x {
26+
return $@a unless $depth;
27+
flat do for ^$start -> \x {
2828
get-numbers start => x + 1,
2929
depth => $depth -1, |@a,x;
3030
}
3131
}
3232

33-
say [+] -1, gather for get-numbers() -> @a {
34-
my $v = [+] @a »**» 5;
35-
my $b = [+] $v.comb »**» 5;
33+
say [+] gather for get-numbers() -> @a {
34+
my $v = [+] @a »**» 5;
35+
my $b = [+] $v.comb.list »**» 5;
3636
take $b if $v == $b;
37+
LAST take -1;
3738
}
3839

3940
# vim: expandtab shiftwidth=4 ft=perl6

categories/euler/prob053-gerdr.pl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@
2727

2828
my @result;
2929

30-
[1], -> @p { [0, @p Z+ @p, 0] } ... * \ # generate Pascal's triangle
30+
$[1], -> @p { $[0, |@p Z+ |@p, 0] } ... * \ # generate Pascal's triangle
3131
==> (*[0..100])() \
3232
==> map *.list \
3333
==> grep * > 1_000_000 \
3434
==> elems() \
3535
==> @result; # work around .say not yet handling feeds in Rakudo 2015.02
3636
#==> say;
37-
@result.say;
37+
@result[0].say;
3838

3939
# vim: expandtab shiftwidth=4 ft=perl6

categories/euler/prob054-andreoss.pl

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
L<https://projecteuler.net/problem=54>
1010
1111
The file, poker.txt, contains one-thousand random hands dealt to two
12-
players. Each line of the file contains ten cards (separated by a single
13-
space): the first five are Player 1's cards and the last five are Player 2's
14-
cards. You can assume that all hands are valid (no invalid characters or
15-
repeated cards), each player's hand is in no specific order, and in each
16-
hand there is a clear winner.
12+
players. Each line of the file contains ten cards (separated by a single
13+
space): the first five are Player 1's cards and the last five are Player 2's
14+
cards. You can assume that all hands are valid (no invalid characters or
15+
repeated cards), each player's hand is in no specific order, and in each
16+
hand there is a clear winner.
1717
18-
How many hands does Player 1 win?
18+
How many hands does Player 1 win?
1919
2020
=end pod
2121

@@ -122,7 +122,7 @@
122122

123123
method !flush {
124124
if [~~] @.cards».suit {
125-
(Flush) => Array[Rank].new: @.cards».rank;
125+
(Flush) => Array[Rank].new: |@.cards».rank;
126126
}
127127
}
128128

@@ -148,7 +148,7 @@
148148
}
149149
method !full-house {
150150
# Three of a kind and a pair.
151-
my Ranks %x{Hand} = self!three-of-kind , self!one-pair;
151+
my Ranks %x{Hand} = flat self!three-of-kind , self!one-pair;
152152
if %x{ThreeOfKind}.defined && %x{OnePair}.defined {
153153
(FullHouse) => Ace
154154
}
@@ -179,7 +179,6 @@
179179
(TwoPairs) => my $y= @pairs».value.max,
180180
(HighCard) => max grep { $_ !~~ $x | $y },@.cards».rank;
181181
}
182-
183182
}
184183

185184
method !one-pair {

categories/euler/prob059-andreoss.pl

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
my constant @common-words = <the was who not did with have does and one that>;
4343

4444
sub infix:<XOR>(@cipher, @password) {
45-
@cipher Z+^ (@password xx *);
45+
@cipher Z+^ flat (@password xx *);
4646
}
4747

4848
sub as-code(Str $w) {
@@ -56,19 +56,19 @@ sub infix:<XOR>(@cipher, @password) {
5656
sub guess-password(Str $w, @cipher) {
5757
my @word = as-code $w;
5858

59-
my @chunks := @cipher.rotor((@word.elems) => -(@word.elems - 1));
59+
my @chunks = @cipher.rotor((@word.elems) => -(@word.elems - 1));
6060
my %tries;
6161
my $offset = 0;
6262

6363
for @chunks -> @chunk {
64-
64+
6565
my @password = @chunk[^3] XOR @word;
66+
6667
my $password = as-word @password;
67-
68+
6869
next unless $password ~~ /^^ <[a..z]> ** 3 $$/ ;
69-
7070
my $decrypted = as-word @cipher[$offset .. *] XOR @password;
71-
71+
7272
my $count = [+] do for @common-words.grep({$_ !~~ $w}) -> $word {
7373
elems $decrypted ~~ m:g:i/$word/
7474
}
@@ -86,7 +86,9 @@ sub infix:<XOR>(@cipher, @password) {
8686
sub MAIN(Bool :$verbose = False,
8787
:$file = $*SPEC.catdir($*PROGRAM-NAME.IO.dirname, 'cipher.txt'),
8888
:$word = @common-words[0],
89-
:$pass is copy) {
89+
:$pass is copy,
90+
Bool :$test = False) {
91+
return TEST if $test;
9092
die "'$file' is missing" unless $file.IO.e ;
9193
my @cipher = map *.Int, split /<[,]>/ , slurp $file;
9294

@@ -97,12 +99,18 @@ sub infix:<XOR>(@cipher, @password) {
9799
}
98100

99101
my $decrypted = as-word @cipher XOR as-code($pass);
100-
102+
101103
say "The message: {$decrypted.perl}" if $verbose;
102-
103104
say [+] as-code $decrypted;
104-
105105
say "Done in {now - BEGIN now}" if $verbose;
106106
}
107107

108+
sub TEST {
109+
use Test;
110+
is as-code("abc"), [97,98,99], "as-code works";
111+
is as-word(100,101,102), "def", "as-word works";
112+
is as-word([79,59,12] XOR [103,111,100]), "(Th", "XOR works";
113+
done;
114+
}
115+
108116
# vim: expandtab shiftwidth=4 ft=perl6

categories/euler/prob065-andreoss.pl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
continued-fraction :depth($depth - 1), @sequence
6161
}
6262

63-
my @e := gather { take 2; take (1; $_; 1) for 2,4 ... * };
63+
my @e = lazy gather { take 2; (1, $_, 1)».&take for 2,4 ... * };
6464

6565
say [+] continued-fraction(@e, depth => 100).numerator.comb;
6666

categories/euler/prob081-moritz.pl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@
2626

2727
my @m;
2828

29-
my $matrix-file = $*SPEC.catdir($*PROGRAM-NAME.IO.dirname, 'matrix.txt');
29+
my $matrix-file = $*SPEC.catdir(#`{$*PROGRAM-NAME.IO.dirname} "./categories/euler/", 'matrix.txt');
3030
my $f = open $matrix-file or die "Can't open file for reading: $!";
31-
for $f.lines {
32-
@m.push: [ .comb(/\d+/) ];
31+
for $f.lines <-> $line {
32+
@m.push: $line.comb(/\d+/).Array.item;
3333
}
3434
$f.close;
3535

categories/euler/prob098-andreoss.pl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,16 @@ (@x)
6161

6262
my %words = anagrams(@words);
6363
my $longest-word = %words.keys.max;
64+
6465
my %squares = anagrams(
65-
(1 ... (10**($longest-word + 1).sqrt)) »**» 2
66+
(1 ... (10**($longest-word + 1).sqrt)).list »**» 2
6667
);
6768

6869

6970
say max do for 3 ... $longest-word -> \size {
71+
next unless %words{size};
7072
do for @(%words{size}) -> @pair {
73+
next unless %squares{size};
7174
do for @(%squares{size}) -> @nums {
7275
if correspond(@pair, @nums) {
7376
$verbose and say "@pair[] => @nums[]" ;

t/categories/euler.t

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -366,9 +366,9 @@ skip("prob034 takes too long in tests");
366366
unless $skip {
367367
subtest {
368368
plan 1;
369-
my $problem = "prob034";
370-
my @authors = <quinny>;
371-
my $expected-output = 40730;
369+
my $problem = "prob034";
370+
my @authors = <quinny>;
371+
my $expected-output = 40730;
372372

373373
check-example-solutions($problem, $expected-output, @authors)
374374
}, "prob034";
@@ -456,16 +456,18 @@ subtest {
456456
check-example-solutions($problem, $expected-output, @authors)
457457
}, "prob059";
458458

459-
subtest {
460-
plan 1;
461-
462-
my $problem = "prob060";
463-
my @authors = <andreoss>;
464-
my $expected-output = 26033;
459+
skip("prob060 takes too long to run (about 90m)");
460+
unless $skip {
461+
subtest {
462+
plan 1;
465463

466-
check-example-solutions($problem, $expected-output, @authors)
467-
}, "prob060";
464+
my $problem = "prob060";
465+
my @authors = <andreoss>;
466+
my $expected-output = 26033;
468467

468+
check-example-solutions($problem, $expected-output, @authors)
469+
}, "prob060";
470+
};
469471

470472
subtest {
471473
plan 2;
@@ -687,8 +689,8 @@ sub run-example($name) {
687689
my $base-dir = "categories/euler";
688690
my $script-path = $base-dir ~ "/" ~ $name;
689691
$script-path = "$script-path.pl".IO.e
690-
?? "$script-path.pl"
691-
!! "$script-path.p6";
692+
?? "$script-path.pl"
693+
!! "$script-path.p6";
692694
my $base-cmd = "perl6 $script-path";
693695
my $output = qqx{$base-cmd};
694696

0 commit comments

Comments
 (0)