This question already has answers here:
Options for processing X elements of an array at a time using Ruby
(2 answers)
Closed 8 years ago.
In a Rails 3.2 application, I have an array like that :
arr = ["a", "b", "c", "d", "e"]
And I want to split it in 3 parts like this :
[["a", "b"], ["c", "d"], ["e"]]
I want to a have function to have the first part of this array.
I always want to have 3 parts. So, if I have :
["a", "b", "c", "d", "e", "f", "g", "h"]
I want to have :
[["a", "b", "c"], ["d", "e", "f"], ["g", "h"]]
Do you have a simple solution?
["a", "b", "c", "d", "e"].in_groups_of(2, false)
# => [["a", "b"], ["c", "d"], ["e"]]
If you always want 3 parts, this should do it
parts_count = 3
group_count = arr.length.fdiv(parts_count).ceil
arr.in_groups_of(group_count, false)
# => [["a", "b"], ["c", "d"], ["e"]]
More info here.
Related
I've got an array in the following form:
[["First", ["a", "b", "c"]], ["Second", ["d", "e"]], ["Third", ["g", "h", "i"]]]
Is there a way to somehow display this information on a Rails template using iterators? I need something like this:
First - a, b, c
Second - d, e,
Third - g, h, i.
Or this is impossible and I should modify the initial array form?
Thanks in advance.
Without modifing the main array you could try with each_with_index inside each for the main array of arrays, then checking for the first value you can skip it and get the array of letters:
array = [["First", ["a", "b", "c"]], ["Second", ["d", "e"]], ["Third", ["g", "h", "i"]]]
array.each do |main|
main.each_with_index do |value, index|
next if index.zero?
p value
end
end
# => ["a", "b", "c"]
# ["d", "e"]
# ["g", "h", "i"]
Or if you want to access it as a hash it'd be easier:
array = [["First", ["a", "b", "c"]], ["Second", ["d", "e"]], ["Third", ["g", "h", "i"]]]
array.to_h.each do |_, value|
p value
end
# => ["a", "b", "c"]
# ["d", "e"]
# ["g", "h", "i"]
Suppose, I have an array of five elements ['a','b','c','d','e']. I want combinations of four elements but I want that 'a' and 'b' not be together in any of combinations. How would I do that?
Expected result should be
['a', 'c', 'd', 'e']
['b', 'c', 'd', 'e']
Please write a more generalize solution.I am using ruby's combination method.I here just write an example.It may be possible that the combination number may vary(like there may be a array of 9 with size of 7 combinations) and also It may needed that I want two elements say a and e and d and f should not be together in any of combination.I know it bit confusing please let me know if I need to explain.Would really appreciate any help.
['a','b','c','d','e'].permutation(4).reject do |e|
e.include?('a') && e.include?('b')
end
Or, if you do not care about element order (credits to #mark-thomas,) use Array#combination:
['a','b','c','d','e'].combination(4).reject do |e|
e.include?('a') && e.include?('b')
end
#⇒ [["a", "c", "d", "e"], ["b", "c", "d", "e"]]
Please note, this approach is eager to resources. I’dn’t recommend to use it on big arrays.
I think you are looking for something like that:
def combine(array, should_not_be_together, size)
aux = []
add_together = true
while aux.size < size
random = Random.rand(2)
if random == 1
aux << array.sample
elsif add_together
aux << should_not_be_together.sample
add_together = false
end
aux.uniq!
end
aux
end
array = ['c', 'd', 'e']
should_not_be_together = ['a', 'b']
size = 4
combine(array, should_not_be_together, size)
Results:
# => ["c", "a", "d", "e"]
# => ["e", "b", "c", "d"]
# => ["e", "a", "c", "d"]
# => ["c", "d", "b", "e"]
# => ["d", "b", "e", "c"]
# => ["a", "c", "e", "d"]
# => ["c", "b", "d", "e"]
# => ["c", "a", "e", "d"]
I am new to Ruby. I am creating a rails platform where I have encountered the following issue:
I have my first array, which for the sake of simplicity I call 'capital' as:
capital = [A,B,C,D]. I have a second array small = [a,b,c,d]. The elements of small have many to one relationship with the elements of the capital, as:
capital
has_many:small
These two are linked through two tables in my database, so that valid combination of these arrays can be printed by
capital.each do |x|
small.each do |y|
puts x,y
end
end
which prints out valid combinations of x,y as defined in the database. This works perfectly fine.
Now, here is the issue: I have a third array array_3, which contains some combinations of the elements of capital and small, as:
array_3 = [(A,c), (C,d), (D,b)]
I want to print all valid combinations of elements of capital and small as defined in the database, such that the combination is not present in array_3. How do I go about it?
capital = %w(A B C D)
small = %w(a b c d)
array_3 = [%w(A c), %w(C d), %w(D b)]
capital.product(small) - array_3
#=> [["A", "a"], ["A", "b"], ["A", "d"], ["B", "a"], ["B", "b"], ["B", "c"], ["B", "d"], ["C", "a"], ["C", "b"], ["C", "c"], ["D", "a"], ["D", "c"], ["D", "d"]]
You can do as
capital.each do |x|
small.each do |y|
puts x,y unless array_3.include?([x,y])
end
end
Here is for all combination of capital and small which exists in array_3
capital.each do |x|
small.each do |y|
puts "[#{x}, #{y}]" unless array_3.exclude?([x,y])
puts "[#{y}, #{x}]" unless array_3.exclude?([y,x])
end
end
Check the Code in console:
> small
=> ["a", "b", "c", "d"]
> capital
=> ["A", "B", "C", "D"]
> array_3
=> [["A", "c"], ["C", "d"], ["D", "b"], ["A", "A"], ["C", "C"], ["a", "C"], ["b", "A"]]
> capital.each do |x|
> small.each do |y|
> puts "[#{x}, #{y}]" unless array_3.exclude?([x,y])
> puts "[#{y}, #{x}]" unless array_3.exclude?([y,x])
> end
> end
Output:
=>[b, A] # [small, capital]
[A, c] # [capital, small]
[a, C] # [small, capital]
[C, d] # [capital, small]
[D, b] # [capital, small]
Note: You can use if array_3.include? instead of unless array_3.exclude? which will return the same result.
For your more info about exclude? and include? as you are new in Ruby. ;)
I need to parse and display solr facets which are returned in either JSON or Ruby formar:
Collections: [ "a", 1, "b", 2, "c", 3, "d", 4, ... ]
into
{"a"=>1, "b"=>2, "c"=>3, "d"=>4}
What is the cleanest way?
EDIT: Well now that we know what you actually want, a hash ...
collections = ["a", 1, "b", 2, "c", 3, "d", 4]
Hash[*collections]
# => {"a"=>1, "b"=>2, "c"=>3, "d"=>4}
Original answer: I may not understand your goal but...
collections = ["a", 1, "b", 2, "c", 3, "d", 4]
collections.each_slice(2).map{ |(x, y)| "#{x} - #{y}" }
# => ["a - 1", "b - 2", "c - 3", "d - 4"]
What i see you want to do is maybe a hash ? {a => "1", b => "2"} ??
If so, read below:
collections = [ "a", 1, "b", 2, "c", 3, "d", 4]
result = Hash[*collections.flatten]
result prints {"a"=>1, "b"=>2, "c"=>3, "d"=>4}
I have a scenario outline that compares the results of a method to the array that should be returned. So I get a series of statements like this when they don't match:
expected: ["a", "b", "c", "d", "e", "f"]
got: ["c", "d", "e", "f", "g"] (using ==)
Diff:
## -1,2 +1,8 ##
-["a", "b", "c", "d", "e", "f"]
+["c",
+ "d",
+ "e",
+ "f",
+ "g"]
This is not the most succinct or helpful output. It would be much more helpful to be dipslay like:
expected: ["a", "b", "c", "d", "e", "f"]
got: ["c", "d", "e", "f", "g"] (using ==)
Diff:
## -1,2 +1,8 ##
-["a", "b"]
+["g"]
This way I could instantly see what values were additional or missing.
Use the Array difference method:
(expected_array - actual_array).should == []