Order disjoint list items: Difference between revisions

→‎{{header|Ruby}}: Add version using sprintf syntax
(→‎{{header|Ruby}}: Add version using sprintf syntax)
Line 2,161:
A B B A | B A -> B A B A
</pre>
===sprintf version===
<lang ruby>ar = [
['the cat sat on the mat', 'mat cat'],
['the cat sat on the mat', 'cat mat'],
['A B C A B C A B C' , 'C A C A'],
['A B C A B D A B E' , 'E A D A'],
['A B' , 'B' ],
['A B' , 'B A' ],
['A B B A' , 'B A' ]
]
 
res = ar.map do|m,n|
mm = m.dup
nn = n.split
nn.each {|item| mm.sub!(item, "%s")} #sub! only subs the first match
mm % nn
end
 
puts res
</lang>
 
=={{header|Scala}}==
1,149

edits