smillence1992
BAN USERPython
# number of left and right parenthesis not used yet
def foo(left,right):
if left==0:
temp = [')' for i in range(right)]
return [''.join(temp)]
if left==right:
lst = foo(left-1,right)
#what if lst == []
return ['('+x for x in lst]
else:
lst1 = foo(left-1,right)
lst2 = foo(left,right-1)
l1= ['('+x for x in lst1]
l2=[')'+x for x in lst2]
return l1+l2
if __name__ == '__main__':
for string in foo(3,3):
print string
Python
import copy
def foo_bruth_force(Set,String):
positions = {char:[] for char in Set}
for i in range(len(String)):
if String[i] in positions:
positions[String[i]].append(i)
# positions {'a':[0,7],'b':[1,2,4,6],'c':[3,5],'d'....,'e',}
positions = [positions[key] for key in positions]
output = min(helper(positions), key=minmax )
return String[output[0]:output[-1]+1]
# lst is a list of lists
# return a list of numbers
def helper(lst):
lst = copy.deepcopy(lst)
if len(lst)==0: return []
sublst = lst.pop()# sublst [0,7]
right = helper(lst) # a list of lists
output = []
for x in sublst:
if len(lst)!=0:
output += [ [x]+sub for sub in right]
else:
output.append([x])
return output
def minmax(lst):
lst.sort()
return lst[-1]-lst[0]
print foo_bruth_force(['a','b','c'],'abbcbcba')
Python
- smillence1992 February 16, 2015