HW 2.6 works for all cases but marked incorrect..

def find_first(search,target):
    first=search.find(target)
        if first==-1:
            print -1
            return -1
        else:
            return search.find(target)

def find_last(search,target):
    if find_first(search,target)!=-1:
        next=find_first(search,target)
        while 1:
            a,next=search.find(target,next),search.find(target,next+1)
            #print next
            if next==-1:
                print a
                return a

asked 07 Mar '12, 20:53

Ashenafi%20Tadesse%20Mulat's gravatar image

Ashenafi Tad...
376
accept rate: 100%

edited 07 Mar '12, 21:19

Thom%20Blake's gravatar image

Thom Blake
4.4k104681


4 Answers:

The problem is, that you have two print calls (one in find_first, the other in find_last) which outputs two values on one call to find_last, which is not correct, you should always print just one value on every call.

link

answered 07 Mar '12, 20:59

implicit_knowledge's gravatar image

implicit_kno...
3.7k82858

One code path in your find_last function fails to return value. That is find_last never execute return statement when find_first returns -1.

Correct version:

def find_last(search,target):
  if find_first(search,target)!=-1:
    next=find_first(search,target)
    while 1:
        a,next=search.find(target,next),search.find(target,next+1)
        #print next
        if next==-1:
            print a
            return a
  else:
    return -1
link

answered 07 Mar '12, 21:05

red75prim's gravatar image

red75prim
475

Your code print None instead of -1 when there is no match, try to check this:

print find_last('aaaa', 'b')

link

answered 07 Mar '12, 21:00

Lele's gravatar image

Lele
2.7k31534

i tried it .. it prints -1 .. please check it ..

(07 Mar '12, 21:10) Ashenafi Tad... Ashenafi%20Tadesse%20Mulat's gravatar image

thank you guys for your fast response..I will try to re-write it based on your suggestion

link

answered 07 Mar '12, 21:16

Ashenafi%20Tadesse%20Mulat's gravatar image

Ashenafi Tad...
376

Your answer
Question text:

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×15,287
×23

Asked: 07 Mar '12, 20:53

Seen: 94 times

Last updated: 07 Mar '12, 21:19