我的代码是子集sum的解决方案:
import numpy as np
def subset_sum(theSet,target):
n=len(theSet)
if(target==0):
return np.empty([0]) #emptySet
if(target<0 or n==0):
return None
theAuxSet=subset_sum(theSet[0:n-1],target)
if (theAuxSet!=None):
return theAuxSet
theAuxSet=subset_sum(theSet[0:n-1],target-theSet[n-1])
if(theAuxSet!=None):
return np.append(theAuxSet,theSet[n-1])
return None
if __name__ == '__main__':
theSet=np.array([8,6,7,5,3,10,9])
target=15
solution=[]
print subset_sum(theSet,target)
以下错误已经发生。当错误没有发生时,代码按预期工作:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 699, in runfile
execfile(filename, namespace)
File "/usr/local/lib/python2.7/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 81, in execfile
builtins.execfile(filename, *where)
File "/home/daniel/Documents/Cursos/Caju/UNB/Métodos de Análise Econômica - UNB/2016/Slides/Part I - Aula 10 Backtracking/subsetSum.py", line 25, in <module>
subset_sum(theSet,target)
File "/home/daniel/Documents/Cursos/Caju/UNB/Métodos de Análise Econômica - UNB/2016/Slides/Part I - Aula 10 Backtracking/subsetSum.py", line 10, in subset_sum
theAuxSet=subset_sum(theSet[0:n-1],target)
File "/home/daniel/Documents/Cursos/Caju/UNB/Métodos de Análise Econômica - UNB/2016/Slides/Part I - Aula 10 Backtracking/subsetSum.py", line 10, in subset_sum
theAuxSet=subset_sum(theSet[0:n-1],target)
File "/home/daniel/Documents/Cursos/Caju/UNB/Métodos de Análise Econômica - UNB/2016/Slides/Part I - Aula 10 Backtracking/subsetSum.py", line 10, in subset_sum
theAuxSet=subset_sum(theSet[0:n-1],target)
File "/home/daniel/Documents/Cursos/Caju/UNB/Métodos de Análise Econômica - UNB/2016/Slides/Part I - Aula 10 Backtracking/subsetSum.py", line 10, in subset_sum
theAuxSet=subset_sum(theSet[0:n-1],target)
File "/home/daniel/Documents/Cursos/Caju/UNB/Métodos de Análise Econômica - UNB/2016/Slides/Part I - Aula 10 Backtracking/subsetSum.py", line 13, in subset_sum
theAuxSet=subset_sum(theSet[0:n-1],target-theSet[n-1])
File "/home/daniel/Documents/Cursos/Caju/UNB/Métodos de Análise Econômica - UNB/2016/Slides/Part I - Aula 10 Backtracking/subsetSum.py", line 11, in subset_sum
if (theAuxSet!=None):
File "/usr/lib/python2.7/warnings.py", line 29, in _show_warning
file.write(formatwarning(message, category, filename, lineno, line))
File "/usr/local/lib/python2.7/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 119, in write
self.old_stderr.write(colored_text)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 45: ordinal not in range(128)