tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
48
Task/Power-set/Ruby/power-set.rb
Normal file
48
Task/Power-set/Ruby/power-set.rb
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
# Based on http://johncarrino.net/blog/2006/08/11/powerset-in-ruby/
|
||||
# See the link if you want a shorter version. This was intended to show the reader how the method works.
|
||||
class Array
|
||||
# Adds a power_set method to every array, i.e.: [1, 2].power_set
|
||||
def power_set
|
||||
|
||||
# Injects into a blank array of arrays.
|
||||
# acc is what we're injecting into
|
||||
# you is each element of the array
|
||||
inject([[]]) do |acc, you|
|
||||
|
||||
# Set up a new array to add into
|
||||
ret = []
|
||||
|
||||
# For each array in the injected array,
|
||||
acc.each do |i|
|
||||
|
||||
# Add itself into the new array
|
||||
ret << i
|
||||
|
||||
# Merge the array with a new array of the current element
|
||||
ret << i + [you]
|
||||
end
|
||||
|
||||
# Return the array we're looking at to inject more.
|
||||
ret
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
# A more functional and even clearer variant.
|
||||
def func_power_set
|
||||
inject([[]]) { |ps,item| # for each item in the Array
|
||||
ps + # take the powerset up to now and add
|
||||
ps.map { |e| e + [item] } # it again, with the item appended to each element
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
#A direct translation of the "power array" version above
|
||||
class Set
|
||||
def powerset
|
||||
inject(Set[Set[]]) do |ps, item|
|
||||
ps.union ps.map {|e| e.union (Set.new [item])}
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue