Skip to content

Commit b40eaab

Browse files
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.
1 parent 478f4df commit b40eaab

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

100+ Python challenging programming exercises.txt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2371,5 +2371,35 @@ solutions=solve(numheads,numlegs)
23712371
print solutions
23722372

23732373
#----------------------------------------#
2374+
Level 2:
2375+
Slightly difficult for beginners but doable,
2376+
however, should be relatively easy for
2377+
experienced programmers.
23742378

2379+
Question:
2380+
Write a program that iterates through a 2D matrix
2381+
and return the last occurrence location of a integer.
2382+
2383+
Example 1:
2384+
[[3,2,3],[0,5,3],[9,1,7]] --> Return the last occurrence of the integer 3.
2385+
The program should return (1,2).
2386+
2387+
Example 2:
2388+
[[3,2,3],[0,5,3],[9,1,7]] --> Return the last occurrence of the integer 1.
2389+
The program should return (2,1).
2390+
2391+
Example 3:
2392+
[[3,2,3],[0,5,3],[9,1,7],[2,0,2]] --> Return the last occurrence of the integer 2.
2393+
The program should return (3,2).
23752394

2395+
Hints:
2396+
Use range(), len(), and mutiple loops to traverse
2397+
the mutiple lists within the list.
2398+
2399+
Solution:
2400+
def lastoccurrence(numlist, num):
2401+
for i in range(len(numlist)-1,-1,-1):
2402+
for j in range(len(numlist[i])-1,-1,-1):
2403+
if numlist[i][j] == num:
2404+
return i,j
2405+
#----------------------------------------#

0 commit comments

Comments
 (0)