May be a little too many for today.. but meh.
This problem is pretty confusing to me. This function takes a list of strings as a parameter and returns every string that is a substring of the one that precedes it. So
- [“hope”, “hop”, “hopefully”, “test”, “testing”] will return [‘hop’]
- [“hopefully”, “hope”, “hop”, “testing”, “test”] will return [‘hope’, ‘hop’, ‘test’]
Excuse the mess of code here, I’m still learning.
def findSubStrs(lst):
'list ==> list, return list of all strings that are substrings of their predecessor in lst'
res = []
for a in lst:
if len(int(a-1)) > len(lst):
res = res + [a]
return res
I figured that len(int(a-1)) would work to check the preceding string, but I just got the error message “TypeError: unsupported operand type(s) for -: ‘str’ and ‘int'” The only result I found that worked was len(a) < 3 or some other int, but that doesn’t return everything I need.
how bout
for example
to do this without a list comprehension you can just use a simple loop