June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View 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)).
].

View file

@ -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")

View 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

View file

@ -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()}")
}

View file

@ -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

View file

@ -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 ;