help_me_with_php Posted December 14, 2012 Share Posted December 14, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/271981-function-for-an-output-of-indicies/ Share on other sites More sharing options...
Psycho Posted December 14, 2012 Share Posted December 14, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/271981-function-for-an-output-of-indicies/#findComment-1399306 Share on other sites More sharing options...
help_me_with_php Posted December 14, 2012 Author Share Posted December 14, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/271981-function-for-an-output-of-indicies/#findComment-1399313 Share on other sites More sharing options...
Christian F. Posted December 14, 2012 Share Posted December 14, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/271981-function-for-an-output-of-indicies/#findComment-1399328 Share on other sites More sharing options...
help_me_with_php Posted December 14, 2012 Author Share Posted December 14, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/271981-function-for-an-output-of-indicies/#findComment-1399357 Share on other sites More sharing options...
Barand Posted December 14, 2012 Share Posted December 14, 2012 $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 Quote Link to comment https://forums.phpfreaks.com/topic/271981-function-for-an-output-of-indicies/#findComment-1399365 Share on other sites More sharing options...
help_me_with_php Posted December 14, 2012 Author Share Posted December 14, 2012 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... Quote Link to comment https://forums.phpfreaks.com/topic/271981-function-for-an-output-of-indicies/#findComment-1399368 Share on other sites More sharing options...
Barand Posted December 14, 2012 Share Posted December 14, 2012 As I wouldn't know how many of the set of integers would be required to sum to to the target number, I'd write a recursive function to sum the various combinations. Quote Link to comment https://forums.phpfreaks.com/topic/271981-function-for-an-output-of-indicies/#findComment-1399382 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.