From b40eaab3520de5adae9d65938bdee8e7685bcd94 Mon Sep 17 00:00:00 2001 From: Rohit Barua <83600150+Rohit-beep-droid@users.noreply.github.com> Date: Fri, 7 May 2021 15:04:59 -0400 Subject: [PATCH] Update 100+ Python challenging programming exercises.txt Added a question and solution for a program called lastoccurrence(). Bit more difficult for beginners, however, it is doable. This should not be much of a challenge for experienced programmers. --- ...thon challenging programming exercises.txt | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) 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 +#----------------------------------------#