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. 7 7402
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.
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....
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
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.
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
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. :)
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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"...
|
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>
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |