473,395 Members | 1,452 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Iterate over array element combinations

I need to iterate over combinations of n array elements taken r at a
time. Because the value of r may vary quite a bit between program
invocations, I'd like to avoid simply hardcoding r loops. I assume the
best way to do this would be either using closures or creating some
sort of iterator class.

Any guidance on how to get started?
Thanks,
Jacob.

Oct 1 '06 #1
7 7398
VK
I need to iterate over combinations of n array elements taken r at a
time.
The algorithm description is not fully clear. If you are trying to
implement a sort of Shannon's clairvoyant, you may find interesting the
thread "Looping through variable number of arrays variable times?" at
<http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/65f35a3a759cd883>

If not then sorry for a wrong guess, more details could help.

Oct 1 '06 #2

VK wrote:
I need to iterate over combinations of n array elements taken r at a
time.

The algorithm description is not fully clear. If you are trying to
implement a sort of Shannon's clairvoyant, you may find interesting the
thread "Looping through variable number of arrays variable times?" at
<http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/65f35a3a759cd883>

If not then sorry for a wrong guess, more details could help.
You're right. I really didn;t explain this very well at all.

I have an array of n floats. What I need to do is calculate a function
of the elements of every subset of n containing exactly r elements
(actually it's r or fewer, but for the sake of simplicity let's just
say exactly r) and then figure the sum of the functions

So for example given array = (a, b, c, d):

For r = 2 I'd be looking for:
f(a,b) + f(a,c) + f(a,d) + f(b,c) + f(b,d) + f(c,d)

and for r=3 I'd be looking for:
f(a,b,c) + f(a,b,d) + f(a,c,d) + f(b,c,d)
etc....

Oct 1 '06 #3
VK

Jacob JKW wrote:
I have an array of n floats. What I need to do is calculate a function
of the elements of every subset of n containing exactly r elements
(actually it's r or fewer, but for the sake of simplicity let's just
say exactly r) and then figure the sum of the functions

So for example given array = (a, b, c, d):

For r = 2 I'd be looking for:
f(a,b) + f(a,c) + f(a,d) + f(b,c) + f(b,d) + f(c,d)

and for r=3 I'd be looking for:
f(a,b,c) + f(a,b,d) + f(a,c,d) + f(b,c,d)

etc....
I see now... I guess the most helpful array method here would be
slice(lbound, ubound)

function iterator(r) {

var subset = new Array();
var len = myArray.length;
var lim = len - r;

for (var i=0; i<lim; i++) {
subset = myArray.slice(i, r-1);

for (var j=r; j<len; j++) {
methodCall(subset.push(myArray[j]));
}

}

}

I did not check it for working, just some thoughts - I feel like I lost
some +/- 1 for indexes

Oct 1 '06 #4
JRS: In article <11**********************@k70g2000cwa.googlegroups .com>,
dated Sun, 1 Oct 2006 01:33:36 remote, seen in
news:comp.lang.javascript, Jacob JKW <ja******@yahoo.composted :
>I need to iterate over combinations of n array elements taken r at a
time. Because the value of r may vary quite a bit between program
invocations, I'd like to avoid simply hardcoding r loops. I assume the
best way to do this would be either using closures or creating some
sort of iterator class.

Any guidance on how to get started?
See <URL:http://www.merlyn.demon.co.uk/js-misc0.htm#CP>.
Combinations are generated recursively, though AIUI any recursive
algorithm can be expressed iteratively.

function Comb(n, a, z, D) { // List combinations - D starts empty
if (n==0) { D[D.length] = z ; return }
for (var j=0 ; j < a.length ; j++)
Comb(n-1, a.slice(j+1), z+a[j], D)
return }

function TestComb() { var S = ["a","b","c","d"], D, k
document.writeln("TestComb() :")
for (k=0 ; k <= S.length ; k++) {
D = [] ; Comb(k, S, "", D)
document.writeln("Comb(", k, ") = ", D) } }

document.writeln("<pre>")
TestComb()
document.writeln("<\/pre>")
gives:-

TestComb() :
Comb(0) =
Comb(1) = a,b,c,d
Comb(2) = ab,ac,ad,bc,bd,cd
Comb(3) = abc,abd,acd,bcd
Comb(4) = abcd
So :-
D = [] ; Comb(2, S = ["a","b","c","d"], "", D) ;
gives D as ['ab', 'ac', 'ad', 'bc', 'bd', 'cd'] .
There should be a way of adapting that so that each element of array D
is not a string but an array of combinations of the elements of the
parameter array. Change f to take that array as a parameter, using
within it the array called arguments - and you might be more or less
there.

It's a good idea to read the newsgroup and its FAQ. See below.
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Oct 1 '06 #5
Dr John Stockton wrote:
JRS: In article <11**********************@k70g2000cwa.googlegroups .com>,
dated Sun, 1 Oct 2006 01:33:36 remote, seen in
news:comp.lang.javascript, Jacob JKW <ja******@yahoo.composted :
I need to iterate over combinations of n array elements taken r at a
time. Because the value of r may vary quite a bit between program
invocations, I'd like to avoid simply hardcoding r loops. I assume the
best way to do this would be either using closures or creating some
sort of iterator class.

Any guidance on how to get started?

See <URL:http://www.merlyn.demon.co.uk/js-misc0.htm#CP>.
Combinations are generated recursively, though AIUI any recursive
algorithm can be expressed iteratively.
<snip code sample from http://www.merlyn.demon.co.uk/js-misc0.htm#CP>
This is perfect!. Thank you very much. :)

Jacob

Oct 2 '06 #6

VK wrote:
Jacob JKW wrote:
I have an array of n floats. What I need to do is calculate a function
of the elements of every subset of n containing exactly r elements
(actually it's r or fewer, but for the sake of simplicity let's just
say exactly r) and then figure the sum of the functions

So for example given array = (a, b, c, d):

For r = 2 I'd be looking for:
f(a,b) + f(a,c) + f(a,d) + f(b,c) + f(b,d) + f(c,d)

and for r=3 I'd be looking for:
f(a,b,c) + f(a,b,d) + f(a,c,d) + f(b,c,d)

etc....

I see now... I guess the most helpful array method here would be
slice(lbound, ubound)

function iterator(r) {

var subset = new Array();
var len = myArray.length;
var lim = len - r;

for (var i=0; i<lim; i++) {
subset = myArray.slice(i, r-1);

for (var j=r; j<len; j++) {
methodCall(subset.push(myArray[j]));
}

}

}

I did not check it for working, just some thoughts - I feel like I lost
some +/- 1 for indexes
TYhis looks rather good as well.

For no real reason I decided to move forward with John's suggestion
above. But thanks anyway for your help on this. Highly appreciated. :)

Oct 2 '06 #7
JRS: In article <11*********************@k70g2000cwa.googlegroups. com>,
dated Mon, 2 Oct 2006 07:25:51 remote, seen in
news:comp.lang.javascript, Jacob JKW <ja******@yahoo.composted :
>
<snip code sample from http://www.merlyn.demon.co.uk/js-misc0.htm#CP>
This is perfect!. Thank you very much. :)
It was merely OK; I've improved it.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Oct 3 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: ChronoFish | last post by:
Hi there, I want to iterate through an array starting at a known index. However the indexes are not linear. For example I have an array of events keyed by timestamp. $eventList = array...
2
by: deko | last post by:
I have a file that contains the output of time(). A different time is on each line of the file, and each line represents a visit to the website. I want to calculate the total visits per day,...
10
by: BCC | last post by:
Ive been googling and reading through my books but I haven't figured out a solution (much less an elegant one) to create a multidimensional array with a runtime determined number of dimensions. I...
15
by: alanbe | last post by:
Greetings I am making a flashcard type application to help me in my TCP/IP protocols test. My instructor will test us periodically on how a device or networking function relates to the OSI...
9
by: tony collier | last post by:
i have created a 7-dimensional array which has 19.5 million elements. when i try to create 8-dimensional array i get out of bounds system exception. does anyone know if this limitation is due...
2
by: Rob | last post by:
I have a function exposed from a C DLL which takes a pointer to an array of strings as a param which then allocates that array of strings and returns. I'm using P/Invoke to call this function...
4
by: Norman Fritag | last post by:
Hi there, >>>__ 1020.83, 2305.22, 1176.86, 755.12, 123.41 __ 1976.1, 1325.99, 947, 718.03, 414.32 __ 1020.83, 1976.1, 352.5, 947, 718.03, 366.98 Their IDs were as...
2
by: Nico | last post by:
Dear all, I created the following php form. <FORM METHOD="POST" ACTION="prova.php"> <b>Combination</b><br><br> Element 1: <INPUT NAME="el1" TYPE="TEXT"> <BR> Element 2: <INPUT NAME="el2"...
1
by: Nico | last post by:
Hi all, I've created the following code: <?php session_start(); ?> <FORM METHOD="POST" ACTION="prova.php"> Add <b>Combination</b><br><br>
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.