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
29
Task/Set/Apex/set.apex
Normal file
29
Task/Set/Apex/set.apex
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
public class MySetController{
|
||||
public Set<String> strSet {get; private set; }
|
||||
public Set<Id> idSet {get; private set; }
|
||||
|
||||
public MySetController(){
|
||||
//Initialize to an already known collection. Results in a set of abc,def.
|
||||
this.strSet = new Set<String>{'abc','abc','def'};
|
||||
|
||||
//Initialize to empty set and add in entries.
|
||||
this.strSet = new Set<String>();
|
||||
this.strSet.add('abc');
|
||||
this.strSet.add('def');
|
||||
this.strSet.add('abc');
|
||||
//Results in {'abc','def'}
|
||||
|
||||
//You can also get a set from a map in Apex. In this case, the account ids are fetched from a SOQL query.
|
||||
Map<Id,Account> accountMap = new Map<Id,Account>([Select Id,Name From Account Limit 10]);
|
||||
Set<Id> accountIds = accountMap.keySet();
|
||||
|
||||
//If you have a set, you can also use it with the bind variable syntax in SOQL:
|
||||
List<Account> accounts = [Select Name From Account Where Id in :accountIds];
|
||||
|
||||
//Like other collections in Apex, you can use a for loop to iterate over sets:
|
||||
for(Id accountId : accountIds){
|
||||
Account a = accountMap.get(accountId);
|
||||
//Do account stuffs here.
|
||||
}
|
||||
}
|
||||
}
|
||||
33
Task/Set/EchoLisp/set.echolisp
Normal file
33
Task/Set/EchoLisp/set.echolisp
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
; use { } to read a set
|
||||
(define A { 1 2 3 4 3 5 5}) → { 1 2 3 4 5 } ; duplicates are removed from a set
|
||||
; or use make-set to make a set from a list
|
||||
(define B (make-set ' ( 3 4 5 6 7 8 8))) → { 3 4 5 6 7 8 }
|
||||
(set-intersect A B) → { 3 4 5 }
|
||||
(set-intersect? A B) → #t ; predicate
|
||||
(set-union A B) → { 1 2 3 4 5 6 7 8 }
|
||||
(set-substract A B) → { 1 2 }
|
||||
(set-sym-diff A B) → { 1 2 6 7 8 } ; ∆ symmetric difference
|
||||
(set-equal? A B) → #f
|
||||
(set-equal? { a b c} { c b a}) → #t ; order is unimportant
|
||||
(set-subset? A B) → #f ; B in A or B = A
|
||||
(set-subset? A { 3 4 }) → #t
|
||||
(member 4 A) → (4 5) ; same as #t : true
|
||||
(member 9 A) → #f
|
||||
|
||||
; check basic equalities
|
||||
(set-equal? A (set-union (set-intersect A B) (set-substract A B))) → #t
|
||||
(set-equal? (set-union A B) (set-union (set-sym-diff A B) (set-intersect A B))) → #t
|
||||
|
||||
; × : cartesian product of two sets : all pairs (a . b) , a in A, b in B
|
||||
; returns a list (not a set)
|
||||
(define A { albert simon})
|
||||
(define B { antoinette ornella marylin})
|
||||
|
||||
(set-product A B)
|
||||
→ ((albert . antoinette) (albert . marylin) (albert . ornella) (simon . antoinette) (simon . marylin) (simon . ornella))
|
||||
|
||||
; sets elements may be sets
|
||||
{ { a b c} {c b a } { a b d}} → { { a b c } { a b d } } ; duplicate removed
|
||||
|
||||
; A few functions return sets :
|
||||
(primes 10) → { 2 3 5 7 11 13 17 19 23 29 }
|
||||
23
Task/Set/FunL/set.funl
Normal file
23
Task/Set/FunL/set.funl
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
A = {1, 2, 3}
|
||||
B = {3, 4, 5}
|
||||
C = {1, 2, 3, 4, 5}
|
||||
D = {2, 1, 3}
|
||||
|
||||
println( '2 is in A: ' + (2 in A) )
|
||||
println( '4 is in A: ' + (4 in A) )
|
||||
println( 'A union B: ' + A.union(B) )
|
||||
println( 'A intersect B: ' + A.intersect(B) )
|
||||
println( 'A difference B: ' + A.diff(B) )
|
||||
println( 'A subset of B: ' + A.subsetOf(B) )
|
||||
println( 'A subset of B: ' + A.subsetOf(C) )
|
||||
println( 'A equal B: ' + (A == B) )
|
||||
println( 'A equal D: ' + (A == D) )
|
||||
|
||||
S = set( A )
|
||||
|
||||
println( 'S (mutable version of A): ' + S )
|
||||
S.add( 4 )
|
||||
println( 'S with 4 added: ' + S )
|
||||
println( 'S subset of C: ' + S.subsetOf(C) )
|
||||
S.remove( 1 )
|
||||
println( 'S after 1 removed: ' + S )
|
||||
28
Task/Set/LFE/set.lfe
Normal file
28
Task/Set/LFE/set.lfe
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
> (set set-1 (sets:new))
|
||||
#(set 0 16 16 8 80 48 ...)
|
||||
> (set set-2 (sets:add_element 'a set-1))
|
||||
#(set 1 16 16 8 80 48 ...)
|
||||
> (set set-3 (sets:from_list '(a b)))
|
||||
#(set 2 16 16 8 80 48 ...)
|
||||
> (sets:is_element 'a set-2)
|
||||
true
|
||||
> (set union (sets:union set-2 set-3))
|
||||
#(set 2 16 16 8 80 48 ...)
|
||||
> (sets:to_list union)
|
||||
(a b)
|
||||
> (set intersect (sets:intersection set-2 set-3))
|
||||
#(set 1 16 16 8 80 48 ...)
|
||||
> (sets:to_list intersect)
|
||||
(a)
|
||||
> (set subtr (sets:subtract set-3 set-2))
|
||||
#(set 1 16 16 8 80 48 ...)
|
||||
> (sets:to_list subtr)
|
||||
(b)
|
||||
> (sets:is_subset set-2 set-3)
|
||||
true
|
||||
> (=:= set-2 set-3)
|
||||
false
|
||||
> (set set-4 (sets:add_element 'b set-2))
|
||||
#(set 2 16 16 8 80 48 ...)
|
||||
> (=:= set-3 set-4)
|
||||
true
|
||||
25
Task/Set/Lasso/set.lasso
Normal file
25
Task/Set/Lasso/set.lasso
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
// Extend set type
|
||||
define set->issubsetof(p::set) => .intersection(#p)->size == .size
|
||||
define set->oncompare(p::set) => .intersection(#p)->size - .size
|
||||
|
||||
// Set creation
|
||||
local(set1) = set('j','k','l','m','n')
|
||||
local(set2) = set('m','n','o','p','q')
|
||||
|
||||
//Test m ∈ S -- "m is an element in set S"
|
||||
#set1 >> 'm'
|
||||
|
||||
// A ∪ B -- union; a set of all elements either in set A or in set B.
|
||||
#set1->union(#set2)
|
||||
|
||||
//A ∩ B -- intersection; a set of all elements in both set A and set B.
|
||||
#set1->intersection(#set2)
|
||||
|
||||
//A ∖ B -- difference; a set of all elements in set A, except those in set B.
|
||||
#set1->difference(#set2)
|
||||
|
||||
//A ⊆ B -- subset; true if every element in set A is also in set B.
|
||||
#set1->issubsetof(#set2)
|
||||
|
||||
//A = B -- equality; true if every element of set A is in set B and vice-versa.
|
||||
#set1 == #set2
|
||||
19
Task/Set/Nim/set.nim
Normal file
19
Task/Set/Nim/set.nim
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
var # creation
|
||||
s = {0,3,5,10}
|
||||
t = {3..20, 50..55}
|
||||
|
||||
if 5 in s: echo "5 is in!" # element test
|
||||
|
||||
var
|
||||
c = s + t # union
|
||||
d = s * t # intersection
|
||||
e = s - t # difference
|
||||
|
||||
if s <= t: echo "s ⊆ t" # subset
|
||||
|
||||
if s <= t: echo "s ⊂ t" # strong subset
|
||||
|
||||
if s == t: echo "s = s" # equality
|
||||
|
||||
s.incl(4) # add 4 to set
|
||||
s.excl(5) # remove 5 from set
|
||||
65
Task/Set/Sidef/set-1.sidef
Normal file
65
Task/Set/Sidef/set-1.sidef
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
class Set(*set) {
|
||||
|
||||
method init {
|
||||
var elems = set;
|
||||
set = Hash.new;
|
||||
elems.each { |e| self += e }
|
||||
}
|
||||
|
||||
method +(elem) {
|
||||
set{elem} = elem;
|
||||
self;
|
||||
}
|
||||
|
||||
method del(elem) {
|
||||
set.delete(elem);
|
||||
}
|
||||
|
||||
method has(elem) {
|
||||
set.has_key(elem);
|
||||
}
|
||||
|
||||
method ∪(Set that) {
|
||||
Set(set.values..., that.values...);
|
||||
}
|
||||
|
||||
method ∩(Set that) {
|
||||
Set(set.keys.grep{ |k| k ∈ that } \
|
||||
.map { |k| set{k} }...);
|
||||
}
|
||||
|
||||
method ∖(Set that) {
|
||||
Set(set.keys.grep{|k| !(k ∈ that) } \
|
||||
.map {|k| set{k} }...);
|
||||
}
|
||||
|
||||
method ^(Set that) {
|
||||
var d = ((self ∖ that) ∪ (that ∖ self));
|
||||
Set(d.values...);
|
||||
}
|
||||
|
||||
method count { set.len }
|
||||
|
||||
method ≡(Set that) {
|
||||
(self ∖ that -> count.is_zero) && (that ∖ self -> count.is_zero);
|
||||
}
|
||||
|
||||
method values { set.values }
|
||||
|
||||
method ⊆(Set that) {
|
||||
that.set.keys.each { |k|
|
||||
k ∈ self || return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
method to_s {
|
||||
"Set{" + set.values.map{|e| "#{e}"}.sort.join(', ') + "}"
|
||||
}
|
||||
}
|
||||
|
||||
class Object {
|
||||
method ∈(Set set) {
|
||||
set.has(self);
|
||||
}
|
||||
}
|
||||
22
Task/Set/Sidef/set-2.sidef
Normal file
22
Task/Set/Sidef/set-2.sidef
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
var x = Set(1, 2, 3);
|
||||
5..7 -> each { |i| x += i };
|
||||
|
||||
var y = Set(1, 2, 4, x);
|
||||
|
||||
say "set x is: #{x}";
|
||||
say "set y is: #{y}";
|
||||
|
||||
[1,2,3,4,x].each { |elem|
|
||||
say ("#{elem} is ", elem ∈ y ? '' : 'not', " in y");
|
||||
}
|
||||
|
||||
var (w, z);
|
||||
say ("union: ", x ∪ y);
|
||||
say ("intersect: ", x ∩ y);
|
||||
say ("z = x ∖ y = ", z = (x ∖ y) );
|
||||
say ("y is ", x ⊆ y ? "" : "not ", "a subset of x");
|
||||
say ("z is ", x ⊆ z ? "" : "not ", "a subset of x");
|
||||
say ("z = (x ∪ y) ∖ (x ∩ y) = ", z = ((x ∪ y) ∖ (x ∩ y)));
|
||||
say ("w = x ^ y = ", w = (x ^ y));
|
||||
say ("w is ", w ≡ z ? "" : "not ", "equal to z");
|
||||
say ("w is ", w ≡ x ? "" : "not ", "equal to x");
|
||||
27
Task/Set/Swift/set.swift
Normal file
27
Task/Set/Swift/set.swift
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
var s1 : Set<Int> = [1, 2, 3, 4]
|
||||
let s2 : Set<Int> = [3, 4, 5, 6]
|
||||
println(s1.union(s2)) // union; prints "[5, 6, 2, 3, 1, 4]"
|
||||
println(s1.intersect(s2)) // intersection; prints "[3, 4]"
|
||||
println(s1.subtract(s2)) // difference; prints "[2, 1]"
|
||||
println(s1.isSubsetOf(s1)) // subset; prints "true"
|
||||
println(Set<Int>([3, 1]).isSubsetOf(s1)) // subset; prints "true"
|
||||
println(s1.isStrictSubsetOf(s1)) // proper subset; prints "false"
|
||||
println(Set<Int>([3, 1]).isStrictSubsetOf(s1)) // proper subset; prints "true"
|
||||
println(Set<Int>([3, 2, 4, 1]) == s1) // equality; prints "true"
|
||||
println(s1 == s2) // equality; prints "false"
|
||||
println(s1.contains(2)) // membership; prints "true"
|
||||
println(Set<Int>([1, 2, 3, 4]).isSupersetOf(s1)) // superset; prints "true"
|
||||
println(Set<Int>([1, 2, 3, 4]).isStrictSupersetOf(s1)) // proper superset; prints "false"
|
||||
println(Set<Int>([1, 2, 3, 4, 5]).isStrictSupersetOf(s1)) // proper superset; prints "true"
|
||||
println(s1.exclusiveOr(s2)) // symmetric difference; prints "[5, 6, 2, 1]"
|
||||
println(s1.count) // cardinality; prints "4"
|
||||
s1.insert(99) // mutability
|
||||
println(s1) // prints "[99, 2, 3, 1, 4]"
|
||||
s1.remove(99) // mutability
|
||||
println(s1) // prints "[2, 3, 1, 4]"
|
||||
s1.unionInPlace(s2) // mutability
|
||||
println(s1) // prints "[5, 6, 2, 3, 1, 4]"
|
||||
s1.subtractInPlace(s2) // mutability
|
||||
println(s1) // prints "[2, 1]"
|
||||
s1.exclusiveOrInPlace(s2) // mutability
|
||||
println(s1) // prints "[5, 6, 2, 3, 1, 4]"
|
||||
2
Task/Set/jq/set-1.jq
Normal file
2
Task/Set/jq/set-1.jq
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
{"a":true, "b":true } == {"b":true, "a":true}.
|
||||
{"a":true} + {"b":true } == { "a":true, "b":true}
|
||||
19
Task/Set/jq/set-10.jq
Normal file
19
Task/Set/jq/set-10.jq
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
# If A and B are sets, then A-B is emitted
|
||||
def difference(A;B):
|
||||
(A|length) as $al
|
||||
| (B|length) as $bl
|
||||
| if $al == 0 then [] elif $bl == 0 then A
|
||||
else
|
||||
reduce range(0; $al + $bl) as $k
|
||||
( [0, 0, []];
|
||||
.[0] as $i | .[1] as $j
|
||||
| if $i < $al and $j < $bl then
|
||||
if A[$i] == B[$j] then [ $i+1, $j+1, .[2] ]
|
||||
elif A[$i] < B[$j] then [ $i+1, $j, .[2] + [A[$i]] ]
|
||||
else [ $i , $j+1, .[2] ]
|
||||
end
|
||||
elif $i < $al then [ $i+1, $j, .[2] + [A[$i]] ]
|
||||
else .
|
||||
end
|
||||
) | .[2]
|
||||
end ;
|
||||
22
Task/Set/jq/set-11.jq
Normal file
22
Task/Set/jq/set-11.jq
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
# merge input array with array x by comparing the heads of the arrays in turn;
|
||||
# if both arrays are sorted, the result will be sorted:
|
||||
def merge(x):
|
||||
length as $length
|
||||
| (x|length) as $xl
|
||||
| if $length == 0 then x
|
||||
elif $xl == 0 then .
|
||||
else
|
||||
. as $in
|
||||
| reduce range(0; $xl + $length) as $z
|
||||
# state [ix, xix, ans]
|
||||
( [0, 0, []];
|
||||
if .[0] < $length and ((.[1] < $xl and $in[.[0]] <= x[.[1]]) or .[1] == $xl)
|
||||
then [(.[0] + 1), .[1], (.[2] + [$in[.[0]]]) ]
|
||||
else [.[0], (.[1] + 1), (.[2] + [x[.[1]]]) ]
|
||||
end
|
||||
) | .[2]
|
||||
end ;
|
||||
|
||||
def union(A;B):
|
||||
A|merge(B)
|
||||
| reduce .[] as $m ([]; if length == 0 or .[length-1] != $m then . + [$m] else . end);
|
||||
10
Task/Set/jq/set-12.jq
Normal file
10
Task/Set/jq/set-12.jq
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
def subset(A;B):
|
||||
# TCO
|
||||
def _subset:
|
||||
if .[0]|length == 0 then true
|
||||
elif .[1]|length == 0 then false
|
||||
elif .[0][0] == .[1][0] then [.[0][1:], .[1][1:]] | _subset
|
||||
elif .[0][0] < .[1][0] then false
|
||||
else [ .[0], .[1][1:] ] | _subset
|
||||
end;
|
||||
[A,B] | _subset;
|
||||
11
Task/Set/jq/set-13.jq
Normal file
11
Task/Set/jq/set-13.jq
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
def intersect:
|
||||
.[0] as $A | .[1] as $B
|
||||
| ($A|length) as $al
|
||||
| ($B|length) as $bl
|
||||
| if $al == 0 or $bl == 0 then false
|
||||
else
|
||||
($B | bsearch($A[0])) as $b
|
||||
| if $b >= 0 then true
|
||||
else [$A[1:], $B[- (1 + $b) :]] | intersect
|
||||
end
|
||||
end;
|
||||
2
Task/Set/jq/set-2.jq
Normal file
2
Task/Set/jq/set-2.jq
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
def is_stringset:
|
||||
. as $in | type == "object" and reduce keys[] as $key (true; . and $in[$key] == true);
|
||||
1
Task/Set/jq/set-3.jq
Normal file
1
Task/Set/jq/set-3.jq
Normal file
|
|
@ -0,0 +1 @@
|
|||
T | has(m)
|
||||
4
Task/Set/jq/set-4.jq
Normal file
4
Task/Set/jq/set-4.jq
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
# Set-intersection: A ∩ B
|
||||
def stringset_intersection(A;B):
|
||||
reduce (A|keys)[] as $k
|
||||
({}; if (B|has($k)) then . + {($k):true} else . end);
|
||||
4
Task/Set/jq/set-5.jq
Normal file
4
Task/Set/jq/set-5.jq
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
# stringset_difference: A \ B
|
||||
def stringset_difference(A;B):
|
||||
reduce (A|keys)[] as $k
|
||||
({}; if (B|has($k)) then . else . + {($k):true} end);
|
||||
4
Task/Set/jq/set-6.jq
Normal file
4
Task/Set/jq/set-6.jq
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
# A ⊆ B iff string_subset(A;B)
|
||||
def stringset_subset(A;B):
|
||||
reduce (A|keys)[] as $k
|
||||
(true; . and (B|has($k)));
|
||||
5
Task/Set/jq/set-7.jq
Normal file
5
Task/Set/jq/set-7.jq
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
def is_set:
|
||||
. as $in
|
||||
| type == "array" and
|
||||
reduce range(0;length-1) as $i
|
||||
(true; if . then $in[$i] < $in[$i+1] else false end);
|
||||
1
Task/Set/jq/set-8.jq
Normal file
1
Task/Set/jq/set-8.jq
Normal file
|
|
@ -0,0 +1 @@
|
|||
def is_member(m): bsearch(m) > -1;
|
||||
18
Task/Set/jq/set-9.jq
Normal file
18
Task/Set/jq/set-9.jq
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
# If A and B are sets, then intersection(A;B) emits their intersection:
|
||||
def intersection(A;B):
|
||||
(A|length) as $al
|
||||
| (B|length) as $bl
|
||||
| if $al == 0 or $bl == 0 then []
|
||||
else
|
||||
reduce range(0; $al + $bl) as $k
|
||||
( [0, 0, []];
|
||||
.[0] as $i | .[1] as $j
|
||||
| if $i < $al and $j < $bl then
|
||||
if A[$i] == B[$j] then [ $i+1 , $j+1, .[2] + [A[$i]]]
|
||||
elif A[$i] < B[$j] then [ $i+1 , $j, .[2] ]
|
||||
else [ $i , $j+1, .[2] ]
|
||||
end
|
||||
else .
|
||||
end
|
||||
) | .[2]
|
||||
end ;
|
||||
Loading…
Add table
Add a link
Reference in a new issue