Ruby: Turn an array of arrays into a hash with 3 key value pairs - ruby

Ok started over, apologies for my verbose and poorly structured original post.
My question is basically this: is it possible to take an array of arrays and divide it into three equal-ish parts, sending each part to a hash where there are three key value pairs as described below?
Sample input: an array of arrays like:
orig_array = [[13, 11, 19, 17, 12, 5, 3], [3, 9, 2, 20], [5, 21, 15, 4],
[18, 14, 16, 10], [6, 1, 8, 7], [15, 4, 17, 6], [3, 19, 13, 14], [9, 21, 12, 7],
[20, 11, 2, 18], [8, 10, 1, 16], [10, 6, 21, 17], [15, 11, 14, 19], [13, 2, 9, 18],
[5, 12, 16, 7], [20, 4, 1,8]]
Desired output: a hash where each key is a number starting with 1 and counting up, and each value is one third of the array, like:
hash = { 1=>[[array of arrays containing first 1/3rd elements from orig_array]],
2=>[[array of arrays containing next 1/3rd elements from orig_array]]
3=>[[array of arrays containing next 1/3rd remaining elements from orig_array]]}
To be clear, this is part of an exercise for a class. The exercise is not to find a way to divide an array of arrays like this, I just feel like doing so could be part of one potential solution and am looking for guidance. Thanks!

def unique_group_of_three(array)
multiple = array.size / 3
return "Your collection is too small" if multiple < 5
multiples = [multiple, multiple *2]
array = array.shuffle
{
:first => array[0...multiple].uniq,
:second => array[multiple...multiples[1]].uniq,
:third => array[multiples[1]..-1]].uniq
}
end

Related

How to solve combinations in card game for n people in r rounds (just one encounter)

There is a famous card game in Germany called "Doppelkopf".
Usually, you play "Doppelkopf" with 4 players, but you can also play it with a table of 5 players, where one player is just watching.
(Where everyone "has the cards" once in a round, meaning everyone has the right to play the first card once every round.)
Every year, my family organizes a "Doppelkopf" tournament with 3 rounds (r).
Depending on the availabilty of my relatives, every year the number of participants varies.
Expecting a minimun of participant of 16 people, the number (n) in this experiment can rise up unlimited (as does the number of rounds r).
Naturally, my relatives do not want to be paired with someone twice, since they want to exchange gossip most efficiently!
There we have:
n - Participants
r- Rounds
t_total = n // 4 # Total Tables (round down of n)
t_5 = n % 4 # Tables of 5s
t_4 = t_total - t_5 # Tables of 4s
pos_pair = n * (n - 1) / 2 # possible pairs (n over 2)
nec_pair = (t_5 * 10 + t_4 * 6) * r # necessary pairs
I was instructed with the aim to minimize the encounters (if possible to set encounters == 1 for everyone)!
Since, I do not want to solve the problem for P{n={16, ..., 32}, r=3} (which I did for some cases), but to solve it with any given P{n∈N, r∈N} , there is a discrepancy between my abilities and the requirements for a solution!
Therefore, I would like to ask the community to help me with this problem, to solve it for any given P{n∈N, r∈N}!
And also to prove, if this problem is not solvable for any P{n∈N, r∈N}, which is given "if pos_pair < nec_pair".
Here are two solutions for P{n=20, r=3}:
which very much solves my "Doppelkopf" tournament problem:
('Best result was ', [[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16], [17, 18, 19, 20]], [[16, 12, 8, 18], [13, 1, 5, 9], [15, 4, 17, 6], [2, 19, 7, 10], [3, 11, 20, 14]], [[14, 9, 17, 7], [13, 20, 8, 2], [5, 4, 12, 19], [6, 16, 11, 1], [15, 18, 10, 3]]])
('Best result was ', [[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16], [17, 18, 19, 20]], [[19, 11, 13, 3], [2, 15, 9, 8], [1, 16, 18, 6], [14, 7, 17, 10], [4, 12, 20, 5]], [[17, 8, 3, 12], [20, 9, 16, 7], [15, 11, 6, 4], [2, 13, 10, 18], [1, 19, 14, 5]]])
But in order to solve this problem with an arbitrary n and r I have come to no conclusion.
In my opinion, there are three ways to go about this problem in a computational solution or approximation.
First, you can iterate about rounds, and assign every player to a
table without having collision, remembering pairs and appeareances
in total (not to exeed total rounds)
Secondly, you can iterate about tables, which seems to be helpful with participants, that are a multiple of 2 (see for P{n=16, r=5}
https://matheplanet.com/default3.html?call=viewtopic.php?topic=85206&ref=https%3A%2F%2Fwww.google.com%2F)
also remeber pairs and appearances, but mainly follow a certain
patters as described in the link, which I somehow can not scale to
other numbers!!
There is somehow a mathemathical way to descibe this procedure and conclude a solution
Even though, this is more of a mathematical question (and I don't know where to ask those questions), I am interested in the algorithmic solution!

How do I make a hash out of many arrays?

I have an array that looks like this:
[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15]]
How would I turn that into a hash that looks similar to this:
{1=>[6, 11], 2=>[7, 12], 3=>[8, 13], 4=>[9, 14], 5=>[10, 15]]
Any help would be appreciated! Trying to do this in Ruby.
foo = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15]]
foo.transpose.map { |x, *y| [x, y] }.to_h
That's a really strange way of mapping things, but with a clever method signature it's not too hard:
def pivot(keys, *values)
keys.each_with_index.map do |key, i|
[ key, values.map { |v| v[i] } ]
end.to_h
end
Then you'd call it with a splat:
a = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15]]
pivot(*a)
# => {1=>[6, 11], 2=>[7, 12], 3=>[8, 13], 4=>[9, 14], 5=>[10, 15]}
I kind of like zip:
a = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15]]
a[0].zip(a[1].zip(a[2])).to_h
The downside is that it's hardwired for three subarrays.
This can be generalized with a splat, so
a = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20]]
a[0].zip(a[1].zip(*a.drop(2))).to_h
yields
{1=>[6, 11, 16], 2=>[7, 12, 17], 3=>[8, 13, 18], 4=>[9, 14, 19], 5=>[10, 15, 20]}
without any additional levels of zipping required.
Let's say your array is stored under the variable name array I would go about it like this:
hash = {}
array[0].each.with_index do |value, i|
hash[value] = [array[1][i], array[2][i]]
end
A kind of mixture of pjs and ndn's answers:
arr.first.zip(arr[1..-1].transpose).to_h
Also very similarly (posted by CarySwoveland) in comments:
arr.first.zip(arr.drop(1).transpose).to_h

Find largest element from multidimensional array of ints

I have an array of ints similar to this:
values = [[3, 4, 15, 16, 5, 13, 2], [1, 12, 13, 2, 10, -1], [11, 12, 1, 9, -2], [1, -10, -2, -13], [-11, -3, -14], [8, -3], [-11]]
How would I retrieve the index of the largest number, e.g. 16 == values[0][3]?
Don't sure I get you correct, but anyway:
1) If you need the largest element:
values.flatten.max
2) If you need largest element from each subarray:
values.map{|x| x.max}
UPD
About indexes:
largest_element = values.flatten.max
values.each_with_index do |e,i|
if e.include?(largest_element)
p i
values[i].each_with_index{|e, i| p i if e == largest_el}
end
end

Splitting an array by performing an arithmetic function in ruby

I have an array in Ruby like [3,4,5] and I want to create sub-arrays by diving or multiplying. For example, I want to multiply each number in the array by 2, 3, and 4, returning [[6,9,12],[8,12,16],[10,15,20]]
After that, what's the best way to count the total number of units? In this example, it would be 9, while array.count would return 3.
Thanks
The simplest way I could think of was:
[3,4,5].map { |v|
[3,4,5].map { |w|
w * v
}
}
I'm sure there is a more elegant way.
As for the count you can use flatten to turn it into a single array containing all the elements.
[[9, 12, 15], [12, 16, 20], [15, 20, 25]].flatten
=> [9, 12, 15, 12, 16, 20, 15, 20, 25]
You might find it convenient to use matrix operations for this, particularly if it is one step among several involving matrices, vectors, and/or scalars.
Code
require 'matrix'
def doit(arr1, arr2)
(Matrix.column_vector(arr2) * Matrix.row_vector(arr1)).to_a
end
def nbr_elements(arr1, arr2) arr1.size * arr2.size end
Examples
arr1 = [3,4,5]
arr2 = [3,4,5]
doit(arr1, arr2)
#=> [[ 9, 12, 15],
# [12, 16, 20],
# [15, 20, 25]]
nbr_elements(arr1, arr2)
#=> 9
doit([1,2,3], [4,5,6,7])
#=> [[4, 8, 12],
# [5, 10, 15],
# [6, 12, 18],
# [7, 14, 21]]
nbr_elements([1,2,3], [4,5,6,7])
#=> 12
Alternative
If you don't want to use matrix operations, you could do it like this:
arr2.map { |e| [e].product(arr1).map { |e,f| e*f } }
Here's an example:
arr1 = [1,2,3]
arr2 = [4,5,6,7]
arr2.map { |e| [e].product(arr1).map { |e,f| e*f } }
#=> [[4, 8, 12],
# [5, 10, 15],
# [6, 12, 18],
# [7, 14, 21]]

Remove duplicates from nested array

I have an Array of Arrays that contains numbers in a particular order. I want to remove the duplicates out of the nested arrays, but there is a hierarchy: If a number occurs in a lower-index of the array, remove all duplicates down the Array chain.
Example:
nums = [[10, 6, 14], [6], [10, 6, 9], [10, 13, 6], [10, 13, 6, 9, 16], [10, 13]]
nums[0] contains [10,6,14] so any subsequent mention of 10,6,14 should be removed from the other arrays in the chain, meaning nums[2] should have 10,6 removed and only 9 should remain.
I'm having trouble doing this with nested loops, can any Ruby wizards help please?
This should do it:
input = [[10, 6, 14], [6], [10, 6, 9], [10, 13, 6], [10, 13, 6, 9, 16], [10, 13]]
seen = []
output = input.map do |numbers|
new = numbers.uniq - seen
seen += new
new
end
# => output is [[10, 6, 14], [], [9], [13], [16], []]
If you want to remove the empty lists in the output, simply
output.reject!(&:empty?)
require 'set'
nums = [[10, 6, 14], [6], [10, 6, 9], [10, 13, 6], [10, 13, 6, 9, 16], [10, 13]]
found = Set.new
new_nums = []
for subarray in nums do
sub_new = []
for i in subarray do
if not found.member? i
sub_new << i
end
found << i
end
new_nums << sub_new
end
puts(nums.inspect)
puts(new_nums.inspect)
Yet another way. It keeps original order of elements in arrays:
require 'set'
nums = [[10, 6, 14], [6], [10, 6, 9], [10, 13, 6], [10, 13, 6, 9, 16], [10, 13]]
nums2 = nums.inject([[], Set.new]) do |(output, seen), ary|
[output << ary.reject { |a| seen.include?(a) }, seen.union(ary)]
end[0]
p nums2
# [[10, 6, 14], [], [9], [13], [16], []]
Is the following incorrect? Should the [6] be removed or not?
nums = [[10, 6, 14], [6], [10, 6, 9], [10, 13, 6], [10, 13, 6, 9, 16], [10, 13]]
def remove_duplicate_numbers( array )
seen = []
array.map{ |sub_array|
result = sub_array - seen
seen += sub_array
result
}
end
p remove_duplicate_numbers( nums )
#=> [[10, 6, 14], [], [9], [13], [16], []]
If this is not what you want, please post the actual output you expect for your array.

Resources