Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9225839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:40:46+00:00 2026-06-18T04:40:46+00:00

May be a little too many for today.. but meh. This problem is pretty

  • 0

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

  1. [“hope”, “hop”, “hopefully”, “test”, “testing”] will return [‘hop’]
  2. [“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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T04:40:47+00:00Added an answer on June 18, 2026 at 4:40 am

    how bout

    print [my_list[i] for i in range(1,len(my_list)) if my_list[i] in my_list[i-1]]
    

    for example

    >>> def findSubStrs(my_list):
    ...     return [my_list[i] for i in range(1,len(my_list)) if my_list[i] in my_list[i-1]]
    >>> findSubStrs(["hope", "hop", "hopefully", "test", "testing"] )
    ['hop']
    >>> findSubStrs(["hopefully", "hope", "hop", "testing", "test"])
    ['hope', 'hop', 'test']
    

    to do this without a list comprehension you can just use a simple loop

    for i in range(1,len(my_list)):
        if my_list[i] in my_list[i-1]:
            print my_list[i]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This problem may be a little broad, but I'm going to go ahead and
This question may seem a little bit stackoverflow-implementation specific, but I have seen a
The title may be a little confusing, but I don't know how else to
I realize something like this has been asked, but this may be a little
The problem may appear in other browsers too, I haven't checked yet. Sorry but
I have researched this topic, but am afraid I have too little experience with
This may be a little unconventional way of asking for help but my code
Forgive me in advance if this question is a little too open-ended, but I've
My question may be a little confusing, but anyway. My school is going to
This may be a little hard to describe since I don't have a sample.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.