H2Q6 - What is incorrect in this code?

def find_last(s,t): r=-1 p=0 while p!=-1: p=s.find(t,p+1) if p!=-1: r=p if s=="": if t=="": r=0 else: r=-1 return r

asked 12 Mar '12, 04:41

Mirko-1's gravatar image

Mirko-1
11
accept rate: 0%

edited 12 Mar '12, 04:42


2 Answers:

find_last("aaa", "aaa") --> -1 but it should give 0

You never check the first position in the string, since the first time you do a find, you input p+1 which is 1.

link

answered 12 Mar '12, 05:03

fnenu-1's gravatar image

fnenu-1 ♦♦
18.5k1981231

edited 12 Mar '12, 05:04

I just wrote that edit :P :P :P

(12 Mar '12, 05:06) Benoît Destrubé Beno%C3%AEt%20Destrub%C3%A9's gravatar image

This code won't work for find_last ('abbbb','a')
(it only starts looking after the first character)

link

answered 12 Mar '12, 05:03

Beno%C3%AEt%20Destrub%C3%A9's gravatar image

Benoît Destrubé
2.6k21555

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,273
×23

Asked: 12 Mar '12, 04:41

Seen: 107 times

Last updated: 12 Mar '12, 05:06