Sort numbers lexicographically: Difference between revisions
(Add Swift) |
|||
Line 897: | Line 897: | ||
-22: [-1, -10, -11, -12, -13, -14, -15, -16, -17, -18, -19, -2, -20, -21, -22, -3, -4, -5, -6, -7, -8, -9, 0, 1] |
-22: [-1, -10, -11, -12, -13, -14, -15, -16, -17, -18, -19, -2, -20, -21, -22, -3, -4, -5, -6, -7, -8, -9, 0, 1] |
||
</pre> |
</pre> |
||
=={{header|Swift}}== |
|||
<lang swift>func lex(n: Int) -> [Int] { |
|||
return stride(from: 1, through: n, by: n.signum()).map({ String($0) }).sorted().compactMap(Int.init) |
|||
} |
|||
print("13: \(lex(n: 13))") |
|||
print("21: \(lex(n: 21))") |
|||
print("-22: \(lex(n: -22))")</lang> |
|||
{{out}} |
|||
<pre>13: [1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9] |
|||
21: [1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 2, 20, 21, 3, 4, 5, 6, 7, 8, 9] |
|||
-22: [-1, -10, -11, -12, -13, -14, -15, -16, -17, -18, -19, -2, -20, -21, -22, -3, -4, -5, -6, -7, -8, -9, 0, 1]</pre> |
|||
=={{header|VBA}}== |
=={{header|VBA}}== |
Revision as of 17:20, 18 March 2020
You are encouraged to solve this task according to the task description, using any language you may know.
- Task
Given an integer n, return 1──►n (inclusive) in lexicographical order.
Show all output here on this page.
- Example
Given 13,
return: [1,10,11,12,13,2,3,4,5,6,7,8,9].
Ada
<lang Ada>WITH Ada.Containers.Generic_Array_Sort, Ada.Text_IO; USE Ada.Text_IO; PROCEDURE Main IS
TYPE Natural_Array IS ARRAY (Positive RANGE <>) OF Natural; FUNCTION Less (L, R : Natural) RETURN Boolean IS (L'Img < R'Img); PROCEDURE Sort_Naturals IS NEW Ada.Containers.Generic_Array_Sort (Positive, Natural, Natural_Array, Less); PROCEDURE Show (Last : Natural) IS A : Natural_Array (1 .. Last); BEGIN FOR I IN A'Range LOOP A (I) := I; END LOOP; Sort_Naturals (A); FOR I IN A'Range LOOP Put (A (I)'Img); END LOOP; New_Line; END Show;
BEGIN
Show (13); Show (21);
END Main; </lang>
- Output:
1 10 11 12 13 2 3 4 5 6 7 8 9 1 10 11 12 13 14 15 16 17 18 19 2 20 21 3 4 5 6 7 8 9
- Output:
AWK
Robust with checks
<lang AWK>
- syntax: GAWK -f SORT_NUMBERS_LEXICOGRAPHICALLY.AWK
- sorting:
- PROCINFO["sorted_in"] is used by GAWK
- SORTTYPE is used by Thompson Automation's TAWK
BEGIN {
prn(0) prn(1) prn(13) prn(9,10) prn(-11,+11) prn(-21) prn("",1) prn(+1,-1) exit(0)
} function prn(n1,n2) {
if (n1 <= 0 && n2 == "") { n2 = 1 } if (n2 == "") { n2 = n1 n1 = 1 } printf("%d to %d: %s\n",n1,n2,snl(n1,n2))
} function snl(start,stop, arr,i,str) {
if (start == "") { return("error: start=blank") } if (start > stop) { return("error: start>stop") } for (i=start; i<=stop; i++) { arr[i] } PROCINFO["sorted_in"] = "@ind_str_asc" ; SORTTYPE = 2 for (i in arr) { str = sprintf("%s%s ",str,i) } sub(/ $/,"",str) return(str)
} </lang>
- Output:
0 to 1: 0 1 1 to 1: 1 1 to 13: 1 10 11 12 13 2 3 4 5 6 7 8 9 9 to 10: 10 9 -11 to 11: -1 -10 -11 -2 -3 -4 -5 -6 -7 -8 -9 0 1 10 11 2 3 4 5 6 7 8 9 -21 to 1: -1 -10 -11 -12 -13 -14 -15 -16 -17 -18 -19 -2 -20 -21 -3 -4 -5 -6 -7 -8 -9 0 1 0 to 1: error: start=blank 1 to -1: error: start>stop
Alternative, using GAWK's builtin sort
This version explicitly casts integers as strings during list generation and uses the builtin sort available in GAWK on element values. <lang AWK>BEGIN {
n=13 for (i=1; i<=n; i++) a[i]=i"" asort(a) for (k in a) printf "%d ", a[k]
}</lang>
- Output:
1 10 11 12 13 2 3 4 5 6 7 8 9
BBC BASIC
<lang bbcbasic> N%=13
PRINT "[" FNLexOrder(0) CHR$127 "]" END
DEF FNLexOrder(nr%) : LOCAL i%, s$ FOR i%=nr% TO nr% + 9 IF i% > N% EXIT FOR IF i% > 0 s$+=STR$i% + "," + FNLexOrder(i% * 10) NEXT =s$</lang>
- Output:
[1,10,11,12,13,2,3,4,5,6,7,8,9]
C
<lang c>#include <math.h>
- include <stdio.h>
- include <stdlib.h>
- include <string.h>
int compareStrings(const void *a, const void *b) {
const char **aa = (const char **)a; const char **bb = (const char **)b; return strcmp(*aa, *bb);
}
void lexOrder(int n, int *ints) {
char **strs; int i, first = 1, last = n, k = n, len; if (n < 1) { first = n; last = 1; k = 2 - n; } strs = malloc(k * sizeof(char *)); for (i = first; i <= last; ++i) { if (i >= 1) len = (int)log10(i) + 2; else if (i == 0) len = 2; else len = (int)log10(-i) + 3; strs[i-first] = malloc(len); sprintf(strs[i-first], "%d", i); } qsort(strs, k, sizeof(char *), compareStrings); for (i = 0; i < k; ++i) { ints[i] = atoi(strs[i]); free(strs[i]); } free(strs);
}
int main() {
int i, j, k, n, *ints; int numbers[5] = {0, 5, 13, 21, -22}; printf("In lexicographical order:\n\n"); for (i = 0; i < 5; ++i) { k = n = numbers[i]; if (k < 1) k = 2 - k; ints = malloc(k * sizeof(int)); lexOrder(n, ints); printf("%3d: [", n); for (j = 0; j < k; ++j) { printf("%d ", ints[j]); } printf("\b]\n"); free(ints); } return 0;
}</lang>
- Output:
In lexicographical order: 0: [0 1] 5: [1 2 3 4 5] 13: [1 10 11 12 13 2 3 4 5 6 7 8 9] 21: [1 10 11 12 13 14 15 16 17 18 19 2 20 21 3 4 5 6 7 8 9] -22: [-1 -10 -11 -12 -13 -14 -15 -16 -17 -18 -19 -2 -20 -21 -22 -3 -4 -5 -6 -7 -8 -9 0 1]
C#
<lang csharp>using static System.Console; using static System.Linq.Enumerable;
public class Program {
public static void Main() { foreach (int n in new [] { 0, 5, 13, 21, -22 }) WriteLine($"{n}: {string.Join(", ", LexOrder(n))}"); }
public static IEnumerable<int> LexOrder(int n) => (n < 1 ? Range(n, 2 - n) : Range(1, n)).OrderBy(i => i.ToString());
}</lang>
- Output:
0: 0, 1 5: 1, 2, 3, 4, 5 13: 1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9 21: 1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 2, 20, 21, 3, 4, 5, 6, 7, 8, 9 -22: -1, -10, -11, -12, -13, -14, -15, -16, -17, -18, -19, -2, -20, -21, -22, -3, -4, -5, -6, -7, -8, -9, 0, 1
C++
<lang cpp>#include <algorithm>
- include <iostream>
- include <numeric>
- include <string>
- include <vector>
void lexicographical_sort(std::vector<int>& numbers) {
std::vector<std::string> strings(numbers.size()); std::transform(numbers.begin(), numbers.end(), strings.begin(), [](int i) { return std::to_string(i); }); std::sort(strings.begin(), strings.end()); std::transform(strings.begin(), strings.end(), numbers.begin(), [](const std::string& s) { return std::stoi(s); });
}
std::vector<int> lexicographically_sorted_vector(int n) {
std::vector<int> numbers(n >= 1 ? n : 2 - n); std::iota(numbers.begin(), numbers.end(), std::min(1, n)); lexicographical_sort(numbers); return numbers;
}
template <typename T> void print_vector(std::ostream& out, const std::vector<T>& v) {
out << '['; if (!v.empty()) { auto i = v.begin(); out << *i++; for (; i != v.end(); ++i) out << ',' << *i; } out << "]\n";
}
int main(int argc, char** argv) {
for (int i : { 0, 5, 13, 21, -22 }) { std::cout << i << ": "; print_vector(std::cout, lexicographically_sorted_vector(i)); } return 0;
}</lang>
- Output:
0: [0,1] 5: [1,2,3,4,5] 13: [1,10,11,12,13,2,3,4,5,6,7,8,9] 21: [1,10,11,12,13,14,15,16,17,18,19,2,20,21,3,4,5,6,7,8,9] -22: [-1,-10,-11,-12,-13,-14,-15,-16,-17,-18,-19,-2,-20,-21,-22,-3,-4,-5,-6,-7,-8,-9,0,1]
Clojure
<lang clojure>(def n 13) (sort-by str (range 1 (inc n)))</lang>
- Output:
(1 10 11 12 13 2 3 4 5 6 7 8 9)
COBOL
<lang cobol> identification division.
program-id. LexicographicalNumbers.
data division. working-storage section. 78 MAX-NUMBERS value 21. 77 i pic 9(2). 77 edited-number pic z(2). 01 lex-table. 05 table-itms occurs MAX-NUMBERS. 10 number-lex pic x(2).
procedure division. main. *>-> Load numbers perform varying i from 1 by 1 until i > MAX-NUMBERS move i to edited-number move edited-number to number-lex(i) call "C$JUSTIFY" using number-lex(i), "Left" end-perform
*>-> Sort in lexicographic order sort table-itms ascending number-lex
*>-> Show ordered numbers display "[" no advancing perform varying i from 1 by 1 until i > MAX-NUMBERS display function trim(number-lex(i)) no advancing if i < MAX-NUMBERS display ", " no advancing end-if end-perform display "]" stop run .</lang>
- Output:
[1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 2, 20, 21, 3, 4, 5, 6, 7, 8, 9]
Factor
<lang factor>USING: formatting kernel math.parser math.ranges sequences sorting ; IN: rosetta-code.lexicographical-numbers
- lex-order ( n -- seq )
[1,b] [ number>string ] map natural-sort [ string>number ] map ;
{ 13 21 -22 } [ dup lex-order "%3d: %[%d, %]\n" printf ] each</lang>
- Output:
13: { 1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9 } 21: { 1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 2, 20, 21, 3, 4, 5, 6, 7, 8, 9 } -22: { -1, -10, -11, -12, -13, -14, -15, -16, -17, -18, -19, -2, -20, -21, -22, -3, -4, -5, -6, -7, -8, -9, 0, 1 }
Go
<lang go>package main
import (
"fmt" "sort" "strconv"
)
func lexOrder(n int) []int {
first, last, k := 1, n, n if n < 1 { first, last, k = n, 1, 2-n } strs := make([]string, k) for i := first; i <= last; i++ { strs[i-first] = strconv.Itoa(i) } sort.Strings(strs) ints := make([]int, k) for i := 0; i < k; i++ { ints[i], _ = strconv.Atoi(strs[i]) } return ints
}
func main() {
fmt.Println("In lexicographical order:\n") for _, n := range []int{0, 5, 13, 21, -22} { fmt.Printf("%3d: %v\n", n, lexOrder(n)) }
}</lang>
- Output:
In lexicographical order: 0: [0 1] 5: [1 2 3 4 5] 13: [1 10 11 12 13 2 3 4 5 6 7 8 9] 21: [1 10 11 12 13 14 15 16 17 18 19 2 20 21 3 4 5 6 7 8 9] -22: [-1 -10 -11 -12 -13 -14 -15 -16 -17 -18 -19 -2 -20 -21 -22 -3 -4 -5 -6 -7 -8 -9 0 1]
Haskell
<lang Haskell>import Data.List (sort)
task :: (Ord b, Show b) => [b] -> [b] task = map snd . sort . map (\i -> (show i, i))
main = print $ task [1 .. 13]</lang>
Which we could also write, in a point-free style as: <lang haskell>import Data.List (sort)
task
:: (Ord b, Show b) => [b] -> [b]
task = map snd . sort . map (show >>= (,))
main = print $ task [1 .. 13]</lang>
and the simplest approach might be sortOn show (which only evaluates show once for each item).
<lang haskell>import Data.List (sortOn)
main :: IO () main = print $ sortOn show [1 .. 13]</lang>
- Output:
[1,10,11,12,13,2,3,4,5,6,7,8,9]
J
<lang J>task=: [: (/: ":"0) 1 + i. task 13</lang>
- Output:
1 10 11 12 13 2 3 4 5 6 7 8 9
Java
Requires Java 8 or later.
<lang java>import java.util.List;
import java.util.stream.*;
public class LexicographicalNumbers {
static List<Integer> lexOrder(int n) { int first = 1, last = n; if (n < 1) { first = n; last = 1; } return IntStream.rangeClosed(first, last) .mapToObj(Integer::toString) .sorted() .map(Integer::valueOf) .collect(Collectors.toList()); }
public static void main(String[] args) { System.out.println("In lexicographical order:\n"); int[] ints = {0, 5, 13, 21, -22}; for (int n : ints) { System.out.printf("%3d: %s\n", n, lexOrder(n)); } }
}</lang>
- Output:
In lexicographical order: 0: [0, 1] 5: [1, 2, 3, 4, 5] 13: [1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9] 21: [1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 2, 20, 21, 3, 4, 5, 6, 7, 8, 9] -22: [-1, -10, -11, -12, -13, -14, -15, -16, -17, -18, -19, -2, -20, -21, -22, -3, -4, -5, -6, -7, -8, -9, 0, 1]
Julia
<lang julia>lexorderedsequence(n) = sort(collect(n > 0 ? (1:n) : n:1), lt=(a,b) -> string(a) < string(b))
for i in [0, 5, 13, 21, -32]
println(lexorderedsequence(i))
end
</lang>
- Output:
[0, 1] [1, 2, 3, 4, 5] [1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9] [1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 2, 20, 21, 3, 4, 5, 6, 7, 8, 9] [-1, -10, -11, -12, -13, -14, -15, -16, -17, -18, -19, -2, -20, -21, -22, -23, -24, -25, -26, -27, -28, -29, -3, -30, -31, -32, -4, -5, -6, -7, -8, -9, 0, 1]
Kotlin
<lang scala>// Version 1.2.51
fun lexOrder(n: Int): List<Int> {
var first = 1 var last = n if (n < 1) { first = n last = 1 } return (first..last).map { it.toString() }.sorted().map { it.toInt() }
}
fun main(args: Array<String>) {
println("In lexicographical order:\n") for (n in listOf(0, 5, 13, 21, -22)) { println("${"%3d".format(n)}: ${lexOrder(n)}") }
}</lang>
- Output:
In lexicographical order: 0: [0, 1] 5: [1, 2, 3, 4, 5] 13: [1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9] 21: [1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 2, 20, 21, 3, 4, 5, 6, 7, 8, 9] -22: [-1, -10, -11, -12, -13, -14, -15, -16, -17, -18, -19, -2, -20, -21, -22, -3, -4, -5, -6, -7, -8, -9, 0, 1]
Lua
Lua's in-built table.sort function will sort a table of strings into lexicographical order by default. This task therefore becomes trivial by converting each number to a string before adding it to the table. <lang lua>function lexNums (limit)
local numbers = {} for i = 1, limit do table.insert(numbers, tostring(i)) end table.sort(numbers) return numbers
end
local numList = lexNums(13) print(table.concat(numList, " "))</lang>
- Output:
1 10 11 12 13 2 3 4 5 6 7 8 9
M2000 Interpreter
<lang M2000 Interpreter> Module Checkit {
Function lexicographical(N) { const nl$=Chr$(13)+Chr$(10) If N<>0 then { if N=1 then =(1,) : Exit Document A$ For k=1 to N-1 A$=Str$(k,"")+{ } Next k A$=Str$(N,"") Method A$, "SetBinaryCompare" Sort A$ Flush \\ convert strings to numbers in one statement \\ in stack of values Data Param(Replace$(nl$,",", a$)) \\ return stack as array =Array([]) } else =(0,) ' empty array } Print lexicographical(5) ' 1 2 3 4 5 Print lexicographical(13) ' 1 10 11 12 13 2 3 4 5 6 7 8 9 Print lexicographical(21) ' 1 10 11 12 13 14 15 16 17 18 19 2 20 21 3 4 5 6 7 8 9 Print lexicographical(-22) ' -1 -10 -11 -12 -13 -14 -15 -16 -17 -18 -19 -2 -20 -21 -22 -3 -4 -5 -6 -7 -8 -9 0 1
} Checkit Module Checkit {
Function lexicographical$(N) { const nl$=Chr$(13)+Chr$(10) If N<>0 then { if N=1 then =(1,) : Exit Document A$ For k=1 to N-1 A$=Str$(k,"")+{ } Next k A$=Str$(N,"") \\ by default id TextCompare, so 0 an 1 comes first in -22 Method A$, "SetBinaryCompare" Sort A$ Flush ="["+Replace$(nl$," ", a$)+"]" } else =("",) ' empty array } Print lexicographical$(5) ' [1 2 3 4 5] Print lexicographical$(13) ' [1 10 11 12 13 2 3 4 5 6 7 8 9] Print lexicographical$(21) '[1 10 11 12 13 14 15 16 17 18 19 2 20 21 3 4 5 6 7 8 9] Print lexicographical$(-22) ' [-1 -10 -11 -12 -13 -14 -15 -16 -17 -18 -19 -2 -20 -21 -22 -3 -4 -5 -6 -7 -8 -9 0 1]
} Checkit </lang>
Mathematica
<lang Mathematica>SortBy[Range[13],ToString]</lang>
- Output:
{1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9}
Microsoft Small Basic
In Small Basic there is no string comparison: “a”>”b” the result is “False”, “b”>”a” the result is also “False”. It doesn’t help at all. <lang smallbasic>' Lexicographical numbers - 25/07/2018 xx="000000000000000" For n=1 To 3
nn=Text.GetSubText(" 5 13 21",n*4-3,4) ll=Text.GetLength(nn) For i=1 To nn t[i]=i EndFor i=nn-1 k=0 For i=i To 1 Step -1 ok=1 For j=1 To i k=j+1 tj=Text.GetSubText(Text.Append(t[j],xx),1,ll) tk=Text.GetSubText(Text.Append(t[k],xx),1,ll) If tj>tk Then w=t[j] t[j]=t[k] t[k]=w ok=0 EndIf EndFor If ok=1 Then Goto exitfor EndIf EndFor
exitfor:
x="" For i=1 To nn x=x+","+t[i] EndFor TextWindow.WriteLine(nn+":"+Text.GetSubTextToEnd(x,2))
EndFor </lang>
- Output:
5:1,2,3,4,5 13:1,10,11,12,13,2,3,4,5,6,7,8,9 21:1,10,11,12,13,14,15,16,17,18,19,2,20,21,3,4,5,6,7,8,9
Perl
<lang perl>printf("%4d: [%s]\n", $_, join ',', sort $_ > 0 ? 1..$_ : $_..1) for 13, 21, -22</lang>
- Output:
13: [1,10,11,12,13,2,3,4,5,6,7,8,9] 21: [1,10,11,12,13,14,15,16,17,18,19,2,20,21,3,4,5,6,7,8,9] -22: [-1,-10,-11,-12,-13,-14,-15,-16,-17,-18,-19,-2,-20,-21,-22,-3,-4,-5,-6,-7,-8,-9,0,1]
Phix
Idiomatic version - crashes if n<1, and calls sprint() 76 times. <lang Phix>function lexographic(integer i, j)
return compare(sprint(i),sprint(j))
end function
function lex_order(integer n)
return custom_sort(routine_id("lexographic"), tagset(n))
end function
?lex_order(13)</lang>
- Output:
{1,10,11,12,13,2,3,4,5,6,7,8,9}
Alternative version, handles n<1, and for n=13 (say) it calls sprint() only 13 times instead of 76. <lang Phix>function lex_order(integer n)
integer {lo,hi} = iff(n<1?{n-1,1}:{0,n}), l = hi-lo sequence s = repeat(0,l) for i=1 to l do s[i] = {sprint(lo+i),lo+i} end for s = sort(s) for i=1 to l do s[i] = s[i][2] end for return s
end function
?lex_order(13) ?lex_order(0) ?lex_order(-22)</lang>
- Output:
{1,10,11,12,13,2,3,4,5,6,7,8,9} {0,1} {-1,-10,-11,-12,-13,-14,-15,-16,-17,-18,-19,-2,-20,-21,-22,-3,-4,-5,-6,-7,-8,-9,0,1}
PicoLisp
<lang PicoLisp>(println
(by format sort (range 1 13) ) )</lang>
- Output:
(1 10 11 12 13 2 3 4 5 6 7 8 9)
PureBasic
<lang purebasic>EnableExplicit
Procedure lexOrder(n, Array ints(1))
Define first = 1, last = n, k = n, i If n < 1 first = n last = 1 k = 2 - n EndIf Dim strs.s(k - 1) For i = first To last strs(i - first) = Str(i) Next SortArray(strs(), #PB_Sort_Ascending) For i = 0 To k - 1 ints(i) = Val(Strs(i)) Next
EndProcedure
If OpenConsole()
PrintN(~"In lexicographical order:\n") Define i, j, n, k For i = 0 To 4 Read n k = n If n < 1 k = 2 - n EndIf Dim ints(k - 1) lexOrder(n, ints()) Define.s ns = RSet(Str(n), 3) Print(ns + ": [") For j = 0 To k - 1 Print(Str(ints(j)) + " ") Next j PrintN(~"\b]") Next i Input() End
DataSection Data.i 0, 5, 13, 21, -22 EndDataSection
EndIf</lang>
- Output:
In lexicographical order: 0: [0 1] 5: [1 2 3 4 5] 13: [1 10 11 12 13 2 3 4 5 6 7 8 9] 21: [1 10 11 12 13 14 15 16 17 18 19 2 20 21 3 4 5 6 7 8 9] -22: [-1 -10 -11 -12 -13 -14 -15 -16 -17 -18 -19 -2 -20 -21 -22 -3 -4 -5 -6 -7 -8 -9 0 1]
Python
<lang python>n=13 print(sorted(range(1,n+1), key=str))</lang>
- Output:
[1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9]
Racket
<lang racket>#lang racket
(define (lex-sort n) (sort (if (< 0 n) (range 1 (add1 n)) (range n 2))
string<? #:key number->string))
(define (show n) (printf "~a: ~a\n" n (lex-sort n)))
(show 0) (show 1) (show 5) (show 13) (show 21) (show -22)</lang>
- Output:
0: (0 1) 1: (1) 5: (1 2 3 4 5) 13: (1 10 11 12 13 2 3 4 5 6 7 8 9) 21: (1 10 11 12 13 14 15 16 17 18 19 2 20 21 3 4 5 6 7 8 9) -22: (-1 -10 -11 -12 -13 -14 -15 -16 -17 -18 -19 -2 -20 -21 -22 -3 -4 -5 -6 -7 -8 -9 0 1)
Raku
(formerly Perl 6)
It is somewhat odd that the task name is sort numbers lexicographically but immediately backtracks in the task header to sorting integers lexicographically. Why only integers? This will sort ANY real numbers lexicographically. For a non-integer, assumes that the given number is a hard boundary and 1 is a "soft" boundary. E.G. The given number is definitely included; 1 is only a threshold, it is included if it matches exactly. (Could be the other way around, this it the way I choose.) <lang perl6>sub lex (Real $n, $step = 1) {
($n < 1 ?? ($n, * + $step …^ * > 1) !! ($n, * - $step …^ * < 1)).sort: ~*
}
- TESTING
for 13, 21, -22, (6, .333), (-4, .25), (-5*π, e) {
my ($bound, $step) = |$_, 1; say "Boundary:$bound, Step:$step >> ", lex($bound, $step).join: ', ';
}</lang>
- Output:
Boundary:13, Step:1 >> 1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9 Boundary:21, Step:1 >> 1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 2, 20, 21, 3, 4, 5, 6, 7, 8, 9 Boundary:-22, Step:1 >> -1, -10, -11, -12, -13, -14, -15, -16, -17, -18, -19, -2, -20, -21, -22, -3, -4, -5, -6, -7, -8, -9, 0, 1 Boundary:6, Step:0.333 >> 1.005, 1.338, 1.671, 2.004, 2.337, 2.67, 3.003, 3.336, 3.669, 4.002, 4.335, 4.668, 5.001, 5.334, 5.667, 6 Boundary:-4, Step:0.25 >> -0.25, -0.5, -0.75, -1, -1.25, -1.5, -1.75, -2, -2.25, -2.5, -2.75, -3, -3.25, -3.5, -3.75, -4, 0, 0.25, 0.5, 0.75, 1 Boundary:-15.707963267948966, Step:2.718281828459045 >> -10.271399611030876, -12.989681439489921, -15.707963267948966, -2.116554125653742, -4.834835954112787, -7.553117782571832, 0.6017277028053032
REXX
This REXX version allows the starting and ending number to be specified via the command line (CL),
as well as the increment. Negative numbers are supported and need not be integers.
<lang rexx>/*REXX pgm displays a horizontal list of a range of numbers sorted lexicographically.*/
parse arg LO HI INC . /*obtain optional arguments from the CL*/
if LO== | LO=="," then LO= 1 /*Not specified? Then use the default.*/
if HI== | HI=="," then HI= 13 /* " " " " " " */
if INC== | INC=="," then INC= 1 /* " " " " " " */
- = 0 /*for actual sort, start array with 1.*/
do j=LO to HI by INC /*construct an array from LO to HI.*/ #= # + 1; @.#= j / 1 /*bump counter; define array element. */ end /*j*/ /* [↑] Also, normalize the element #. */
call Lsort # /*sort numeric array with a simple sort*/ $= /*initialize a horizontal numeric list.*/
do k=1 for #; $= $','@.k /*construct " " " */ end /*k*/ /* [↑] prefix each number with a comma*/ /* [↓] display a continued SAY text.*/
say 'for ' LO"──►"HI ' by ' INC " (inclusive), " # ,
' elements sorted lexicographically:'
say '['strip($, "L", ',')"]" /*strip leading comma, bracket the list*/ exit /*stick a fork in it, we're all done. */ /*──────────────────────────────────────────────────────────────────────────────────────*/ Lsort: procedure expose @.; parse arg n; m= n-1 /*N: is the number of @ array elements.*/
do m=m by -1 until ok; ok= 1 /*keep sorting the @ array until done.*/ do j=1 for m; k= j+1; if @.j>>@.k then parse value @.j @.k 0 with @.k @.j ok end /*j*/ /* [↑] swap 2 elements, flag as ¬done.*/ end /*m*/; return</lang>
- output when using the default inputs:
for 1──►13 by 1 (inclusive), 13 elements sorted lexicographically: [1,10,11,12,13,2,3,4,5,6,7,8,9]
- output when using the input of: 1 34
for 1──►34 by 1 (inclusive), 34 elements sorted lexicographically: [1,10,11,12,13,14,15,16,17,18,19,2,20,21,22,23,24,25,26,27,28,29,3,30,31,32,33,34,4,5,6,7,8,9]
- output when using the input of: -11 22
for -11──►22 by 1 (inclusive), 34 elements sorted lexicographically: [-1,-10,-11,-2,-3,-4,-5,-6,-7,-8,-9,0,1,10,11,12,13,14,15,16,17,18,19,2,20,21,22,3,4,5,6,7,8,9]
- output when using the input of: -4 8 0.5
for -4──►8 by 0.5 (inclusive), 25 elements sorted lexicographically: [-0.5,-1,-1.5,-2,-2.5,-3,-3.5,-4,0,0.5,1,1.5,2,2.5,3,3.5,4,4.5,5,5.5,6,6.5,7,7.5,8]
Ring
<lang ring>
- Project : Lexicographical numbers
lex = 1:13 strlex = list(len(lex)) for n = 1 to len(lex)
strlex[n] = string(lex[n])
next strlex = sort(strlex) see "Lexicographical numbers = " showarray(strlex)
func showarray(vect)
see "[" svect = "" for n = 1 to len(vect) svect = svect + vect[n] + "," next svect = left(svect, len(svect) - 1) see svect + "]" + nl
</lang> Output:
Lexicographical numbers = [1,10,11,12,13,2,3,4,5,6,7,8,9]
Ruby
<lang ruby>n = 13 p (1..n).sort_by(&:to_s) </lang>
- Output:
[1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9]
Scala
- Output:
Best seen in running your browser either by ScalaFiddle (ES aka JavaScript, non JVM) or Scastie (remote JVM).
<lang Scala>object LexicographicalNumbers extends App { def ints = List(0, 5, 13, 21, -22)
def lexOrder(n: Int): Seq[Int] = (if (n < 1) n to 1 else 1 to n).sortBy(_.toString)
println("In lexicographical order:\n") for (n <- ints) println(f"$n%3d: ${lexOrder(n).mkString("[",", ", "]")}%s")
}</lang>
Sidef
<lang ruby>func lex_order (n) {
[range(1, n, n.sgn)...].sort_by { Str(_) }
}
[13, 21, -22].each {|n|
printf("%4s: %s\n", n, lex_order(n))
}</lang>
- Output:
13: [1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9] 21: [1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 2, 20, 21, 3, 4, 5, 6, 7, 8, 9] -22: [-1, -10, -11, -12, -13, -14, -15, -16, -17, -18, -19, -2, -20, -21, -22, -3, -4, -5, -6, -7, -8, -9, 0, 1]
Swift
<lang swift>func lex(n: Int) -> [Int] {
return stride(from: 1, through: n, by: n.signum()).map({ String($0) }).sorted().compactMap(Int.init)
}
print("13: \(lex(n: 13))") print("21: \(lex(n: 21))") print("-22: \(lex(n: -22))")</lang>
- Output:
13: [1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9] 21: [1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 2, 20, 21, 3, 4, 5, 6, 7, 8, 9] -22: [-1, -10, -11, -12, -13, -14, -15, -16, -17, -18, -19, -2, -20, -21, -22, -3, -4, -5, -6, -7, -8, -9, 0, 1]
VBA
<lang VB>Public Function sortlexicographically(N As Integer)
Dim arrList As Object Set arrList = CreateObject("System.Collections.ArrayList") For i = 1 To N arrList.Add CStr(i) Next i arrList.Sort Dim item As Variant For Each item In arrList Debug.Print item & ", "; Next
End Function
Public Sub main()
Call sortlexicographically(13)
End Sub</lang>
- Output:
1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9,
zkl
<lang zkl>fcn lexN(n){ n.pump(List,'+(1),"toString").sort().apply("toInt") }</lang> <lang zkl>foreach n in (T(5,13,21)){ println("%2d: %s".fmt(n,lexN(n).concat(","))) }</lang>
- Output:
5: 1,2,3,4,5 13: 1,10,11,12,13,2,3,4,5,6,7,8,9 21: 1,10,11,12,13,14,15,16,17,18,19,2,20,21,3,4,5,6,7,8,9