September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -1,10 +0,0 @@
package Power_Set is
type Set is array (Positive range <>) of Positive;
Empty_Set: Set(1 .. 0);
generic
with procedure Visit(S: Set);
procedure All_Subsets(S: Set); -- calles Visit once for each subset of S
end Power_Set;

View file

@ -1,20 +0,0 @@
package body Power_Set is
procedure All_Subsets(S: Set) is
procedure Visit_Sets(Unmarked: Set; Marked: Set) is
Tail: Set := Unmarked(Unmarked'First+1 .. Unmarked'Last);
begin
if Unmarked = Empty_Set then
Visit(Marked);
else
Visit_Sets(Tail, Marked & Unmarked(Unmarked'First));
Visit_Sets(Tail, Marked);
end if;
end Visit_Sets;
begin
Visit_Sets(S, Empty_Set);
end All_Subsets;
end Power_Set;

View file

@ -1,28 +0,0 @@
with Ada.Text_IO, Ada.Command_Line, Power_Set;
procedure Print_Power_Set is
procedure Print_Set(Items: Power_Set.Set) is
First: Boolean := True;
begin
Ada.Text_IO.Put("{ ");
for Item of Items loop
if First then
First := False; -- no comma needed
else
Ada.Text_IO.Put(", "); -- comma, to separate the items
end if;
Ada.Text_IO.Put(Ada.Command_Line.Argument(Item));
end loop;
Ada.Text_IO.Put_Line(" }");
end Print_Set;
procedure Print_All_Subsets is new Power_Set.All_Subsets(Print_Set);
Set: Power_Set.Set(1 .. Ada.Command_Line.Argument_Count);
begin
for I in Set'Range loop -- initialize set
Set(I) := I;
end loop;
Print_All_Subsets(Set); -- do the work
end;

View file

@ -0,0 +1,28 @@
with Ada.Text_IO, Ada.Command_Line;
use Ada.Text_IO, Ada.Command_Line;
procedure powerset is
procedure print_subset (set : natural) is
-- each i'th binary digit of "set" indicates if the i'th integer belongs to "set" or not.
k : natural := set;
first : boolean := true;
begin
Put ("{");
for i in 1..Argument_Count loop
if k mod 2 = 1 then
if first then
first := false;
else
Put (",");
end if;
Put (Argument (i));
end if;
k := k / 2; -- we go to the next bit of "set"
end loop;
Put_Line("}");
end print_subset;
begin
for i in 0..2**Argument_Count-1 loop
print_subset (i);
end loop;
end powerset;

View file

@ -1,29 +1,30 @@
-- POWER SET -----------------------------------------------------------------
-- powerset :: [a] -> [[a]]
on powerset(xs)
script subSet
on lambda(acc, x)
script consX
on lambda(y)
on |λ|(acc, x)
script cons
on |λ|(y)
{x} & y
end lambda
end |λ|
end script
acc & map(consX, acc)
end lambda
acc & map(cons, acc)
end |λ|
end script
foldr(subSet, {{}}, xs)
end powerset
--------------------------------------------------------------------------------------
-- TEST
-- TEST ----------------------------------------------------------------------
on run
script test
on lambda(x)
on |λ|(x)
set {setName, setMembers} to x
{setName, powerset(setMembers)}
end lambda
end |λ|
end script
map(test, [¬
@ -34,11 +35,9 @@ on run
--> {{"Set [1,2,3]", {{}, {3}, {2}, {2, 3}, {1}, {1, 3}, {1, 2}, {1, 2, 3}}},
--> {"Empty set", {{}}},
--> {"Set containing only empty set", {{}, {{}}}}}
end run
-- GENERIC FUNCTIONS ---------------------------------------------------------------
-- GENERIC FUNCTIONS ---------------------------------------------------------
-- foldr :: (a -> b -> a) -> a -> [b] -> a
on foldr(f, startValue, xs)
@ -46,7 +45,7 @@ on foldr(f, startValue, xs)
set v to startValue
set lng to length of xs
repeat with i from lng to 1 by -1
set v to lambda(v, item i of xs, i, xs)
set v to |λ|(v, item i of xs, i, xs)
end repeat
return v
end tell
@ -58,7 +57,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
@ -71,7 +70,7 @@ on mReturn(f)
f
else
script
property lambda : f
property |λ| : f
end script
end if
end mReturn

View file

@ -1,28 +0,0 @@
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
static void powerset(int argc, char** argv)
{
unsigned int i, j, bits, i_max = 1U << argc;
if (argc >= sizeof(i) * CHAR_BIT) {
fprintf(stderr, "Error: set too large\n");
exit(1);
}
for (i = 0; i < i_max ; ++i) {
printf("{");
for (bits = i, j = 0; bits; bits >>= 1, ++j) {
if (bits & 1)
printf(bits > 1 ? "%s, " : "%s", argv[j]);
}
printf("}\n");
}
}
int main(int argc, char* argv[])
{
powerset(argc - 1, argv + 1);
return 0;
}

View file

@ -1,17 +0,0 @@
% ./a.out 1 2 3 4
{}
{1}
{2}
{1, 2}
{3}
{1, 3}
{2, 3}
{1, 2, 3}
{4}
{1, 4}
{2, 4}
{1, 2, 4}
{3, 4}
{1, 3, 4}
{2, 3, 4}
{1, 2, 3, 4}

View file

@ -1,2 +1,2 @@
powerSet :: [a] -> [[a]]
powerset = foldr (\x acc -> acc ++ map (x:) acc) [[]]
powerSet = foldr (\x acc -> acc ++ map (x:) acc) [[]]

View file

@ -1,15 +1,2 @@
import qualified Data.Set as Set
type Set=Set.Set
unionAll :: (Ord a) => Set (Set a) -> Set a
unionAll = Set.fold Set.union Set.empty
--slift is the analogue of liftA2 for sets.
slift :: (Ord a, Ord b, Ord c) => (a->b->c) -> Set a -> Set b -> Set c
slift f s0 s1 = unionAll (Set.map (\e->Set.map (f e) s1) s0)
--a -> {{},{a}}
makeSet :: (Ord a) => a -> Set (Set a)
makeSet = (Set.insert Set.empty) . Set.singleton.Set.singleton
powerSet :: (Ord a) => Set a -> Set (Set a)
powerSet = (Set.fold (slift Set.union) (Set.singleton Set.empty)) . Set.map makeSet
powerSet :: [a] -> [[a]]
powerSet = foldr ((mappend <*>) . fmap . (:)) (pure [])

View file

@ -1,2 +1,15 @@
Prelude Data.Set> powerSet fromList [1,2,3]
fromList [fromList [], fromList [1], fromList [1,2], fromList [1,2,3], fromList [1,3], fromList [2], fromList [2,3], fromList [3]]
import qualified Data.Set as Set
type Set=Set.Set
unionAll :: (Ord a) => Set (Set a) -> Set a
unionAll = Set.fold Set.union Set.empty
--slift is the analogue of liftA2 for sets.
slift :: (Ord a, Ord b, Ord c) => (a->b->c) -> Set a -> Set b -> Set c
slift f s0 s1 = unionAll (Set.map (\e->Set.map (f e) s1) s0)
--a -> {{},{a}}
makeSet :: (Ord a) => a -> Set (Set a)
makeSet = (Set.insert Set.empty) . Set.singleton.Set.singleton
powerSet :: (Ord a) => Set a -> Set (Set a)
powerSet = (Set.fold (slift Set.union) (Set.singleton Set.empty)) . Set.map makeSet

View file

@ -0,0 +1,2 @@
Prelude Data.Set> powerSet fromList [1,2,3]
fromList [fromList [], fromList [1], fromList [1,2], fromList [1,2,3], fromList [1,3], fromList [2], fromList [2,3], fromList [3]]

View file

@ -0,0 +1,38 @@
// version 1.1.3
class PowerSet<T>(val items: List<T>) {
private lateinit var combination: IntArray
init {
println("Power set of $items comprises:")
for (m in 0..items.size) {
combination = IntArray(m)
generate(0, m)
}
}
private fun generate(k: Int, m: Int) {
if (k >= m) {
println(combination.map { items[it] })
}
else {
for (j in 0 until items.size)
if (k == 0 || j > combination[k - 1]) {
combination[k] = j
generate(k + 1, m)
}
}
}
}
fun main(args: Array<String>) {
val itemsList = listOf(
listOf(1, 2, 3, 4),
emptyList<Int>(),
listOf(emptyList<Int>())
)
for (items in itemsList) {
PowerSet(items)
println()
}
}

View file

@ -0,0 +1,33 @@
sequence powerset
integer step = 1
function pst(object key, object /*data*/, object /*user_data*/)
integer k = 1
while k<length(powerset) do
k += step
for j=1 to step do
powerset[k] = append(powerset[k],key)
k += 1
end for
end while
step *= 2
return 1
end function
function power_set(integer d)
powerset = repeat({},power(2,dict_size(d)))
step = 1
traverse_dict(routine_id("pst"),0,d)
return powerset
end function
integer d1234 = new_dict()
setd(1,0,d1234)
setd(2,0,d1234)
setd(3,0,d1234)
setd(4,0,d1234)
?power_set(d1234)
integer d0 = new_dict()
?power_set(d0)
setd({},0,d0)
?power_set(d0)

View file

@ -1,5 +0,0 @@
(define (power_set_iter set)
(let loop ((res '(())) (s set))
(if (empty? s)
res
(loop (append (map (lambda (i) (cons (car s) i)) res) res) (cdr s)))))

View file

@ -1,32 +0,0 @@
'((e d c b a)
(e d c b)
(e d c a)
(e d c)
(e d b a)
(e d b)
(e d a)
(e d)
(e c b a)
(e c b)
(e c a)
(e c)
(e b a)
(e b)
(e a)
(e)
(d c b a)
(d c b)
(d c a)
(d c)
(d b a)
(d b)
(d a)
(d)
(c b a)
(c b)
(c a)
(c)
(b a)
(b)
(a)
())

View file

@ -0,0 +1,123 @@
SIMSET
BEGIN
LINK CLASS LOF_INT(N); INTEGER N;;
LINK CLASS LOF_LOF_INT(H); REF(HEAD) H;;
REF(HEAD) PROCEDURE MAP(P_LI, P_LLI);
REF(HEAD) P_LI;
REF(HEAD) P_LLI;
BEGIN
REF(HEAD) V_RESULT;
V_RESULT :- NEW HEAD;
IF NOT P_LLI.EMPTY THEN BEGIN
REF(LOF_LOF_INT) V_LLI;
V_LLI :- P_LLI.FIRST QUA LOF_LOF_INT;
WHILE V_LLI =/= NONE DO BEGIN
REF(HEAD) V_NEWLIST;
V_NEWLIST :- NEW HEAD;
! ADD THE SAME 1ST ELEMENT TO EVERY NEWLIST ;
NEW LOF_INT(P_LI.FIRST QUA LOF_INT.N).INTO(V_NEWLIST);
IF NOT V_LLI.H.EMPTY THEN BEGIN
REF(LOF_INT) V_LI;
V_LI :- V_LLI.H.FIRST QUA LOF_INT;
WHILE V_LI =/= NONE DO BEGIN
NEW LOF_INT(V_LI.N).INTO(V_NEWLIST);
V_LI :- V_LI.SUC;
END;
END;
NEW LOF_LOF_INT(V_NEWLIST).INTO(V_RESULT);
V_LLI :- V_LLI.SUC;
END;
END;
MAP :- V_RESULT;
END MAP;
REF(HEAD) PROCEDURE SUBSETS(P_LI);
REF(HEAD) P_LI;
BEGIN
REF(HEAD) V_RESULT;
IF P_LI.EMPTY THEN BEGIN
V_RESULT :- NEW HEAD;
NEW LOF_LOF_INT(NEW HEAD).INTO(V_RESULT);
END ELSE BEGIN
REF(HEAD) V_SUBSET, V_MAP;
REF(LOF_INT) V_LI;
V_SUBSET :- NEW HEAD;
V_LI :- P_LI.FIRST QUA LOF_INT;
! SKIP OVER 1ST ELEMENT ;
IF V_LI =/= NONE THEN V_LI :- V_LI.SUC;
WHILE V_LI =/= NONE DO BEGIN
NEW LOF_INT(V_LI.N).INTO(V_SUBSET);
V_LI :- V_LI.SUC;
END;
V_RESULT :- SUBSETS(V_SUBSET);
V_MAP :- MAP(P_LI, V_RESULT);
IF NOT V_MAP.EMPTY THEN BEGIN
REF(LOF_LOF_INT) V_LLI;
V_LLI :- V_MAP.FIRST QUA LOF_LOF_INT;
WHILE V_LLI =/= NONE DO BEGIN
NEW LOF_LOF_INT(V_LLI.H).INTO(V_RESULT);
V_LLI :- V_LLI.SUC;
END;
END;
END;
SUBSETS :- V_RESULT;
END SUBSETS;
PROCEDURE PRINT_LIST(P_LI); REF(HEAD) P_LI;
BEGIN
OUTTEXT("[");
IF NOT P_LI.EMPTY THEN BEGIN
INTEGER I;
REF(LOF_INT) V_LI;
I := 0;
V_LI :- P_LI.FIRST QUA LOF_INT;
WHILE V_LI =/= NONE DO BEGIN
IF I > 0 THEN OUTTEXT(",");
OUTINT(V_LI.N, 0);
V_LI :- V_LI.SUC;
I := I+1;
END;
END;
OUTTEXT("]");
END PRINT_LIST;
PROCEDURE PRINT_LIST_LIST(P_LLI); REF(HEAD) P_LLI;
BEGIN
OUTTEXT("[");
IF NOT P_LLI.EMPTY THEN BEGIN
INTEGER I;
REF(LOF_LOF_INT) V_LLI;
I := 0;
V_LLI :- P_LLI.FIRST QUA LOF_LOF_INT;
WHILE V_LLI =/= NONE DO BEGIN
IF I > 0 THEN BEGIN
OUTTEXT(",");
! OUTIMAGE;
END;
PRINT_LIST(V_LLI.H);
V_LLI :- V_LLI.SUC;
I := I+1;
END;
END;
OUTTEXT("]");
OUTIMAGE;
END PRINT_LIST_LIST;
INTEGER N;
REF(HEAD) V_RANGE;
REF(HEAD) V_LISTS;
V_RANGE :- NEW HEAD;
V_LISTS :- SUBSETS(V_RANGE);
PRINT_LIST_LIST(V_LISTS);
OUTIMAGE;
FOR N := 1 STEP 1 UNTIL 4 DO BEGIN
NEW LOF_INT(N).INTO(V_RANGE);
V_LISTS :- SUBSETS(V_RANGE);
PRINT_LIST_LIST(V_LISTS);
OUTIMAGE;
END;
END.

View file

@ -0,0 +1,4 @@
fcn pwerSet(list){
(0).pump(list.len(),List, Utils.Helpers.pickNFrom.fp1(list),
T(Void.Write,Void.Write) ) .append(list)
}