Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Exercise 7 (fun_exercise_7)
- The function takes in input a list of strings x and returns an integer ptr if and only if x[ptr]
- is a substring of at least one of the other strings in x. Otherwise, it returns -1.
- ptr is a pointer
- if one string is a substring of another, then return the position of the substring as ptr
- ################################
- my idea:
- so what i would do is use a while i < len(x) loop to find out if
- x[y] is substring of x[z], return y
- is x[y] a substring of x[z+1]?
- keep increasing the z until either a substring has been found, or until list has been completely indexed;
- if this is the case then increase x[y] by 1
- increase x[y] and repeat the same operation
- until the list has been completely searched
- if still no matches, return -1
- """
- def fun_exercise_7(x):
- for ptr in range(len(x)):
- for str in x:
- if x[ptr] != str:
- if str in x[ptr]:
- return ptr
- return -1
- #
- print(fun_exercise_7(["goat"])) # -1
- print(fun_exercise_7(["soul", "soulmate", "origin"])) # 0
- print(fun_exercise_7(["FASER", "submission", "online", "drive", "frequent"])) # -1
- print(fun_exercise_7(["banana", "applejuice", "kiwi", "strawberry", "apple", "peer"])) # 4
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement