Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
18
Task/Set-consolidation/Julia/set-consolidation-1.julia
Normal file
18
Task/Set-consolidation/Julia/set-consolidation-1.julia
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
function consolidate{T}(a::Array{Set{T},1})
|
||||
1 < length(a) || return a
|
||||
b = copy(a)
|
||||
c = Set{T}[]
|
||||
while 1 < length(b)
|
||||
x = shift!(b)
|
||||
cme = true
|
||||
for (i, y) in enumerate(b)
|
||||
!isempty(intersect(x, y)) || continue
|
||||
cme = false
|
||||
b[i] = union(x, y)
|
||||
break
|
||||
end
|
||||
!cme || push!(c, x)
|
||||
end
|
||||
push!(c, b[1])
|
||||
return c
|
||||
end
|
||||
17
Task/Set-consolidation/Julia/set-consolidation-2.julia
Normal file
17
Task/Set-consolidation/Julia/set-consolidation-2.julia
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
p = Set(["A", "B"])
|
||||
q = Set(["C", "D"])
|
||||
r = Set(["B", "D"])
|
||||
s = Set(["H", "I", "K"])
|
||||
t = Set(["F", "G", "H"])
|
||||
|
||||
println("p = ", p)
|
||||
println("q = ", q)
|
||||
println("r = ", r)
|
||||
println("s = ", s)
|
||||
println("t = ", t)
|
||||
|
||||
println("consolidate([p, q]) =\n ", consolidate([p, q]))
|
||||
println("consolidate([p, r]) =\n ", consolidate([p, r]))
|
||||
println("consolidate([p, q, r]) =\n ", consolidate([p, q, r]))
|
||||
println("consolidate([p, q, r, s, t]) =\n ",
|
||||
consolidate([p, q, r, s, t]))
|
||||
|
|
@ -1,53 +1,52 @@
|
|||
/*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}'
|
||||
/*REXX program demonstrates a method of consolidating some sample sets. */
|
||||
@.=; @.1 = '{A,B} {C,D}'
|
||||
@.2 = "{A,B} {B,D}"
|
||||
@.3 = '{A,B} {C,D} {D,B}'
|
||||
@.4 = '{H,I,K} {A,B} {C,D} {D,B} {F,G,H}'
|
||||
@.5 = '{snow,ice,slush,frost,fog} {icebergs,icecubes} {rain,fog,sleet}'
|
||||
|
||||
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=
|
||||
say ' the old sets=' space(bunch)
|
||||
do j=1 while @.j\=='' /*traipse through each of sample sets. */
|
||||
call SETconsolidate @.j /*have the function do the heavy work. */
|
||||
end /*j*/
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────ISIN SUBRoutine───────────────────────────*/
|
||||
isIn: return wordpos(arg(1), arg(2))\==0 /*is (word) arg1 in set arg2 ? */
|
||||
/*──────────────────────────────────SETCONSOLIDATE subroutine─────────────────*/
|
||||
SETconsolidate: procedure; parse arg old,new; #=words(old) /*nullify NEW.*/
|
||||
say ' the old set=' space(old)
|
||||
|
||||
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.*/
|
||||
do k=1 for # /* [↓] change all commas to a blank. */
|
||||
!.k=translate(word(old,k), , '},{') /*create a list of words (aka, a set).*/
|
||||
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 /*has changed.*/
|
||||
@.set=@.set @.other; @.other=
|
||||
do until \changed; changed=0 /*consolidate some sets (well, maybe).*/
|
||||
do set=1 for #-1
|
||||
do item=1 for words(!.set); x=word(!.set,item)
|
||||
do other=set+1 to #
|
||||
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)
|
||||
end /*item */
|
||||
end /*set */
|
||||
end /*until*/
|
||||
/* ╔╦══════════════════════════════════════════════════elide any duplicates.*/
|
||||
do set=1 for #; $= /*nullify $ */
|
||||
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 until \isIn(x, @.set)
|
||||
_=wordpos(x, @.set)
|
||||
@.set=subword(@.set,1,_-1) ',' subword(@.set,_+1) /*purify set.*/
|
||||
end /*until ¬isIn*/
|
||||
end /*items*/
|
||||
@.set=translate(strip(new), ',', " ")
|
||||
end /*set*/
|
||||
$=$ x /*build new. */
|
||||
do until \isIn(x, !.set)
|
||||
_=wordpos(x, !.set)
|
||||
!.set=subword(!.set,1,_-1) ',' subword(!.set,_+1) /*purify set.*/
|
||||
end /*until ¬isIn ··· */
|
||||
end /*items*/
|
||||
!.set=translate(strip($), ',', " ")
|
||||
end /*set*/
|
||||
|
||||
do new=1 for n; if @.new=='' then iterate
|
||||
newBunch=space(newbunch '{'@.new"}")
|
||||
end /*new*/
|
||||
do i=1 for #; if !.i=='' then iterate
|
||||
new=space(new '{'!.i"}")
|
||||
end /*i*/
|
||||
|
||||
say ' the new sets=' newBunch; say
|
||||
say ' the new set=' new; say
|
||||
return
|
||||
/*──────────────────────────────────ISIN subroutine─────────────────────*/
|
||||
isIn: return wordpos(arg(1), arg(2))\==0 /*is (word) arg1 in set arg2? */
|
||||
|
|
|
|||
33
Task/Set-consolidation/VBScript/set-consolidation.vb
Normal file
33
Task/Set-consolidation/VBScript/set-consolidation.vb
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
Function consolidate(s)
|
||||
sets = Split(s,",")
|
||||
n = UBound(sets)
|
||||
For i = 1 To n
|
||||
p = i
|
||||
ts = ""
|
||||
For j = i To 1 Step -1
|
||||
If ts = "" Then
|
||||
p = j
|
||||
End If
|
||||
ts = ""
|
||||
For k = 1 To Len(sets(p))
|
||||
If InStr(1,sets(j-1),Mid(sets(p),k,1)) = 0 Then
|
||||
ts = ts & Mid(sets(p),k,1)
|
||||
End If
|
||||
Next
|
||||
If Len(ts) < Len(sets(p)) Then
|
||||
sets(j-1) = sets(j-1) & ts
|
||||
sets(p) = "-"
|
||||
ts = ""
|
||||
Else
|
||||
p = i
|
||||
End If
|
||||
Next
|
||||
Next
|
||||
consolidate = s & " = " & Join(sets," , ")
|
||||
End Function
|
||||
|
||||
'testing
|
||||
test = Array("AB","AB,CD","AB,CD,DB","HIK,AB,CD,DB,FGH")
|
||||
For Each t In test
|
||||
WScript.StdOut.WriteLine consolidate(t)
|
||||
Next
|
||||
Loading…
Add table
Add a link
Reference in a new issue