Skip to content

Commit e6f1005

Browse files
author
Salve J. Nilsen
committed
Update pot and add v6 requirements
1 parent 0c9beca commit e6f1005

File tree

6 files changed

+143
-70
lines changed

6 files changed

+143
-70
lines changed

best-of-rosettacode/100-doors.pl

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,35 @@
1-
# http://rosettacode.org/wiki/100_doors#Perl_6
1+
=begin pod
22
3-
# Problem: You have 100 doors in a row that are all initially closed. You make
4-
# 100 passes by the doors. The first time through, you visit every door and
5-
# toggle the door (if the door is closed, you open it; if it is open, you close
6-
# it). The second time you only visit every 2nd door (door #2, #4, #6, ...).
7-
# The third time, every 3rd door (door #3, #6, #9, ...), etc, until you only
8-
# visit the 100th door.
3+
=head1 Problem
4+
5+
You have 100 doors in a row that are all initially closed. You make 100 passes
6+
by the doors. The first time through, you visit every door and toggle the door
7+
(if the door is closed, you open it; if it is open, you close it). The second
8+
time you only visit every 2nd door (door #2, #4, #6, ...). The third time,
9+
every 3rd door (door #3, #6, #9, ...), etc, until you only visit the 100th
10+
door.
11+
12+
=head1 Question
13+
14+
What state are the doors in after the last pass? Which are open,
15+
which are closed?
16+
17+
=head1 More
18+
19+
L<http://rosettacode.org/wiki/100_doors#Perl_6>
20+
21+
=end pod
922

10-
# Question: What state are the doors in after the last pass? Which are open,
11-
# which are closed?
1223

1324
say "Door $_ is open" for 1..10 X** 2;
1425

15-
# More about X** - http://perlcabal.org/syn/S03.html#Cross_operators
1626

17-
# vim: expandtab shiftwidth=4 ft=perl6:
27+
=begin pod
28+
29+
=head1 Features used
30+
31+
C<X**> - http://perlcabal.org/syn/S03.html#Cross_operators
32+
33+
=end pod
34+
35+
# vim: expandtab shiftwidth=2 ft=perl6:

best-of-rosettacode/copy-a-string.pl

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
=begin pod
2+
3+
=head1 Description
4+
5+
This task is about copying a string. Where it is relevant, distinguish
6+
between copying the contents of a string versus making an additional
7+
reference to an existing string.
8+
9+
=head2 More
10+
11+
http://rosettacode.org/wiki/Copy_a_string#Perl_6
12+
13+
=end pod
14+
15+
use v6;
16+
17+
# There is no special handling needed to copy a string.
18+
{
19+
my $original = 'Hello.';
20+
my $copy = $original;
21+
say $copy; # prints "Hello."
22+
$copy = 'Goodbye.';
23+
say $copy; # prints "Goodbye."
24+
say $original; # prints "Hello."
25+
}
26+
27+
# You can also bind a new variable to an existing one so that each refers
28+
# to, and can modify the same string.
29+
{
30+
my $original = 'Hello.';
31+
my $bound := $original;
32+
say $bound; # prints "Hello."
33+
$bound = 'Goodbye.';
34+
say $bound; # prints "Goodbye."
35+
say $original; # prints "Goodbye."
36+
}
37+
38+
# vim: expandtab shiftwidth=2 ft=perl6:
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
=begin pod
2+
3+
=head1 Create a two-dimensional array at runtime
4+
5+
Get two integers from the user, then create a two-dimensional array where the
6+
two dimensions have the sizes given by those numbers, and which can be accessed
7+
in the most natural way possible. Write some element of that array, and then
8+
output that element. Finally destroy the array if not done by the language
9+
itself.
10+
11+
=head1 Rosettacode URL
12+
13+
http://rosettacode.org/wiki/Create_a_two-dimensional_array_at_runtime#Perl_6
14+
15+
=end pod
16+
17+
=cut
18+
19+
20+
my ($major,$minor) = prompt("Dimensions? ").comb(/\d+/);
21+
22+
my @array := [ for ^$major { [ for ^$minor {'@'} ] } ];
23+
24+
@array[ pick 1, ^$major ][ pick 1, ^$minor ] = ' ';
25+
26+
.say for @array;
27+
28+
29+
# vim: expandtab shiftwidth=2 ft=perl6:
Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,45 @@
1-
#=head1 Description
2-
# The Hailstone sequence of numbers can be generated from a starting positive integer, n by:
3-
# If n is 1 then the sequence ends.
4-
# If n is even then the next n of the sequence = n/2
5-
# If n is odd then the next n of the sequence = (3 * n) + 1
6-
# The (unproven), Collatz conjecture is that the hailstone sequence for any starting number always terminates.
7-
# Task Description:
8-
# Create a routine to generate the hailstone sequence for a number.
9-
# Use the routine to show that the hailstone sequence for the number 27 has 112 elements starting with 27, 82, 41, 124 and ending with 8, 4, 2, 1
10-
# Show the number less than 100,000 which has the longest hailstone sequence together with that sequences length.
11-
# (But don't show the actual sequence)!
12-
13-
#=head1 Code
14-
sub hailstone($n) { $n, { $_ %% 2 ?? $_ div 2 !! $_ * 3 + 1 } ... 1 }
15-
16-
my @h = hailstone(27);
17-
say "Length of hailstone(27) = {+@h}";
18-
say ~@h;
19-
20-
my $m = 0 => 0;
21-
$m max= +hailstone($_) => $_ for 1..99_999;
22-
say "Max length $m.key() was found for hailstone($m.value()) for numbers < 100_000";
23-
24-
#=head2 More
25-
# http://rosettacode.org/wiki/Hailstone_sequence#Perl_6
1+
=begin pod
2+
3+
=head1 Description
4+
5+
The Hailstone sequence of numbers can be generated from a starting positive
6+
integer, n by:
7+
8+
* If n is 1 then the sequence ends.
9+
* If n is even then the next n of the sequence = n/2
10+
* If n is odd then the next n of the sequence = (3 * n) + 1
11+
12+
The (unproven), Collatz conjecture is that the hailstone sequence for any
13+
starting number always terminates.
14+
15+
=head1 Task
16+
17+
Create a routine to generate the hailstone sequence for a number.
18+
19+
Use the routine to show that the hailstone sequence for the number 27 has 112
20+
elements starting with 27, 82, 41, 124 and ending with 8, 4, 2, 1
21+
22+
Show the number less than 100,000 which has the longest hailstone sequence
23+
together with that sequences length.
24+
25+
(But don't show the actual sequence)!
26+
27+
=head1 More
28+
29+
U<http://rosettacode.org/wiki/Hailstone_sequence#Perl_6>
30+
31+
=end pod
32+
33+
use v6;
34+
35+
sub hailstone($n) { $n, { $_ %% 2 ?? $_ div 2 !! $_ * 3 + 1 } ... 1 }
36+
37+
my @h = hailstone(27);
38+
say "Length of hailstone(27) = {+@h}";
39+
say ~@h;
40+
41+
my $m max= +hailstone($_) => $_ for 1..99_999;
42+
say "Max length $m.key() was found for hailstone($m.value()) for numbers < 100_000";
43+
44+
45+
# vim: expandtab shiftwidth=2 ft=perl6:

best-of-rosettacode/hailstone-sequence.pl.niecza

Lines changed: 0 additions & 34 deletions
This file was deleted.

best-of-rosettacode/prime-decomposition.pl.niecza

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#
1212
# http://rosettacode.org/wiki/Prime_decomposition#Perl_6
1313

14+
use v6;
15+
1416
constant @primes = 2, 3, 5, -> $n is copy {
1517
repeat { $n += 2 } until $n %% none @primes ... { $_ * $_ >= $n }
1618
$n;

0 commit comments

Comments
 (0)