Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...I also believe that we all can contribute to each other's growth by sharing knowlege and experiences. I would love to take my skills and help people around the world solve problems..."

Geography

Where in the world do Tek-Tips members come from?
tpkemme (Programmer)
13 Oct 10 17:03
I have a pretty simple coding function that returns all possible substrings of a string including the string itself

def main():
    str = raw_input ("Enter first string: ")
    size = len(str)
    for sub_len in range (1, size + 1):
      for idx in range (0, size - sub_len + 1):
        sub_str = str[idx : idx + sub_len]
        print sub_str
          
main()

the current output looks like this for the string 'abcd'
a
b
c
d
ab
bc
cd
abc
bcd
abcd

my question is, how can i get the program to print the substrings in the opposite order in terms of length? i.e.:

abcd
abc
bcd
ab
bc
cd
a
b
c
d
IPGuru (TechnicalUser)
14 Oct 10 5:26
My quick hack of your code gives

CODE

    str=raw_input('Enter String?')
    size=len(str)+1
    for sublen in range(size,0,-1):
        for idx in range(size-sublen):
            print "string %s" %str[idx:idx+sublen]

seems to work

I do not Have A.D.D. im just easily, Hey look a Squirrel!

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close