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
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.
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.
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! .)
–> 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
Hi fellow coders! I passed the challenge with this code (didn’t peek) =>
def substring_between_letters(word, start, end):
start = word.find(start)
end = word.find(end)
if not start == -1 and not end == -1:
return word[1 + start:end]
else:
return word
Can someone tell me if this code is legit?
I find puzzling this part word[1 + start:end]; not so sure what I did there.
Came up with this, which will return word, unless it can find start, and find end, and the index of start is smaller than the index of n.
def substring_between_letters(word, start, end):
if start in word and end in word and word.find(start)<word.find(end):
arg1 = word.find(start)
arg2 = word.find(end)
return word[arg1+1:arg2]
else:
return word
I guess if the index of end is smaller that the index of start and you still want the substring between the two first instances of the letters, you could have this:
def substring_between_letters(word, start, end):
if start in word and end in word and word.find(start)<word.find(end):
arg1 = word.find(start)
arg2 = word.find(end)
return word[arg1+1:arg2]
if start in word and end in word and word.find(start)>word.find(end):
arg1 = word.find(end)
arg2 = word.find(start)
return word[arg1+1:arg2]
else:
return word
In your approach you first get the index at which you found the character that is the same as in your variable start.
Later on you want to return the value(characters) between your start and end variable, so basically the next index you found for start and the previous index you found end. To achive this and the way slicing works you need to add +1 to the index in start. No need to do this for end because it will be sliced to the previous index on its own. Look into slicing for a more professional explanation
For the excersize you code seems legit to me, only did i think you if selection is hard to read. In my approach i included one more fault-check e.g. invalid order of input or same letter input.
def substring_between_letters(word, start, end):
entry = word.find(start) + 1
exit = word.find(end)
if entry == -1 or exit == -1:
return "At least one letter not included"
if entry >= exit:
return "Invalid Input"
return word[entry:exit]
def substring_between_letters(word, start, end):
# simplify the notation to mark the location of `start` and `end` in `word`
s = word.find(start) # a value of `-1` means letter is not in string
e = word.find(end) # a value of `-1` means letter is not in string
# make sure both `start` and `end` are in `word`
if (s != -1) and (e != -1):
# return all letters between `start` and `end` (print nothing if `s==e`; return the substring in order even if `s > e`)
if s < e:
return word[s+1 : e]
elif s > e:
return word[e+1 : s]
else:
return ''
# if either `start` or `end` are not in string, return `word`
return word
Here are the tests I used to check my code (I added some that the lesson did not include):
# Test 1
print(substring_between_letters("apple", "p", "e"))
# Test 2
# should print "pl"
# Test 3
print(substring_between_letters("apple", "p", "c"))
# should print "apple"
# Test 4
print(substring_between_letters("apple", "q", "l"))
# should print "apple"
# Test 5
print(substring_between_letters("apple", "e", "p"))
# should print "pl"
# Test 6
print(substring_between_letters("apple", "l", "l"))
# should print ""