Skip to content

Instantly share code, notes, and snippets.

@guilhermebr
Last active December 11, 2015 21:09
Show Gist options
  • Save guilhermebr/4660771 to your computer and use it in GitHub Desktop.
Save guilhermebr/4660771 to your computer and use it in GitHub Desktop.
Facebook Hackercup 2013
20
a(()(:)(((():(a):a))))a(()(aa()a:())a))a()aa:)a((():)()())a:aa())(:)()():)(:()()a():(((()a))
a((:()(a):aa()::())(:(::)()()(a::)(a))()))a:):)(:)a()(((a)):::((:(:(((:a((()(a)(()):aa)))())))
(a())(::)(a))():(((a(()(:))a(:)))(:(:(:((():)(a))(:))(a)():(:(()aa):)(a((())a)a((a):)()(:(
(a(aa()a)()aa(a:((a(()()(((()aa::)(::(aa:)(a()(:))(aa)a):a)()::):(a))))):((a(:(a():):()::(
(:)
i am sick today (:()
)(
::((:))(((:)(aaa)(a())()(a:)(:)(:)()):)a())aa)())(():a):()::):)a()())a()):):(:a)a):()(a)(a)
(:a))
::):):)((():)(:(a((::)::(:(((((:(a):((a())(:):::((a(a(())(:)(()aa(:))(aa)a:)))))a(:))a)a(:)
(::a((a)a:()):):a)aa:)a(:::))(a())aa(a():))(:)a)((():)(:a:)a))):a(a)((:()(()())a))()a((()a))
a(a)(a)::(a:()a()a)():)(:)aaa)(((a:(())()a):)(()))()(a)aa)a(:)()):a(:((a())a)()(a):a)a(():)
hacker cup: started :):)
:a()(())(:a(:(aa)a)):aaa()a(((a(:()))a))a():()a:)(a)aa(a)((():):aa((a)a((()):)((a)(((aa:(()))))
a(aa):a()()aa(a)a(((((a:)()()(::aa::(a)::(:():((aa)a:()::(((a)():)))((a(()((a)aa(a):((:::)))))
:((
aa(()())((a))(:((()a:()())(a):)())a():(:)))(:):a):)()(((a((()a(a()((aa(:)))a)((aa((a))(a)))))))
(()a:::)a((:))(::a((a)(::aa((a):(:)(:)a()a(()))))facebook is hiring:)()()()a(a((:(a((:)()()))a))
(:)())a(:():)((a:a()()()(())((:a:(:():())):):(:((aa)()(:(:)a))a:(:):a)):()((())()a::::()(:)
:(a):(:)aa)a(:()::():))a:aaa:)(:)((()()))a()(((()(:)))(:(aa:()())())a((a)a:(:()))(a((():)))

Balanced Smileys

Your friend John uses a lot of emoticons when you talk to him on Messenger. In addition to being a person who likes to express himself through emoticons, he hates unbalanced parenthesis so much that it makes him go :(

Sometimes he puts emoticons within parentheses, and you find it hard to tell if a parenthesis really is a parenthesis or part of an emoticon.

A message has balanced parentheses if it consists of one of the following:

  • An empty string ""
  • One or more of the following characters: 'a' to 'z', ' ' (a space) or ':' (a colon)
  • An open parenthesis '(', followed by a message with balanced parentheses, followed by a close parenthesis ')'.
  • A message with balanced parentheses followed by another message with balanced parentheses.
  • A smiley face ":)" or a frowny face ":(" Write a program that determines if there is a way to interpret his message while leaving the parentheses balanced.

Input

The first line of the input contains a number T (1 ≤ T ≤ 50), the number of test cases. The following T lines each contain a message of length s that you got from John.

Output

For each of the test cases numbered in order from 1 to T, output "Case #i: " followed by a string stating whether or not it is possible that the message had balanced parentheses. If it is, the string should be "YES", else it should be "NO" (all quotes for clarity only)

Constraints

1 ≤ length of s ≤ 100

Example input

5
:((
i am sick today (:()
(:)
hacker cup: started :):)
)(

Example output

Case #1: NO
Case #2: YES
Case #3: YES
Case #4: YES
Case #5: NO
# Balanced Smileys for Facebook Hackercup 2013
# fb.com/gbrezende
# Guilherme Rezende <guilhermebr@gmail.com>
def rule1(word):
"""
rule: If an empty string
"""
if word == '':
return True
return False
def rule2(word):
"""
rule: One or more of the following characters: 'a' to 'z', ' ' (a space) or ':' (a colon)
added to rule parenthesis chacacter.
"""
for char in word:
if not char.isalpha() and char != ' ' and char != ':' and char != '(' and char != ')':
return False
return True
def rule3(word):
"""
rule: An open parenthesis '(', followed by a message with balanced parentheses, followed by a close parenthesis ')'
"""
open_parenthesis = 0
close_parenthesis = 0
for char in word:
if char == '(' and close_parenthesis <= open_parenthesis:
open_parenthesis += 1
elif char == ')':
if open_parenthesis == 0:
return False
if close_parenthesis > open_parenthesis:
return False
close_parenthesis += 1
if open_parenthesis != close_parenthesis:
return False
return True
def rule4(word):
"""
rule: A smiley face ":)" or a frowny face ":("
"""
position = word.find(':')
while position != -1:
if word[position+1] in '()':
word = word[:position] + word[position+2:]
else:
position +=1
position = word.find(':', position)
return word
def checkBalanced(word):
"""
The logical to check all rules and return if is a balanced smiley or not.
"""
if rule1(word):
return True
if rule2(word):
if rule3(word):
return True
else:
word = rule4(word)
if rule3(word):
return True
else:
return False
return False
#__main__
try:
inFile = open('balanced_smileystxt.txt', 'r')
outFile = open('output2.txt', 'w+')
numLines = inFile.readline()
words = inFile.readlines()
numLines = int(numLines.strip('\n'))
line = 1
while line <= numLines:
word = words[line-1].strip('\n')
if checkBalanced(word):
outFile.write('Case #%d: YES\n' % (line))
else:
outFile.write('Case #%d: NO\n' % (line))
line += 1
finally:
inFile.close()
outFile.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment