Elisey Ozerov
Elisey Ozerov

Reputation: 230

Comparing two different solutions to a quiz

I've been going through a course on CS on Udemy, and got a quiz to solve.

Here's what is says:

Define a procedure, find_last, that takes as input two strings, a search string and a target string, and returns the last position in the search string where the target string appears, or -1 if there are no occurrences.

Example: find_last('aaaa', 'a') returns 3

The first solution is mine, the last is theirs. My question is, in what ways is theirs better than mine, and how could I think next time when solving this kind of a problem, so I would come to a solution faster and better. (I've spent at least 45 min thinking of a solution).

def find_last(s, t):
    some = s.find(t)
    i = some
    while s.find(t, i) != -1:
        some=s.find(t, i)
        i=i+1
   return some

def find_last(s,t):
    last_pos = -1
    while True:
        pos = s.find(t, last_pos+1)
        if pos==-1:
            return last_pos
        last_pos = pos

Here are the examples:

print find_last('aaaa', 'a') # returns 3
print find_last('aaaaa', 'aa') # returns 3
print find_last('aaaa', 'b') # returns -1
print find_last("111111111", "1") # returns 8
print find_last("222222222", "") # returns 9
print find_last("", "3") # returns -1
print find_last("", "") # returns 0

Upvotes: 1

Views: 88

Answers (2)

godaygo
godaygo

Reputation: 2268

I think the main idea of the course is to learn algorithms and this exercise is good to start with (regardless if the solution is not the most efficient way to resolve such problems in a current programming language). So, their solution is better because they 'jump' during iteration, and you steps by 1, after you found index of first occurrence of t. I will try to explain with simple example:

You have got a string s = 'abbbabbba' and you need to find 'a'. When you use your function, let it be fun_last_1(s, 'a'):

def find_last_1(s, t):
    some = s.find(t)           # some = 0
    i = some                   # i = 0
    while s.find(t, i) != -1:  # ok
        some=s.find(t, i)      # you again start from 0 (some = 0) ???
        i=i+1                  # and from here you steps by 1
                               # put print(i) in this row 
    return some                # in total you will have 9 iterations

you will need 9 iterations to achieve final result in this case. While they need only 4, lets add a print statement inside their loop:

def find_last_2(s,t):
    last_pos = -1                   
    while True:                     
        pos = s.find(t, last_pos+1) 
        print(pos)                   # add print here
        if pos==-1:                 
            return last_pos         
        last_pos = pos               # here they jumps

>>> find_last_2(s,'a')
0
4
8
-1

# and of course it will return 8

You can also add this debuggy print() inside the loop in your function and compare:

>>> find_last_1(s, 'a')
1
2
3
4
5
6
7
8
9

# and of course it will return 8

Hope this will help you to understand the difference.

Upvotes: 1

Jean-François Fabre
Jean-François Fabre

Reputation: 140246

No need to code anything, just use python built-in string "right find":

print('aaaa'.rfind('a'))

result: 3

print('bbbbb'.rfind('a'))

result: -1

also works for "more-than-1-char" search strings of course

print('bbbxb'.rfind('bx'))

result: 2

Of course, in most widespread platforms like Linux or Windows, the processing is native: means you can't beat the speed.

Edit: someone kindly suggested to code your find_last in one line. Very nice:

find_last = lambda x,y: x.rfind(y)

Upvotes: 4

Related Questions