# Discrete Structures Class

• September 8th, 2013, 07:21 PM
rharvell
Discrete Structures Class
Write an algorithm that returns the index of the last occurrence of the value key in a sequence s1, …,sn. If key is not in the sequence, the algorithm returns 0. Example, if the sequence is 12 11 12 23 and key is 12, the algorithm returns 3.

I need full explanation because I am not understanding this class just yet.
• September 8th, 2013, 08:50 PM
Hikaros
Re: Discrete Structures Class
You may find these quite useful List of Videos for Intermediate Java

specially the first 2 and then starting from collections (they are short, like 5 minutes long each)
• September 8th, 2013, 09:13 PM
jps
Re: Discrete Structures Class
Quote:

Originally Posted by rharvell
Write an algorithm that returns the index of the last occurrence of the value key

That means start at the end, or the last element, and work toward the front looking for the requested value. For example, in 12 11 12 23, it means start at 23, is 23 the key (12), no, keep looking. Move back to 12. Is 12 the key (12), yes. Return it. Note that it is in index 2 not index 0. Also beware that this is to return the key, not the positional index, and 0 represents "not found"
Quote:

Originally Posted by rharvell
in a sequence s1, …,sn.

This just means the set of elements can be any given length.
Quote:

Originally Posted by rharvell
If key is not in the sequence, the algorithm returns 0.

If the requested key (12 in the example) is not found, return 0.