Set consolidation: Difference between revisions

→‎{{header|Ruby}}: change data(String->Symbol), correction of the control structure.
m (→‎{{header|jq}}: simplify)
(→‎{{header|Ruby}}: change data(String->Symbol), correction of the control structure.)
Line 1,448:
<lang ruby>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.mapeach do |sets|
loop until sets.combination(2).none? do {|a,b| a.merge(b) && sets.delete(b) if a.intersect?(b)}
if a.intersect?(b) then
a.merge(b)
sets.delete(b)
end
end
p sets
end</lang>
{{out}}
</lang>
{{Output}}
<pre>
[#<Set: {":A", ":B"}>, #<Set: {":C", ":D"}>]
[#<Set: {":A", ":B", ":D"}>]
[#<Set: {":A", ":B", ":D", ":C"}>]
[#<Set: {":H", ":I", ":K", ":F", ":G"}>, #<Set: {":A", ":B", ":D", ":C"}>]
</pre>
Note: After execution, the contents of tests are exchanged.
 
=={{header|Scala}}==
<lang Scala>object SetConsolidation extends App {
Anonymous user