2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,3 +1,4 @@
|
|||
;Task:
|
||||
Define a function/method/subroutine which sorts a sequence ("table") of sequences ("rows") of strings ("cells"), by one of the strings. Besides the input to be sorted, it shall have the following optional parameters:
|
||||
:{|
|
||||
|
|
||||
|
|
@ -17,3 +18,4 @@ Do not implement a sorting algorithm; this task is about the interface. If you c
|
|||
|
||||
See also:
|
||||
* [[Named Arguments]]
|
||||
<br><br>
|
||||
|
|
|
|||
35
Task/Optional-parameters/ALGOL-68/optional-parameters.alg
Normal file
35
Task/Optional-parameters/ALGOL-68/optional-parameters.alg
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
# as the options have distinct types (INT, BOOL and PROC( STRING, STRING )INT) the #
|
||||
# easiest way to support these optional parameters in Algol 68 would be to have an array #
|
||||
# with elements of these types #
|
||||
# See the Named Arguments sample for cases where the option types are not distinct #
|
||||
|
||||
# default comparison function #
|
||||
PROC default compare = ( STRING a, b )INT: IF a < b THEN -1 ELIF a = b THEN 0 ELSE 1 FI;
|
||||
|
||||
# sorting procedure #
|
||||
PROC configurable sort = ( [,]STRING data, []UNION( INT, BOOL, PROC( STRING, STRING )INT ) options )VOID:
|
||||
BEGIN
|
||||
# set initial values for the options #
|
||||
INT sort column := 2 LWB data;
|
||||
BOOL reverse sort := FALSE;
|
||||
PROC( STRING, STRING )INT comparator := default compare;
|
||||
# overide from the supplied options #
|
||||
FOR opt pos FROM LWB options TO UPB options DO
|
||||
CASE options[ opt pos ]
|
||||
IN ( PROC( STRING, STRING )INT p ): comparator := p
|
||||
, ( INT c ): sort column := c
|
||||
, ( BOOL r ): reverse sort := r
|
||||
ESAC
|
||||
OD
|
||||
# do the sort .... #
|
||||
END # configurable sort # ;
|
||||
|
||||
# example calls #
|
||||
[ 1 : 2, 1 : 3 ]STRING data := ( ( "a", "bb", "cde" ), ( "x", "abcdef", "Q" ) );
|
||||
|
||||
# sort data, default comparison, first column, reverse order #
|
||||
configurable sort( data, ( TRUE ) );
|
||||
# sort data, second column, ignore first chaacter when sorting, normal order #
|
||||
configurable sort( data, ( 2, ( STRING a, STRING b )INT: default compare( a[ LWB a + 1 : ], b[ LWB b + 1 : ] ) ) );
|
||||
# default sort #
|
||||
configurable sort( data, () )
|
||||
31
Task/Optional-parameters/Elixir/optional-parameters.elixir
Normal file
31
Task/Optional-parameters/Elixir/optional-parameters.elixir
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
defmodule Optional_parameters do
|
||||
def sort( table, options\\[] ) do
|
||||
options = options ++ [ ordering: :lexicographic, column: 0, reverse: false ]
|
||||
ordering = options[ :ordering ]
|
||||
column = options[ :column ]
|
||||
reverse = options[ :reverse ]
|
||||
sorted = sort( table, ordering, column )
|
||||
if reverse, do: Enum.reverse( sorted ), else: sorted
|
||||
end
|
||||
|
||||
defp sort( table, :lexicographic, column ) do
|
||||
Enum.sort_by( table, &elem( &1, column ) )
|
||||
end
|
||||
defp sort( table, :numeric, column ) do
|
||||
Enum.sort_by( table, &elem( &1, column ) |> String.to_integer )
|
||||
end
|
||||
|
||||
def task do
|
||||
table = [ { "123", "456", "0789" },
|
||||
{ "456", "0789", "123" },
|
||||
{ "0789", "123", "456" } ]
|
||||
IO.write "sort defaults "; IO.inspect sort( table )
|
||||
IO.write " & reverse "; IO.inspect sort( table, reverse: true )
|
||||
IO.write "sort column 2 "; IO.inspect sort( table, column: 2)
|
||||
IO.write " & reverse "; IO.inspect sort( table, column: 2, reverse: true)
|
||||
IO.write "sort numeric "; IO.inspect sort( table, ordering: :numeric)
|
||||
IO.write " & reverse "; IO.inspect sort( table, ordering: :numeric, reverse: true)
|
||||
end
|
||||
end
|
||||
|
||||
Optional_parameters.task
|
||||
43
Task/Optional-parameters/Lua/optional-parameters.lua
Normal file
43
Task/Optional-parameters/Lua/optional-parameters.lua
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
function showTable(tbl)
|
||||
if type(tbl)=='table' then
|
||||
local result = {}
|
||||
for _, val in pairs(tbl) do
|
||||
table.insert(result, showTable(val))
|
||||
end
|
||||
return '{' .. table.concat(result, ', ') .. '}'
|
||||
else
|
||||
return (tostring(tbl))
|
||||
end
|
||||
end
|
||||
|
||||
function sortTable(op)
|
||||
local tbl = op.table or {}
|
||||
local column = op.column or 1
|
||||
local reverse = op.reverse or false
|
||||
local cmp = op.cmp or (function (a, b) return a < b end)
|
||||
local compareTables = function (a, b)
|
||||
local result = cmp(a[column], b[column])
|
||||
if reverse then return not result else return result end
|
||||
end
|
||||
table.sort(tbl, compareTables)
|
||||
end
|
||||
|
||||
A = {{"quail", "deer", "snake"},
|
||||
{"dalmation", "bear", "fox"},
|
||||
{"ant", "cougar", "coyote"}}
|
||||
print('original', showTable(A))
|
||||
|
||||
sortTable{table=A}
|
||||
print('defaults', showTable(A))
|
||||
|
||||
sortTable{table=A, column=2}
|
||||
print('col 2 ', showTable(A))
|
||||
|
||||
sortTable{table=A, column=3}
|
||||
print('col 3 ', showTable(A))
|
||||
|
||||
sortTable{table=A, column=3, reverse=true}
|
||||
print('col 3 rev', showTable(A))
|
||||
|
||||
sortTable{table=A, cmp=(function (a, b) return #a < #b end)}
|
||||
print('by length', showTable(A))
|
||||
Loading…
Add table
Add a link
Reference in a new issue