Skip to content

Commit 45e091a

Browse files
committed
260, 001, 002, 003, 004-1
1 parent 62e7fc3 commit 45e091a

File tree

10 files changed

+368
-0
lines changed

10 files changed

+368
-0
lines changed

challenge-001/0rir/raku/ch-1.raku

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env raku
2+
# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ «␤ » ∴
3+
use v6;
4+
5+
=begin comment
6+
001-1
7+
Write a script to replace the character ‘e’ with ‘E’ in the string ‘Perl Weekly Challenge’. Also print the number of times the character ‘e’ is found in the string.
8+
=end comment
9+
10+
my @Test =
11+
'Perl Weekly Challenge', 'PErl WEEkly ChallEngE', 5,
12+
;
13+
14+
plan @Test ÷ 2;
15+
16+
sub func( $a -->Array) {
17+
my @ret;
18+
$_ = $a;
19+
@ret.push: .subst: 'e', 'E', :g ;
20+
@ret.push: + .comb.grep( 'e');
21+
@ret;
22+
}
23+
24+
my @result = func @Test[0];
25+
say "Input: @Test[0]\nXlated: @result[0]\n 'e' count: @result[1]\n";
26+

challenge-001/0rir/raku/ch-2.raku

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env raku
2+
# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ «␤ » ∴
3+
use v6;
4+
use Test;
5+
6+
=begin comment
7+
001-2
8+
Write a one-liner to solve the FizzBuzz problem and print the numbers 1 through 20. However, any number divisible by 3 should be replaced by the word ‘fizz’ and any divisible by 5 by the word ‘buzz’. Those numbers that are both divisible by 3 and 5 become ‘fizzbuzz’.
9+
=end comment
10+
11+
my $exp =
12+
"1 2 fizz 4 buzz fizz 7 8 fizz buzz 11 fizz 13 14 fizzbuzz 16 17 fizz 19 buzz"
13+
;
14+
15+
sub fizzbuzz($sentinel = 20) {
16+
my $ret;
17+
for 1..$sentinel -> $n {
18+
given $n {
19+
when $n %% 3 {
20+
$ret ~= 'fizz';
21+
if $n %% 5 { $ret ~= 'buzz' }
22+
$ret ~= ' ';
23+
}
24+
when $n %% 5 { $ret ~= 'buzz ' }
25+
default { $ret ~= "$n " }
26+
}
27+
}
28+
$ret.trim-trailing;
29+
}
30+
31+
plan 1;
32+
is fizzbuzz(), $exp, 'fizzy';
33+
34+
done-testing;
35+
36+
say "\nfizzbuzzing: &fizzbuzz()";
37+
38+
exit;
39+

challenge-002/0rir/raku/ch-1.raku

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env raku
2+
# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ «␤ » ∴
3+
use v6;
4+
use lib $?FILE.IO.cleanup.parent(2).add("lib");
5+
use Test;
6+
7+
=begin comment
8+
002-1
9+
Write a script or one-liner to remove leading zeros from positive numbers.
10+
=end comment
11+
12+
my @Test =
13+
'00000005.1', '5.1',
14+
'0005.1', '5.1',
15+
'005.1', '5.1',
16+
'05.1', '5.1',
17+
'5.1', '5.1',
18+
5.1, '5.1',
19+
.051, '.051',
20+
0.0051, '.0051',
21+
0.1, '.1',
22+
;
23+
plan @Test ÷ 2;
24+
25+
sub func( $n where $n.Rat > 0 -->Str ){
26+
$n.subst( / ^ '0'+ /);
27+
}
28+
29+
for @Test -> $in, $exp {
30+
is func($in), $exp, "$exp <- $in";
31+
}
32+
33+
done-testing;
34+
35+
exit;
36+

challenge-002/0rir/raku/ch-2.raku

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env raku
2+
# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ «␤ » ∴
3+
use v6;
4+
use lib $?FILE.IO.cleanup.parent(2).add("lib");
5+
use Test;
6+
7+
=begin comment
8+
002-2
9+
Write a script that can convert integers to and from a base35 representation, using the characters 0-9 and A-Y. Dave Jacoby came up with nice description about base35, in case you needed some background.
10+
=end comment
11+
12+
plan 2001;
13+
14+
sub func( Int $a) { $a.base(35) }
15+
16+
my $n = -10_000_000;
17+
while $n < 10_000_000 {
18+
$n += 9999;
19+
is func( $n).parse-base(35), "$n", "N $n -> $n.base(35)";
20+
}
21+
22+
done-testing;

challenge-003/0rir/raku/ch-1.raku

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env raku
2+
# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ «␤ » ∴
3+
use v6;
4+
INIT $*RAT-OVERFLOW = FatRat;
5+
use Test;
6+
7+
=begin comment
8+
003-1
9+
Create a script to generate 5-smooth numbers, whose prime divisors are less or equal to 5.
10+
They are also called Hamming/Regular/Ugly numbers. For more information, please check this
11+
wikipedia.
12+
=end comment
13+
14+
my @Test =
15+
2, 3, 4, 5, 6, 8, 9, 10, 12, 15,
16+
16, 18, 20, 24, 25, 27, 30, 32, 36, 40,
17+
45, 48, 50, 54, 60, 64, 72, 75, 80, 81,
18+
90, 96, 100, 108, 120, 125, 128, 135, 144, 150,
19+
160, 162, 180, 192, 200, 216, 225, 240, 243, 250,
20+
256, 270, 288, 300, 320, 324, 360, 375, 384, 400,
21+
405,
22+
;
23+
24+
sub is-five-smooth( Int $n -->Bool) { return not so prime-factors($n).any > 5; }
25+
26+
constant @prime = 2,3, { $_+=2; $_+=2 until .is-prime; $_; } … ∞;
27+
28+
constant @five-smooth = 2,3,4,5,6, { $_+=1; $_+=1 until is-five-smooth($_); $_ } … ∞;
29+
30+
sub prime-factors( $int -->Array) is export {
31+
my $n = $int;
32+
my @ret;
33+
my $ix = 0;
34+
while $n > 1 {
35+
my $candi = @prime[$ix];
36+
$ix++;
37+
next unless ( $n %% $candi );
38+
$ix = 0;
39+
$n ÷= $candi;
40+
@ret.push: $candi;
41+
}
42+
return @ret;
43+
}
44+
45+
plan 1;
46+
is @five-smooth[0..60], @Test, '@five-smooth[0..60]';
47+
done-testing;
48+
49+
exit;

challenge-003/0rir/raku/ch-2.raku

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env raku
2+
# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ «␤ » ∴
3+
use v6;
4+
5+
=begin comment
6+
002-2
7+
Create a script that generates Pascal Triangle. Accept number of rows
8+
from the command line. The Pascal Triangle should have at least 3 rows.
9+
For more information about Pascal Triangle, check this wikipedia page.
10+
=end comment
11+
12+
my $rows = prompt( "Input number of rows wanted: ").Int;
13+
while $rows < 1 {
14+
say 'Need a positive integer answer.';
15+
$rows = prompt( "Input number of rows wanted").Int;
16+
}
17+
18+
say pascal-fmt80( pascal-triangle( $rows));
19+
20+
exit;
21+
22+
sub deepflat(+@list) { gather @list.deepmap: *.take }
23+
24+
multi pascal-triangle( Int $in where * == 1 --> Array) { [[1],] }
25+
multi pascal-triangle( Int $in where * > 1 --> Array) {
26+
my @a = [[1],];
27+
for 1..^$in -> $r {
28+
@a.push: [deepflat [1, @a[$r - 1].rotor(2 => -1.sum || Empty, 1]];
29+
}
30+
return @a;
31+
}
32+
33+
sub pascal-fmt80( @a) {
34+
die 'Cannot format more than 16 rows.' if +@a > 16;
35+
my $ret;
36+
my $wid = 5;
37+
my $dent = 40;
38+
my $fmt = '%' ~ $wid ~ 'd';
39+
for @a -> @row { $ret ~= ' ' x ($dent -= $wid/2) ~ @row.fmt($fmt, '') ~ "\n" }
40+
$ret;
41+
}
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
unit module Pi1000;
3+
4+
sub Pi( --> RatStr) is export {
5+
<3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989> }

challenge-004/0rir/raku/ch-1.raku

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env raku
2+
INIT $*RAT-OVERFLOW = FatRat;
3+
use lib $?FILE.IO.cleanup.parent;
4+
use Pi1000;
5+
6+
# 004-1 This script outputs PI w/ digit count == its file size.
7+
8+
#________0_________0_________0_________0_________0_________0_________0_______80
9+
#________0_________0_________0_________0_________0_________0_________0______160
10+
#________0_________0_________0_________0_________0_________0_________0______240
11+
#________0_________0_________0_________0_________0_________0_________0______320
12+
#________0_________0_________0_________0_________0_________0_________0______400
13+
#________0_________0_________0_________0_________0_________0_________0______480
14+
#________0_________0_________0_________0_________0_________0_________0______560
15+
#________0_________0_________0_________0_________0_________0_____628
16+
#say $*PROGRAM.s;
17+
18+
constant π = +Pi();
19+
my $scale = $*PROGRAM.s - 2;
20+
die 'Too precise' if $scale1002;
21+
die 'Bad programmer' unless $scale + 2 == + π.round( 10**-$scale).chars;
22+
23+
say π.round( 10**-$scale);

challenge-260/0rir/raku/ch-1.raku

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env raku
2+
# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ «␤ » ∴
3+
use v6;
4+
use Test;
5+
6+
=begin comment
7+
260-1: Unique Occurrences Submitted by: Mohammad Sajid Anwar
8+
You are given an array of integers, @ints. Write a script to return 1 if
9+
the number of occurrences of each value in the given array is unique or 0
10+
otherwise.
11+
12+
Example 1
13+
Input: @ints = (1,2,2,1,1,3)
14+
Output: 1
15+
16+
The number 1 occurred 3 times.
17+
The number 2 occurred 2 times.
18+
The number 3 occurred 1 time.
19+
20+
All occurrences are unique, therefore the output is 1.
21+
Example 2
22+
Input: @ints = (1,2,3)
23+
Output: 0
24+
Example 3
25+
Input: @ints = (-2,0,1,-2,1,1,0,1,-2,9)
26+
Output: 1
27+
=end comment
28+
29+
my @Test =
30+
(1,2,2,1,1,3), True,
31+
(1,2,3), False,
32+
(-2,0,1,-2,1,1,0,1,-2,9), True,
33+
(010000).List, False,
34+
(1100_000, 999_999).List, False,
35+
(1 xx 70, 2 xx 71, 3 xx 72, 4 xx 75, 5 xx 76).flat, True;
36+
;
37+
38+
plan @Test ÷ 2;
39+
40+
sub func( $l -->Bool) {
41+
my $k = $l.Bag.values.sort.List;
42+
$k eqv $k.unique.List;
43+
}
44+
45+
for @Test -> $in, $exp {
46+
is func($in), $exp, ($++).Str;
47+
}
48+
49+
done-testing;
50+
my @int = (1 xx 70, 2 xx 71, 3 xx 72, 4 xx 75, 5 xx 76).flat;
51+
52+
say "\nInput: @int = @int[]\nOutput: ", func(@int).Int;
53+
54+
55+
exit;
56+

challenge-260/0rir/raku/ch-2.raku

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/usr/bin/env raku
2+
# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ «␤ » ∴
3+
use v6;
4+
use lib $?FILE.IO.cleanup.parent(2).add("lib");
5+
use Test;
6+
7+
=begin comment
8+
Task 2: Dictionary Rank
9+
Submitted by: Mark Anderson You are given a word, $word. Write a
10+
script to compute the dictionary rank of the given word.
11+
12+
Example 1
13+
Input: $word = 'CAT'
14+
Output: 3
15+
16+
All possible combinations of the letters:
17+
CAT, CTA, ATC, TCA, ACT, TAC
18+
19+
Arrange them in alphabetical order:
20+
ACT, ATC, CAT, CTA, TAC, TCA
21+
22+
CAT is the 3rd in the list.
23+
Therefore the dictionary rank of CAT is 3.
24+
Example 2
25+
Input: $word = 'GOOGLE'
26+
Output: 88
27+
Example 3
28+
Input: $word = 'SECRET'
29+
Output: 255
30+
=end comment
31+
32+
=begin comment
33+
I am just using the built-in &permutation, but this can be calculated more
34+
efficiently than generating the permutations.
35+
36+
'TADE' and 'TEDA', since they start with the 4th ranked letter, will be in
37+
the index range 4! minus 3! … 4! So with bookkeeping the problem can be
38+
partitioned and repartitioned. This simplification doesn't address that
39+
duplicates share identity.
40+
=end comment
41+
42+
my @Test =
43+
Str, Int,
44+
'', Int,
45+
'XYZ', 1,
46+
"ooO", 1,
47+
'Deet', 1,
48+
'CAT', 3,
49+
'TADE', 19,
50+
'Cat', 3,
51+
'GOOGLE', 88,
52+
'SECRET', 255,
53+
;
54+
plan @Test ÷ 2;
55+
56+
# grind it
57+
multi gen( Str:U $a -->Int) { Int }
58+
multi gen( Str:D $a where * eqv '' -->Int) { Int }
59+
multi gen( Str:D $a -->Int) {
60+
( $a.fc.comb.permutations».join).unique.sort.first( $a.fc, :k) + 1
61+
}
62+
63+
for @Test -> $in, $exp {
64+
is gen($in), gen($in), "$exp.raku() <- $in.raku()";
65+
}
66+
67+
done-testing;
68+
my $word = 'Supercad';
69+
say "\nInput: \$word = $word\nOutput: &gen($word)";
70+
71+

0 commit comments

Comments
 (0)