2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1 +1,5 @@
|
|||
Sort an array (or list) of integers in ascending numerical order. Use a sorting facility provided by the language/library if possible.
|
||||
Sort an array (or list) of integers in ascending numerical order.
|
||||
|
||||
;Task:
|
||||
Use a sorting facility provided by the language/library if possible.
|
||||
<br><br>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,34 @@
|
|||
use framework "Foundation"
|
||||
use scripting additions
|
||||
|
||||
on sort:lst
|
||||
tell current application
|
||||
return ((its (NSArray's arrayWithArray:lst))'s ¬
|
||||
sortedArrayUsingDescriptors:{its (NSSortDescriptor's ¬
|
||||
sortDescriptorWithKey:"self" ascending:true selector:"compare:")}) as list
|
||||
end tell
|
||||
end sort:
|
||||
|
||||
on run
|
||||
|
||||
map(sort_, [[9, 1, 8, 2, 8, 3, 7, 0, 4, 6, 5], ¬
|
||||
["alpha", "beta", "gamma", "delta", "epsilon", "zeta", "eta", "theta", "iota", "kappa", "lambda", "mu"]])
|
||||
|
||||
end run
|
||||
|
||||
|
||||
-- GENERIC FUNCTION FOR THE TEST
|
||||
|
||||
-- map :: (a -> b) -> [a] -> [b]
|
||||
on map(f, xs)
|
||||
script mf
|
||||
property lambda : f
|
||||
end script
|
||||
|
||||
set lng to length of xs
|
||||
set lst to {}
|
||||
repeat with i from 1 to lng
|
||||
set end of lst to mf's lambda(item i of xs, i, xs)
|
||||
end repeat
|
||||
return lst
|
||||
end map
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
babel> nil { zap {1 randlf 100 rem} 20 times collect ! } nest dup lsnum ! --> Create a list of random numbers
|
||||
( 20 47 69 71 18 10 92 9 56 68 71 92 45 92 12 7 59 55 54 24 )
|
||||
babel> ls2lf --> Convert list to array for sorting
|
||||
babel> dup {fnord} merge_sort --> The internal sort operator
|
||||
babel> ar2ls lsnum ! --> Display the results
|
||||
( 7 9 10 12 18 20 24 45 47 54 55 56 59 68 69 71 71 92 92 92 )
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
babel> ( 68 73 63 83 54 67 46 53 88 86 49 75 89 83 28 9 34 21 20 90 )
|
||||
babel> {lt?} lssort ! lsnum !
|
||||
( 9 20 21 28 34 46 49 53 54 63 67 68 73 75 83 83 86 88 89 90 )
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
babel> ( 68 73 63 83 54 67 46 53 88 86 49 75 89 83 28 9 34 21 20 90 ) {gt?} lssort ! lsnum !
|
||||
( 90 89 88 86 83 83 75 73 68 67 63 54 53 49 46 34 28 21 20 9 )
|
||||
12
Task/Sort-an-integer-array/Elena/sort-an-integer-array.elena
Normal file
12
Task/Sort-an-integer-array/Elena/sort-an-integer-array.elena
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
#import system.
|
||||
#import system'routines.
|
||||
#import extensions.
|
||||
|
||||
#symbol program =
|
||||
[
|
||||
#var unsorted := (6, 2, 7, 8, 3, 1, 10, 5, 4, 9).
|
||||
|
||||
unsorted sort:ifOrdered.
|
||||
|
||||
console writeLine:unsorted.
|
||||
].
|
||||
|
|
@ -1 +1 @@
|
|||
my @sorted = sort +*, @a;
|
||||
@a .= sort;
|
||||
|
|
|
|||
|
|
@ -1,37 +1,37 @@
|
|||
/*REXX program sorts (E-sort) an arra y (which contains integers). */
|
||||
numeric digits 30 /*handle larger Euler numbers. */
|
||||
@. = 0 /*default for all Euler numbers. */
|
||||
@.1= 1
|
||||
@.3= -1
|
||||
@.5= 5
|
||||
@.7= -61
|
||||
@.9= 1385
|
||||
@.11= -50521
|
||||
@.13= 2702765
|
||||
@.15= -199360981
|
||||
@.17= 19391512145
|
||||
@.19= -2404879675441
|
||||
@.21= 370371188237525
|
||||
size=21 /*indicate there are 21 Euler #'s*/
|
||||
call tell 'un-sorted' /*display the array before sort. */
|
||||
call esort size /*sort the array of Euler numbers*/
|
||||
call tell ' sorted' /*display the array after sort. */
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────ESORT subroutine────────────────────*/
|
||||
esort: procedure expose @.; parse arg N; h=N
|
||||
do while h>1; h=h%2
|
||||
do i=1 for N-h; j=i; k=h+i
|
||||
do while @.k<@.j
|
||||
parse value @.j @.k with @.k @.j /*swap two elements.*/
|
||||
if h>=j then leave; j=j-h; k=k-h
|
||||
end /*while @.k<@.j*/
|
||||
end /*i*/
|
||||
end /*while h>1*/
|
||||
return
|
||||
/*──────────────────────────────────TELL subroutine─────────────────────*/
|
||||
tell: say center(arg(1),50,'─')
|
||||
do j=1 for size
|
||||
say arg(1) 'array element' right(j,length(size))'='right(@.j,20)
|
||||
end /*j*/
|
||||
say
|
||||
return
|
||||
/*REXX program sorts an array (using E-sort), in this case, the array contains integers.*/
|
||||
numeric digits 30 /*enables handling larger Euler numbers*/
|
||||
@. = 0 /*the default for all Euler numbers. */
|
||||
@.1 = 1
|
||||
@.3 = -1
|
||||
@.5 = 5
|
||||
@.7 = -61
|
||||
@.9 = 1385
|
||||
@.11= -50521
|
||||
@.13= 2702765
|
||||
@.15= -199360981
|
||||
@.17= 19391512145
|
||||
@.19= -2404879675441
|
||||
@.21= 370371188237525
|
||||
size=21 /*indicate that there're 21 Euler #s.*/
|
||||
call tell 'un-sorted' /*display the array before the eSsort. */
|
||||
call eSort size /*sort the array of some Euler numbers.*/
|
||||
call tell ' sorted' /*display the array after the sort. */
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
eSort: procedure expose @.; parse arg N; h=N
|
||||
do while h>1; h=h%2
|
||||
do i=1 for N-h; j=i; k=h+i
|
||||
do while @.k<@.j
|
||||
parse value @.j @.k with @.k @.j /*swap two array elements.*/
|
||||
if h>=j then leave; j=j-h; k=k-h
|
||||
end /*while @.k<@.j*/
|
||||
end /*i*/
|
||||
end /*while h>1*/
|
||||
return
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
tell: say center(arg(1), 50, '─')
|
||||
do j=1 for size
|
||||
say arg(1) 'array element' right(j, length(size))'='right(@.j, 20)
|
||||
end /*j*/
|
||||
say
|
||||
return
|
||||
|
|
|
|||
|
|
@ -1,29 +1,28 @@
|
|||
/*REXX program sorts (using E-sort) a list of some interesting integers.*/
|
||||
/* [↓] quotes aren't needed if all elements in a list are non-negative.*/
|
||||
bell= 1 1 2 5 15 52 203 877 4140 21147 115975 /*some Bell numbers.*/
|
||||
bern= '1 -1 1 0 -1 0 1 0 -1 0 5 0 -691 0 7 0 -3617' /*some Bernoulli num*/
|
||||
perrin= 3 0 2 3 2 5 5 7 10 12 17 22 29 39 51 68 90 /*some Perrin nums. */
|
||||
list=bell bern perrin /*throw 'em───►a pot*/
|
||||
say 'unsorted =' list /*an announcement···*/
|
||||
size=words(list) /*nice to have SIZE.*/
|
||||
do j=1 for size /*build an array, 1 */
|
||||
@.j=word(list,j) /*element at a time.*/
|
||||
end /*j*/
|
||||
call esort size /*sort the stuff. */
|
||||
bList= /*list: null so far.*/
|
||||
do k=1 for size /*build a list. */
|
||||
bList=strip(bList @.k) /*append it to list.*/
|
||||
end /*k*/
|
||||
say ' sorted =' bList /*show & tell time. */
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────ESORT subroutine────────────────────*/
|
||||
esort: procedure expose @.; parse arg N; h=N /*get the item count*/
|
||||
do while h>1; h=h%2 /*partition array. */
|
||||
do i=1 for N-h; j=i; k=h+i
|
||||
do while @.k<@.j
|
||||
parse value @.j @.k with @.k @.j /*swap two elements.*/
|
||||
if h>=j then leave; j=j-h; k=k-h
|
||||
end /*while @.k<@.j*/
|
||||
end /*i*/
|
||||
end /*while h>1*/
|
||||
return
|
||||
/*REXX program sorts (using E─sort) and displays a list of some interesting integers. */
|
||||
Bell= 1 1 2 5 15 52 203 877 4140 21147 115975 /*a few Bell " */
|
||||
Bern= '1 -1 1 0 -1 0 1 0 -1 0 5 0 -691 0 7 0 -3617' /*" " Bernoulli " */
|
||||
Perrin= 3 0 2 3 2 5 5 7 10 12 17 22 29 39 51 68 90 /*" " Perrin " */
|
||||
list=Bell Bern Perrin /*throw them all ───► a pot. */
|
||||
say 'unsorted =' list /*display what's being shown.*/
|
||||
size=words(list) /*nice to have # of elements.*/
|
||||
do j=1 for size /*build an array, a single */
|
||||
@.j=word(list,j) /* ··· element at a time.*/
|
||||
end /*j*/
|
||||
call eSort size /*sort the collection of #s. */
|
||||
$= /*list: define as null so far*/
|
||||
do k=1 for size /*build a list from the array*/
|
||||
$=$ @.k /*append a number to the list*/
|
||||
end /*k*/
|
||||
say ' sorted =' space($) /*display the sorted list. */
|
||||
exit /*stick a fork in it, we're all done.*/
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
eSort: procedure expose @.; parse arg N; h=N /*get item count for array. */
|
||||
do while h>1; h=h%2 /*partition the array. */
|
||||
do i=1 for N-h; j=i; k=h+i
|
||||
do while @.k<@.j /*keep swapping while less. */
|
||||
parse value @.j @.k with @.k @.j /*swap two array elements. */
|
||||
if h>=j then leave; j=j-h; k=k-h
|
||||
end /*while @.k<@.j*/
|
||||
end /*i*/
|
||||
end /*while h>1*/
|
||||
return
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue