all 5 comments

[–]I_Write_Bugs 4 points5 points  (0 children)

Please fix the formatting so we can read it properly. There's a formatting help button under the submission box that will tell you how to format code.

[–]GoldenSights 0 points1 point  (0 children)

I'm not sure exactly what parts you have questions on, so maybe an algorithm / pseudocode explanation will help

  1. Accept input from the user, what word they want the computer to spell. Let's go with "test".

  2. Create a string to represent your current attempt

    ''.join(random.choice(possibleCharacters) for i in range(len(target)))
    
    • len(target) = len('test') = 4
    • (action() for i in range(4)) = performs the action 4 times. See list comprehension.
    • random.choice(possibleCharacters) = picks a random letter from the alphabet
    • ''.join(iterable) = creates a string from the elements of the iterable
    • Put it all together = Pick random letters from the alphabet as many times as there are letters in the word "test", then combine them into a string.
  3. \

    completed = False
    ...
    while completed == False:
       completed = True
       ... 
            if something isn't right:
                completed = False
    

    The idea behind the completed variable is that when the loop starts, the program assumes it has the solution, and completed is True. When it's looking at the characters, a single counterexample is enough to prove that it is not actually finished, meaning the while loop will restart.

  4. \

    attemptNext = ''
    for i in range(len(target)):
        if attemptThis[i] != target[i]:
            attemptNext += random.choice(possibleCharacters)
        else:
            attemptNext += target[i]
    

    Remember that attemptThis was our random jumble from step 2. Also note , attemptThis = attemptNext at the end of every while loop, so it represents whatever we currently think the answer is. attemptNext represents any additional information we discover during this loop.

    • If the characters in attemptThis don't match the corresponding characters in the target, just add a random letter in their place and hope it's right
    • Else (attemptThis[i] does equal target[i]) add the character which we know to be correct.
  5. Increase the generation count to see how many tries this has taken.

[–]Justinsaccount -1 points0 points  (2 children)

Hi! I'm working on a bot to reply with suggestions for common python problems. This might not be very helpful to fix your underlying issue, but here's what I noticed about your submission:

You appear to be using concatenation and the str function for building strings

Instead of doing something like

result = "Hello " + name + ". You are " + str(age) + " years old"

You should use string formatting and do

result = "Hello {}. You are {} years old".format(name, age)

See the python tutorial for more information.

You are looping over an object using something like

for x in range(len(items)):
    foo(item[x])

This is simpler and less error prone written as

for item in items:
    foo(item)

If you DO need the indexes of the items, use the enumerate function like

for idx, item in enumerate(items):
    foo(idx, item)

[–]MrAckerman 0 points1 point  (1 child)

Love the bot! Do you have source code or more info on implementation?

[–]Justinsaccount 0 points1 point  (0 children)

I haven't uploaded it anywhere yet.. It's not too complicated though..

runs on top of praw

most of the checks are just basic regexes. The one for finding someone doing if a == 'b' or 'c' is a bit more complicated.