How to handle that a start and end in the wrong order?

How should our function handle other cases, like where the “start” character appears after the “end character”?

For example, should this code:

substring_between_letters(word = "adogb", start  = "b", end = "a")

return “dog”, or simply return the word “adogb”

Another case is if the two arguments appear in the proper order, but only after they appear in the wrong order, like

substring_between_letters(word = "adogbruna", start = "b", end = "a"

Should this print “dog”, “run”, or the word?

Substring Between

print (substring_between_letters("adogbruna", "b", "a"))
# adogbruna
print (substring_between_letters("dogbruna", "b", "a"))
# run

The first example returns the full word because a comes before b, and we cannot have the end at the beginning.

The second example we remove the starting a, and get the result we expect since a is at the end, and b does precede it.

Similarly, your first example should also return the full word. Switch the a and b around and it will return dog.

2 Likes
def substring_between_letters(word, start, end):
    start = word.find(start)
    end = word.find(end)
    if start and end != -1:
        #print(word[start + 1 : end])
        return word[start + 1: end]
    else:
        return word

One issue I see with that approach is when start and end are the same letters.

substring_between_letters('mountain','n','n')

The return is ''


Played around with the code to work past this issue…

def substring_between_letters(word, start, end):
   before = word.find(start) + 1
   if before == 0:
     return word
   after = word[before:].find(end)
   if after == -1:
     return word
   return word[before : after + before]

print (substring_between_letters('mountain','n','n'))    # tai
4 Likes

what is wrong with my code?
keep present this message, but the parameters are exactly the same

I asked about this in support but they told me to go to forums, sooooo…Bump!

Presumably the function crashes (can’t see it all) and presumably the error message is testing for one thing, but reports something else. So both your code and codecademy’s code each has a bug.

specifically the test contains this code:

except TypeError:
  fail_tests("Your function should accept a parameter `word` and parameters `start` and `end`")

which is suggesting that a TypeError means that there are missing parameters … no it doesn’t, and function parameters can be inspected so there is no reason to do something this crude.

1 Like

When starting this exercise i though but if there’s something like this:

substring_between_letters(“applee”, “p”, “e”)

It might end up printing “pl” instead of “ple” if i will use word.find(end) because it will return the first index of “end” in “word”
So I ended up doing like this:

def substring_between_letters(word, start, end):
  if start in word:
    start_index = word.find(start)
    if end in word:
      for i in range(len(word)):
        if end == word[i]:
          end_index = i
      new_word = word[start_index+1:end_index]
      return new_word
    else:
      return word
  else:
    return word

this part loops through letters in “word” and saves the last index of a letter that is equal to the letter of “end”.

      for i in range(len(word)):
        if end == word[i]:
          end_index = i

hope this will be useful as a different approach for others.

This should handle:

  1. Start and End in a wrong order

  2. Start and End are the same letters

What about searching for start in word, then cutting out the word for what is left after the start, only then searching for ending in what is left

def substring_between_letters(word, start, end):
  startIndex = word.find(start)
  if startIndex == -1:
    return word
  endIndex = word[startIndex + 1:].find(end)
  if endIndex == -1:
    return word
  return word[startIndex + 1: startIndex + 1 + endIndex] 

when I try something like example one, it always returns nothing instead of the full word. Why is that so? I am a bit confused now. Help would be greatly appreciated! .)

Please share what code you are running for us to see. Thanks.

So, if I use this code:

def random_funtion(string, start, end):
index_start = string.find(start)
index_end = string.find(end)
if index_start or index_end == -1:
return string
return string[index_start + 1:index_end]

print(random_funtion(“adogbhjam”, “b”, “a”))

–> it returns the original string but I don’t get why because neither of the indexes is == -1 .
index_start is 4 and index_end is 0.
–> if I delete the if condition, then it just returns nothing

We need to make the same comparison in the first operand as second one.

got it. thank you :grinning:

1 Like