# Python Puzzle Solutions

DevFeed: [Python Puzzle Solutions](<https://devfeed.tech/articles/python-puzzle-solutions-29433.md>)

Original publisher: [Read original article](<http://akaptur.github.com/blog/2013/10/31/python-puzzle-solutions/>)

Published: 2013-10-31T17:47:00Z

Content type: article

Language: en

Sources: [Allison Kaptur](<https://devfeed.tech/sources/allison-kaptur.md>)

Topics: [Python](<https://devfeed.tech/topics/python.md>), [test](<https://devfeed.tech/topics/test.md>), [function](<https://devfeed.tech/topics/function.md>)

Tags: [blog-post](<https://devfeed.tech/tags/blog-post.md>), [puzzle](<https://devfeed.tech/tags/puzzle.md>), [python](<https://devfeed.tech/tags/python.md>), [solutions](<https://devfeed.tech/tags/solutions.md>)

## AI overview

The article clarifies the rules for a Python puzzle and discusses submitted solutions, including Jessica McKellar's stateful solution and a correction that it passes the order test.

## Source excerpt

I really enjoyed seeing all the clever solutions to the python puzzle I posted. You're all very creative! Here's a discussion of the solutions I've seen, plus some clarifications. All spoilers are below the fold. First, clarifications. (These weren't always clear in the problem statement, particularly if you got the problem off of twitter, so award yourself full marks as desired.) Order doesn't matter "Order doesn't matter" means that the three-line version always returns False, and the semicolon version always returns True. You control only the contents of the lines Several people, including Pepijn De Vos, David Wolever, and diarmuidbourke suggested something like the following: 1 2 3 4 5 6 >>> """a; b; c""" == 'a; b; c' True >>> """a ... b ... c""" == 'a; b; c' False I'm being pedantic here, but I rule this cheating, since (a) each line has to be a valid python expression or statement, and a multi-line string literal is only one expression, and (b) the string """a; b; c""" is not the same as the string """a\nb\nc""". Solutions appear below the fold. Solutions! Jessica McKellar Jessica suggests the following solution: 1 2 3 4 5 6 7 8 9 10 11 12 13 >>> global a >>> a = a + "a" if "a" in globals() else "" >>> print(bool(len(a) % 3)) False >>> global a; a = a + "a" if "a" in globals() else ""; print(bool(len(a) % 3)) True >>> def my_function(): ... global a ... a = a + "a" if "a" in globals() else "" ... print(bool(len(a) % 3)) ... >>> my_function() True However, Jessica's solution fails the "order doesn't matter" test, and it is stateful: 1 2 3 4 >>> global a; a = a + "a" if "a" in globals() else ""; print(bool(len(a) % 3)) False >>> global a; a = a + "a" if "a" in globals() else ""; print(bool(len(a) % 3)) True Edit: As Jessica points out, I'm wrong here: her solution does pass the order test. She also notes that the restriction against state wasn't present in the blog post (and she didn't see the original tweet). Full credit to Jessica, then! Javier Novoa Cataño Ja