June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
29
Task/Sort-disjoint-sublist/Elena/sort-disjoint-sublist.elena
Normal file
29
Task/Sort-disjoint-sublist/Elena/sort-disjoint-sublist.elena
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
import extensions.
|
||||
import system'routines.
|
||||
import system'culture.
|
||||
|
||||
extension $op
|
||||
{
|
||||
sortSublist : indices
|
||||
[
|
||||
var subList := indices orderBy(:x)(x);
|
||||
zip(indices selectBy(:i)(self[i]);
|
||||
orderBy(:x)(x)) by(:index:val)( { Index = index. Value = val. } );
|
||||
toArray.
|
||||
|
||||
var list := self clone.
|
||||
subList forEach(:r)
|
||||
[
|
||||
list[r Index] := r Value
|
||||
].
|
||||
|
||||
^ list
|
||||
]
|
||||
}
|
||||
|
||||
program =
|
||||
[
|
||||
var list := ( 7, 6, 5, 4, 3, 2, 1, 0 ).
|
||||
|
||||
console printLine(list sortSublist:(6, 1, 7)).
|
||||
].
|
||||
|
|
@ -1,8 +1,7 @@
|
|||
function sortselected{S<:Real,T<:Integer}(a::AbstractArray{S,1},
|
||||
s::AbstractArray{T,1})
|
||||
function sortselected(a::AbstractVector{<:Real}, s::AbstractVector{<:Integer})
|
||||
sel = unique(sort(s))
|
||||
if sel[1] < 1 || length(a) < sel[end]
|
||||
throw(ArgumentError("Tried to select outside of input array."))
|
||||
throw(BoundsError())
|
||||
end
|
||||
b = collect(copy(a))
|
||||
b[sel] = sort(b[sel])
|
||||
|
|
@ -13,6 +12,4 @@ a = [7, 6, 5, 4, 3, 2, 1, 0]
|
|||
sel = [7, 2, 8]
|
||||
b = sortselected(a, sel)
|
||||
|
||||
print("Original Array: ", a)
|
||||
println(" sorted on ", sel)
|
||||
println("Sorted Array: ", b)
|
||||
println("Original: $a\n\tsorted on $sel\n -> sorted array: $b")
|
||||
|
|
|
|||
6
Task/Sort-disjoint-sublist/K/sort-disjoint-sublist-2.k
Normal file
6
Task/Sort-disjoint-sublist/K/sort-disjoint-sublist-2.k
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
sort : {x[<x]}
|
||||
nums : 7 6 5 4 3 2 1 0
|
||||
i : sort 6 1 7
|
||||
nums[i] : sort nums[i]
|
||||
nums
|
||||
7 0 5 4 3 2 1 6
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
// version 1.1.51
|
||||
|
||||
/* in place sort */
|
||||
fun IntArray.sortDisjoint(indices: Set<Int>) {
|
||||
val sortedSubset = this.filterIndexed { index, _ -> index in indices }.sorted()
|
||||
if (sortedSubset.size < indices.size)
|
||||
throw IllegalArgumentException("Argument set contains out of range indices")
|
||||
indices.sorted().forEachIndexed { index, value -> this[value] = sortedSubset[index] }
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val values = intArrayOf(7, 6, 5, 4, 3, 2, 1, 0)
|
||||
val indices = setOf(6, 1, 7)
|
||||
println("Original array : ${values.asList()} sorted on indices $indices")
|
||||
values.sortDisjoint(indices)
|
||||
println("Sorted array : ${values.asList()}")
|
||||
}
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
values := [7, 6, 5, 4, 3, 2, 1, 0]
|
||||
indices := sortup [6, 1, 7]
|
||||
values#indices := sortup values#indices
|
||||
7 0 5 4 3 2 1 6
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
sub disjointSort( @values is rw , @indices is rw --> List ) {
|
||||
sub disjointSort( @values, @indices --> List ) {
|
||||
my @sortedValues = @values[ @indices ].sort ;
|
||||
for @indices.sort -> $insert {
|
||||
@values[ $insert ] = @sortedValues.shift ;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue