September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -1,5 +1,7 @@
|
|||
use framework "Foundation" -- for basic NSArray sort
|
||||
|
||||
-- DISJOINT SORT -------------------------------------------------------------
|
||||
|
||||
-- disjointSort :: [a] -> [Int] -> [a]
|
||||
on disjointSort(xs, indices)
|
||||
|
||||
|
|
@ -8,9 +10,9 @@ on disjointSort(xs, indices)
|
|||
|
||||
-- valueByIndex :: Int -> a
|
||||
script valueByIndex
|
||||
on lambda(i)
|
||||
on |λ|(i)
|
||||
item i of xs
|
||||
end lambda
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
set subsetSorted to ¬
|
||||
|
|
@ -18,14 +20,14 @@ on disjointSort(xs, indices)
|
|||
|
||||
-- staticOrSorted :: a -> Int -> a
|
||||
script staticOrSorted
|
||||
on lambda(x, i)
|
||||
on |λ|(x, i)
|
||||
set iIndex to elemIndex(i, indicesSorted)
|
||||
if iIndex is missing value then
|
||||
x
|
||||
else
|
||||
item iIndex of subsetSorted
|
||||
end if
|
||||
end lambda
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
-- Sorted subset re-stitched into unsorted remainder of list
|
||||
|
|
@ -33,9 +35,7 @@ on disjointSort(xs, indices)
|
|||
|
||||
end disjointSort
|
||||
|
||||
|
||||
--TEST
|
||||
|
||||
-- TEST ----------------------------------------------------------------------
|
||||
on run
|
||||
-- The indexing of AppleScript lists is 1-based
|
||||
-- so we use {7,2,8} in place of {6,1,7}
|
||||
|
|
@ -43,15 +43,7 @@ on run
|
|||
disjointSort({7, 6, 5, 4, 3, 2, 1, 0}, {7, 2, 8})
|
||||
end run
|
||||
|
||||
|
||||
|
||||
-- GENERIC FUNCTIONS
|
||||
|
||||
-- sort :: [a] -> [a]
|
||||
on sort(lst)
|
||||
((current application's NSArray's arrayWithArray:lst)'s ¬
|
||||
sortedArrayUsingSelector:"compare:") as list
|
||||
end sort
|
||||
-- GENERIC FUNCTIONS ---------------------------------------------------------
|
||||
|
||||
-- elemIndex :: a -> [a] -> Maybe Int
|
||||
on elemIndex(x, xs)
|
||||
|
|
@ -68,7 +60,7 @@ on map(f, xs)
|
|||
set lng to length of xs
|
||||
set lst to {}
|
||||
repeat with i from 1 to lng
|
||||
set end of lst to lambda(item i of xs, i, xs)
|
||||
set end of lst to |λ|(item i of xs, i, xs)
|
||||
end repeat
|
||||
return lst
|
||||
end tell
|
||||
|
|
@ -81,7 +73,13 @@ on mReturn(f)
|
|||
f
|
||||
else
|
||||
script
|
||||
property lambda : f
|
||||
property |λ| : f
|
||||
end script
|
||||
end if
|
||||
end mReturn
|
||||
|
||||
-- sort :: [a] -> [a]
|
||||
on sort(lst)
|
||||
((current application's NSArray's arrayWithArray:lst)'s ¬
|
||||
sortedArrayUsingSelector:"compare:") as list
|
||||
end sort
|
||||
|
|
|
|||
29
Task/Sort-disjoint-sublist/C-sharp/sort-disjoint-sublist.cs
Normal file
29
Task/Sort-disjoint-sublist/C-sharp/sort-disjoint-sublist.cs
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class Test
|
||||
{
|
||||
public static void Main()
|
||||
{
|
||||
var list = new List<int>{ 7, 6, 5, 4, 3, 2, 1, 0 };
|
||||
list.SortSublist(6, 1, 7);
|
||||
Console.WriteLine(string.Join(", ", list));
|
||||
}
|
||||
}
|
||||
|
||||
public static class Extensions
|
||||
{
|
||||
public static void SortSublist<T>(this List<T> list, params int[] indices)
|
||||
where T : IComparable<T>
|
||||
{
|
||||
var sublist = indices.OrderBy(i => i)
|
||||
.Zip(indices.Select(i => list[i]).OrderBy(v => v),
|
||||
(Index, Value) => new { Index, Value });
|
||||
|
||||
foreach (var entry in sublist) {
|
||||
list[entry.Index] = entry.Value;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
import Data.List (sort, elemIndex)
|
||||
|
||||
disjointSort :: [Int] -> [Int] -> [Int]
|
||||
disjointSort xs indices =
|
||||
let indicesSorted = sort indices
|
||||
subsetSorted = sort $ (xs !!) <$> indicesSorted
|
||||
in (\(x, i) ->
|
||||
case elemIndex i indicesSorted of
|
||||
Nothing -> x
|
||||
Just iIndex -> subsetSorted !! iIndex) <$>
|
||||
zip xs [0 ..]
|
||||
|
||||
main :: IO ()
|
||||
main = print $ disjointSort [7, 6, 5, 4, 3, 2, 1, 0] [6, 1, 7]
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
(() => {
|
||||
'use strict';
|
||||
|
||||
// disjointSort :: [a] -> [Int] -> [a]
|
||||
const disjointSort = (xs, indices) => {
|
||||
|
||||
// Sequence of indices discarded
|
||||
const indicesSorted = indices.sort(),
|
||||
subsetSorted = indicesSorted
|
||||
.map(i => xs[i])
|
||||
.sort();
|
||||
|
||||
return xs
|
||||
.map((x, i) => {
|
||||
const iIndex = indicesSorted.indexOf(i);
|
||||
return iIndex !== -1 ? (
|
||||
subsetSorted[iIndex]
|
||||
) : x;
|
||||
});
|
||||
};
|
||||
|
||||
return disjointSort([7, 6, 5, 4, 3, 2, 1, 0], [6, 1, 7]);
|
||||
})();
|
||||
37
Task/Sort-disjoint-sublist/OoRexx/sort-disjoint-sublist.rexx
Normal file
37
Task/Sort-disjoint-sublist/OoRexx/sort-disjoint-sublist.rexx
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
data = .array~of(7, 6, 5, 4, 3, 2, 1, 0)
|
||||
-- this could be a list, array, or queue as well because of polymorphism
|
||||
-- also, ooRexx arrays are 1-based, so using the alternate index set for the
|
||||
-- problem.
|
||||
indexes = .set~of(7, 2, 8)
|
||||
call disjointSorter data, indexes
|
||||
|
||||
say "Sorted data is: ["data~toString("l", ", ")"]"
|
||||
|
||||
::routine disjointSorter
|
||||
use arg data, indexes
|
||||
temp = .array~new(indexes~items)
|
||||
-- we want to process these in a predictable order, so make an array
|
||||
tempIndexes = indexes~makearray
|
||||
-- we can't just assign things back in the same order. The expected
|
||||
-- result requires the items be inserted back in first-to-last index
|
||||
-- order, so we need to sort the index values too
|
||||
tempIndexes~sortWith(.numberComparator~new)
|
||||
do index over tempIndexes
|
||||
temp~append(data[index])
|
||||
end
|
||||
-- sort as numbers
|
||||
temp~sortwith(.numberComparator~new)
|
||||
|
||||
do i = 1 to tempIndexes~items
|
||||
data[tempIndexes[i]] = temp[i]
|
||||
end
|
||||
|
||||
-- a custom comparator that sorts strings as numeric values rather than
|
||||
-- strings
|
||||
::class numberComparator subclass comparator
|
||||
::method compare
|
||||
use strict arg left, right
|
||||
-- perform the comparison on the names. By subtracting
|
||||
-- the two and returning the sign, we give the expected
|
||||
-- results for the compares
|
||||
return (left - right)~sign
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
values :=T(7, 6, 5, 4, 3, 2, 1, 0);
|
||||
indices:=T(6, 1, 7);
|
||||
|
||||
indices.apply(values.get).sort() // a.get(0) == a[0]
|
||||
.zip(indices.sort()) //-->(v,i) == L(L(0,1),L(1,6),L(6,7))
|
||||
.reduce(fcn(newList,[(v,i)]){ newList[i]=v; newList },values.copy())
|
||||
.println(); // new list
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
values :=L(7, 6, 5, 4, 3, 2, 1, 0);
|
||||
|
||||
indices.apply(values.get).sort() // a.get(0) == a[0]
|
||||
.zip(indices.sort()) //-->(v,i) == L(L(0,1),L(1,6),L(6,7))
|
||||
.apply2(fcn([(v,i)],list){ list[i]=v },values);
|
||||
|
||||
values.println(); // modified list
|
||||
Loading…
Add table
Add a link
Reference in a new issue