Jump to content

Recommended Posts

All,

 

I had someone request a function to be written. Here's what they said:

 

can you take a list of ints and get back a list of indices that add up to a variable 'X'?? here's what they asked for (as an example)...

 

Input: ([1,4,7,2], X=3) output: [[0,1]]

 

I'm not quite sure I'm following it. Anyone shed some light on this for me? I'm just not getting what they want, probably because I don't have formal training in coding. Vocabs are always mismatched.

 

thanks.

Link to comment
https://forums.phpfreaks.com/topic/271981-function-for-an-output-of-indicies/
Share on other sites

I'm guessing that the example output is wrong. Your input has four 'indicies' of 1, 4, 7, and 2. The variable you are trying to add up to is 3. The sample output is 0 and 1? 0 + 1 = 1, 1 != 3. I would think the correct output is 1 & 2. Those are two if the indicies from the input that add up to the variable 3.

Yes I realize. This is obvious math-based. What I was confused about was the terminology more than anything. Do you think there's anything else to read into this if the math would make sense? say something like this (which of course makes sense):

 

Input: ([1,4,7,2], X=3) output: [[0,1,2]].

 

the function will perform math to manipulate the input to the output. that's all. I thought they were using "integers" and "indicies" as interchangeable terms, that's all. they have a rep for doing these kinds of things.

 

think it's pretty simple? it sounds like. thanks.

Indices is the multiple of index, so if they were mixing them with integers...

Anyway, let's say one assumes a standard enumerated array, that the numbers given in the example are the values, and that the numbers returned are the indices. If the task is to get the indices of the values that would sum up to the given number in X, then the return value should have been 0,3.

 

I'd ask for a clarification.

If the task is to get the indices of the values that would sum up to the given number in X, then the return value should have been 0,3.

 

 

Christian,

 

Why does it make a difference as to whether the return is 0,3 or 0,1,2?? what's your thinking on it??

 

I will ask them why this is needed, but I know it's not urgent. this happens all the time.

$arr = array (1,4,7,2);
echo '<pre>'.print_r($arr, 1).'</pre>';

/****** results *******
Array
(
   [0] => 1
   [1] => 4
   [2] => 7
   [3] => 2
)
***********************/

 

1, 2 are the integers that add up to 3

 

However, the question you posted was for the indices of those integer ie 0, 3

Barand,

 

Let me just ask you this:

 

what would you personally write in PHP that takes 2 inputs (X, Y): 1) an array of integers (X) and 2) a summation target value (Y).

 

and then produces one output: an array / list of indicies that sum to input #2 (Y).

 

thanks. this is my last question here. this is already too long for being a little bit simple for the gurus...

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.