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. ;)
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 have an array of strings. I'm wanting to change the name of these duplicate strings to append a numerical value to make them unique like so...
Original Array
a, a, A, b, c, D, d
Corrected Array
a, a1, A2, b, c, D, d1
I've gotten close to this with the following code; however, if the strings are a different case structure then they aren't currently considered duplicates with this code snippet. I would like them to be considered duplicates, but yet not change their case in the results array.
duplicate_counter = 1
duplicates = Array.new
duplicates = file_columns.select{ |e| file_columns.count(e) > 1 } # get duplicate column names
duplicates.each{ |x| file_columns.delete(x) }
duplicates.sort!
duplicates.each_with_index do |d, i|
if i > 0
if d == duplicates[i-1]
d = d.strip + duplicate_count.to_s
duplicate_count += 1
else
duplicate_count = 1
end
end
# Add back the column names, but with the appended numerical counts to make them unique
file_columns.push(d)
end
You are over thinking it considerably. I'm sure there are better ways to do this as well, but it gets the job done.
a = ['a', 'a', 'A', 'b', 'c', 'D', 'd']
letters = Hash.new(-1)
a.map do |letter|
l = letter.downcase
letters[l] += 1
if (letters[l] > 0)
"#{letter}#{letters[l]}"
else
"#{letter}"
end
end
Here's a way to do it if letters independent of case are not necessarily grouped. For example, it will convert this array:
arr = %w{ a D a A b c D a d }
#=> ["a", "D", "a", "A", "b", "c", "D", "a", "d"]
to:
["a", "D", "a1", "A2", "b", "c", "D1", "a3", "d2"]
Code
def convert(arr)
arr.each_with_index
.group_by { |c,_| c.downcase }
.values
.flat_map { |c|
c.map
.with_index { |(f,l),i| [i > 0 ? f<<i.to_s : f, l] } }
.sort_by(&:last)
.map(&:first)
end
Example
For arr above:
convert(arr)
#=> ["a", "D", "a1", "A2", "b", "c", "D1", "a3", "d2"]
Explanation
Dear reader, if you are new to Ruby, this may look impossibly complex. If you break it down into steps, however, it's not that bad. After you gain experience and become familiar with commonly-used methods, it will come quite naturally. Here I've used the following methods, chained together so that the return value of each becomes the receiver of the next:
Enumerable#each_with_index
Enumerable#group_by
Hash#values
Enumerable#flat_map
Enumerable#sort_by
Enumerable#first
Here's what's happening.
enum = arr.each_with_index
#=> #<Enumerator: ["a", "D", "a", "A", "b", "c",
# "D", "a", "d"]:each_with_index>
h = enum.group_by { |c,_| c.downcase }
#=> {"a"=>[["a", 0], ["a", 2], ["A", 3], ["a", 7]],
# "d"=>[["D", 1], ["D", 6], ["d", 8]],
# "b"=>[["b", 4]],
# "c"=>[["c", 5]]}
a = h.values
#=> [[["a", 0], ["a", 2], ["A", 3], ["a", 7]],
# [["D", 1], ["D", 6], ["d", 8]],
# [["b", 4]],
# [["c", 5]]]
b = a.flat_map { |c| c.map.with_index { |(f,l),i| [i > 0 ? f<<i.to_s : f, l] } }
#=> [["a", 0], ["a1", 2], ["A2", 3], ["a3", 7], ["D", 1],
# ["D1", 6], ["d2", 8], ["b", 4], ["c", 5]]
c = b.sort_by(&:last)
#=> [["a", 0], ["D", 1], ["a1", 2], ["A2", 3], ["b", 4],
# ["c", 5], ["D1", 6], ["a3", 7], ["d2", 8]]
c.map(&:first)
#=> ["a", "D", "a1", "A2", "b", "c", "D1", "a3", "d2"]
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.
I have an array which contains numbers and alphabets something like:
newArray = ["1 a", "1 b" ,"2 c", "2 a"]
I would like to sort them in a way that the output is expected as follows:
newArray = ["2 a", "2 c" ,"1 a", "1 b"]
What I want to do is sort the numbers in descending order and if the numbers are same, then sort alphabetically
Can I implement a comparison function in sort_by or is there a way to do that using ruby sort
First you should use a better representation of your input. You can parse your existing array for example like this:
arr = newArray.map { |s| x,y = s.split; [x.to_i, y] }
# => [[1, "a"], [1, "b"], [2, "c"], [2, "a"]]
Then we can sort as we wish using sort_by:
arr.sort_by { |x,y| [-x, y] }
# => [[2, "a"], [2, "c"], [1, "a"], [1, "b"]]
Similar to #NiklasB. 's answer above (copied his sort_by)
arr.map(&:split).sort_by { |x,y| [-x.to_i, y] }
=> [["2", "a"], ["2", "c"], ["1", "a"], ["1", "b"]]
In a less elegant way, you can do that
arr.sort! do |p1, p2|
num1, str1 = p1.split(' ')
num2, str2 = p2.split(' ')
if (num1 != num2)
p2 <=> p1
else
p1 <=> p2
end
end
$stdout.puts arr