Skip to content

Instantly share code, notes, and snippets.

@aaronshaver
Created April 11, 2022 21:09
Show Gist options
  • Save aaronshaver/7593f2afb007eadc0a23a4771036799a to your computer and use it in GitHub Desktop.
Save aaronshaver/7593f2afb007eadc0a23a4771036799a to your computer and use it in GitHub Desktop.
find() versus index() in Python
# find() doesn't return an exception, and is perhaps better suited for cases
# where we don't know if the substring exists; it returns -1 if not found
# index() will throw a ValueError exception if the value being searched for doesn't exist
class Solution:
def reversePrefix(self, word: str, ch: str) -> str:
return word[:word.find(ch) + 1][::-1] + word[word.find(ch) + 1:]
# this problem says to reverse the part of the string from 0 to first occurence of character
# then return that reversed output with the rest of the string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment