diff --git a/100+ Python challenging programming exercises.txt b/100+ Python challenging programming exercises.txt index 97af5aaf..2c4ac5df 100644 --- a/100+ Python challenging programming exercises.txt +++ b/100+ Python challenging programming exercises.txt @@ -2371,5 +2371,35 @@ solutions=solve(numheads,numlegs) print solutions #----------------------------------------# +Level 2: +Slightly difficult for beginners but doable, +however, should be relatively easy for +experienced programmers. +Question: +Write a program that iterates through a 2D matrix +and return the last occurrence location of a integer. + +Example 1: +[[3,2,3],[0,5,3],[9,1,7]] --> Return the last occurrence of the integer 3. +The program should return (1,2). + +Example 2: +[[3,2,3],[0,5,3],[9,1,7]] --> Return the last occurrence of the integer 1. +The program should return (2,1). + +Example 3: +[[3,2,3],[0,5,3],[9,1,7],[2,0,2]] --> Return the last occurrence of the integer 2. +The program should return (3,2). +Hints: +Use range(), len(), and mutiple loops to traverse +the mutiple lists within the list. + +Solution: +def lastoccurrence(numlist, num): + for i in range(len(numlist)-1,-1,-1): + for j in range(len(numlist[i])-1,-1,-1): + if numlist[i][j] == num: + return i,j +#----------------------------------------#