/
ch-1.raku
executable file
·82 lines (67 loc) · 2.07 KB
/
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env raku
use v6;
sub quoted_list ( *@list ) {
# given a list, quote the elements and join them with commas
my @quoted = @list.map: { qq{"$_"} };
return @quoted.join(q{, });
}
sub quoted_english_list ( *@list ) {
# given a list, quote the elements and join them
# in a way that makes sense to english speakers
my @quoted = @list.map: { qq{"$_"} };
my $last = @quoted.pop(); # last element in array
if (@quoted == 0) {
# using an array in a scalar context returns
# the number of elements in the array
# there was only one element in the list
return $last;
}
my $joined = join q{, }, @quoted;
if (@quoted > 1) {
# if there's more than element, add an Oxford comma
$joined ~= q{,};
}
return "$joined and $last";
}
sub is_lexically_sorted ($input) {
# get the characters in the input string
# putting $input in quotes casts it as a Str
my @characters = "$input".split("", :skip-empty);
# sort the characters ascending
my @forwards = @characters.sort: { $^a.fc cmp $^b.fc };
# sort the characters descending
my @backwards = @characters.sort: { $^b.fc cmp $^a.fc };
# if the input string is matches either sorted string,
# then return true
return( $input eq @forwards.join("")
||
$input eq @backwards.join("") );
}
sub solution (*@str) {
say "Input: \@str = (" ~ quoted_list(@str) ~ ")";
my @not_lexically_sorted = @str.grep({
!is_lexically_sorted($_)
});
say "Output: " ~ @not_lexically_sorted.elems;
say "";
if (@not_lexically_sorted.elems == 0) {
say "In the given array all elements are"
~ " lexicographically sorted.";
}
elsif (@not_lexically_sorted.elems == 1) {
say "In the given array "
~ quoted_list(@not_lexically_sorted)
~ " is the only element which is not"
~ " lexicographically sorted.";
}
else {
say "In the given array "
~ quoted_english_list(@not_lexically_sorted)
~ " are not lexicographically sorted.";
}
}
say "Example 1:";
solution("abc", "bce", "cae");
say "";
say "Example 2:";
solution("yxz", "cba", "mon");