-
Notifications
You must be signed in to change notification settings - Fork 318
/
ch-1.raku
executable file
·42 lines (36 loc) · 899 Bytes
/
ch-1.raku
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env raku
use v6;
sub magicNumber(@x, @y) {
my @xS = @x.sort;
my @yS = @y.sort;
my $magic = @yS.shift - @xS.shift;
while (@xS) {
if (@yS.shift - @xS.shift != $magic) {
return; # no magic number
}
}
return $magic;
}
sub solution(@x, @y) {
say 'Input: @x = (' ~ @x.join(', ') ~ ')';
say ' @y = (' ~ @y.join(', ') ~ ')';
my $magic = magicNumber(@x, @y);
if ($magic.defined) {
say 'Output: ' ~ $magic;
say "\nThe magic number is $magic.";
say '@x = (' ~ @x.join(', ') ~ ')';
say ' + ' ~ ($magic xx @x.elems).join(' ');
say '@y = (' ~ @x.map(-> $v { $v + $magic }).join(', ') ~ ')';
}
else {
say 'Output: no magic number';
}
}
say "Example 1:";
solution([3, 7, 5], [9, 5, 7]);
say "\nExample 2:";
solution([1, 2, 1], [5, 4, 4]);
say "\nExample 3:";
solution([2], [5]);
say "\nExample 4:";
solution([1, 2], [4, 2]);