Skip to content

Instantly share code, notes, and snippets.

@sing1ee
Created July 11, 2012 02:18
Show Gist options
  • Save sing1ee/3087523 to your computer and use it in GitHub Desktop.
Save sing1ee/3087523 to your computer and use it in GitHub Desktop.
(()()) recursive implement
# !/usr/bin/python
# -*- encoding: utf-8 -*-
import sys
array = list(sys.argv[1])
def n_pair(array):
if len(array) == 0:
return True
if len(array) & 1 == 1:
return False
ret = False
if array[0] == '(' and array[-1] == ')':
ret |= n_pair(array[1 : -1])
if ret:
return True
for k in range(1, len(array)):
ret |= (n_pair(array[0 : k]) & n_pair(array[k :]))
return ret
print n_pair(array)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment