Created
July 11, 2012 02:18
(()()) recursive implement
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# !/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