Advertisement
Guest User

Untitled

a guest
Nov 12th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. """
  2. Exercise 7 (fun_exercise_7)
  3. The function takes in input a list of strings x and returns an integer ptr if and only if x[ptr]
  4. is a substring of at least one of the other strings in x. Otherwise, it returns -1.
  5.  
  6. ptr is a pointer
  7.  
  8. if one string is a substring of another, then return the position of the substring as ptr
  9.  
  10. ################################
  11. my idea:
  12.  
  13. so what i would do is use a while i < len(x) loop to find out if
  14. x[y] is substring of x[z], return y
  15. is x[y] a substring of x[z+1]?
  16. keep increasing the z until either a substring has been found, or until list has been completely indexed;
  17. if this is the case then increase x[y] by 1
  18. increase x[y] and repeat the same operation
  19. until the list has been completely searched
  20. if still no matches, return -1
  21. """
  22.  
  23.  
  24. def fun_exercise_7(x):
  25. for ptr in range(len(x)):
  26. for str in x:
  27. if x[ptr] != str:
  28. if str in x[ptr]:
  29. return ptr
  30. return -1
  31.  
  32.  
  33.  
  34. #
  35.  
  36. print(fun_exercise_7(["goat"])) # -1
  37. print(fun_exercise_7(["soul", "soulmate", "origin"])) # 0
  38. print(fun_exercise_7(["FASER", "submission", "online", "drive", "frequent"])) # -1
  39. print(fun_exercise_7(["banana", "applejuice", "kiwi", "strawberry", "apple", "peer"])) # 4
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement