arrays ruby and combinations
Posted: Sat Jun 21, 2014 5:08 pm
On ruby website there is something like this:
What I'd like to do is this.
First array to push trough combinatorics is made of numbers.
I'd like to get unique midpoints according to formula (A+B)/2.
If the array is:
100
200
300
then the output list would be something like this:
150
200
250
Second array to push through combinatorics is made of strings.
I'd like to get a list of combined strings that looks like this: A-B
If the array of string names is:
A
B
C
then the output list would be something like this:
A-B
A-C
B-C
I refer to combinatorics, because order between two arrays is important (i.e. names and their numbers).
How to make it?
Code: Select all
Examples:
a = [1, 2, 3, 4]
...
a.combination(2).to_a #=> [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]
...
What I'd like to do is this.
First array to push trough combinatorics is made of numbers.
I'd like to get unique midpoints according to formula (A+B)/2.
If the array is:
100
200
300
then the output list would be something like this:
150
200
250
Second array to push through combinatorics is made of strings.
I'd like to get a list of combined strings that looks like this: A-B
If the array of string names is:
A
B
C
then the output list would be something like this:
A-B
A-C
B-C
I refer to combinatorics, because order between two arrays is important (i.e. names and their numbers).
How to make it?