2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -4,3 +4,4 @@ There are basically three approaches seen here:
* Put the elements into a hash table which does not allow duplicates. The complexity is O(''n'') on average, and O(''n''<sup>2</sup>) worst case. This approach requires a hash function for your type (which is compatible with equality), either built-in to your language, or provided by the user.
* Sort the elements and remove consecutive duplicate elements. The complexity of the best sorting algorithms is O(''n'' log ''n''). This approach requires that your type be "comparable", i.e., have an ordering. Putting the elements into a self-balancing binary search tree is a special case of sorting.
* Go through the list, and for each element, check the rest of the list to see if it appears again, and discard it if it does. The complexity is O(''n''<sup>2</sup>). The up-shot is that this always works on any type (provided that you can test for equality).
<br><br>

View file

@ -0,0 +1,31 @@
# use the associative array in the Associate array/iteration task #
# this example uses strings - for other types, the associative #
# array modes AAELEMENT and AAKEY should be modified as required #
PR read "aArray.a68" PR
# returns the unique elements of list #
PROC remove duplicates = ( []STRING list )[]STRING:
BEGIN
REF AARRAY elements := INIT LOC AARRAY;
INT count := 0;
FOR pos FROM LWB list TO UPB list DO
IF NOT ( elements CONTAINSKEY list[ pos ] ) THEN
# first occurance of this element #
elements // list[ pos ] := "";
count +:= 1
FI
OD;
# construct an array of the unique elements from the #
# associative array - the new list will not necessarily be #
# in the original order #
[ count ]STRING result;
REF AAELEMENT e := FIRST elements;
FOR pos WHILE e ISNT nil element DO
result[ pos ] := key OF e;
e := NEXT elements
OD;
result
END; # remove duplicates #
# test the duplicate removal #
print( ( remove duplicates( ( "A", "B", "D", "A", "C", "F", "F", "A" ) ), newline ) )

View file

@ -0,0 +1,2 @@
1 2 3 1 2 3 4 1
1 2 3 4

View file

@ -0,0 +1,3 @@
w←1 2 3 1 2 3 4 1
((⍨w)=w)/w
1 2 3 4

View file

@ -0,0 +1,21 @@
with Ada.Containers.Ordered_Sets;
with Ada.Text_IO; use Ada.Text_IO;
procedure Unique_Set is
package Int_Sets is new Ada.Containers.Ordered_Sets(Integer);
use Int_Sets;
Nums : array (Natural range <>) of Integer := (1,2,3,4,5,5,6,7,1);
Unique : Set;
Set_Cur : Cursor;
Success : Boolean;
begin
for I in Nums'range loop
Unique.Insert(Nums(I), Set_Cur, Success);
end loop;
Set_Cur := Unique.First;
loop
Put_Line(Item => Integer'Image(Element(Set_Cur)));
exit when Set_Cur = Unique.Last;
Set_Cur := Next(Set_Cur);
end loop;
end Unique_Set;

View file

@ -0,0 +1,9 @@
unique({1, 2, 3, "a", "b", "c", 2, 3, 4, "b", "c", "d"})
on unique(x)
set R to {}
repeat with i in x
if i is not in R then set end of R to i's contents
end repeat
return R
end unique

View file

@ -0,0 +1,96 @@
-- CASE INSENSITIVE VERSION
-- nub :: [a] -> [a]
on nub(xs)
-- Eq :: a -> a -> Bool
script Eq
on lambda(x, y)
ignoring case
x = y
end ignoring
end lambda
end script
nubBy(Eq, xs)
end nub
-- TEST
on run {}
{intercalate(space, ¬
nub(splitOn(space, "4 3 2 8 0 1 9 5 1 7 6 3 9 9 4 2 1 5 3 2"))), ¬
intercalate("", ¬
nub(characters of "abcabc ABCABC"))}
--> {"4 3 2 8 0 1 9 5 7 6", "abc "}
end run
---------------------------------------------------------------------------
-- GENERIC FUNCTIONS
-- nubBy :: (a -> a -> Bool) -> [a] -> [a]
on nubBy(fnEq, xxs)
set lng to length of xxs
if lng > 1 then
set x to item 1 of xxs
set xs to items 2 thru -1 of xxs
set p to mReturn(fnEq)
-- notEq :: a -> Bool
script notEq
on lambda(a)
not (p's lambda(a, x))
end lambda
end script
{x} & nubBy(fnEq, filter(notEq, xs))
else
xxs
end if
end nubBy
-- filter :: (a -> Bool) -> [a] -> [a]
on filter(f, xs)
tell mReturn(f)
set lst to {}
set lng to length of xs
repeat with i from 1 to lng
set v to item i of xs
if lambda(v, i, xs) then set end of lst to v
end repeat
return lst
end tell
end filter
-- Lift 2nd class handler function into 1st class script wrapper
-- mReturn :: Handler -> Script
on mReturn(f)
if class of f is script then
f
else
script
property lambda : f
end script
end if
end mReturn
-- FOR THE TEST
-- splitOn :: Text -> Text -> [Text]
on splitOn(strDelim, strMain)
set {dlm, my text item delimiters} to {my text item delimiters, strDelim}
set lstParts to text items of strMain
set my text item delimiters to dlm
return lstParts
end splitOn
-- intercalate :: Text -> [Text] -> Text
on intercalate(strText, lstText)
set {dlm, my text item delimiters} to {my text item delimiters, strText}
set strJoined to lstText as text
set my text item delimiters to dlm
return strJoined
end intercalate

View file

@ -0,0 +1 @@
{"4 3 2 8 0 1 9 5 7 6", "abc "}

View file

@ -1,9 +0,0 @@
unique({1, 2, 3, "a", "b", "c", 2, 3, 4, "b", "c", "d"})
on unique(x)
set R to {}
repeat with i in x
if i is not in R then set end of R to i's contents
end repeat
return R
end unique

View file

@ -0,0 +1,6 @@
data = [ 1, 2, 3, "a", "b", "c", 2, 3, 4, "b", "c", "d" ]
set = []
set.push i for i in data when not (i in set)
console.log data
console.log set

View file

@ -1,28 +1,27 @@
defmodule RC do
# hash table approach
def uniq1(list) do
Enum.reduce(list, HashSet.new, fn x, set -> Set.put(set, x) end)
|> Set.to_list
end
# Set approach
def uniq1(list), do: MapSet.new(list) |> MapSet.to_list
# Sort approach
def uniq2(list), do: Enum.sort(list) |> uniq2([])
defp uniq2([], uniq), do: Enum.reverse(uniq)
defp uniq2([h|t], uniq) when h==hd(uniq), do: uniq2(t, uniq)
defp uniq2([h|t], uniq) , do: uniq2(t, [h | uniq])
def uniq2(list), do: Enum.sort(list) |> Enum.dedup
# Go through the list approach
def uniq3(list), do: uniq3(list, [])
defp uniq3([], uniq), do: Enum.reverse(uniq)
defp uniq3([h|t], uniq) do
if Enum.member?(uniq, h), do: uniq3(t, uniq), else: uniq3(t, [h | uniq])
defp uniq3([], res), do: Enum.reverse(res)
defp uniq3([h|t], res) do
if h in res, do: uniq3(t, res), else: uniq3(t, [h | res])
end
end
list = [1,1,2,1,'redundant',[1,2,3],[1,2,3],'redundant']
IO.inspect Enum.uniq(list)
IO.inspect RC.uniq1(list)
IO.inspect RC.uniq2(list)
IO.inspect RC.uniq3(list)
num = 10000
max = div(num, 10)
list = for _ <- 1..num, do: :rand.uniform(max)
funs = [&Enum.uniq/1, &RC.uniq1/1, &RC.uniq2/1, &RC.uniq3/1]
Enum.each(funs, fn fun ->
result = fun.([1,1,2,1,'redundant',1.0,[1,2,3],[1,2,3],'redundant',1.0])
:timer.tc(fn ->
Enum.each(1..100, fn _ -> fun.(list) end)
end)
|> fn{t,_} -> IO.puts "#{inspect fun}:\t#{t/1000000}\t#{inspect result}" end.()
end)

View file

@ -1,21 +1,22 @@
def list = [1, 2, 3, 'a', 'b', 'c', 2, 3, 4, 'b', 'c', 'd']
assert list.size() == 12
println " Original List: ${list}"
println " Original List: ${list}"
// Filtering the List
// Filtering the List (non-mutating)
def list2 = list.unique(false)
assert list2.size() == 8
assert list.size() == 12
println " Filtered List: ${list2}"
// Filtering the List (in place)
list.unique()
assert list.size() == 8
println " Filtered List: ${list}"
println " Original List, filtered: ${list}"
list = [1, 2, 3, 'a', 'b', 'c', 2, 3, 4, 'b', 'c', 'd']
assert list.size() == 12
def list3 = [1, 2, 3, 'a', 'b', 'c', 2, 3, 4, 'b', 'c', 'd']
assert list3.size() == 12
// Converting to Set
def set = new HashSet(list)
def set = list as Set
assert set.size() == 8
println " Set: ${set}"
// Converting to Order-preserving Set
set = new LinkedHashSet(list)
assert set.size() == 8
println "List-ordered Set: ${set}"
println " Set: ${set}"

View file

@ -0,0 +1,12 @@
import java.util.*;
class Test {
public static void main(String[] args) {
Object[] data = {1, 1, 2, 2, 3, 3, 3, "a", "a", "b", "b", "c", "d"};
Set<Object> uniqueSet = new HashSet<Object>(Arrays.asList(data));
for (Object o : uniqueSet)
System.out.printf("%s ", o);
}
}

View file

@ -0,0 +1,10 @@
import java.util.*;
class Test {
public static void main(String[] args) {
Object[] data = {1, 1, 2, 2, 3, 3, 3, "a", "a", "b", "b", "c", "d"};
Arrays.stream(data).distinct().forEach((o) -> System.out.printf("%s ", o));
}
}

View file

@ -1,7 +0,0 @@
import java.util.Set;
import java.util.HashSet;
import java.util.Arrays;
Object[] data = {1, 2, 3, "a", "b", "c", 2, 3, 4, "b", "c", "d"};
Set<Object> uniqueSet = new HashSet<Object>(Arrays.asList(data));
Object[] unique = uniqueSet.toArray();

View file

@ -0,0 +1,39 @@
(function () {
'use strict';
// nub :: [a] -> [a]
function nub(xs) {
// Eq :: a -> a -> Bool
function Eq(a, b) {
return a === b;
}
// nubBy :: (a -> a -> Bool) -> [a] -> [a]
function nubBy(fnEq, xs) {
var x = xs.length ? xs[0] : undefined;
return x !== undefined ? [x].concat(
nubBy(fnEq, xs.slice(1)
.filter(function (y) {
return !fnEq(x, y);
}))
) : [];
}
return nubBy(Eq, xs);
}
// TEST
return [
nub('4 3 2 8 0 1 9 5 1 7 6 3 9 9 4 2 1 5 3 2'.split(' '))
.map(function (x) {
return Number(x);
}),
nub('chthonic eleemosynary paronomasiac'.split(''))
.join('')
]
})();

View file

@ -0,0 +1,7 @@
fun main(args: Array<String>) {
val data = listOf(1, 2, 3, "a", "b", "c", 2, 3, 4, "b", "c", "d")
val set = data.distinct()
println(data)
println(set)
}

View file

@ -0,0 +1,14 @@
a$ = "2 3 5 7 11 13 17 19 cats 222 -100.2 +11 1.1 +7 7. 7 5 5 3 2 0 4.4 2"
for i = 1 to len(a$)
a1$ = word$(a$,i)
if a1$ = "" then exit for
for i1 = 1 to len(b$)
if a1$ = word$(b$,i1) then [nextWord]
next i1
b$ = b$ + a1$ + " "
[nextWord]
next i
print "Dups:";a$
print "No Dups:";b$

View file

@ -0,0 +1,16 @@
use std::vec::Vec;
use std::collections::HashSet;
use std::hash::Hash;
use std::cmp::Eq;
fn main(){
let mut sample_elements = vec![0u8,0,1,1,2,3,2];
println!("Before removal of duplicates : {:?}", sample_elements);
remove_duplicate_elements(&mut sample_elements);
println!("After removal of duplicates : {:?}", sample_elements);
}
fn remove_duplicate_elements<T : Hash + Eq>(elements: &mut Vec<T>){
let set : HashSet<_> = elements.drain(..).collect();
elements.extend(set.into_iter());
}