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?

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


  count = 1
  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]
       if str2.find(sub_str) > -1:
         if sub_str == str:
           print sub_str
       
           
main()
while not is_indented(text):
    print("Please apply code tag to your code!")

Maybe more exactly (code start tag misspelled to get the code tag work here):

while poster.isnewbie() and post.hascode() and not '[ CODE]' in post:
    print("Be kind and add code tags around your code when posting or you lose indention")

Edited 6 Years Ago by pyTony: n/a

This article has been dead for over six months. Start a new discussion instead.