Iterating hash of arrays to create an array of array - ruby

Suppose I have a hash
#attribute_type = {
typeA: ['a', 'b', 'c'],
typeB: ['1', '2', '3'],
typeC: ['9', '8', '7']
}
I want to iterate over the values so I can create an array having all distinct possible combinations of the three arrays, for example:
['a', '1', '9'], ['a', '1', '8'], ['a', '1', '7'], ['a', '2', '9'], ...
Is this possible?

h = { :typeA=>['a','b','c'], :typeB=>['1','2','3'], :typeC=>['9','8','7'] }
first, *rest = h.values
#=> [["a", "b", "c"], ["1", "2", "3"], ["9", "8", "7"]]
first.product(*rest)
#=> [["a", "1", "9"], ["a", "1", "8"], ["a", "1", "7"],
# ["a", "2", "9"], ["a", "2", "8"], ["a", "2", "7"],
# ["a", "3", "9"], ["a", "3", "8"], ["a", "3", "7"],
# ["b", "1", "9"], ["b", "1", "8"], ["b", "1", "7"],
# ["b", "2", "9"], ["b", "2", "8"], ["b", "2", "7"],
# ["b", "3", "9"], ["b", "3", "8"], ["b", "3", "7"],
# ["c", "1", "9"], ["c", "1", "8"], ["c", "1", "7"],
# ["c", "2", "9"], ["c", "2", "8"], ["c", "2", "7"],
# ["c", "3", "9"], ["c", "3", "8"], ["c", "3", "7"]]
See Array#product.

My 2 cents.
Pretty the same as Cary Swoveland, but just one line:
h.values.first.product(*h.values.drop(1))

Related

palindrome partition ruby no output

Hi I'm doing the palindrome partition problem using recursion. This problem is return all possible palindrome partitions of a given string input.
Input: "aab"Output: [["aa", "b"], ["a", "a", "b"]]
A palindrome partition definition: given a string S, a partition is a set of substrings, each containing one or more characters, such that every substring is a palindrome
My code is below. The issue I'm having is that the result array never gets correctly populated. From a high level I feel like my logic makes sense, but when I try to debug it I'm not really sure what is going on.
def partition(string)
result = []
output = []
dfs(string, 0, output, result)
result
end
def dfs(string, start, output, result)
if start == string.length
result << output
return
end
(start..string.length-1).to_a.each do |i|
if is_palindrome(string, start, i)
output << string[start..(i-start+1)]
dfs(string, i+1, output, result)
output.pop
end
end
end
def is_palindrome(string, start, end_value)
result = true
while start < end_value do
result = false if string[start] != string[end_value]
start += 1
end_value -= 1
end
result
end
puts partition("aab")
Yes, you do want to use recursion. I haven't analyzed your code carefully, but I see one problem is the following in the method dfs:
if start == string.length
result << output
return
end
If the if condition is satisfied, return without an argument will return nil. Perhaps you want return result.
Here is a relatively compact, Ruby-like way of writing it.
def pps(str)
return [[]] if str.empty?
(1..str.size).each_with_object([]) do |i,a|
s = str[0,i]
next unless is_pal?(s)
pps(str[i..-1]).each { |b| a << [s, *b] }
end
end
def is_pal?(str)
str == str.reverse
end
pps "aab"
#=> [["a", "a", "b"],
# ["aa", "b"]]
pps "aabbaa"
#=> [["a", "a", "b", "b", "a", "a"],
# ["a", "a", "b", "b", "aa"],
# ["a", "a", "bb", "a", "a"],
# ["a", "a", "bb", "aa"],
# ["a", "abba", "a"],
# ["aa", "b", "b", "a", "a"],
# ["aa", "b", "b", "aa"],
# ["aa", "bb", "a", "a"],
# ["aa", "bb", "aa"],
# ["aabbaa"]]
pps "aabbbxaa"
#=> [["a", "a", "b", "b", "b", "x", "a", "a"],
# ["a", "a", "b", "b", "b", "x", "aa"],
# ["a", "a", "b", "bb", "x", "a", "a"],
# ["a", "a", "b", "bb", "x", "aa"],
# ["a", "a", "bb", "b", "x", "a", "a"],
# ["a", "a", "bb", "b", "x", "aa"],
# ["a", "a", "bbb", "x", "a", "a"],
# ["a", "a", "bbb", "x", "aa"],
# ["aa", "b", "b", "b", "x", "a", "a"],
# ["aa", "b", "b", "b", "x", "aa"],
# ["aa", "b", "bb", "x", "a", "a"],
# ["aa", "b", "bb", "x", "aa"],
# ["aa", "bb", "b", "x", "a", "a"],
# ["aa", "bb", "b", "x", "aa"],
# ["aa", "bbb", "x", "a", "a"],
# ["aa", "bbb", "x", "aa"]]
pps "abcdefghijklmnopqrstuvwxyz"
#=> [["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
# "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]]
The best way of understanding how this recursion works is add some puts statements and re-run it.
def pps(str)
puts "\nstr=#{str}"
return [[]] if str.empty?
rv = (1..str.size).each_with_object([]) do |i,a|
s = str[0,i]
puts "i=#{i}, a=#{a}, s=#{s}, is_pal?(s)=#{is_pal?(s)}"
next unless is_pal?(s)
pps(str[i..-1]).each { |b| puts "b=#{b}, [s,*b]=#{[s,*b]}"; a << [s, *b] }
puts "a after calling pps=#{a}"
end
puts "rv=#{rv}"
rv
end
pps "aab"
str=aab
i=1, a=[], s=a, is_pal?(s)=true
str=ab
i=1, a=[], s=a, is_pal?(s)=true
str=b
i=1, a=[], s=b, is_pal?(s)=true
str=
b=[], [s,*b]=["b"]
a after calling pps=[["b"]]
rv=[["b"]]
b=["b"], [s,*b]=["a", "b"]
a after calling pps=[["a", "b"]]
i=2, a=[["a", "b"]], s=ab, is_pal?(s)=false
rv=[["a", "b"]]
b=["a", "b"], [s,*b]=["a", "a", "b"]
a after calling pps=[["a", "a", "b"]]
i=2, a=[["a", "a", "b"]], s=aa, is_pal?(s)=true
str=b
i=1, a=[], s=b, is_pal?(s)=true
str=
b=[], [s,*b]=["b"]
a after calling pps=[["b"]]
rv=[["b"]]
b=["b"], [s,*b]=["aa", "b"]
a after calling pps=[["a", "a", "b"], ["aa", "b"]]
i=3, a=[["a", "a", "b"], ["aa", "b"]], s=aab, is_pal?(s)=false
rv=[["a", "a", "b"], ["aa", "b"]]
#=> [["a", "a", "b"], ["aa", "b"]]

How to convert the following into array or hash?

a=b&c=d&e=f&g=h
How to extract this into [a,b,c,d,e,f,g,h]
I know I can use split, but it looks like it can only use only one delimit.
or how to convert into a hash?
split FTW (i.e. the most straightforward, simple way of doing this is):
irb(main):001:0> s = "a=b&c=d&e=f&g=h"
=> "a=b&c=d&e=f&g=h"
irb(main):002:0> s.split(/[=&]/)
=> ["a", "b", "c", "d", "e", "f", "g", "h"]
Other interesting ways of abusing Ruby:
irb(main):001:0> s = "a=b&c=d&e=f&g=h"
=> "a=b&c=d&e=f&g=h"
irb(main):002:0> s.split('=').collect{|x| x.split('&')}.flatten
=> ["a", "b", "c", "d", "e", "f", "g", "h"]
irb(main):003:0> ['=','&'].inject(s) {|t, n| t.split(n).join()}.split('')
=> ["a", "b", "c", "d", "e", "f", "g", "h"]
Also check Cary's and GamesBrainiac's answers for more alternatives :)
You can make a hash very easily with something like this:
myHash = {}
strSplit = "a=b&c=d&e=f&g=h".split("&")
for pair in strSplit
keyValueSplit = pair.split("=")
myHash[keyValueSplit[0]] = keyValueSplit[1]
end
myHash will look like this in the end {"a"=>"b", "c"=>"d", "e"=>"f", "g"=>"h"}
#Mirea's answer is best, but here's another:
s = "a=b&c=d&e=f&g=h"
s.scan /[a-z]/
#=> ["a", "b", "c", "d", "e", "f", "g", "h"]
The regex could of course be adjusted as required. For example:
"123a=b&c=d&E=f&g=h".scan /[A-Za-z0-9]/
#=> ["1", "2", "3", "a", "b", "c", "d", "E", "f", "g", "h"]
or
"1-2-3a=$b&c=d&e=f&g=h".scan /[^=&]/
#=> ["1", "-", "2", "-", "3", "a", "$", "b", "c", "d", "e", "f", "g", "h"]
and so on.
If strings of characters are desired just append + to the character class:
"123a=b&ccc=d&E=f&gg=h".scan /[A-Za-z0-9]+/
#=> ["123a", "b", "ccc", "d", "E", "f", "gg", "h"]
If the string has the alternating form shown in the example, these work as well:
(0..s.size).step(2).map { |i| s[i] }
#=> ["a", "b", "c", "d", "e", "f", "g", "h"]
s.each_char.each_slice(2).map(&:first)
#=> ["a", "b", "c", "d", "e", "f", "g", "h"]
I would use is gsub.
irb(main):001:0> s = "a=b&c=d&e=f&g=h"
=> "a=b&c=d&e=f&g=h"
irb(main):004:0> s.gsub(/[\=\&]/, " ").split()
=> ["a", "b", "c", "d", "e", "f", "g", "h"]
So, what we're doing here is replacing all occurrences of = and & with a single space. We then simply split the string.

Using each_slice to split input starting from end of string?

I would like to split a string into array groups of three as shown in desired output. Using Array#each_slice like this 1_223_213_213.to_s.split('').each_slice(3){|arr| p arr }
Current output: Desired output
# ["1", "2", "2"] # ["0", "0", "1"]
# ["3", "2", "1"] # ["2", "2", "3"]
# ["3", "2", "1"] # ["2", "1", "3"]
# ["3"] # ["2", "1", "3"]
Must work with numbers from (0..trillion). I posted my solution as an answer below. Hoping you all can give me some suggestion(s) to optimize or alternative implements?
Try left-padding with zeros until the string length is an even multiple of your "slice" target:
def slice_with_padding(s, n=3, &block)
s = "0#{s}" while s.to_s.size % n != 0
s.to_s.chars.each_slice(n, &block)
end
slice_with_padding(1_223_213_213) { |x| puts x.inspect }
# ["0", "0", "1"]
# ["2", "2", "3"]
# ["2", "1", "3"]
# ["2", "1", "3"]
slice_with_padding(12_345, 4) { |x| puts x.inspect }
# ["0", "0", "0", "1"]
# ["2", "3", "4", "5"]
You might find this a little more pleasing to your eye:
def slice_by_3(n)
n = n.to_s
l = n.length
[*n.rjust(l % 3 == 0 ? l : l + 3 - l % 3, '0').chars.each_slice(3)]
end
slice_by_3 2_123_456_544_545_355
=> [["0", "0", "2"],
["1", "2", "3"],
["4", "5", "6"],
["5", "4", "4"],
["5", "4", "5"],
["3", "5", "5"]]
Alternatively, if you want a more general solution:
def slice_by_n(num, n=3)
num = num.to_s
l = num.length
[*num.rjust(l % n == 0 ? l : l + n - l % n, '0').chars.each_slice(n)]
end
Here is a possible solution for the problem:
def slice_by_3 number
initial_number = number.to_s.split('').size
number = "00#{number}" if initial_number == 1
modulus = number.to_s.split(/.{3}/).size
padleft = '0' * ( (modulus*3) % number.to_s.split('').size )
("#{padleft}#{number}").split('').each_slice(3){|arr| p arr }
end
slice_by_3 2_123_456_544_545_355
# ["0", "0", "2"]
# ["1", "2", "3"]
# ["4", "5", "6"]
# ["5", "4", "4"]
# ["5", "4", "5"]
# ["3", "5", "5"]
Just seems somewhat complex and I want to believe there is a better way. I appreciate your feedback.
def slice_by_3 number
"000#{number}".split('').reverse
.each_slice(3).to_a[0..-2].reverse
.each { |arr| p arr.reverse }
end
slice_by_3 13_456_544_545_355
# ["0", "1", "3"]
# ["4", "5", "6"]
# ["5", "4", "4"]
# ["5", "4", "5"]
# ["3", "5", "5"]
This code reverses the whole array after adding 3 zeroes to the number start. each_slice(3) then slices to the proper groups (although reversed) plus one which consists of either ["0","0","0"], ["0","0"] or ["0"] depending on the original length of the number.
[0..-2] cuts the last group of zeroes. Then the groups are reversed back, and each group is printed (reversed back).
Here are a couple methods
n = 1_223_213_213.to_s
n.rjust(n.size + n.size % 3,"0").chars.each_slice(3).to_a
OR
n.rjust(15,"0").chars.each_slice(3).drop_while{|a| a.join == "000"}
15 is because you stated the max was a trillion obviously this number means very little as it rejects all results that contain all zeros so any number greater than 15 that is divisible by 3 will work for your example
Another way:
def split_nbr(n)
str = n.to_s
len = str.size
str.rjust(len + (3-len%3)%3, '0').scan(/.../).map(&:chars)
end
split_nbr( 1_223_213_213)
#=> [["0", "0", "1"], ["2", "2", "3"], ["2", "1", "3"], ["2", "1", "3"]]
split_nbr( 11_223_213_213)
#=> [["0", "1", "1"], ["2", "2", "3"], ["2", "1", "3"], ["2", "1", "3"]]
split_nbr(111_223_213_213)
#=> [["1", "1", "1"], ["2", "2", "3"], ["2", "1", "3"], ["2", "1", "3"]]

Partial cartesian product (ensure one value in each set)

Given a set of items [z,a,b,c] I want to find the "cartesian power" (cartesian product with itself n times) but only those results that have a z in them. For example:
normal_values = ["a","b","c"]
p limited_cartesian( normal_values, "z", 2 )
#=> [
#=> ["z", "z"]
#=> ["z", "a"]
#=> ["z", "b"]
#=> ["z", "c"]
#=> ["a", "z"]
#=> ["b", "z"]
#=> ["c", "z"]
#=> ]
I can do this by spinning through the full set and skipping entries that don't have the special value, but I'm wondering if there's a simpler way. Preferably one that allows me to only evaluate the desired entries lazily, without wasting time calculating unwanted entries.
def limited_cartesian( values, special, power )
[special, *values].repeated_permutation(power)
.select{ |prod| prod.include?( special ) }
end
Edit: With v3.0 I finally have something respectable. As is oft the case, the key was looking at the problem the right way. It occurred to me that I could repeatedly permute normal_values << special, power - 1 times, then for each of those permutations, there would be one more element to add. If the permutation contained at least one special, any element of normal_values << special could be added; otherwise, special must be added.
def limited_cartesian( values, special, power )
all_vals = values + [special]
all_vals.repeated_permutation(power-1).map do |p|
if p.include?(special)
*all_vals.each_with_object([]) { |v,a| a << (p + [v]) }
else
p + [special]
end
end
end
limited_cartesian( values, 'z', 1 )
# [["z"]]
limited_cartesian( values, 'z', 2 )
# => [["a", "z"], ["b", "z"], ["c", "z"],
# ["z", "a"], ["z", "b"], ["z", "c"],
# ["z", "z"]]
limited_cartesian( values, 'z', 3 )
# => [["a", "a", "z"], ["a", "b", "z"], ["a", "c", "z"],
# ["a", "z", "a"], ["a", "z", "b"], ["a", "z", "c"],
# ["a", "z", "z"], ["b", "a", "z"], ["b", "b", "z"],
# ["b", "c", "z"], ["b", "z", "a"], ["b", "z", "b"],
# ["b", "z", "c"], ["b", "z", "z"], ["c", "a", "z"],
# ["c", "b", "z"], ["c", "c", "z"], ["c", "z", "a"],
# ["c", "z", "b"], ["c", "z", "c"], ["c", "z", "z"],
# ["z", "a", "a"], ["z", "a", "b"], ["z", "a", "c"],
# ["z", "a", "z"], ["z", "b", "a"], ["z", "b", "b"],
# ["z", "b", "c"], ["z", "b", "z"], ["z", "c", "a"],
# ["z", "c", "b"], ["z", "c", "c"], ["z", "c", "z"],
# ["z", "z", "a"], ["z", "z", "b"], ["z", "z", "c"],
# ["z", "z", "z"]]
This is my v2.1, which works, but is not pretty. I'll leave it for the record.
def limited_cartesian( values, special, power )
ndx = Array(0...power)
ndx[1..-1].each_with_object( [[special]*power] ) do |i,a|
ndx.combination(i).to_a.product(values.repeated_permutation(power-i).to_a)
.each { |pos, val| a << stuff_special(special, pos, val.dup) }
end
end
def stuff_special( special, pos, vals )
pos.each_with_object(Array.new(pos.size + vals.size)) {|j,r|
r[j] = special }.map {|e| e.nil? ? vals.shift : e }
end
# e.g., stuff_special( 'z', [1,4], ["a","b","c"]) => ["a","z","b","c","z"]

ruby array custom combinations

What is a good way to get from this:
['a','b','c',['d1','d2']]
to this:
[['a','b','c','d1']['a','b','c','d2']]
another example, from this:
[['a1','a2'],'b',['c1','c2']]
to this:
[['a1','b','c1'],['a1','b','c2'],['a2','b','c1'],['a2','b','c2']]
edit 1:
Sorry for the confusion and thanks for response so far, the individual contents of the array items doesn't matter but the order must be preserved. The method needs to work for both example because the nested array can be in any position of the outer array, and the nested array can have more the 2 elements.
It's sort of like a regex with multiple or conditions
ab(c|d)
expand to match abc and abd
It is a bit hard to know exactly what you want, but this produces something quite similar:
# Create a list:
a = [['a1','a2'],'b',['c1','c2']]
# Split it into sub-arrays and single values:
list, other = a.partition{|x|x.is_a? Array}
# Split the array in order to get the product:
list_first, list_rest = list.pop, list
# Get the product and add the others_values:
p list_first.product(*list_rest).map{|list| list+other}
#=> [["c1", "a1", "b"], ["c1", "a2", "b"], ["c2", "a1", "b"], ["c2", "a2", "b"]]
1st:
arr1 = ['a','b','c',['d1','d2']]
*a, b = arr1
# ["a", "b", "c", ["d1", "d2"]]
a
# ["a", "b", "c"]
b
# ["d1", "d2"]
b.map{|x| a+[x]}
# [["a", "b", "c", "d1"], ["a", "b", "c", "d2"]]
and 2nd:
a, b, c = [["a1", "a2"], "b", ["c1", "c2"] ]
a.product c
#=> [["a1", "c1"], ["a1", "c2"], ["a2", "c1"], ["a2", "c2"]]
a.product(c).map{|x| x<<b}
#=> [["a1", "c1", "b"], ["a1", "c2", "b"], ["a2", "c1", "b"], ["a2", "c2", "b"]]
#or little less readable:
a.product(c).map{|x| [ x[0], b, x[1] ]}
# [["a1", "b", "c1"], ["a1", "b", "c2"], ["a2", "b", "c1"], ["a2", "b", "c2"]]
hirolau got me really close, here is what I ended with so the order is preserved:
# given a sample list
sample = [['a','b'],'c','d',['e','f'],'g',['h','i']]
# partition out the arrays
arrs, non_arrays = sample.partition {|sample| sample.is_a? Array}
# work out all possible products
first_elem, *the_rest = arrs
products = first_elem.product(*the_rest)
# finally swap it back in to get all valid combinations with order preserved
combinations = []
products.each do |p|
combinations << sample.map {|elem| elem.is_a?(Array) ? p.shift : elem}
end
# combinations
=> [["a", "c", "d", "e", "g", "h"],
["a", "c", "d", "e", "g", "i"],
["a", "c", "d", "f", "g", "h"],
["a", "c", "d", "f", "g", "i"],
["b", "c", "d", "e", "g", "h"],
["b", "c", "d", "e", "g", "i"],
["b", "c", "d", "f", "g", "h"],
["b", "c", "d", "f", "g", "i"]]

Resources