SingPath is an interactive problem set website that tracks your progression within multiple programming languages. Unfortunately it has some bugs. The largest is the error:
Your code took too long to return. Your solution may be stuck in an infinite loop. Please try again.
appearing multiple times when you run your problem set code. Now all you have to do to correct the bug while answering a problem set question is to just rerun the code.
However, within the problem creator it creates a HUGE issue. Essentially you can run the code until it works in the creator as well which is not the problem for testing. The issue stems from submitting the problem/solution due to the fact that the submit button reruns the code. This means you need to get two consecutive correct answers which are near impossible to get (I’ve been trying for an hour now). The fact that creating a problem is required to continue within the problem set is quite ruined on the buggy creator.
So (finally) here’s my question, How can I make the following code run without this error in the creator?
Code:
import string
import random
# Takes a blank string then replaces it with 3 random letters/numbers.
def string3(s1):
s2 = ''
def randstring (size = 3, chars = string.ascii_lowercase + string.digits):
a = ''.join(random.choice(chars) for x in range(size))
return a
''' If the string brought in isnot equal to the string generated keep
rerunning the random string generator '''
while s1 != s2:
s2 = randstring()
return 'True'
Sample function calls:
>>> string3('abc')
'True'
>>> string3('a2d')
'True'
>>> string3('234')
'True'
What the code does:
The Infinite Monkey Theorem states that
Given enough time, a hypothetical monkey typing at random would, as part of its output, almost surely produce all of Shakespeare’s plays.
Create a function string3 that would randomly generate strings of 3 characters until those character’s are equal to the string received from the function. Hint: Search for a random string generator for reference. All characters in a string will be lower case.
I am a beginner programmer and I would like if some comments were put it so I can understand more complicated solutions although I would be grateful for any help/suggestions!
Also, I have already notified the support staff at SingPath for the bug but I would like to get as much practice in today as I can! If it’s impossible to potentially fix the bug on the client side I completely understand! Best of luck!
IMPORTANT EDIT
I forgot to mention that this bug will happen regardless of loops within the code. While my program is very intense with the constant random assignment of characters I know it isn’t the issue (it might be AN issue but not THE issue XD). If you don’t believe me try to create a problem here: http://singpath.appspot.com/
Even a simple function declaration and return such as:
def add_one(n):
return n+1
Can cause the loop bug! If I can do anything on my side (exception statement?) Please let me know!
Your code may be slow, but you’re going to see that error no matter what you do. I just logged in there and started my way down the Python path and got the same error with the not-very-computationally intensive:
What appears to be happening to that site is that it’s being slammed by too much traffic. (Back in the day, I’d say they were being slash-dotted, but I guess it’d be more correct to say they’re being stackoverflowed. 😉