My solution to the second Colossal Cue Adventure puzzle

A little bit of lunchtime coding.

def closeBracket(openbracket):
 if openbracket == '{':
 return '}'
 elif openbracket == '(':
 return ')'
 elif openbracket == '[':
 return ']'

if __name__ == "__main__":
 testcase = ('{{[{{{{}}{{}}}[]}[][{}][({[(({{[][()()]}}{[{{{}}}]}))][()]{[[{((()))({}(())[][])}][]()]}{()[()]}]})][]]}{{}[]}}')
 teststack = []
 position = 0

for c in testcase:
 if c == '{' or c == '(' or c == '[':
 #push the open character on the stack

 if c == '}' or c == ')' or c == ']':
 #check first to see if the stack is empty and
 #if it is report the position
 if not teststack:
 print "Failed at position %d" % position
 #it's not empty, so pop the last appended character off
 f = teststack.pop()
 #if this character is the respective close character
 #of the character that got popped off, we're good
 if closeBracket(f) == c:
 print "All good at position %d" % position
 #if the popped character isn't the respective close
 #character, then fail and report position
 elif closeBracket(f) != c:
 print "Failed at position %d" % position
 position = position + 1

Oof, the source code formatting in WordPress is so bad. I guess my interspersed comments aren’t helping readability. Here’s a Gist:

I used Python because it has .append() and .pop() for lists ready to go. Every open bracket in the string in testcase gets pushed onto the stack (appended onto the list), and when a close bracket is encountered I pop the last pushed open bracket off the stack. This popped off open bracket is sent to closeBracket(), and closeBracket() returns the respective close bracket. So, if the encountered close bracket matches the respective close bracket of the popped of open bracket, we’re all good. If it doesn’t (or if the list is empty) it fails and reports the position.

A ham-fisted explanation for a ham-fisted program.

Had I not already been familiar with the notion of a stack I’d have been super lost on puzzle #2. I worry that there is a new trick to the third puzzle, so this might take me a bit longer to figure out.

I think that this whole puzzle set is part of Cueup’s job application process. I hope they eventually post some of the interesting solutions their applicants submit. Nice of them to make it public – it’s rather enjoyable (so far!).

This entry was posted in Hobbies, Programming. Bookmark the permalink.

3 Responses to My solution to the second Colossal Cue Adventure puzzle

  1. R Koneri says:

    Hey, I’m stuck with the below replies since a day and a half.
    “Near you is a rabbit using a computer, reading JIRA tickets.”
    “I think I saw something in the ticket about newlines not counting as characters.”

    Would you please help me what did you reply to arrive at this second question? Thanks a lot!

  2. dna says:

    My code:
    1. count opening parens, brackets, and braces separately
    2. count closing parens, brackets, and braces separately
    3. if for any reason closing parens, brackets or braces outnumber the opening ones, return the index
    4. remember to subtract the number of newlines

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s