Answer to Question #179147 in Python for jayanth

Question #179147

Index of Last Occurrence

Write a program to print the index of the last occurrence of the given number N in the list.Input


The first line of input will contain space-separated integers.

The second line of input will contain an integer N.Output


The output should be the index of the last occurrence of the number N.Explanation


For example, if the given list of numbers and N are

2 4 5 6 7 8 2 4 5 2 3 8
2


Number 2 present at index locations 0, 6, 9, as the last occurrence, is at index 9. So the output should be 9.

Sample Input 1

2 4 5 6 7 8 2 4 5 2 3 8

2

Sample Output 1

9

Sample Input 2

65 87 96 31 32 86 57 69 20 42 32 32

32




1
Expert's answer
2021-04-07T10:04:21-0400
#The first line of input will contain space-separated integers.
integersStr=input("")
integers=integersStr.split(' ')
#The second line of input will contain an integer N.
N=int(input(""))
#Search the index of the last occurrence of the number N.
indexLastOccurrenceNumber=-1
for i, number in enumerate(integers, start=0):
    if int(number)==N:
        indexLastOccurrenceNumber=i


#Display the index of the last occurrence of the number N.
print(str(indexLastOccurrenceNumber))

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS