Skip to content

Update 100+ Python challenging programming exercises.txt #147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions 100+ Python challenging programming exercises.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
#----------------------------------------#