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

You can reverse the for loop and use a step of -1, or append the results to a list and reverse the list before printing.

so now that i have gotten farther in the code, it prints the substrings correctly, but i can't get the program to stop correctly. basically, i'm looking for the longest common substring.

def main():
input1 =raw_input('Enter string: ')
input2 = raw_input('Enter second string: ')
if input1 > input2:
str = input2
str2 = input1
elif input2 > input1:
str = input1
str2 = input2
else:
str = input1
str2 = input2
print str, str2


size=len(str)+1
for sub_len in range(size, 0 ,-1):
for idx in range(0, size - sub_len):
sub_str = str[idx : idx + sub_len]

for x in range(len(str)):
if str2.find(sub_str) > -1 and len(sub_str) == len(str) - x:
print sub_str
break

i need help with last part. it correctly finds all the substrings but it doesn't stop at the highest one but instead keeps going. since x is in the range 0-len(str), should the first value of x that is plugged into the following condition be 0? and if it is, how do i get it to stop as soon as the condition is true? because either i put break on the same column as print sub_str and it prints all the substrings or i put it on the same column as the if conditional and it only works if the substring is one of the original strings. any helP?

so now that i have gotten farther in the code, it prints the substrings correctly, but i can't get the program to stop correctly. basically, i'm looking for the longest common substring.

i need help with last part. it correctly finds all the substrings but it doesn't stop at the highest one but instead keeps going. since x is in the range 0-len(str), should the first value of x that is plugged into the following condition be 0? and if it is, how do i get it to stop as soon as the condition is true? because either i put break on the same column as print sub_str and it prints all the substrings or i put it on the same column as the if conditional and it only works if the substring is one of the original strings. any helP?

def main():
  input1 =raw_input('Enter string: ')
  input2 = raw_input('Enter second string: ')
  if input1 > input2:
      str = input2
      str2 = input1
  elif input2 > input1:
      str = input1
      str2 = input2
  else:
      str = input1
      str2 = input2
  print str, str2
  
  
  size=len(str)+1
  for sub_len in range(size, 0 ,-1):
    for idx in range(0, size - sub_len):
       sub_str = str[idx : idx + sub_len]
      
       for x in range(len(str)):
         if str2.find(sub_str) > -1 and len(sub_str) == len(str) - x:
           print sub_str
         break

I do not understand your programs logic but there is thread of writing lcs with my code (which ihatehippies optimized little) for it:
http://www.daniweb.com/forums/post1242448.html#post1242448

if input1 > input2:
      str = input2
      str2 = input1
  elif input2 > input1:
      str = input1
      str2 = input2
  else:
      str = input1
      str2 = input2

Does same as this:

str, str2 = (input1, input2) if input2 >= input1 else (input2, input1)

That is it put str the inputwhich is earlier in alphabetic order in str and other one to str2, reason for this I do not understand.
str is very bad variable name as you are hiding str function to produce strings from objects.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.