0

I am making a pattern recogniser and I ran into a VERY strange error. If the pattern is recognised it prints out ["x", "x", "2"] and i set variable worked to t. But when I print out worked on the last line it prints out ["x", "x", "x"]

(See the part of code marked # _____________ IMPORTANT __________ #)

    import random

    def check_pattern(pattern, rule):
        for i, RULE in enumerate(pattern):
            if RULE != "x":
                if rule[i] != RULE:
                    return False
        return True

    if __name__ == "__main__":
        numbers = [002,212,212,432,132,142,322,992,972,692,942,472]

        #numbers = [a.replace("\n", "") for a in numbers]

        print("Finding pattern in {}".format(numbers))

        patterns = []
        possible = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "x"]

        found = False
        pattern = ["x", "x", "x"]
        working = []
        worked = []
        for i, rule in enumerate(pattern):
            for attemptRule in possible:
                testRule = pattern
                testRule[i] = str(attemptRule)
                works = True

                for num in numbers:
                    numList = [a for a in str(num)]
                    if check_pattern(testRule, numList):
                        pass
                        #print("Pattern {} works with {}".format(t, numList))
                    else:
                        works = False
                        #print("Pattern {} doesnt work with {}".format(t, numList))
                if works:
                    # ____________________ IMPORTANT ______________________ #
                    if testRule != ['x', 'x', 'x']:
                        print(testRule)                    
                        worked = testRule
                    # ____________________ IMPORTANT ______________________ #
                    working.append(testRule)
        #print("Pattern {} works".format(working))
    print(worked)

Thanks.

dbugger
  • 15,868
  • 9
  • 31
  • 33
jotjern
  • 460
  • 5
  • 18
  • In line 31: numList = [a for a in num] TypeError: 'int' object is not iterable – Emanuele Paolini Jan 02 '17 at 09:24
  • I now call str() on the numbers and change 1 to "1" and so on. Updating post, thanks for reply but i dont get any errors – jotjern Jan 02 '17 at 09:29
  • I find it hard to read the code, what is it supposed to do? Can you give some examples of input and expected output? – fafl Jan 02 '17 at 09:32
  • Uhhhh i think i already have expected input and output but ill make it more clear... – jotjern Jan 02 '17 at 09:33
  • 1
    I don't really try to understand your code but `testRule = pattern` is probably the problem, see http://stackoverflow.com/questions/2612802/how-to-clone-or-copy-a-list – polku Jan 02 '17 at 09:33
  • Thanks so much! That solved the problem – jotjern Jan 02 '17 at 09:35
  • right now you have a SyntaxError on number `002` (line 11). To fix it, remove the 2 leading 0 then replace `str(num)` by `"%3d" % num` line 31. – Guillaume Jan 02 '17 at 09:37

1 Answers1

0

I found my answear. The problem was that i didn't clone the list and overwrote it instead. Thanks to polku i understood. Thanks everyone.

jotjern
  • 460
  • 5
  • 18