September Morn Update
This commit is contained in:
parent
4e2d22a71d
commit
aac6731f2c
6856 changed files with 141342 additions and 21127 deletions
180
Task/Power-set/ABAP/power-set.abap
Normal file
180
Task/Power-set/ABAP/power-set.abap
Normal file
|
|
@ -0,0 +1,180 @@
|
|||
report z_powerset.
|
||||
|
||||
interface set.
|
||||
methods:
|
||||
add_element
|
||||
importing
|
||||
element_to_be_added type any
|
||||
returning
|
||||
value(new_set) type ref to set,
|
||||
|
||||
remove_element
|
||||
importing
|
||||
element_to_be_removed type any
|
||||
returning
|
||||
value(new_set) type ref to set,
|
||||
|
||||
contains_element
|
||||
importing
|
||||
element_to_be_found type any
|
||||
returning
|
||||
value(contains) type abap_bool,
|
||||
|
||||
get_size
|
||||
returning
|
||||
value(size) type int4,
|
||||
|
||||
is_equal
|
||||
importing
|
||||
set_to_be_compared_with type ref to set
|
||||
returning
|
||||
value(equal) type abap_bool,
|
||||
|
||||
get_elements
|
||||
exporting
|
||||
elements type any table,
|
||||
|
||||
stringify
|
||||
returning
|
||||
value(stringified_set) type string.
|
||||
endinterface.
|
||||
|
||||
|
||||
class string_set definition.
|
||||
public section.
|
||||
interfaces:
|
||||
set.
|
||||
|
||||
|
||||
methods:
|
||||
constructor
|
||||
importing
|
||||
elements type stringtab optional,
|
||||
|
||||
build_powerset
|
||||
returning
|
||||
value(powerset) type ref to string_set.
|
||||
|
||||
|
||||
private section.
|
||||
data elements type stringtab.
|
||||
endclass.
|
||||
|
||||
|
||||
class string_set implementation.
|
||||
method constructor.
|
||||
loop at elements into data(element).
|
||||
me->set~add_element( element ).
|
||||
endloop.
|
||||
endmethod.
|
||||
|
||||
|
||||
method set~add_element.
|
||||
if not line_exists( me->elements[ table_line = element_to_be_added ] ).
|
||||
append element_to_be_added to me->elements.
|
||||
endif.
|
||||
|
||||
new_set = me.
|
||||
endmethod.
|
||||
|
||||
|
||||
method set~remove_element.
|
||||
if line_exists( me->elements[ table_line = element_to_be_removed ] ).
|
||||
delete me->elements where table_line = element_to_be_removed.
|
||||
endif.
|
||||
|
||||
new_set = me.
|
||||
endmethod.
|
||||
|
||||
|
||||
method set~contains_element.
|
||||
contains = cond abap_bool(
|
||||
when line_exists( me->elements[ table_line = element_to_be_found ] )
|
||||
then abap_true
|
||||
else abap_false ).
|
||||
endmethod.
|
||||
|
||||
|
||||
method set~get_size.
|
||||
size = lines( me->elements ).
|
||||
endmethod.
|
||||
|
||||
|
||||
method set~is_equal.
|
||||
if set_to_be_compared_with->get_size( ) ne me->set~get_size( ).
|
||||
equal = abap_false.
|
||||
|
||||
return.
|
||||
endif.
|
||||
|
||||
loop at me->elements into data(element).
|
||||
if not set_to_be_compared_with->contains_element( element ).
|
||||
equal = abap_false.
|
||||
|
||||
return.
|
||||
endif.
|
||||
endloop.
|
||||
|
||||
equal = abap_true.
|
||||
endmethod.
|
||||
|
||||
|
||||
method set~get_elements.
|
||||
elements = me->elements.
|
||||
endmethod.
|
||||
|
||||
|
||||
method set~stringify.
|
||||
stringified_set = cond string(
|
||||
when me->elements is initial
|
||||
then `∅`
|
||||
when me->elements eq value stringtab( ( `∅` ) )
|
||||
then `{ ∅ }`
|
||||
else reduce string(
|
||||
init result = `{ `
|
||||
for element in me->elements
|
||||
next result = cond string(
|
||||
when element eq ``
|
||||
then |{ result }∅, |
|
||||
when strlen( element ) eq 1 and element ne `∅`
|
||||
then |{ result }{ element }, |
|
||||
else |{ result }\{{ element }\}, | ) ) ).
|
||||
|
||||
stringified_set = replace(
|
||||
val = stringified_set
|
||||
regex = `, $`
|
||||
with = ` }`).
|
||||
endmethod.
|
||||
|
||||
|
||||
method build_powerset.
|
||||
data(powerset_elements) = value stringtab( ( `` ) ).
|
||||
|
||||
loop at me->elements into data(element).
|
||||
do lines( powerset_elements ) times.
|
||||
if powerset_elements[ sy-index ] ne `∅`.
|
||||
append |{ powerset_elements[ sy-index ] }{ element }| to powerset_elements.
|
||||
else.
|
||||
append element to powerset_elements.
|
||||
endif.
|
||||
enddo.
|
||||
endloop.
|
||||
|
||||
powerset = new string_set( powerset_elements ).
|
||||
endmethod.
|
||||
endclass.
|
||||
|
||||
|
||||
start-of-selection.
|
||||
data(set1) = new string_set( ).
|
||||
data(set2) = new string_set( ).
|
||||
data(set3) = new string_set( ).
|
||||
|
||||
write: |𝑷( { set1->set~stringify( ) } ) -> { set1->build_powerset( )->set~stringify( ) }|, /.
|
||||
|
||||
set2->set~add_element( `∅` ).
|
||||
write: |𝑷( { set2->set~stringify( ) } ) -> { set2->build_powerset( )->set~stringify( ) }|, /.
|
||||
|
||||
set3->set~add_element( `1` )->add_element( `2` )->add_element( `3` )->add_element( `3` )->add_element( `4`
|
||||
)->add_element( `4` )->add_element( `4` ).
|
||||
write: |𝑷( { set3->set~stringify( ) } ) -> { set3->build_powerset( )->set~stringify( ) }|, /.
|
||||
|
|
@ -2,27 +2,21 @@ 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;
|
||||
for set in 0..2**Argument_Count-1 loop
|
||||
Put ("{");
|
||||
declare
|
||||
k : natural := set;
|
||||
first : boolean := true;
|
||||
begin
|
||||
for i in 1..Argument_Count loop
|
||||
if k mod 2 = 1 then
|
||||
Put ((if first then "" else ",") & Argument (i));
|
||||
first := false;
|
||||
end if;
|
||||
k := k / 2; -- we go to the next bit of "set"
|
||||
end loop;
|
||||
end;
|
||||
Put_Line("}");
|
||||
end loop;
|
||||
end powerset;
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
|
||||
// powerset :: [a] -> [[a]]
|
||||
const powerset = xs =>
|
||||
xs.reduceRight((a, x) => a.concat(a.map(y => [x].concat(y))), [
|
||||
xs.reduceRight((a, x) => [...a, ...a.map(y => [x, ...y])], [
|
||||
[]
|
||||
]);
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import sets, hashes
|
||||
|
||||
proc hash(x): THash =
|
||||
proc hash(x: HashSet[int]): Hash =
|
||||
var h = 0
|
||||
for i in x: h = h !& hash(i)
|
||||
result = !$h
|
||||
|
|
|
|||
|
|
@ -1,29 +1,34 @@
|
|||
/*REXX program displays a power set; items may be anything (but can't have blanks).*/
|
||||
parse arg S /*allow the user specify optional set. */
|
||||
if S='' then S= 'one two three four' /*Not specified? Then use the default.*/
|
||||
@='{}' /*start process with a null power set. */
|
||||
N=words(S); do chunk=1 for N /*traipse through the items in the set.*/
|
||||
@=@ combN(N, chunk) /*take N items, a CHUNK at a time. */
|
||||
end /*chunk*/
|
||||
w=length(2**N) /*the number of items in the power set.*/
|
||||
do k=1 for words(@) /* [↓] show combinations, one per line*/
|
||||
say right(k, w) word(@, k) /*display a single combination to term.*/
|
||||
end /*k*/
|
||||
@= '{}' /*start process with a null power set. */
|
||||
N= words(S); do chunk=1 for N /*traipse through the items in the set.*/
|
||||
@=@ combN(N, chunk) /*take N items, a CHUNK at a time. */
|
||||
end /*chunk*/
|
||||
w= length(2**N) /*the number of items in the power set.*/
|
||||
do k=1 for words(@) /* [↓] show combinations, one per line*/
|
||||
say right(k, w) word(@, k) /*display a single combination to term.*/
|
||||
end /*k*/
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
combN: procedure expose S; parse arg x,y; base=x+1; bbase=base-y; !.=0
|
||||
do p=1 for y; !.p=p; end /*p*/
|
||||
combN: procedure expose S; parse arg x,y; base= x + 1; bbase= base - y
|
||||
!.= 0
|
||||
do p=1 for y; !.p= p
|
||||
end /*p*/
|
||||
$=
|
||||
do j=1; L=
|
||||
do d=1 for y; L=L','word(S, !.d)
|
||||
end /*d*/
|
||||
$=$ '{'strip(L, "L", ',')"}"
|
||||
!.y=!.y+1; if !.y==base then if .combU(y-1) then leave
|
||||
do j=1; L=
|
||||
do d=1 for y; L= L','word(S, !.d)
|
||||
end /*d*/
|
||||
|
||||
$=$ '{'strip(L, "L", ',')"}"; !.y= !.y + 1
|
||||
if !.y==base then if .combU(y - 1) then leave
|
||||
end /*j*/
|
||||
return strip($) /*return with a partial powerset chunk.*/
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
.combU: procedure expose !. y bbase; parse arg d; if d==0 then return 1; p=!.d
|
||||
do u=d to y; !.u=p+1; if !.u==bbase+u then return .combU(u-1)
|
||||
p=!.u
|
||||
.combU: procedure expose !. y bbase; parse arg d; if d==0 then return 1
|
||||
p= !.d
|
||||
do u=d to y; !.u= p + 1
|
||||
if !.u==bbase+u then return .combU(u-1)
|
||||
p= !.u
|
||||
end /*u*/
|
||||
return 0
|
||||
|
|
|
|||
46
Task/Power-set/VBA/power-set.vba
Normal file
46
Task/Power-set/VBA/power-set.vba
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
Option Base 1
|
||||
Private Function power_set(ByRef st As Collection) As Collection
|
||||
Dim subset As Collection, pwset As New Collection
|
||||
For i = 0 To 2 ^ st.Count - 1
|
||||
Set subset = New Collection
|
||||
For j = 1 To st.Count
|
||||
If i And 2 ^ (j - 1) Then subset.Add st(j)
|
||||
Next j
|
||||
pwset.Add subset
|
||||
Next i
|
||||
Set power_set = pwset
|
||||
End Function
|
||||
Private Function print_set(ByRef st As Collection) As String
|
||||
'assume st is a collection of collections, holding integer variables
|
||||
Dim s() As String, t() As String
|
||||
ReDim s(st.Count)
|
||||
'Debug.Print "{";
|
||||
For i = 1 To st.Count
|
||||
If st(i).Count > 0 Then
|
||||
ReDim t(st(i).Count)
|
||||
For j = 1 To st(i).Count
|
||||
Select Case TypeName(st(i)(j))
|
||||
Case "Integer": t(j) = CStr(st(i)(j))
|
||||
Case "Collection": t(j) = "{}" 'assumes empty
|
||||
End Select
|
||||
Next j
|
||||
s(i) = "{" & Join(t, ", ") & "}"
|
||||
Else
|
||||
s(i) = "{}"
|
||||
End If
|
||||
Next i
|
||||
print_set = "{" & Join(s, ", ") & "}"
|
||||
End Function
|
||||
Public Sub rc()
|
||||
Dim rcset As New Collection, result As Collection
|
||||
For i = 1 To 4
|
||||
rcset.Add i
|
||||
Next i
|
||||
Debug.Print print_set(power_set(rcset))
|
||||
Set rcset = New Collection
|
||||
Debug.Print print_set(power_set(rcset))
|
||||
Dim emptyset As New Collection
|
||||
rcset.Add emptyset
|
||||
Debug.Print print_set(power_set(rcset))
|
||||
Debug.Print
|
||||
End Sub
|
||||
Loading…
Add table
Add a link
Reference in a new issue