Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
|
|
@ -0,0 +1,21 @@
|
|||
-- The parametrized module NO-DUP-LIST(ELEMENTS :: TRIV) defines the signature of simple Haskell like list structure.
|
||||
-- The removal of duplicates is handled by the equational properties listed after the signature in brackets {}
|
||||
-- The binary operation _,_ is associative, commutative, and idempotent.
|
||||
-- This list structure does not permit duplicates, they are removed during evaluation (called reduction in CafeOBJ)
|
||||
-- Actual code is contained in module called NO-DUP-LIST.
|
||||
-- The tests are performed after opening instantiated NO-DUP-LIST with various concrete types.
|
||||
-- For further details see: http://www.ldl.jaist.ac.jp/cafeobj/
|
||||
mod! NO-DUP-LIST(ELEMENTS :: TRIV) {
|
||||
[ List < Elem < Elt] -- Sorts in Ordered Sorted Algebra
|
||||
op [] : -> List { prec: 0 } -- Empty List
|
||||
op _,_ : Elt Elt -> Elt { comm assoc idem prec: 80 l-assoc }
|
||||
op [_] : Elt -> List { prec: 0 }
|
||||
}
|
||||
|
||||
-- Test on lists of INT, CHARACTER, and STRING
|
||||
open NO-DUP-LIST(INT)
|
||||
reduce [ 1 , 2 , 1 , 1 ] . -- Gives [ 1 , 2 ]
|
||||
open NO-DUP-LIST(CHARACTER)
|
||||
reduce [ 'a' , 'b' , 'a' , 'a' ] . -- Gives [ 'a' , 'b' ]
|
||||
open NO-DUP-LIST(STRING)
|
||||
reduce [ "abc" , "def" , "abc" ] . -- Gives [ "def" , "abc" ]
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
<String|Integer>[] data = [1, 2, 3, "a", "b", "c", 2, 3, 4, "b", "c", "d"];
|
||||
<String|Integer>[] unique = HashSet { *data }.sequence();
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
inNumbers := DATASET([{1},{2},{3},{4},{1},{1},{7},{8},{9},{9},{0},{0},{3},{3},{3},{3},{3}], {INTEGER Field1});
|
||||
DEDUP(SORT(inNumbers,Field1));
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
' FB 1.05.0 Win64
|
||||
|
||||
Sub removeDuplicates(a() As Integer, b() As Integer)
|
||||
Dim lb As Integer = LBound(a)
|
||||
Dim ub As Integer = UBound(a)
|
||||
If ub = -1 Then Return '' empty array
|
||||
Redim b(lb To ub)
|
||||
b(lb) = a(lb)
|
||||
Dim count As Integer = 1
|
||||
Dim unique As Boolean
|
||||
|
||||
For i As Integer = lb + 1 To ub
|
||||
unique = True
|
||||
For j As Integer = lb to i - 1
|
||||
If a(i) = a(j) Then
|
||||
unique = False
|
||||
Exit For
|
||||
End If
|
||||
Next j
|
||||
If unique Then
|
||||
b(lb + count) = a(i)
|
||||
count += 1
|
||||
End If
|
||||
Next i
|
||||
|
||||
If count > 0 Then Redim Preserve b(lb To lb + count - 1)
|
||||
End Sub
|
||||
|
||||
Dim a(1 To 10) As Integer = {1, 2, 1, 4, 5, 2, 15, 1, 3, 4}
|
||||
Dim b() As Integer
|
||||
removeDuplicates a(), b()
|
||||
|
||||
For i As Integer = LBound(b) To UBound(b)
|
||||
Print b(i); " ";
|
||||
Next
|
||||
|
||||
Print
|
||||
Print "Press any key to quit"
|
||||
Sleep
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
local(
|
||||
x = array(3,4,8,1,8,1,4,5,6,8,9,6),
|
||||
y = array
|
||||
)
|
||||
with n in #x where #y !>> #n do => { #y->insert(#n) }
|
||||
// result: array(3, 4, 8, 1, 5, 6, 9)
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
import sequtils, algorithm, intsets
|
||||
|
||||
# Go through the list, and for each element, check the rest of the list to see
|
||||
# if it appears again,
|
||||
var items = @[1, 2, 3, 2, 3, 4, 5, 6, 7]
|
||||
echo deduplicate(items) # O(n^2)
|
||||
|
||||
proc filterDup(xs): seq[int] =
|
||||
result = @[xs[0]]
|
||||
var last = xs[0]
|
||||
for x in xs[1..xs.high]:
|
||||
if x != last:
|
||||
result.add(x)
|
||||
last = x
|
||||
|
||||
# Put the elements into a hash table which does not allow duplicates.
|
||||
var s = initIntSet()
|
||||
for x in items:
|
||||
s.incl(x)
|
||||
echo s
|
||||
|
||||
# Sort the elements and remove consecutive duplicate elements.
|
||||
sort(items, system.cmp[int]) # O(n log n)
|
||||
echo filterDup(items) # O(n)
|
||||
|
|
@ -0,0 +1,44 @@
|
|||
sequence test
|
||||
|
||||
function alpha(integer i, integer j)
|
||||
integer res
|
||||
res = compare(test[i],test[j])
|
||||
if res=0 then
|
||||
res = compare(i,j)
|
||||
end if
|
||||
return res
|
||||
end function
|
||||
|
||||
function unique(sequence s)
|
||||
sequence at, valid = repeat(1,length(s))
|
||||
object last, this
|
||||
integer ai, nxt
|
||||
|
||||
test = s
|
||||
at = custom_sort(routine_id("alpha"),tagset(length(test)))
|
||||
last = test[at[1]]
|
||||
for i=2 to length(at) do
|
||||
ai = at[i]
|
||||
this = test[ai]
|
||||
valid[ai] = last!=this
|
||||
last = this
|
||||
end for
|
||||
|
||||
nxt = find(0,valid)
|
||||
if nxt then
|
||||
for i=nxt+1 to length(test) do
|
||||
if valid[i] then
|
||||
test[nxt] = test[i]
|
||||
nxt += 1
|
||||
end if
|
||||
end for
|
||||
test = test[1..nxt-1]
|
||||
end if
|
||||
return test
|
||||
end function
|
||||
|
||||
?join(unique(split("Now is the time for all good men to come to the aid of the party.")))
|
||||
?unique({1, 2, 1, 4, 5, 2, 15, 1, 3, 4})
|
||||
?unique({1, 2, 3, "a", "b", "c", 2, 3, 4, "b", "c", "d"})
|
||||
?unique({1,3,2,9,1,2,3,8,8,1,0,2})
|
||||
?unique("chthonic eleemosynary paronomasiac")
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
list = ["Now", "is", "the", "time", "for", "all", "good", "men", "to", "come", "to", "the", "aid", "of", "the", "party."]
|
||||
for i = 1 to len(list)
|
||||
for j = i + 1 to len(list)
|
||||
if list[i] = list[j] del(list, j) j-- ok
|
||||
next
|
||||
next
|
||||
|
||||
for n = 1 to len(list)
|
||||
see list[n] + " "
|
||||
next
|
||||
see nl
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
var ary = [1,1,2,1,'redundant',[1,2,3],[1,2,3],'redundant'];
|
||||
say ary.uniq.dump;
|
||||
say ary.last_uniq.dump;
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
function undupe(arr) {
|
||||
var t = {};
|
||||
foreach(arr, function(key, val) {
|
||||
t[val] = true;
|
||||
});
|
||||
|
||||
var r = {};
|
||||
foreach(t, function(key) {
|
||||
r[sizeof r] = key;
|
||||
});
|
||||
|
||||
return r;
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
println(Array(Set([3,2,1,2,3,4])))
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
func uniq<T: Hashable>(lst: [T]) -> [T] {
|
||||
var seen = Set<T>(minimumCapacity: lst.count)
|
||||
return lst.filter { x in
|
||||
let unseen = !seen.contains(x)
|
||||
seen.insert(x)
|
||||
return unseen
|
||||
}
|
||||
}
|
||||
|
||||
println(uniq([3,2,1,2,3,4]))
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
func uniq<T: Equatable>(lst: [T]) -> [T] {
|
||||
var seen = [T]()
|
||||
return lst.filter { x in
|
||||
let unseen = find(seen, x) == nil
|
||||
if (unseen) {
|
||||
seen.append(x)
|
||||
}
|
||||
return unseen
|
||||
}
|
||||
}
|
||||
|
||||
println(uniq([3,2,1,2,3,4]))
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
LOCAL i As Integer, n As Integer, lcOut As String
|
||||
CLOSE DATABASES ALL
|
||||
CLEAR
|
||||
CREATE CURSOR nums (num I)
|
||||
INDEX ON num TAG num COLLATE "Machine"
|
||||
SET ORDER TO 0
|
||||
n = 50
|
||||
RAND(-1)
|
||||
FOR i = 1 TO n
|
||||
INSERT INTO nums VALUES (RanInt(1, 10))
|
||||
ENDFOR
|
||||
SELECT num, COUNT(num) As cnt FROM nums ;
|
||||
GROUP BY num INTO CURSOR grouped
|
||||
LIST OFF TO FILE grouped.txt NOCONSOLE
|
||||
lcOut = ""
|
||||
SCAN
|
||||
lcOut = lcOut + TRANSFORM(num) + ","
|
||||
ENDSCAN
|
||||
lcOut = LEFT(lcOut, LEN(lcOut)-1)
|
||||
? lcOut
|
||||
|
||||
FUNCTION RanInt(tnLow As Integer, tnHigh As Integer) As Integer
|
||||
RETURN INT((tnHigh - tnLow + 1)*RAND() + tnLow)
|
||||
ENDFUNC
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
def (dedup l)
|
||||
let exists (table)
|
||||
collect+each x l
|
||||
unless exists.x
|
||||
yield x
|
||||
exists.x <- 1
|
||||
|
|
@ -0,0 +1 @@
|
|||
@uniq [1 2 3 2 1 2 3] ; returns [1 2 3]
|
||||
|
|
@ -0,0 +1 @@
|
|||
[4,3,2,1,1,2,3,4] | unique
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
def removeAllButFirst:
|
||||
|
||||
# The hash table functions all expect the hash table to be the input.
|
||||
|
||||
# Is x in the hash table?
|
||||
def hashed(x):
|
||||
(x|tostring) as $value
|
||||
| .[$value] as $bucket
|
||||
| $bucket and (.[$value] | index([x]));
|
||||
|
||||
# Add x to the hash table:
|
||||
def add_hash(x):
|
||||
(x|tostring) as $value
|
||||
| .[$value] as $bucket
|
||||
| if $bucket and ($bucket | index([x])) then .
|
||||
else .[$value] += [x]
|
||||
end;
|
||||
|
||||
reduce .[] as $item
|
||||
( [[], {}]; # [array, hash]
|
||||
if .[1] | hashed($item) then .
|
||||
else [ (.[0] + [$item]), (.[1] | add_hash($item)) ]
|
||||
end)
|
||||
| .[0];
|
||||
Loading…
Add table
Add a link
Reference in a new issue