Python Forum
Udacity while loop question
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Udacity while loop question
#1
Hi!
I'm doing the Intro to Computer Science course on Udacity and I'm trying to:

#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

# Make sure your procedure has a return statement.

This is my attempt, but it won't work. Can anyone explain what I did wrong?

def find_last(text,s):
   x = -1
   b = text[x]
   while b != s:
       x = x-1
   return x
Reply
#2
the following line is not valid code
b = text[x]
what is this supposed to do?
Reply
#3
b = text[x]

This is supposed to assign to b the letter of the string at position x, which is assigned to -1 at the top.
I've tested it and it works though.

text = 'abcabcabcabc'
x = -1
b = text[x]
print b
The result that is printed is 'c'.
Reply
#4
(Jul-21-2017, 01:00 PM)Larz60+ Wrote: the following line is not valid code
b = text[x]
what is this supposed to do?

Actually it is :-)

>>> text = 'sometext'
>>> x=-1
>>> b=text[x]
>>> b
't'
>>>
Reply
#5
The point is that when you change x, b is not changed. So you are always testing the last letter of the string (b). You want to test directly on text[x].

However, that is only going to work for single character search strings. If you have a two character search string, like find_last('aababcabcd', 'ab'), it will never match. You might want to look at the startswith method of strings.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
And stop using single letter variables.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#7
Luckily, that's actually already built into python.

>>> find_last = lambda haystack, needle: haystack.rfind(needle)
>>> find_last("ababababspam", "ab")
6
>>> find_last("ababababspam", "cat")
-1
>>> help(str.rfind)
Help on method_descriptor:

rfind(...)
   S.rfind(sub[, start[, end]]) -> int

   Return the highest index in S where substring sub is found,
   such that sub is contained within S[start:end].  Optional
   arguments start and end are interpreted as in slice notation.

   Return -1 on failure.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Many iterations for loop question adesimone 9 1,819 Nov-12-2022, 07:08 PM
Last Post: deanhystad
  Please check whether the code about the for loop question is correct. (SyntaxError) lilliancsk01 10 2,568 Nov-08-2022, 01:25 PM
Last Post: deanhystad
  Beginner Python Question: FIzz Buzz using while loop camoyn13 2 1,789 Sep-20-2022, 09:00 AM
Last Post: deanhystad
  Repeat question (for loop) rs74 7 6,458 Jun-17-2020, 03:17 PM
Last Post: rs74
  Question about running comparisons through loop from input value Sunioj 2 2,398 Oct-15-2019, 03:15 PM
Last Post: jefsummers
  while loop question Tripler 4 2,939 Jul-24-2018, 06:37 AM
Last Post: buran
  Noob Udacity Help CastyMcBoozer 3 2,964 Apr-26-2018, 10:01 PM
Last Post: micseydel
  Loop question kraven 3 3,621 Sep-10-2017, 07:31 AM
Last Post: wavic
  Question about loop Pires 4 3,547 Jul-23-2017, 03:01 AM
Last Post: Pires

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020