Page 1 of 1

arrays ruby and combinations

Posted: Sat Jun 21, 2014 5:08 pm
by tester
On ruby website there is something like this:

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?

Re: arrays ruby and combinations

Posted: Sat Jun 21, 2014 5:52 pm
by trogluddite
Wierd, never noticed that this one returned an 'Enumerator' instead of Array.
But handy, because '.map' can immediately follow without needing to convert to array in the middle - should be very fast!
Combinations.fsm
(600 Bytes) Downloaded 908 times

NB) If you need exact 1 to 1 correlation between index of mid-points and index of Strings, remove '.uniq' - it will remove identical values, maybe leaving the number array shorter than the String array.

Re: arrays ruby and combinations

Posted: Sat Jun 21, 2014 10:47 pm
by tester
The string array must be exact to the array of numbers. By "unique" I meant unique index combinations (i.e. to avoid Ato B and B to A). In this step - output numbers refer to midpoint descriptions. Thanks!

Re: arrays ruby and combinations

Posted: Sun Jun 22, 2014 12:37 am
by tester
Okay, question for step 2. Similar thing as above, but now we have two separate arrays of numbers and two separate arrays of descriptors.

What I would like to do here - is to combine each element from array 1 with each element from array 2.
It would be like this

Text arrays:

A
B

C
D
E

results in

A-B
A-C
A-D
B-C
B-D
B-E

And numeric arrays go in the same way, just by the equation: x - y.

So the end result - are two synchronized arrays, one with descriptors, one with calculated data.

How to make this one?

For the rest of things (picking indexes for values in specified range), I think I have working modules somewhere around.

Re: arrays ruby and combinations

Posted: Sun Jun 22, 2014 4:08 pm
by tester
A little help here please. :-)

Re: arrays ruby and combinations

Posted: Sun Jun 22, 2014 7:52 pm
by trogluddite
I think this is right - I'm guessing that "A-B, A-C, A-D" is a typo - should be "A-C, A-D, A-E". If not, I'm not quite sure how to make sense of the specification...
Combinations 002.fsm
(582 Bytes) Downloaded 924 times

Re: arrays ruby and combinations

Posted: Sun Jun 22, 2014 7:59 pm
by tester
Yes, it was my mistake (long night; playing with new i-crap device since few days; the longer I have it the more I think I start do programming for them, but not because I like them - lack of apps that I would like to use...).
Thanks!