Skip to main content
Code Review

Return to Question

added 570 characters in body; edited tags
Source Link
200_success
  • 145.5k
  • 22
  • 190
  • 478

I am currently trying to teach myself some programming. I have started to work with Python by doing this challenge.

For each test case, I need to find the sum of the self-similarities of a string with each of its suffixes. For example, given the string ababaa, the self-similarity scores are

  • 6 (because ababaa = ababaa)
  • 0 (because ababaababaa)
  • 3 (because ababaa and abaa share three initial characters)
  • 0 (because ababaabaa)
  • 1 (because ababaa and aa share one initial character)
  • 1 (because ababaa and a share one initial character)

... for a total score of 11.

When I test run it works out fine, however when I run this code with longer strings it takes too long time to run, so the site shuts down the program. Each input string consists of up to 100000 lowercase characters.

Is this because this code make unnecessary loops? Or what else might the problem be here?

# Solution.py for https://www.hackerrank.com/challenges/string-similarity
import sys
for line in sys.stdin:
 if line.islower():
 line = line[:-1] # line will be compared to suffix
 line_length = len(line)
 points = 0
 for s in xrange(0,line_length):
 suffix = line[s:]
 suffix_length = len(suffix)
 count = 1
 while count < suffix_length+1:
 if line.startswith(suffix[:1]): # if line and first character of suffix mach
 if line.startswith(suffix[:suffix_length]):
 points += len(suffix[:suffix_length])
 break
 else:
 if line.startswith(suffix[:count+1]):
 count += 1
 elif line.startswith(suffix[:count]):
 points += len(suffix[:count])
 break
 else:
 break
 print points

I am currently trying to teach myself some programming. I have started to work with Python by doing this challenge. When I test run it works out fine, however when I run this code with longer strings it takes too long time to run, so the site shuts down the program.

Is this because this code make unnecessary loops? Or what else might the problem be here?

# Solution.py for https://www.hackerrank.com/challenges/string-similarity
import sys
for line in sys.stdin:
 if line.islower():
 line = line[:-1] # line will be compared to suffix
 line_length = len(line)
 points = 0
 for s in xrange(0,line_length):
 suffix = line[s:]
 suffix_length = len(suffix)
 count = 1
 while count < suffix_length+1:
 if line.startswith(suffix[:1]): # if line and first character of suffix mach
 if line.startswith(suffix[:suffix_length]):
 points += len(suffix[:suffix_length])
 break
 else:
 if line.startswith(suffix[:count+1]):
 count += 1
 elif line.startswith(suffix[:count]):
 points += len(suffix[:count])
 break
 else:
 break
 print points

I am currently trying to teach myself some programming. I have started to work with Python by doing this challenge.

For each test case, I need to find the sum of the self-similarities of a string with each of its suffixes. For example, given the string ababaa, the self-similarity scores are

  • 6 (because ababaa = ababaa)
  • 0 (because ababaababaa)
  • 3 (because ababaa and abaa share three initial characters)
  • 0 (because ababaabaa)
  • 1 (because ababaa and aa share one initial character)
  • 1 (because ababaa and a share one initial character)

... for a total score of 11.

When I test run it works out fine, however when I run this code with longer strings it takes too long time to run, so the site shuts down the program. Each input string consists of up to 100000 lowercase characters.

Is this because this code make unnecessary loops? Or what else might the problem be here?

# Solution.py for https://www.hackerrank.com/challenges/string-similarity
import sys
for line in sys.stdin:
 if line.islower():
 line = line[:-1] # line will be compared to suffix
 line_length = len(line)
 points = 0
 for s in xrange(0,line_length):
 suffix = line[s:]
 suffix_length = len(suffix)
 count = 1
 while count < suffix_length+1:
 if line.startswith(suffix[:1]): # if line and first character of suffix mach
 if line.startswith(suffix[:suffix_length]):
 points += len(suffix[:suffix_length])
 break
 else:
 if line.startswith(suffix[:count+1]):
 count += 1
 elif line.startswith(suffix[:count]):
 points += len(suffix[:count])
 break
 else:
 break
 print points
edited tags
Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
typos and title
Source Link
rolfl
  • 98.1k
  • 17
  • 219
  • 419

How do I make this program run less unnecessary loops? Hackers Ranking String Similarity

I am currently trying to teach myself some programming. I have started to work with Python by doing this challenge. When I test run it works out fine, however when I run this code with longer strings it takes totoo long time to run, so the site shuts down the program.

Is this because this code make unnecessary loops? Or what else might the problem be here?

# Solution.py for https://www.hackerrank.com/challenges/string-similarity
import sys
for line in sys.stdin:
 if line.islower():
 line = line[:-1] # line will be compared to suffix
 line_length = len(line)
 points = 0
 for s in xrange(0,line_length):
 suffix = line[s:]
 suffix_length = len(suffix)
 count = 1
 while count < suffix_length+1:
 if line.startswith(suffix[:1]): # if line and first character of suffix mach
 if line.startswith(suffix[:suffix_length]):
 points += len(suffix[:suffix_length])
 break
 else:
 if line.startswith(suffix[:count+1]):
 count += 1
 elif line.startswith(suffix[:count]):
 points += len(suffix[:count])
 break
 else:
 break
 print points

How do I make this program run less unnecessary loops?

I am currently trying to teach myself some programming. I have started to work with Python by doing this challenge. When I test run it works out fine, however when I run this code with longer strings it takes to long time to run, so the site shuts down the program.

Is this because this code make unnecessary loops? Or what else might the problem be here?

# Solution.py for https://www.hackerrank.com/challenges/string-similarity
import sys
for line in sys.stdin:
 if line.islower():
 line = line[:-1] # line will be compared to suffix
 line_length = len(line)
 points = 0
 for s in xrange(0,line_length):
 suffix = line[s:]
 suffix_length = len(suffix)
 count = 1
 while count < suffix_length+1:
 if line.startswith(suffix[:1]): # if line and first character of suffix mach
 if line.startswith(suffix[:suffix_length]):
 points += len(suffix[:suffix_length])
 break
 else:
 if line.startswith(suffix[:count+1]):
 count += 1
 elif line.startswith(suffix[:count]):
 points += len(suffix[:count])
 break
 else:
 break
 print points

Hackers Ranking String Similarity

I am currently trying to teach myself some programming. I have started to work with Python by doing this challenge. When I test run it works out fine, however when I run this code with longer strings it takes too long time to run, so the site shuts down the program.

Is this because this code make unnecessary loops? Or what else might the problem be here?

# Solution.py for https://www.hackerrank.com/challenges/string-similarity
import sys
for line in sys.stdin:
 if line.islower():
 line = line[:-1] # line will be compared to suffix
 line_length = len(line)
 points = 0
 for s in xrange(0,line_length):
 suffix = line[s:]
 suffix_length = len(suffix)
 count = 1
 while count < suffix_length+1:
 if line.startswith(suffix[:1]): # if line and first character of suffix mach
 if line.startswith(suffix[:suffix_length]):
 points += len(suffix[:suffix_length])
 break
 else:
 if line.startswith(suffix[:count+1]):
 count += 1
 elif line.startswith(suffix[:count]):
 points += len(suffix[:count])
 break
 else:
 break
 print points
Tweeted twitter.com/#!/StackCodeReview/status/315233215860334592
Post Migrated Here from programmers.stackexchange.com (revisions)
Source Link
Daniel Samani
Daniel Samani
Loading
lang-py

AltStyle によって変換されたページ (->オリジナル) /