Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -0,0 +1,48 @@
PROGRAM DISJOINT
DIM LST%[7],INDICES%[2]
DIM L%[7],I%[2],Z%[2]
PROCEDURE SHOWLIST(L%[]->O$)
LOCAL I%
O$="["
FOR I%=0 TO UBOUND(L%,1) DO
O$=O$+STR$(L%[I%])+", "
END FOR
O$=LEFT$(O$,LEN(O$)-2)+"]"
END PROCEDURE
PROCEDURE SORT(Z%[]->Z%[])
LOCAL N%,P%,FLIPS%
P%=UBOUND(Z%,1)
FLIPS%=TRUE
WHILE FLIPS% DO
FLIPS%=FALSE
FOR N%=0 TO P%-1 DO
IF Z%[N%]>Z%[N%+1] THEN SWAP(Z%[N%],Z%[N%+1]) FLIPS%=TRUE
END FOR
END WHILE
END PROCEDURE
PROCEDURE SortDisJoint(L%[],I%[]->L%[])
LOCAL J%,N%
LOCAL DIM T%[2]
N%=UBOUND(I%,1)
FOR J%=0 TO N% DO
T%[J%]=L%[I%[J%]]
END FOR
SORT(I%[]->I%[])
SORT(T%[]->T%[])
FOR J%=0 TO N% DO
L%[I%[J%]]=T%[J%]
END FOR
END PROCEDURE
BEGIN
LST%[]=(7,6,5,4,3,2,1,0)
INDICES%[]=(6,1,7)
SortDisJoint(LST%[],INDICES%[]->LST%[])
ShowList(LST%[]->O$)
PRINT(O$)
END PROGRAM

View file

@ -0,0 +1,16 @@
(define (sort-disjoint values indices)
(define sorted (list-sort <
(for/list [(v values) (i (in-naturals))]
#:when (member i indices) v)))
(for/list [(v values) (i (in-naturals))]
(if (not (member i indices)) v
(begin0
(first sorted)
(set! sorted (rest sorted))))))
(define (task)
(sort-disjoint '[7 6 5 4 3 2 1 0] {6 1 7}))
(task)
→ (7 0 5 4 3 2 1 6)

View file

@ -0,0 +1,15 @@
import algorithm
proc sortDisjoinSublist[T](data: var seq[T], indices: seq[int]) =
var indices = indices
sort indices, cmp[T]
var values: seq[T] = @[]
for i in indices: values.add data[i]
sort values, cmp[T]
for j, i in indices: data[i] = values[j]
var d = @[7, 6, 5, 4, 3, 2, 1, 0]
sortDisjoinSublist(d, @[6, 1, 7])
echo d

View file

@ -0,0 +1,33 @@
function uniq(sequence s)
integer last=s[1], this, ndx = 1
for i=2 to length(s) do
this = s[i]
if this!=last then
ndx += 1
s[ndx] = this
last = this
end if
end for
return s[1..ndx]
end function
function disjoint_sort(sequence s, sequence idx)
sequence copies
if length(idx)>1 then
-- must ensure there are no duplicates,
-- otherwise eg {7,2,8,8} -> {2,7,8,8},
-- but {1,6,0,0} -> {0,0,1,6}
idx = uniq(sort(idx))
copies = repeat(0, length(idx))
for i=1 to length(idx) do
copies[i] = s[idx[i]]
end for
copies = sort(copies)
for i=1 to length(idx) do
s[idx[i]] = copies[i]
end for
end if
return s
end function
?disjoint_sort({7,6,5,4,3,2,1,0},{7,2,8})

View file

@ -0,0 +1,9 @@
func disjointSort(values, indices) {
values[indices.sort] = [values[indices]].sort...
}
var values = [7, 6, 5, 4, 3, 2, 1, 0];
var indices = [6, 1, 7];
disjointSort(values, indices);
say values;

View file

@ -0,0 +1,26 @@
struct DisjointSublistView<T> : MutableCollectionType {
let array : UnsafeMutablePointer<T>
let indexes : [Int]
subscript (position: Int) -> T {
get {
return array[indexes[position]]
}
set {
array[indexes[position]] = newValue
}
}
var startIndex : Int { return 0 }
var endIndex : Int { return indexes.count }
func generate() -> IndexingGenerator<DisjointSublistView<T>> { return IndexingGenerator(self) }
}
func sortDisjointSublist<T : Comparable>(inout array: [T], indexes: [Int]) {
var d = DisjointSublistView(array: &array, indexes: sorted(indexes))
sort(&d)
}
var a = [7, 6, 5, 4, 3, 2, 1, 0]
let ind = [6, 1, 7]
sortDisjointSublist(&a, ind)
println(a)

View file

@ -0,0 +1,9 @@
def setpaths(indices; values):
reduce range(0; indices|length) as $i
(.; .[indices[$i]] = values[$i]);
def disjointSort(indices):
(indices|unique) as $ix # "unique" sorts
# Set $sorted to the sorted array of values at $ix:
| ([ .[ $ix[] ] ] | sort) as $sorted
| setpaths( $ix; $sorted) ;

View file

@ -0,0 +1 @@
[7, 6, 5, 4, 3, 2, 1, 0] | disjointSort( [6, 1, 7] )