If you have a problem or need to report a bug please email : support@dsprobotics.com
There are 3 sections to this support area:
DOWNLOADS: access to product manuals, support files and drivers
HELP & INFORMATION: tutorials and example files for learning or finding pre-made modules for your projects
USER FORUMS: meet with other users and exchange ideas, you can also get help and assistance here
NEW REGISTRATIONS - please contact us if you wish to register on the forum
Users are reminded of the forum rules they sign up to which prohibits any activity that violates any laws including posting material covered by copyright
arrays ruby and combinations
7 posts
• Page 1 of 1
arrays ruby and combinations
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?
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
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
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!
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.
But handy, because '.map' can immediately follow without needing to convert to array in the middle - should be very fast!
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!
Don't stagnate, mutate to create!
-
trogluddite - Posts: 1730
- Joined: Fri Oct 22, 2010 12:46 am
- Location: Yorkshire, UK
Re: arrays ruby and combinations
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.
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
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.
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.
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
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.
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
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...
All schematics/modules I post are free for all to use - but a credit is always polite!
Don't stagnate, mutate to create!
Don't stagnate, mutate to create!
-
trogluddite - Posts: 1730
- Joined: Fri Oct 22, 2010 12:46 am
- Location: Yorkshire, UK
Re: arrays ruby and combinations
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!
Thanks!
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
Feel free to donate. Thank you for your contribution.
- tester
- Posts: 1786
- Joined: Wed Jan 18, 2012 10:52 pm
- Location: Poland, internet
7 posts
• Page 1 of 1
Who is online
Users browsing this forum: No registered users and 78 guests