Write a Program That Reads in Three Strings and Sorts Them Lexicographically Python Site
Sort numbers lexicographically
Y'all are encouraged to solve this job according to the task description, using any linguistic communication you may know.
- Task
Given an integer due north, return ane──►northward (inclusive) in lexicographical order.
Testify all output here on this page.
- Case
Given thirteen,
return: [1,10,xi,12,thirteen,2,3,four,5,6,vii,8,9].
Contents
- 1 11l
- 2 Action!
- 3 Ada
- 4 APL
- v AppleScript
- six Arturo
- seven AutoHotkey
- 8 AWK
- 8.1 Robust with checks
- 8.2 Culling, using GAWK's builtin sort
- ix Bacon
- 10 BBC BASIC
- 11 C
- 12 C#
- 13 C++
- fourteen Clojure
- 15 COBOL
- sixteen Common Lisp
- 17 Factor
- 18 Fōrmulæ
- 19 FreeBASIC
- 20 Go
- 21 Haskell
- 22 Isabelle
- 23 J
- 24 Java
- 25 Ksh
- 26 jq
- 27 Julia
- 28 Kotlin
- 29 Lambdatalk
- 30 Lua
- 31 M2000 Interpreter
- 32 Mathematica/Wolfram Language
- 33 Microsoft Small Basic
- 34 MUMPS
- 35 Nim
- 36 Perl
- 37 Phix
- 38 PicoLisp
- 39 Prolog
- xl PureBasic
- 41 Python
- 42 Quackery
- 43 Racket
- 44 Raku
- 45 REXX
- 46 Band
- 47 Carmine
- 48 Rust
- 49 Scala
- 50 Sidef
- 51 Swift
- 52 Tcl
- 53 VBA
- 54 Wren
- 55 zkl
11l [edit]
V n = thirteen
print(sorted(Array(i..n), cardinal' i -> String(i)))
[1, 10, 11, 12, 13, 2, 3, 4, 5, vi, 7, 8, ix]
Action! [edit]
PROC PrintArray(INT ARRAY a INT size)
INT iPut('[)
FOR i=0 TO size-1
DO
IF i>0 Then Put(' ) FI
PrintI(a(i))
OD
Put(']) PutE()
RETURNINT FUNC Compare(INT a1,a2)
CHAR Assortment s1(10),s2(10)
INT resStrI(a1,s1) StrI(a2,s2)
res=SCompare(s1,s2)
RETURN (res)PROC InsertionSort(INT Assortment a INT size)
INT i,j,valueFOR i=1 TO size-1
DO
value=a(i)
j=i-ane
WHILE j>=0 AND Compare(a(j),value)>0
DO
a(j+1)=a(j)
j==-ane
OD
a(j+1)=value
OD
RETURNPROC Exam(INT ARRAY a INT size)
PrintE("Array before sort:")
PrintArray(a,size)
InsertionSort(a,size)
PrintE("Array afterwards sort:")
PrintArray(a,size)
PutE()
ReturnPROC Main()
Ascertain COUNT_A="13"
Ascertain COUNT_B="50"
INT Array a(COUNT_A),b(COUNT_B)
BYTE iFOR i=1 TO COUNT_A
DO a(i-1)=i ODFOR i=1 TO COUNT_B
Exercise b(i-one)=i ODExam(a,COUNT_A)
Test(b,COUNT_B)
Return
Screenshot from Atari eight-bit computer
Array before sort: [ane 2 three four 5 vi seven viii nine 10 11 12 13] Array after sort: [1 10 11 12 xiii 2 3 iv v six 7 eight 9] Array before sort: [1 ii iii four v half-dozen 7 8 9 10 11 12 thirteen xiv 15 sixteen 17 18 19 20 21 22 23 24 25 26 27 28 29 xxx 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50] Assortment later sort: [1 10 eleven 12 xiii 14 xv sixteen 17 18 nineteen 2 twenty 21 22 23 24 25 26 27 28 29 three xxx 31 32 33 34 35 36 37 38 39 iv forty 41 42 43 44 45 46 47 48 49 5 50 6 vii eight 9]
Ada [edit]
WITH Ada.Containers.Generic_Array_Sort, Ada.Text_IO;
Utilise Ada.Text_IO;
PROCEDURE Main IS
Type Natural_Array IS ARRAY (Positive RANGE <>) OF Natural;
FUNCTION Less (50, R : Natural) Return Boolean IS (L'Img < R'Img);
Process Sort_Naturals IS NEW Ada.Containers.Generic_Array_Sort
(Positive, Natural, Natural_Array, Less);
PROCEDURE Prove (Concluding : Natural) IS
A : Natural_Array ( one .. Terminal );
BEGIN
FOR I IN A'Range LOOP A (I) := I; Finish LOOP;
Sort_Naturals (A);
FOR I IN A'Range LOOP Put (A (I)'Img); END LOOP;
New_Line;
Finish Testify;
Brainstorm
Show ( 13 );
Show ( 21 );
END Chief;
ane ten 11 12 xiii 2 3 4 5 half dozen 7 8 9 1 10 eleven 12 13 14 15 sixteen 17 xviii 19 ii 20 21 3 4 5 6 seven 8 ix
APL [edit]
Dyalog APL (with origin 0, ⎕IO←0)
{⍎¨{⍵[⍋⍵]}⍕¨1+⍳⍵} thirteen 1 10 11 12 xiii 2 iii iv v 6 7 viii 9 {⍎¨{⍵[⍋⍵]}⍕¨one+⍳⍵} 21 1 x 11 12 thirteen 14 xv 16 17 18 xix 2 twenty 21 3 iv 5 6 seven 8 9 AppleScript [edit]
For fast execution of the task as specified, this accept on the BBC BASIC method below generates the integers in the required order:
on oneToNLexicographically(north)
script o
holding output : { }on otnl(i)
ready j to i + nine - i mod 10
if (j > n) then set j to n
repeat with i from i to j
fix end of my output to i
tell i * 10 to if ( it ≤ n) then my otnl( information technology )
stop repeat
end otnl
stop scripto's otnl( 1 )
render o's output
finish oneToNLexicographically-- Test code:
oneToNLexicographically( 13 )
--> {ane, 10, eleven, 12, 13, ii, 3, 4, v, 6, vii, 8, 9}oneToNLexicographically( 123 )
--> {1, 10, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 11, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 12, 120, 121, 122, 123, xiii, fourteen, xv, xvi, 17, 18, 19, two, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 3, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 4, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, v, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 6, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, seven, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, viii, fourscore, 81, 82, 83, 84, 85, 86, 87, 88, 89, 9, ninety, 91, 92, 93, 94, 95, 96, 97, 98, 99}
In the unlikely outcome of it ever being necessary to sort a given list of integers in this fashion, one possibility is to create some other list containing text versions of the integers and to sort this while rearranging the integer versions in parallel.
use sorter : script "Custom Iterative Ternary Merge Sort" -- <https://macscripter.net/viewtopic.php?pid=194430#p194430>on sortLexicographically(integerList) -- Sorts integerList in place.
set astid to AppleScript's text detail delimiters
gear up AppleScript's text item delimiters to linefeed
set textList to paragraphs of (integerList equally text )
set AppleScript's text item delimiters to astidconsidering hyphens but ignoring numeric strings
tell sorter to sort(textList, one, - ane, {slave:{integerList} } )
end considering
end sortLexicographically-- Examination lawmaking:
prepare someIntegers to { i, 2, - 6, iii, 4, 5, - 10, 6, 7, viii, ix, 10, xi, 12, 13, - 2, - 5, - 1, - 4, - 3, 0 }
sortLexicographically(someIntegers)
render someIntegers
--> {-1, -10, -2, -three, -4, -five, -vi, 0, i, 10, xi, 12, 13, 2, iii, iv, 5, 6, vii, eight, nine}
Arturo [edit]
arr: ane..13
print sort map arr => [ to :string]
1 10 xi 12 13 2 3 4 5 6 7 viii 9
AutoHotkey [edit]
n2lexicog(n) {
Arr := [ ] , list := ""
loop % due north
listing .= A_Index "`n"
Sort , list
for chiliad, v in StrSplit(Trim(list, "`n" ) , "`n" )
Arr.Push(v)
return Arr
}
Examples:
n := 13
10 := n2lexicog(north)
for k, v in x
output .= v ", "
MsgBox % "[" Trim(output, ", " ) "]" ; prove output
return
[1, 10, 11, 12, 13, ii, 3, 4, five, half-dozen, vii, 8, 9]
AWK [edit]
Robust with checks [edit]
# syntax: GAWK -f SORT_NUMBERS_LEXICOGRAPHICALLY.AWK
#
# sorting:
# PROCINFO["sorted_in"] is used by GAWK
# SORTTYPE is used by Thompson Automation'south TAWK
#
BEGIN {
prn( 0 )
prn( 1 )
prn( thirteen )
prn( 9,10 )
prn( - xi,+ 11 )
prn( - 21 )
prn( "",1 )
prn( + 1,- ane )
exit( 0 )
}
function prn(n1,n2) {
if (n1 <= 0 && n2 == "" ) {
n2 = one
}
if (n2 == "" ) {
n2 = n1
n1 = i
}
printf ( "%d to %d: %south\n",n1,n2,snl(n1,n2) )
}
function snl(offset,stop, arr,i,str) {
if (start == "" ) {
return ( "error: commencement=blank" )
}
if (start > stop) {
render ( "fault: start>stop" )
}
for (i=start; i<=finish; i++ ) {
arr[i]
}
PROCINFO[ "sorted_in" ] = "@ind_str_asc" ; SORTTYPE = 2
for (i in arr) {
str = sprintf ( "%s%due south ",str,i)
}
sub ( / $/,"",str)
return (str)
}
0 to 1: 0 one 1 to 1: ane i to thirteen: 1 10 11 12 13 2 3 four v half-dozen 7 8 ix ix to 10: 10 nine -xi to 11: -1 -10 -xi -2 -3 -4 -5 -6 -7 -8 -9 0 1 10 eleven two 3 4 5 half dozen vii 8 9 -21 to one: -one -10 -11 -12 -thirteen -fourteen -15 -16 -17 -18 -nineteen -two -20 -21 -3 -four -5 -6 -7 -8 -nine 0 1 0 to one: error: offset=blank 1 to -1: error: start>terminate
Culling, using GAWK's builtin sort [edit]
This version explicitly casts integers as strings during list generation and uses the builtin sort bachelor in GAWK on chemical element values.
Begin {
n=xiii
for (i=1; i<=north; i++ )
a[i]=i""
asort(a)
for (k in a)
printf "%d ", a[m]
}
ane 10 11 12 13 2 3 4 5 6 7 8 9
Bacon [edit]
Create a delimited string with numbers and employ SORT$.
CONST n = 13
FOR ten = 1 TO n
result$ = Append$(event$, 0, STR$(10))
Next
Impress SORT$(result$)
i 10 11 12 thirteen 2 3 four v six 7 viii 9
BBC Basic [edit]
N%=13
Print "[" LEFT$(FNLexOrder(0)) "]"
CeaseDEF FNLexOrder(nr%) : LOCAL i%, s$
FOR i%=nr% TO nr% + nine
IF i% > N% EXIT FOR
IF i% > 0 s$+=STR$i% + "," + FNLexOrder(i% * 10)
Adjacent
=s$
[ane,ten,eleven,12,13,2,3,iv,five,half-dozen,7,8,9]
C [edit]
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <cord.h>int compareStrings( const void *a, const void *b) {
const char **aa = ( const char ** )a;
const char **bb = ( const char ** )b;
render strcmp ( *aa, *bb) ;
}void lexOrder( int due north, int *ints) {
char **strs;
int i, first = ane , last = n, grand = n, len;
if (northward < i ) {
commencement = n; terminal = i ; grand = 2 - n;
}
strs = malloc (k * sizeof ( char * ) ) ;
for (i = kickoff; i <= last; ++i) {
if (i >= 1 ) len = ( int ) log10 (i) + ii ;
else if (i == 0 ) len = 2 ;
else len = ( int ) log10 ( -i) + 3 ;
strs[i-first] = malloc (len) ;
sprintf (strs[i-starting time] , "%d" , i) ;
}
qsort (strs, one thousand, sizeof ( char * ) , compareStrings) ;
for (i = 0 ; i < k; ++i) {
ints[i] = atoi (strs[i] ) ;
free (strs[i] ) ;
}
free (strs) ;
}int main( ) {
int i, j, g, n, *ints;
int numbers[ v ] = { 0 , 5 , 13 , 21 , - 22 } ;
printf ( "In lexicographical guild:\n \n" ) ;
for (i = 0 ; i < 5 ; ++i) {
thousand = north = numbers[i] ;
if (k < 1 ) k = 2 - thousand;
ints = malloc (k * sizeof ( int ) ) ;
lexOrder(n, ints) ;
printf ( "%3d: [" , northward) ;
for (j = 0 ; j < m; ++j) {
printf ( "%d " , ints[j] ) ;
}
printf ( "\b]\n" ) ;
free (ints) ;
}
return 0 ;
}
In lexicographical guild: 0: [0 ane] v: [one 2 3 4 5] 13: [1 10 eleven 12 13 two 3 4 v 6 7 8 9] 21: [1 10 11 12 xiii fourteen 15 xvi 17 xviii nineteen ii 20 21 iii iv five half dozen 7 8 9] -22: [-1 -10 -eleven -12 -13 -14 -15 -16 -17 -18 -19 -2 -twenty -21 -22 -three -4 -5 -6 -7 -8 -ix 0 1]
C# [edit]
using static System . Console ;
using static System . Linq . Enumerable ;public form Program
{
public static void Main( ) {
foreach ( int n in new [ ] { 0, 5, 13, 21, - 22 } ) WriteLine($"{northward}: {string.Join(", ", LexOrder(northward))}" ) ;
}public static IEnumerable< int > LexOrder( int due north) => (due north < one ? Range(north, ii - n) : Range( ane, n) ) . OrderBy (i => i. ToString ( ) ) ;
}
0: 0, 1 5: one, 2, 3, 4, 5 13: 1, ten, eleven, 12, 13, two, 3, iv, 5, 6, 7, eight, 9 21: 1, x, 11, 12, 13, fourteen, 15, 16, 17, xviii, 19, 2, 20, 21, 3, iv, 5, 6, 7, 8, nine -22: -1, -x, -11, -12, -13, -14, -fifteen, -sixteen, -17, -18, -xix, -2, -20, -21, -22, -3, -four, -5, -6, -7, -viii, -9, 0, 1
C++ [edit]
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cord>
#include <vector>void lexicographical_sort(std:: vector < int > & numbers) {
std:: vector <std:: string > strings(numbers.size ( ) ) ;
std:: transform (numbers.brainstorm ( ), numbers.end ( ), strings.begin ( ),
[ ] ( int i) { render std:: to_string (i) ; } ) ;
std:: sort (strings.begin ( ), strings.end ( ) ) ;
std:: transform (strings.begin ( ), strings.terminate ( ), numbers.begin ( ),
[ ] ( const std:: string & s) { render std:: stoi (due south) ; } ) ;
}std:: vector < int > lexicographically_sorted_vector( int north) {
std:: vector < int > numbers(n >= 1 ? north : 2 - northward) ;
std:: iota (numbers.brainstorm ( ), 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 ( !5.empty ( ) ) {
car i = five.brainstorm ( ) ;
out << *i++ ;
for ( ; i ! = v.end ( ) ; ++i)
out << ',' << *i;
}
out << "]\n" ;
}int primary( int argc, char ** argv) {
for ( int i : { 0, 5, 13, 21, - 22 } ) {
std:: cout << i << ": " ;
print_vector(std:: cout, lexicographically_sorted_vector(i) ) ;
}
render 0 ;
}
0: [0,one] 5: [1,2,three,four,5] 13: [1,ten,11,12,xiii,ii,3,4,5,6,vii,8,9] 21: [i,x,11,12,xiii,14,15,16,17,18,19,2,20,21,3,4,v,half-dozen,7,viii,9] -22: [-1,-10,-11,-12,-13,-14,-15,-16,-17,-18,-xix,-two,-20,-21,-22,-3,-iv,-5,-6,-7,-viii,-9,0,1]
Clojure [edit]
( def due north xiii )
( sort-by str ( range 1 ( inc n) ) )
(1 x xi 12 thirteen two three 4 5 6 7 8 9)
COBOL [edit]
identification division .
program-id . LexicographicalNumbers.information partition .
working-storage section .
78 MAX-NUMBERS value 21 .
77 i picture show ix ( two ) .
77 edited-number motion-picture show z( two ) .01 lex-table .
05 table-itms occurs MAX-NUMBERS.
10 number-lex pic x( 2 ) .procedure division .
principal .
*>-> Load numbers
perform varying i from ane 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"
finish-perform*>-> Sort in lexicographic lodge
sort tabular array-itms ascending number-lex*>-> Show ordered numbers
display "[" no advancing
perform varying i from 1 by ane until i > MAX-NUMBERS
display function trim(number-lex(i) ) no advancing
if i < MAX-NUMBERS
display ", " no advancing
end-if
cease-perform
display "]"
stop run
.
[i, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 2, xx, 21, 3, 4, 5, 6, 7, 8, 9]
Common Lisp [edit]
( defun lexicographic-sort (n)
(sort (alexandria: iota n : start ane ) #'string<= : fundamental #'write-to-string) )
(lexicographic-sort 13 )
(1 10 11 12 13 ii 3 four 5 6 7 8 9)
Gene [edit]
USING: formatting kernel math.parser math.ranges sequences
sorting ;
IN: rosetta-code.lexicographical-numbers: lex-order ( n -- seq )
[i,b] [ number>cord ] map natural-sort
[ string>number ] map ;{ 13 21 -22 } [ dup lex-club "%3d: %[%d, %]\northward" printf ] each
xiii: { 1, 10, xi, 12, thirteen, 2, 3, 4, five, 6, seven, 8, nine } 21: { 1, x, eleven, 12, 13, 14, 15, sixteen, 17, xviii, nineteen, 2, 20, 21, 3, 4, 5, 6, 7, viii, 9 } -22: { -1, -10, -eleven, -12, -13, -14, -15, -16, -17, -18, -nineteen, -two, -20, -21, -22, -iii, -four, -5, -6, -7, -8, -9, 0, one } Fōrmulæ [edit]
Fōrmulæ programs are non textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there tin can be multiple visual representations of the same plan. Fifty-fifty though it is possible to take textual representation —i.east. XML, JSON— they are intended for storage and transfer purposes more visualization and edition.
Programs in Fōrmulæ are created/edited online in its website, All the same they run on execution servers. Past default remote servers are used, but they are limited in retention and processing ability, since they are intended for sit-in and casual use. A local server can be downloaded and installed, it has no limitations (information technology runs in your own reckoner). Because of that, example programs can be fully visualized and edited, just some of them volition non run if they require a moderate or heavy computation/retentiveness resources, and no local server is being used.
In this folio you can meet the program(s) related to this task and their results.
FreeBASIC [edit]
function leq( n as integer, m as integer ) equally boolean
if str (north)<=str (yard) then return true else return false
end functionsub shellsort(s( ) as integer )
dim every bit integer n = ubound (s)
dim as integer i, inc = north
dim as boolean donepractise
inc\=2.two
if inc = 0 then inc = 1
do
done = false
for i = 0 to n - inc
if leq(s(i+inc), s(i) ) so
bandy s(i), s(i + inc)
done = true
stop if
side by side
loop until done = 0
loop until inc = i
end subdim as integer north, i
input northward
dim as integer s( 0 to due north-1 )
for i = 0 to north-1
southward(i) = i+1
next ishellsort(s( ) )
print "[";
for i = 0 to n-one
print s(i);
if i<due north-1 and then print ", ";
next i
print "]"
? thirteen [ 1, 10, 11, 12, xiii, ii, 3, four, five, six, 7, 8, nine]
Go [edit]
package mainimport (
"fmt"
"sort"
"strconv"
)func lexOrder(northward int ) [] int {
start, last, k := 1 , n, north
if n < 1 {
commencement, last, k = north, ane , 2 -n
}
strs := make ([] string , k)
for i := offset; i <= last; i ++ {
strs[ i -offset] = strconv.Itoa( i )
}
sort.Strings(strs)
ints := make ([] int , k)
for i := 0 ; i < g; i ++ {
ints[ i ], _ = strconv.Atoi(strs[ i ])
}
return ints
}func principal() {
fmt.Println( "In lexicographical order:\n" )
for _, n := range [] int { 0 , v , 13 , 21 , - 22 } {
fmt.Printf( "%3d: %v\due north" , northward, lexOrder(n))
}
}
In lexicographical social club: 0: [0 1] 5: [1 ii 3 iv 5] 13: [ane 10 11 12 13 two 3 iv 5 6 7 eight 9] 21: [1 ten 11 12 13 fourteen xv xvi 17 eighteen xix 2 twenty 21 3 4 5 6 7 8 ix] -22: [-one -ten -xi -12 -13 -14 -xv -16 -17 -18 -nineteen -2 -20 -21 -22 -3 -iv -5 -six -vii -8 -nine 0 one]
Haskell [edit]
import Data.Listing (sort)task :: ( Ord b, Show b) => [b] -> [b]
chore = map snd . sort . map (\i -> ( show i, i) )chief = print $ task [ one .. 13 ]
Which nosotros could also write, in a bespeak-free style every bit:
import Data.Listing (sort)chore
:: ( Ord b, Bear witness b)
=> [b] -> [b]
job = map snd . sort . map ( show >>= ( , ) )main = print $ job [ 1 .. 13 ]
and the simplest arroyo might be sortOn testify (which only evaluates show once for each item).
import Information.List (sortOn)main :: IO ( )
main = print $ sortOn bear witness [ 1 .. 13 ]
[1,10,11,12,13,2,3,4,v,6,7,eight,9]
Isabelle [edit]
theory LexList
imports
Chief
"~~/src/HOL/Library/Char_ord"
"~~/src/HOL/Library/List_Lexorder"
begindefinition ord_ascii_zero :: nat where
"ord_ascii_zero == of_char (CHR ''0'')"text‹Get the cord representation for a single digit.›
definition ascii_of_digit :: "nat ⇒ string" where
"ascii_of_digit n ≡ if north ≥ 10 then undefined else [char_of (n + ord_ascii_zero)]"fun ascii_of :: "nat ⇒ string" where
"ascii_of due north = (if n < x
then ascii_of_digit due north
else ascii_of (n div 10) @ ascii_of_digit (n mod ten))"lemma ‹ascii_of 123 = ''123''› by code_simp
value ‹sort (map ascii_of (upt 1 13))›
finish
"[''1'', ''10'', ''11'', ''12'', ''ii'', ''three'', ''4'', ''5'', ''6'', ''vii'', ''8'', ''9'']" :: "char list list"
J [edit]
task=: [: (/: ":"0 ) one + i.
chore 13
1 10 eleven 12 13 2 iii iv 5 half-dozen 7 8 9
Java [edit]
Requires Coffee 8 or later on.
import coffee.util.List ;
import java.util.stream.* ;public class LexicographicalNumbers {
static List<Integer> lexOrder( int due north) {
int first = 1, final = n;
if (due north < 1 ) {
starting time = northward;
last = 1 ;
}
return IntStream.rangeClosed (first, last)
.mapToObj ( Integer ::toString)
.sorted ( )
.map ( Integer ::valueOf)
.collect (Collectors.toList ( ) ) ;
}public static void chief( String [ ] args) {
Organisation.out.println ( "In lexicographical order:\n" ) ;
int [ ] ints = { 0, v, 13, 21, - 22 } ;
for ( int north : ints) {
Organization.out.printf ( "%3d: %south\n", northward, lexOrder(n) ) ;
}
}
}
In lexicographical order: 0: [0, ane] 5: [1, 2, iii, 4, 5] 13: [1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, viii, ix] 21: [1, 10, 11, 12, xiii, 14, xv, 16, 17, 18, nineteen, 2, twenty, 21, iii, 4, v, 6, vii, 8, 9] -22: [-1, -x, -11, -12, -13, -14, -15, -16, -17, -18, -19, -two, -20, -21, -22, -3, -4, -5, -6, -7, -eight, -9, 0, 1]
Ksh [edit]
#!/bin/ksh# Sort numbers lexicographically
# # Variables:
#
integer Northward=${ane:-13}# # Functions:
## # Office _fillarray(arr, N) - fill up assoc. array ane -> Northward
#
function _fillarray {
typeset _arr ; nameref _arr="$1"
typeset _N ; integer _N=$2
typeset _i _st _en ; integer _i _st _en(( ! _N )) && _arr=0 && return
(( _N<0 )) && _st=${_N} && _en=1
(( _N>0 )) && _st=one && _en=${_N}for ((_i=_st; _i<=_en; _i++)); exercise
_arr[${_i}]=${_i}
done
}######
# main #
######set -a -due south -A arr
typeset -A arr
_fillarray arr ${N}print -- ${arr[*]}
one 10 11 12 thirteen 2 3 4 5 vi 7 eight 9
jq [edit]
Works with gojq, the Become implementation of jq
def sort_range($a;$b): [range($a;$b)] | sort_by(tostring);# Case
# jq's alphabetize origin is 0, so ...
sort_range(ane;14)
[1,10,11,12,13,2,3,iv,5,6,7,eight,ix]
Julia [edit]
lexorderedsequence(n) = sort(collect(n > 0 ? (ane:n) : n:1), lt=(a,b) -> string(a) < string(b))for i in [0, 5, 13, 21, -32]
println(lexorderedsequence(i))
terminate
[0, 1] [1, 2, iii, 4, 5] [ane, 10, eleven, 12, 13, 2, 3, 4, v, 6, 7, 8, ix] [1, 10, 11, 12, 13, fourteen, fifteen, 16, 17, 18, 19, 2, 20, 21, 3, four, 5, 6, 7, viii, 9] [-one, -10, -xi, -12, -thirteen, -14, -15, -sixteen, -17, -18, -xix, -two, -20, -21, -22, -23, -24, -25, -26, -27, -28, -29, -three, -xxx, -31, -32, -4, -5, -6, -7, -8, -9, 0, 1]
Kotlin [edit]
// Version 1.2.51fun lexOrder(n: Int) : List<Int> {
var outset = 1
var last = n
if (n < i ) {
get-go = n
final = 1
}
return (first..concluding ).map { it.toString ( ) }.sorted ( ).map { it.toInt ( ) }
}fun main(args: Array<Cord> ) {
println( "In lexicographical guild:\n" )
for (due north in listOf( 0, five, xiii, 21, -22 ) ) {
println( "${" %3d".format(n)}: ${lexOrder(due north)}" )
}
}
In lexicographical order: 0: [0, 1] v: [i, two, 3, 4, five] 13: [one, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, eight, nine] 21: [1, ten, xi, 12, xiii, 14, xv, xvi, 17, 18, nineteen, 2, twenty, 21, 3, 4, five, six, 7, 8, nine] -22: [-1, -10, -11, -12, -13, -14, -15, -16, -17, -xviii, -19, -two, -twenty, -21, -22, -3, -4, -v, -half dozen, -7, -eight, -9, 0, one]
Lambdatalk [edit]
ane ) lexicographically sorting a sequence of numbers
{ Southward.sort before one 2 three 4 v 6 7 8 9 10 xi 12 13 }
-> 1 10 eleven 12 13 two three 4 5 half-dozen 7 eight 92 ) lexicographically sorting an array of numbers
{A.sort! earlier {A.new i 2 iii iv five six vii 8 9 10 eleven 12 xiii } }
-> [ 1,10,eleven,12,xiii,2,3,4,five,half dozen,seven,8,9 ]
Lua [edit]
Lua's in-built table.sort function will sort a table of strings into lexicographical order past default. This job therefore becomes trivial by converting each number to a string earlier calculation information technology to the table.
function lexNums (limit)
local numbers = { }
for i = i , limit exercise
table.insert (numbers, tostring (i) )
finish
table.sort (numbers)
return numbers
finishlocal numList = lexNums( 13 )
impress ( table.concat (numList, " " ) )
1 ten 11 12 xiii 2 3 4 5 6 7 8 9
M2000 Interpreter [edit]
Module Checkit {
Role lexicographical(N) {
const nl$=Chr$(13)+Chr$(10)
If N<>0 so {
if N=1 then =(1,) : Exit
Certificate A$
For yard=1 to North-1
A$=Str$(k,"")+{
}
Next 1000
A$=Str$(Due north,"")
Method A$, "SetBinaryCompare"
Sort A$
Flush
\\ convert strings to numbers in i argument
\\ in stack of values
Data Param(Replace$(nl$,",", a$))
\\ return stack every bit array
=Assortment([])
} else =(0,) ' empty assortment
}
Impress lexicographical(5) ' 1 2 3 4 v
Print lexicographical(xiii) ' 1 10 11 12 13 2 iii 4 5 half-dozen 7 viii nine
Print lexicographical(21) ' ane 10 eleven 12 13 14 xv 16 17 18 19 ii twenty 21 3 4 5 6 7 eight 9
Print lexicographical(-22) ' -1 -10 -eleven -12 -13 -14 -15 -16 -17 -xviii -nineteen -2 -xx -21 -22 -3 -4 -5 -vi -7 -8 -ix 0 one
}
Checkit
Module Checkit {
Part lexicographical$(Northward) {
const nl$=Chr$(13)+Chr$(ten)
If North<>0 then {
if N=ane then =(one,) : Exit
Certificate A$
For k=1 to N-1
A$=Str$(k,"")+{
}
Next chiliad
A$=Str$(North,"")
\\ by default id TextCompare, so 0 an 1 comes offset in -22
Method A$, "SetBinaryCompare"
Sort A$
Flush
="["+Replace$(nl$," ", a$)+"]"} else =("",) ' empty assortment
}
Print lexicographical$(v) ' [1 2 3 four v]
Print lexicographical$(13) ' [1 10 11 12 xiii two 3 4 5 6 7 8 9]
Print lexicographical$(21) '[one ten 11 12 13 xiv xv 16 17 18 19 2 20 21 3 iv five 6 7 8 9]
Print lexicographical$(-22) ' [-1 -10 -11 -12 -thirteen -xiv -xv -16 -17 -18 -19 -2 -twenty -21 -22 -three -4 -v -half-dozen -vii -viii -9 0 1]
}
Checkit
Mathematica/Wolfram Language [edit]
SortBy[Range[13],ToString]
{1, 10, 11, 12, 13, 2, iii, 4, v, 6, 7, eight, nine} Microsoft Modest Basic [edit]
In Small Basic there is no string comparison: "a">"b" the issue is "False", "b">"a" the result is also "Faux". It doesn't help at all.
' Lexicographical numbers - 25/07/2018
xx="000000000000000"
For northward=1 To iii
nn=Text.GetSubText(" 5 13 21",n*4-3,4)
ll=Text.GetLength(nn)
For i=one 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.Suspend(t[j],xx),one,ll)
tk=Text.GetSubText(Text.Suspend(t[grand],xx),one,ll)
If tj>tk Then
w=t[j]
t[j]=t[one thousand]
t[thou]=w
ok=0
EndIf
EndFor
If ok=i Then
Goto exitfor
EndIf
EndFor
exitfor:
10=""
For i=1 To nn
x=x+","+t[i]
EndFor
TextWindow.WriteLine(nn+":"+Text.GetSubTextToEnd(ten,2))
EndFor
5:1,two,iii,4,v 13:i,10,11,12,xiii,2,iii,four,5,6,7,8,ix 21:1,10,11,12,13,14,15,sixteen,17,18,nineteen,2,twenty,21,3,iv,5,6,7,eight,9
MUMPS [edit]
This shows a few unique features of MUMPS:
- There is only ane datatype which is implicitly coerced to string, integer, or floating-point as necessary.
- MUMPS arrays sort automatically
- The condensed version shows that there are no reserved keywords
SortLexographically(northward)
new array,i,j
for i=1:i:n set assortment(i_" ")=""
for gear up j=$order(assortment(j)) quit:j="" write j
quit
This could also be written:
SortLexographically(n) n a,i,j f i=ane:i:n southward a(i_" ")=""
f s j=$o(a(j)) q:j="" westward j
q
- Usage
do SortLexographically(thirteen)
1 10 11 12 xiii two 3 four 5 half-dozen 7 8 9
Nim [edit]
import algorithm, sequtilsfor north in [0, 5, 13, 21, -22]:
let s = if northward > 1: toSeq(1..north) else: toSeq(countdown(1, n))
echo s.sortedByIt($it)
@[0, 1] @[ane, 2, 3, iv, 5] @[1, 10, 11, 12, 13, 2, 3, 4, 5, vi, 7, 8, 9] @[i, 10, eleven, 12, 13, 14, 15, sixteen, 17, 18, 19, ii, 20, 21, 3, iv, 5, six, 7, eight, 9] @[-1, -10, -11, -12, -xiii, -fourteen, -xv, -16, -17, -18, -19, -ii, -20, -21, -22, -3, -4, -5, -6, -7, -8, -9, 0, 1]
Perl [edit]
printf ( "%4d: [%south]\n" , $_ , join ',' , sort $_ > 0 ? 1 .. $_ : $_ .. 1 ) for 13 , 21 , - 22
13: [ane,10,xi,12,13,2,iii,four,5,half-dozen,7,8,9] 21: [1,10,11,12,13,xiv,15,16,17,eighteen,19,ii,20,21,3,4,5,six,vii,eight,9] -22: [-1,-10,-eleven,-12,-13,-xiv,-15,-xvi,-17,-eighteen,-19,-two,-20,-21,-22,-three,-4,-5,-6,-vii,-8,-9,0,1]
Phix [edit]
Idiomatic version - crashes if n<1, and calls dart() 76 times.
function lexographic(integer i, j)
return compare(sprint(i),dart(j))
end rolefunction lex_order(integer n)
return custom_sort(routine_id("lexographic"), tagset(n))
end part?lex_order(13)
{1,x,11,12,13,2,3,4,five,6,7,8,nine} Culling version, handles n<1, and for north=13 (say) information technology calls sprint() merely 13 times instead of 76.
function lex_order(integer north)
integer {lo,hi} = iff(n<1?{due north-1,1}:{0,n}), l = hi-lo
sequence south = echo(0,50)
for i=1 to l do due south[i] = {sprint(lo+i),lo+i} cease for
s = sort(southward)
for i=ane to fifty exercise south[i] = south[i][two] end for
return s
end function?lex_order(13)
?lex_order(0)
?lex_order(-22)
{1,10,eleven,12,13,2,iii,4,5,half-dozen,seven,8,9} {0,ane} {-1,-10,-11,-12,-13,-14,-15,-16,-17,-18,-xix,-ii,-20,-21,-22,-3,-iv,-v,-half dozen,-7,-8,-nine,0,1} PicoLisp [edit]
(println
(by
format
sort
(range 1 xiii) ) )
(1 10 11 12 thirteen two 3 4 five 6 vii 8 9)
Prolog [edit]
lexicographical_sort(Numbers, Sorted_numbers) :-
number_strings(Numbers, Strings) ,
sort(Strings, Sorted_strings) ,
number_strings(Sorted_numbers, Sorted_strings) .number_strings( [ ] , [ ] ) :- ! .
number_strings( [ Number |Numbers] , [String|Strings] ) :-
number_string( Number , Cord) ,
number_strings(Numbers, Strings) .number_list(From, To, [ ] ) :-
From > To,
! .
number_list(From, To, [From|Rest] ) :-
Adjacent is From + 1 ,
number_list(Side by side, To, Remainder) .lex_sorted_number_list( Number , List) :-
( Number < ane - >
number_list( Number , 1 , Numbers)
;
number_list( ane , Number , Numbers)
) ,
lexicographical_sort(Numbers, Listing) .test( Number ) :-
lex_sorted_number_list( Number , List) ,
writef( '%w: %due west\due north' , [ Number , List] ) .main:-
examination( 0 ) ,
test( five ) ,
exam( 13 ) ,
test( 21 ) ,
test( - 22 ) .
0: [0,1] 5: [ane,2,three,4,5] 13: [1,10,11,12,xiii,2,iii,4,5,6,7,eight,ix] 21: [ane,x,11,12,13,14,fifteen,xvi,17,18,19,2,xx,21,3,four,5,vi,7,8,9] -22: [-1,-10,-eleven,-12,-13,-xiv,-15,-16,-17,-eighteen,-nineteen,-2,-20,-21,-22,-3,-iv,-5,-6,-7,-8,-9,0,1]
PureBasic [edit]
EnableExplicitProcedure lexOrder(due north, Array ints( 1 ) )
Define kickoff = 1, last = northward, one thousand = n, i
If north < 1
first = n
last = 1
g = 2 - due north
EndIf
Dim strs.s(k - 1 )
For i = starting time To concluding
strs(i - first) = Str (i)
Next
SortArray (strs( ), #PB_Sort_Ascending)
For i = 0 To k - 1
ints(i) = Val (Strs(i) )
Next
EndProcedureIf OpenConsole ( )
PrintN ( ~ "In lexicographical social club:\n" )
Ascertain i, j, north, g
For i = 0 To 4
Read n
k = due north
If n < 1
k = 2 - northward
EndIf
Dim ints(thousand - 1 )
lexOrder(n, ints( ) )
Define.s ns = RSet ( Str (n), 3 )
Print (ns + ": [" )
For j = 0 To yard - 1
Print ( Str (ints(j) ) + " " )
Next j
PrintN ( ~ "\b]" )
Side by side i
Input ( )
EndDataSection
Data.i 0, 5, 13, 21, - 22
EndDataSection
EndIf
In lexicographical club: 0: [0 1] 5: [i 2 three four 5] xiii: [1 10 eleven 12 13 ii 3 4 5 6 7 viii ix] 21: [1 10 xi 12 13 14 15 xvi 17 18 19 ii 20 21 iii 4 5 vi vii 8 9] -22: [-ane -10 -eleven -12 -13 -fourteen -15 -xvi -17 -eighteen -19 -ii -xx -21 -22 -three -4 -5 -6 -vii -8 -9 0 1]
Python [edit]
n= 13
print ( sorted ( range ( 1 ,n+one ) , fundamental= str ) )
[i, 10, 11, 12, 13, 2, three, four, 5, six, 7, eight, 9]
Quackery [edit]
[ [] bandy times
[ i^ one+ number$
nested join ]
sort$
[] swap
witheach
[ $->n drib bring together ] ] is chore ( north --> [ )13 task echo
[ 1 10 11 12 13 two 3 4 v 6 7 8 nine ]
Racket [edit]
#lang dissonance(define (lex-sort n) (sort (if (< 0 due north) (range 1 (add1 northward)) (range n 2))
string<? #:key number->cord))(define (evidence n) (printf "~a: ~a\n" n (lex-sort n)))
(bear witness 0)
(prove 1)
(bear witness 5)
(evidence 13)
(testify 21)
(show -22)
0: (0 1) 1: (one) v: (ane ii 3 four five) 13: (1 ten 11 12 13 2 iii iv 5 6 7 8 ix) 21: (i ten xi 12 thirteen 14 15 16 17 18 19 2 20 21 3 4 five 6 seven 8 9) -22: (-ane -10 -11 -12 -13 -fourteen -15 -16 -17 -18 -19 -2 -20 -21 -22 -three -4 -5 -6 -7 -eight -nine 0 1)
Raku [edit]
(formerly Perl half dozen)
It is somewhat odd that the job name is sort numbers lexicographically just immediately backtracks in the task header to sorting integers lexicographically. Why only integers? This volition sort ANY real numbers lexicographically. For a non-integer, assumes that the given number is a difficult purlieus and 1 is a "soft" boundary. E.One thousand. The given number is definitely included; 1 is merely a threshold, information technology is included if it matches exactly. (Could be the other style around, this it the way I choose.)
sub lex (Real $n , $step = 1 ) {
( $n < 1 ?? ( $n , * + $footstep …^ * > 1 )
!! ( $n , * - $step …^ * < i ) ) . sort : ~*
}# TESTING
for 13 , 21 , - 22 , ( vi , .333) , ( - four , .25) , ( - v *π, due east) {
my ( $bound , $step ) = | $_ , 1 ;
say "Boundary:$jump, Step:$step >> " , lex( $bound , $footstep ) . bring together : ', ' ;
}
Boundary:13, Step:1 >> 1, 10, 11, 12, 13, 2, 3, 4, 5, six, 7, 8, nine Boundary:21, Step:ane >> i, 10, 11, 12, 13, 14, 15, xvi, 17, 18, 19, 2, 20, 21, 3, 4, 5, half-dozen, 7, 8, 9 Boundary:-22, Footstep:1 >> -i, -10, -11, -12, -13, -xiv, -15, -sixteen, -17, -eighteen, -19, -2, -xx, -21, -22, -3, -4, -5, -half-dozen, -7, -8, -9, 0, one Purlieus:six, Step:0.333 >> one.005, i.338, 1.671, 2.004, 2.337, 2.67, 3.003, three.336, 3.669, 4.002, 4.335, 4.668, 5.001, five.334, v.667, half dozen Boundary:-iv, Step:0.25 >> -0.25, -0.5, -0.75, -1, -1.25, -one.5, -1.75, -two, -two.25, -2.5, -ii.75, -3, -3.25, -three.5, -3.75, -4, 0, 0.25, 0.v, 0.75, i Boundary:-15.707963267948966, Stride:2.718281828459045 >> -10.271399611030876, -12.989681439489921, -15.707963267948966, -2.116554125653742, -4.834835954112787, -7.553117782571832, 0.6017277028053032
REXX [edit]
This REXX version allows the starting and catastrophe numbers to be specified via the command line (CL),
likewise as the increase. Negative numbers are supported and need not be integers.
/*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 apply the default.*/
if HI=='' | Howdy=="," so Hullo= xiii /* " " " " " " */
if INC=='' | INC=="," so INC= i /* " " " " " " */
#= 0 /*for actual sort, starting time array with 1.*/
exercise j=LO to HI by INC /*construct an array from LO to HI.*/
#= # + 1; @.#= j / ane /*bump counter; define array element. */
end /*j*/ /* [↑] As well, normalize the element #. */
call Lsort # /*sort numeric array with a simple sort*/
$= /*initialize a horizontal numeric list.*/
exercise k=one for #; $= $','@.m /*construct " " " */
terminate /*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: process expose @.; parse arg n; m= northward-one /*N: is the number of @ array elements.*/
do m=thou by -ane until ok; ok= 1 /*keep sorting the @ array until done.*/
do j=i for m; thou= j+one; if @.j>>@.g then parse value @.j @.grand 0 with @.k @.j ok
end /*j*/ /* [↑] swap 2 elements, flag as ¬done.*/
end /*m*/; return
- output when using the default inputs:
for 1──►thirteen by 1 (inclusive), xiii elements sorted lexicographically: [1,10,11,12,13,two,iii,4,five,vi,7,eight,9]
- output when using the input of: i 34
for 1──►34 by one (inclusive), 34 elements sorted lexicographically: [1,ten,11,12,xiii,xiv,15,16,17,18,19,two,xx,21,22,23,24,25,26,27,28,29,iii,30,31,32,33,34,iv,five,6,7,8,ix]
- output when using the input of: -11 22
for -11──►22 by 1 (inclusive), 34 elements sorted lexicographically: [-1,-x,-11,-2,-iii,-iv,-5,-6,-7,-8,-nine,0,1,ten,11,12,xiii,14,fifteen,xvi,17,xviii,19,two,20,21,22,iii,4,v,6,vii,8,9]
- output when using the input of: -4 viii 0.five
for -four──►8 by 0.5 (inclusive), 25 elements sorted lexicographically: [-0.5,-1,-1.5,-two,-2.v,-iii,-iii.v,-4,0,0.5,i,1.5,ii,ii.5,iii,three.5,four,iv.5,5,five.5,6,6.5,7,7.5,8]
Ring [edit]
# Project : Lexicographical numberslex = 1:13
strlex = list(len(lex))
for n = one to len(lex)
strlex[northward] = string(lex[northward])
side by side
strlex = sort(strlex)
see "Lexicographical numbers = "
showarray(strlex)func showarray(vect)
meet "["
svect = ""
for n = 1 to len(vect)
svect = svect + vect[n] + ","
side by side
svect = left(svect, len(svect) - one)
encounter svect + "]" + nl
Output:
Lexicographical numbers = [1,10,11,12,xiii,2,3,4,5,6,7,8,ix]
Ruby [edit]
n = 13
p ( i..north ).sort_by ( &:to_s)
[i, 10, xi, 12, 13, 2, three, four, 5, six, 7, 8, ix]
Rust [edit]
fn lex_sorted_vector(num: i32) -> Vec<i32> {
allow (min, max) = if num >= i { (ane, num) } else { (num, 1) };
let mut str: Vec<Cord> = (min..=max).map(|i| i.to_string()).collect();
str.sort();
str.iter().map(|s| southward.parse::<i32>().unwrap()).collect()
} fn principal() {
for northward in &[0, v, xiii, 21, -22] {
println!("{}: {:?}", due north, lex_sorted_vector(*n));
}
}
0: [0, ane] 5: [1, two, iii, 4, v] 13: [one, ten, xi, 12, 13, 2, three, iv, 5, 6, 7, eight, 9] 21: [1, 10, 11, 12, 13, 14, 15, xvi, 17, 18, 19, two, xx, 21, 3, four, 5, 6, 7, eight, nine] -22: [-1, -10, -11, -12, -13, -14, -15, -16, -17, -18, -19, -2, -20, -21, -22, -3, -four, -v, -6, -7, -8, -9, 0, 1]
Scala [edit]
Best seen in running your browser either by ScalaFiddle (ES aka JavaScript, non JVM) or Scastie (remote JVM).
object LexicographicalNumbers extends App { def ints = List( 0, 5, thirteen, 21, -22 )def lexOrder(n: Int) : Seq[Int] = ( if (northward < 1 ) n to ane else 1 to n).sortBy ( _.toString )
println( "In lexicographical society:\north" )
for (due north <- ints) println(f"$due north%3d: ${lexOrder(n).mkString(" [ ",", ", " ] ")}%s" )}
Sidef [edit]
func lex_order (n) {
[range( 1, n, northward.sgn )...].sort_by { Str(_) }
}[ 13, 21, - 22 ].each { |n|
printf ( "%4s: %s\n", n, lex_order(north) )
}
13: [one, ten, 11, 12, 13, 2, 3, 4, 5, 6, 7, viii, ix] 21: [ane, 10, 11, 12, 13, 14, fifteen, 16, 17, eighteen, 19, 2, twenty, 21, 3, 4, five, 6, 7, 8, 9] -22: [-1, -x, -11, -12, -xiii, -14, -xv, -16, -17, -eighteen, -19, -2, -20, -21, -22, -3, -iv, -5, -half-dozen, -7, -8, -9, 0, one]
Swift [edit]
func lex(n: Int) -> [Int] {
return stride(from: i, through: n, by: n.signum()).map({ String($0) }).sorted().compactMap(Int.init)
} impress("13: \(lex(n: xiii))")
print("21: \(lex(n: 21))")
impress("-22: \(lex(n: -22))")
13: [one, 10, 11, 12, 13, 2, 3, 4, 5, half dozen, 7, 8, 9] 21: [i, 10, 11, 12, 13, 14, fifteen, 16, 17, 18, 19, 2, xx, 21, 3, 4, 5, six, 7, viii, 9] -22: [-1, -10, -11, -12, -thirteen, -14, -15, -16, -17, -eighteen, -19, -2, -20, -21, -22, -3, -four, -5, -6, -7, -eight, -9, 0, 1]
Tcl [edit]
proc iota {num {commencement 0 } {step ane } } {
set up res { }
set finish [+ $start [ * $stride $num ] ]
for { set n $commencement } { $n != $cease } { incr n $step } {
lappend res $n
}
return $res
}puts [ lsort [iota 13 one ] ]
1 10 xi 12 thirteen 2 three 4 5 6 7 8 9
VBA [edit]
Public Function sortlexicographically(N Equally Integer)
Dim arrList As Object
Set arrList = CreateObject("Organization.Collections.ArrayList")
For i = 1 To N
arrList.Add CStr(i)
Side by side i
arrList.Sort
Dim item As Variant
For Each particular In arrList
Debug.Impress detail & ", ";
Adjacent
Cease OfficePublic Sub principal()
Call sortlexicographically(13)
End Sub
1, 10, 11, 12, 13, 2, 3, 4, 5, six, 7, viii, 9,
Wren [edit]
import "/sort" for Sortvar a = ( 1 .. 13 ) . map { |i| "%(i)" } . toList
Sort. quick (a)
System. print (a)
[1, 10, 11, 12, 13, two, three, four, 5, 6, 7, eight, ix]
zkl [edit]
fcn lexN(northward){ n.pump(List,'+(1),"toString").sort().apply("toInt") } foreach n in (T(five,13,21)){ println("%2d: %due south".fmt(n,lexN(northward).concat(","))) } five: 1,two,3,4,v 13: 1,ten,11,12,xiii,2,3,4,5,six,7,8,9 21: 1,x,11,12,13,xiv,15,16,17,xviii,19,2,20,21,iii,4,5,half dozen,vii,8,9
Source: http://www.rosettacode.org/wiki/Sort_numbers_lexicographically
0 Response to "Write a Program That Reads in Three Strings and Sorts Them Lexicographically Python Site"
Enviar um comentário