arrays ruby and combinations

For general discussion related FlowStone
Post Reply
tester
Posts: 1786
Joined: Wed Jan 18, 2012 10:52 pm
Location: Poland, internet

arrays ruby and combinations

Post 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?
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
User avatar
trogluddite
Posts: 1730
Joined: Fri Oct 22, 2010 12:46 am
Location: Yorkshire, UK

Re: arrays ruby and combinations

Post 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 909 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.
All schematics/modules I post are free for all to use - but a credit is always polite!
Don't stagnate, mutate to create!
tester
Posts: 1786
Joined: Wed Jan 18, 2012 10:52 pm
Location: Poland, internet

Re: arrays ruby and combinations

Post 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!
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
tester
Posts: 1786
Joined: Wed Jan 18, 2012 10:52 pm
Location: Poland, internet

Re: arrays ruby and combinations

Post 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.
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
tester
Posts: 1786
Joined: Wed Jan 18, 2012 10:52 pm
Location: Poland, internet

Re: arrays ruby and combinations

Post by tester »

A little help here please. :-)
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
User avatar
trogluddite
Posts: 1730
Joined: Fri Oct 22, 2010 12:46 am
Location: Yorkshire, UK

Re: arrays ruby and combinations

Post 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
All schematics/modules I post are free for all to use - but a credit is always polite!
Don't stagnate, mutate to create!
tester
Posts: 1786
Joined: Wed Jan 18, 2012 10:52 pm
Location: Poland, internet

Re: arrays ruby and combinations

Post 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!
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
Post Reply