-
Notifications
You must be signed in to change notification settings - Fork 0
/
sortstring.tiny
55 lines (45 loc) · 1.63 KB
/
sortstring.tiny
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
################################################################
#
# Tiny function for sorting the characters in a string in O(n) time.
#
################################################################
#
# Routine to sort the characters in a string
#
fn SortString str {
# limiting sort to Ascii (8-bit) chars
array = [<int[]>].new(256)
# count the occurrances of a character in the string, storing the
# result in the array index by the characters codepoint
foreach (i in 0..str.length-1) {
array[str[i]] += 1
}
# retrieve the codepoints with a non-zero occurance and expand them
result = foreach (i in 0 .. array.count-1) {
if (array[i] > 0) {
([<string>] [<char>] i) * array[i]
}
}
# Join the results into a single string and return it
return result |> join
}
fn SortString2 str -> [<int[]>].new(256)
|> {
arr ->
# count each letter occurance at it's codepoint
str.ToCharArray() |> foreach {arr[it] += 1};
arr
}
|> map {
if (it) {
([<string>][<char>] it2) * it
}
}
|> join
strToSort = "Hello world! Bonjour monde! How are you doing?"
println('Using iterative function')
strToSort |> SortString |> println
println('Using pipeline function')
strToSort |> SortString2 |> println
println('Using buildin sort')
strToSort.ToCharArray() |> sort |> join |> println