Jump to content

Sort the letters of string in alphabetical order: Difference between revisions

→‎{{header|Raku}}: Add an alternate, somewhat pedantic version
(→‎{{header|Raku}}: Add an alternate, somewhat pedantic version)
Line 333:
 
=={{header|Raku}}==
===Simi-realistic version===
<lang perl6>sub sort_within_string ( $_ is copy ) {
constant @lexographic_order = sort *.fc, map &chr, 1..255;
Line 352 ⟶ 353:
.aaccddeeeeeeffghhhiiiillmmmNnnooooooooorrrstttttttuwy
</pre>
 
===Following the actual task title / description===
Following a strict interpretation of the actual task title and description.
 
* Sorts letters. Only letters. Into alphabetical order. Regardless of case. '''EVERYTHING''' else is ignored / pushed to the end of the "sorted" string. Not '''ASCII''' order. Not '''EBCDIC''' order. Only '''alphabetical''' order. If it ain't in the alphabet, it ain't sorted.
* Sorts letters of the string two characters at a time '''as a string'''. No breaking up the string into a list or array, sorting that then joining back together; or picking characters out of a string to generate a new string. Sorts a string, as a string, in place.
 
Sorted output is wrapped in double guillemots to make it easier to see where it starts and ends.
 
<lang perl6>sub moronic-sort ($string is copy) {
my $chars = $string.chars;
loop {
for ^$chars {
if ($string.substr($_, 1).fc gt $string.substr($_ + 1, 1).fc and $string.substr($_ + 1, 1) ~~ /<:L>/)
or $string.substr($_, 1) ~~ /<:!L>/ {
$string = $string.substr(0, $_) ~ $string.substr($_ , 2).flip ~ $string.substr($_ + 2 min $chars);
}
}
last if $++ >= $chars;
}
$string
}
 
sub wrap ($whatever) { '»»' ~ $whatever ~ '««' }
 
 
# Test sort the exact string as specified in the task title.
say "moronic-sort 'string'\n" ~ wrap moronic-sort 'string';
 
 
# Other tests demonstrating the extent of the stupidity of this task.
say "\nLonger test sentence\n" ~
wrap moronic-sort q[This is a moronic sort. It's only concerned with sorting letters, so everthing else is pretty much ignored / pushed to the end. It also doesn't much care about letter case, so there is no upper / lower case differentiation.];
 
 
say "\nExtended test string:\n" ~ my $test = (32..126)».chr.pick(*).join;
say wrap moronic-sort $test;</lang>
 
{{out}}
<pre>moronic-sort 'string'
»»ginrst««
 
Longer test sentence
»»aaaaaaabccccccccddddddeeeeeeeeeeeeeeeeeeeeeeeeeffggghhhhhhhhiiiIiiiiiIiiiillllllmmmnnnnnnnnnnnnoooooooooooooooopppprrrrrrrrrrrrrrssssssssssssssssTtttttttttttttttttttuuuuuvwwyy , / . . ' , / . ' ««
 
Extended test string:
!kjyxAa+,LGh_8?3lXEwW-D]Ku|SY[@VF\.op{=q>MT 1tJ/$nN(Z*%&9^v57")`PCiOHQe'RUb<gs;6}#cfmrzd42B~0I:
»»AabBCcDdEeFfGghHiIjJkKLlMmnNoOpPqQRrSsTtuUVvwWxXyYZz[@\.{=> 1/$(*%&9^57")`'<;6}#42~0:!+,_8?3-]|««</pre>
 
=={{header|REXX}}==
10,343

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.