Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -1,8 +1,8 @@
|
|||
import std.stdio, std.algorithm, std.array;
|
||||
|
||||
dchar[][] consolidate(dchar[][] sets) {
|
||||
dchar[][] consolidate(dchar[][] sets) @safe {
|
||||
foreach (set; sets)
|
||||
set.sort;
|
||||
set.sort();
|
||||
|
||||
foreach (i, ref si; sets[0 .. $ - 1]) {
|
||||
if (si.empty)
|
||||
|
|
@ -17,7 +17,7 @@ dchar[][] consolidate(dchar[][] sets) {
|
|||
return sets.filter!"!a.empty".array;
|
||||
}
|
||||
|
||||
void main() {
|
||||
void main() @safe {
|
||||
[['A', 'B'], ['C','D']].consolidate.writeln;
|
||||
|
||||
[['A','B'], ['B','D']].consolidate.writeln;
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
import std.stdio, std.algorithm, std.array;
|
||||
|
||||
dchar[][] consolidate(dchar[][] sets) {
|
||||
dchar[][] consolidate(dchar[][] sets) @safe {
|
||||
foreach (set; sets)
|
||||
set.sort;
|
||||
set.sort();
|
||||
|
||||
dchar[][] consolidateR(dchar[][] s) {
|
||||
if (s.length < 2)
|
||||
|
|
@ -20,7 +20,7 @@ dchar[][] consolidate(dchar[][] sets) {
|
|||
return consolidateR(sets);
|
||||
}
|
||||
|
||||
void main() {
|
||||
void main() @safe {
|
||||
[['A', 'B'], ['C','D']].consolidate.writeln;
|
||||
|
||||
[['A','B'], ['B','D']].consolidate.writeln;
|
||||
|
|
|
|||
|
|
@ -1,53 +1,53 @@
|
|||
/*REXX program shows how to consolidate a sample bunch of sets. */
|
||||
sets.= /*assign all SETS. to null. */
|
||||
/*REXX program demonstrates how to consolidate a sample bunch of sets.*/
|
||||
sets. = /*assign all SETS. to null.*/
|
||||
sets.1 = '{A,B} {C,D}'
|
||||
sets.2 = "{A,B} {B,D}"
|
||||
sets.3 = '{A,B} {C,D} {D,B}'
|
||||
sets.4 = '{H,I,K} {A,B} {C,D} {D,B} {F,G,H}'
|
||||
sets.5 = '{snow,ice,slush,frost,fog} {iceburgs,icecubes} {rain,fog,sleet}'
|
||||
|
||||
do j=1 while sets.j\=='' /*traipse through the sample sets*/
|
||||
do j=1 while sets.j\=='' /*traipse through the sample sets*/
|
||||
call SETcombo sets.j /*have the other guy do the work.*/
|
||||
end /*j*/
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────SETcombo subroutine─────────────────*/
|
||||
SETcombo: procedure; parse arg bunch; n=words(bunch); newBunch=
|
||||
/*──────────────────────────────────SETCOMBO subroutine─────────────────*/
|
||||
SETcombo: procedure; parse arg bunch; n=words(bunch); newBunch=
|
||||
say ' the old sets=' space(bunch)
|
||||
|
||||
do k=1 for n /*change all commas to a blank. */
|
||||
do k=1 for n /* [↓] change commas to a blank.*/
|
||||
@.k=translate(word(bunch,k),,'},{') /*create a list of words (=a set)*/
|
||||
end /*k*/ /*... and also remove the braces.*/
|
||||
end /*k*/ /*··· and also remove the braces.*/
|
||||
|
||||
do until \changed; changed=0 /*consolidate some sets (maybe).*/
|
||||
do set=1 for n-1
|
||||
do item=1 for words(@.set); x=word(@.set,item)
|
||||
do other=set+1 to n
|
||||
if isIn(x,@.other) then do; changed=1
|
||||
@.set=@.set @.other; @.other=
|
||||
iterate set
|
||||
end
|
||||
do until \changed; changed=0 /*consolidate some sets (maybe).*/
|
||||
do set=1 for n-1
|
||||
do item=1 for words(@.set); x=word(@.set,item)
|
||||
do other=set+1 to n
|
||||
if isIn(x,@.other) then do; changed=1 /*has changed.*/
|
||||
@.set=@.set @.other; @.other=
|
||||
iterate set
|
||||
end
|
||||
end /*other*/
|
||||
end /*item*/
|
||||
end /*set*/
|
||||
end /*until ¬changed*/
|
||||
|
||||
do set=1 for n; new= /*remove duplicates in a set. */
|
||||
do items=1 for words(@.set)
|
||||
x=word(@.set,items); if x==',' then iterate; if x=='' then leave
|
||||
do set=1 for n; new= /*remove duplicates in a set. */
|
||||
do items=1 for words(@.set); x=word(@.set, items)
|
||||
if x==',' then iterate; if x=='' then leave
|
||||
new=new x /*start building the new set. */
|
||||
do forever; if \isIn(x,@.set) then leave
|
||||
_=wordpos(x,@.set)
|
||||
do until \isIn(x, @.set)
|
||||
_=wordpos(x, @.set)
|
||||
@.set=subword(@.set,1,_-1) ',' subword(@.set,_+1) /*purify set.*/
|
||||
end /*forever*/
|
||||
end /*until ¬isIn*/
|
||||
end /*items*/
|
||||
@.set=translate(strip(new),','," ")
|
||||
@.set=translate(strip(new), ',', " ")
|
||||
end /*set*/
|
||||
|
||||
do new=1 for n; if @.new=='' then iterate
|
||||
do new=1 for n; if @.new=='' then iterate
|
||||
newBunch=space(newbunch '{'@.new"}")
|
||||
end /*new*/
|
||||
|
||||
say ' the new sets=' newBunch; say
|
||||
return
|
||||
/*──────────────────────────────────isIn subroutine─────────────────────*/
|
||||
isIn: return wordpos(arg(1),arg(2))\==0 /*is (word) arg1 in set arg2? */
|
||||
/*──────────────────────────────────ISIN subroutine─────────────────────*/
|
||||
isIn: return wordpos(arg(1), arg(2))\==0 /*is (word) arg1 in set arg2? */
|
||||
|
|
|
|||
|
|
@ -1,17 +1,13 @@
|
|||
require 'set'
|
||||
|
||||
tests = [[['A', 'B'], ['C','D']],
|
||||
[['A','B'], ['B','D']],
|
||||
[['A','B'], ['C','D'], ['D','B']],
|
||||
[['H','I','K'], ['A','B'], ['C','D'], ['D','B'], ['F','G','H']]]
|
||||
tests = tests.map{|sets| sets.map(&:to_set)}
|
||||
tests = [[[:A,:B], [:C,:D]],
|
||||
[[:A,:B], [:B,:D]],
|
||||
[[:A,:B], [:C,:D], [:D,:B]],
|
||||
[[:H,:I,:K], [:A,:B], [:C,:D], [:D,:B], [:F,:G,:H]]]
|
||||
tests.map!{|sets| sets.map(&:to_set)}
|
||||
|
||||
tests.map do |sets|
|
||||
loop until sets.combination(2).none? do |a,b|
|
||||
if a.intersect?(b) then
|
||||
a.merge(b)
|
||||
sets.delete(b)
|
||||
end
|
||||
tests.each do |sets|
|
||||
until sets.combination(2).none?{|a,b| a.merge(b) && sets.delete(b) if a.intersect?(b)}
|
||||
end
|
||||
p sets
|
||||
end
|
||||
|
|
|
|||
27
Task/Set-consolidation/Scala/set-consolidation.scala
Normal file
27
Task/Set-consolidation/Scala/set-consolidation.scala
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
object SetConsolidation extends App {
|
||||
def consolidate[Type](sets: Set[Set[Type]]): Set[Set[Type]] = {
|
||||
var result = sets // each iteration combines two sets and reiterates, else returns
|
||||
for (i <- sets; j <- sets - i; k = i.intersect(j);
|
||||
if result == sets && k.nonEmpty) result = result - i - j + i.union(j)
|
||||
if (result == sets) sets else consolidate(result)
|
||||
}
|
||||
|
||||
// Tests:
|
||||
def parse(s: String) =
|
||||
s.split(",").map(_.split("").toSet).toSet
|
||||
def pretty[Type](sets: Set[Set[Type]]) =
|
||||
sets.map(_.mkString("{",",","}")).mkString(" ")
|
||||
val tests = List(
|
||||
parse("AB,CD") -> Set(Set("A", "B"), Set("C", "D")),
|
||||
parse("AB,BD") -> Set(Set("A", "B", "D")),
|
||||
parse("AB,CD,DB") -> Set(Set("A", "B", "C", "D")),
|
||||
parse("HIK,AB,CD,DB,FGH") -> Set(Set("A", "B", "C", "D"), Set("F", "G", "H", "I", "K"))
|
||||
)
|
||||
require(Set("A", "B", "C", "D") == Set("B", "C", "A", "D"))
|
||||
assert(tests.forall{case (test, expect) =>
|
||||
val result = consolidate(test)
|
||||
println(s"${pretty(test)} -> ${pretty(result)}")
|
||||
expect == result
|
||||
})
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue