cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Your Friends List is a way to easily have access to the community members that you interact with the most! X

Finding index

fahmed-2
13-Aquamarine

Finding index

Hello,

 

I am working with an array of positive and negative numbers. My logic involves such that it has to find the index value of a changed sign number immediately above. For example, in my array, if I work with "-4" it has to find the index number of "2", not "3". Similarly, if I work with "4" it has to find the index number of "-5", not "-4". Please help me with this problem. My version is Prime 4.0.

 

Thanks,

Faisal

ACCEPTED SOLUTION

Accepted Solutions
Werner_E
25-Diamond I
(To:fahmed-2)

If you want to ignore the first few values when you chose a value which occurs multiple times in your vector, then the attached file should help.

 

Concerning your answers: Sure the question about the value which occurs multiple times in the vector was the most important one and I am glad you realized yourself at last that this is a problematic situation.

But as with the others - stating that this or that problematic situation will not occur as you will never enter an input value which has no sign change before, does not help much. In my opinion when programming a function, it must always be exactly determined what should happen in every possible input case. So even though at the moment you do not intend to input an invalid value, it must be defined what the reaction of the program should be if one day you do. The reaction could be a customized error message, or a return value which signals that something unusual had happened (could be a NaN or an invalid index value).

Here is a routine which copes with the most obvious errors and I decided that the function should fail with an appropriate error message without returning any result.

B1.png

You can find a version which uses if-statements instead of the if functions in the attached sheet.

Without any customized error messages it could be reduced to

B2.png

and here is yet another, different approach (could be a 1-liner which also works in Prime Express if you don't mind typing "ORIGIN" instead of just "O" five times) - again without error handling (which I still regard as a bad design error):

B3.png

BTW, you claimed that Luc's approach would not return the index but the value itself. Thats not true! Luc's function "f" returns the index as you demanded. He just showed after calling his function which value would be addressed. His version avoids programming and implements the necessary loops via recursive calls of an if-function. That way his approach also works in Prime Express. Its quite easy to modify his version to fit the new conditions. Additionally I got rid of the function "Sign" and changed the order of the arguments to bring it in line with my functions.

B4.png

So its up to you now to decide for one of the versions.

 

View solution in original post

15 REPLIES 15
Werner_E
25-Diamond I
(To:fahmed-2)

Whats the input (apart from the vector) of the function your a looking for and whats the output?

 

Is the output

a) the value with a different sign right before the input

or

b) the index of the value with the different sign

As I understand your description its b). What should be returned if no suitable value is found? Maybe ORIGIN-1 or maybe NaN?

 

Its the input

a) the index of a value in the vector

or

b) the value itself?

If b), what should be the output when the input is a value which occurs multiple times? I in your example vector, what would be the out for -1? Should it be 1 or 3 or both in a result vector? What is the output if the input is 1?

 

What about zeros? Do the count as changed sign or not?

What if the input value is zero?

What should be the output if no value with different sign is found above the input value?

fahmed-2
13-Aquamarine
(To:Werner_E)

The input will be an array of positive and negative numbers. Yes, there can be zeros and that should be considered as a positive. The goal is to find the index of a changed sign number just immediately previous. For example, in my array, if I work with "-4" it has to find the index number of "2", not "3". Similarly, if I work with "4" it has to find the index number of "-5", not "-4". Please help me with this problem. My version is Prime 4.0.

Werner_E
25-Diamond I
(To:fahmed-2)

You haven't answered the questions!

You simply repeated what you had already written in your first post.

 

Additionally:

If zero is considered positive, then 5;0;4 would mean no sign change, bit -5;0;-4 is considers a sign change. Correct?

fahmed-2
13-Aquamarine
(To:Werner_E)

yes, it is considered as a sign change. Till now, my array will have different signed numbers so it will not be a case where all has the same sign numbers. The input will be the values itself but my task is to find the index numbers where I have a sign change as mentioned previously. Thank you

Werner_E
25-Diamond I
(To:fahmed-2)

Still not clear!

 

So the input (apart from the vector) is a value, not an index, but the output is an index, correct?

So a new question arouses: What, if the  input value is a number which does not occur in the vector? What should the function return? Error message, NaN, ORIGIN-1 (an invalid index)

 

I'm referring to my answers above now

What should be returned if no suitable value is found? Maybe ORIGIN-1 or maybe NaN?

This is the case, if the vector is [1;2;3;4;-5;6] and your input is 3. There is no sign change before the 3

 

If b), what should be the output when the input is a value which occurs multiple times? I in your example vector, what would be the out for -1? Should it be the index of the 1 or the index of the 3 or both in a result vector? What is the output if the input is 1?

 


@fahmed-2 wrote:

yes, it is considered as a sign change.

So, just to be sure: [4;0;5] is a vector without any sign changes and [-4;0;-5] is considered a vector with two sign changes, correct?

 


Till now, my array will have different signed numbers so it will not be a case

This does not matter as the function to be written has to cope with all eventualities. And even if there are always values with different signs in your vector it might happen that there are no sign changes in front of a specific value as written above. Furthermore you may chose the very first element of the vector which has no predecessor and of course no sign change before. It must also be specified how the program should react in this case.

fahmed-2
13-Aquamarine
(To:Werner_E)

I am sorry I did not explain clearly. The main focus of my problem is to determine the index number when it had experienced a sign change in the latest past.  If the problem does not experience a sign change then I have a separate approach for that. In my analysis, I have encountered an array which had several positive numbers and several negative numbers together and I need to find the index number where it last experienced a sign change. That is why I have shown the attached example as, if I work with "-4" it has to find the index number of "2", not "3". Similarly, if I work with "4" it has to find the index number of "-5", not "-4". If you are concerned about what if the input does not match the given array then please consider that the input is based on the given array, it will not input anything outside that. And "0" is considered as a positive number.

LucMeekes
23-Emerald III
(To:fahmed-2)

So your example "array" contains the numbers 1,-1,3,-1,2,-2,-3,-4,1,-5,1 and 4.

What do you mean by: 'work with "-4" ', and how does that relate to the index numbers of "2" and "3"?

Obviously "-4" cannot be an index, but the value -4 occurs as 8th element in the array, that is at index position 7, because by default index starts numbering at 0.

 

You have to be MUCH more clear about what you want.

Luc

fahmed-2
13-Aquamarine
(To:LucMeekes)

Thank you for your answer. Let me start over again. I have an array that has a set of positive and negative numbers, not index values. The user will give an input that is within the given array of numbers. In the array, some positive and some negative numbers are placed side by side as attached in the example. The program is only meant to find the index number in the "earliest previous" within the given array where it has an opposite sign. The word "work with" is meant to be "if the input is ". For example, in my array, if the user inputs "-4" the program has to trace the index number of "2", not "3". Similarly, if the user input is "4" it has to trace the index number of "-5", not "-4". The program is not concerned what if there is no "sign" change. My version is Prime 4.0.

LucMeekes
23-Emerald III
(To:fahmed-2)

Does this do the job?

 

Success!
Luc

fahmed-2
13-Aquamarine
(To:LucMeekes)

No, it is giving me the number of the opposite sign but I need the index of that opposite sign number, not the number itself

fahmed-2
13-Aquamarine
(To:fahmed-2)

I think I found a solution but I believe there is a much smarter and shorter way. Also, it does not do well if some numbers are repetitive. For example, I have two "-3" s and if I need to work with the later one, it will not give the right index value

Werner_E
25-Diamond I
(To:fahmed-2)


@fahmed-2 wrote:

I think I found a solution but I believe there is a much smarter and shorter way. Also, it does not do well if some numbers are repetitive. For example, I have two "-3" s and if I need to work with the later one, it will not give the right index value


Unfortunately you avoided to answer all of my questions, systematically one after the other.
The problem you experience with repetitive numbers was in fact already addressed by one of my questions - here is the question for the third time:

"If b), what should be the output when the input is a value which occurs multiple times? I in your example vector, what would be the out for -1? Should it be the index of the 1 or the index of the 3 or both in a result vector? What is the output if the input is 1?"

How should a program know which of the number -3's in your new example you would like to "work with" if you just provide the number -3 as input?

I can think of different ways to deal with that situation:

  1. Always use the very first occurrence of the number and ignore the others (this is what Luc's approach and yours,too, are doing).
    Given your remark it seems not to be what you want
  2. Always use the very last occurrence of the number and ignore the others. I suspect this is also not what you want to happen.
  3. The program could return the result indices for ALL of the multiple numbers in a vector.
    In your new example the result of input=-3 would be a vector with the elements 1 and 4, the indices of the second number 1 and number 2.
    If this is what you like, you would further have to decide and say if you want the result be a vector in all cases or just when there are multiple results.
  4. Provide as input the index of the number you want to work with instead of the number itself.
    For me this seems to be the most straightforward approach.

 

Another unanswered question is the decision what you want to happen if no sign change is found. In your example this is the case when your input is 1. Your approach fails with an error and Lucs function returns NaN instead of an index number.

 

fahmed-2
13-Aquamarine
(To:Werner_E)

Except the second one, I think I have addressed your questions

 

1. What, if the input value is a number which does not occur in the vector? What should the function return? Error message, NaN, ORIGIN-1 (an invalid index)

 

My reply was: If you are concerned about what if the input does not match the given array then please consider that the input is based on the given array, it will not input anything outside that

 

3. What should be returned if no suitable value is found? Maybe ORIGIN-1 or maybe NaN?

 

My reply was: If the problem does not experience a sign change then I have a separate approach for that

 

Regarding number 2 problem, I think  your solution as "Always use the very last occurrence of the number and ignore the others" will fit my logic. Luc's approach is great but as I mentioned, I need the index number of the first occurrence of opposite sign number above, not the number itself

Werner_E
25-Diamond I
(To:fahmed-2)

If you want to ignore the first few values when you chose a value which occurs multiple times in your vector, then the attached file should help.

 

Concerning your answers: Sure the question about the value which occurs multiple times in the vector was the most important one and I am glad you realized yourself at last that this is a problematic situation.

But as with the others - stating that this or that problematic situation will not occur as you will never enter an input value which has no sign change before, does not help much. In my opinion when programming a function, it must always be exactly determined what should happen in every possible input case. So even though at the moment you do not intend to input an invalid value, it must be defined what the reaction of the program should be if one day you do. The reaction could be a customized error message, or a return value which signals that something unusual had happened (could be a NaN or an invalid index value).

Here is a routine which copes with the most obvious errors and I decided that the function should fail with an appropriate error message without returning any result.

B1.png

You can find a version which uses if-statements instead of the if functions in the attached sheet.

Without any customized error messages it could be reduced to

B2.png

and here is yet another, different approach (could be a 1-liner which also works in Prime Express if you don't mind typing "ORIGIN" instead of just "O" five times) - again without error handling (which I still regard as a bad design error):

B3.png

BTW, you claimed that Luc's approach would not return the index but the value itself. Thats not true! Luc's function "f" returns the index as you demanded. He just showed after calling his function which value would be addressed. His version avoids programming and implements the necessary loops via recursive calls of an if-function. That way his approach also works in Prime Express. Its quite easy to modify his version to fit the new conditions. Additionally I got rid of the function "Sign" and changed the order of the arguments to bring it in line with my functions.

B4.png

So its up to you now to decide for one of the versions.

 

fahmed-2
13-Aquamarine
(To:Werner_E)

Thank you very much

Announcements

Top Tags