finish recursion function, complete functionality
This commit is contained in:
parent
b8ebc7ff78
commit
87f62712ea
@ -13,31 +13,27 @@ def answer(minions):
|
|||||||
optimal = [ [], 0 ]
|
optimal = [ [], 0 ]
|
||||||
|
|
||||||
for i,minion in enumerate(minions):
|
for i,minion in enumerate(minions):
|
||||||
recurse_minions([i],minions,1-probabilityOf(minion),minion[0])
|
optimal = goDeeper(optimal,[i],minions,1-probabilityOf(minion),minion[0])
|
||||||
|
|
||||||
return optimal[0]
|
return optimal[0]
|
||||||
|
|
||||||
def recurse_minions(used,minions,pn,t):
|
def goDeeper(optimal,used,minions,pn,t):
|
||||||
global optimal
|
"""My recursion function to evaluate all the combinations of interrogation orders"""
|
||||||
for i, minion in enumerate(minions):
|
for i, minion in enumerate(minions):
|
||||||
try:
|
if i not in used:
|
||||||
used.index(i)
|
|
||||||
next
|
|
||||||
except ValueError:
|
|
||||||
# Counterintuitive warning: ValueError indicates that 'i' does not
|
|
||||||
# exist within the list of currently used minions. This means that
|
|
||||||
# we process this minion. Lack of ValueError indicates that this
|
|
||||||
# minion has already been processed.
|
|
||||||
p = pn*(1-probabilityOf(minion))
|
p = pn*(1-probabilityOf(minion))
|
||||||
time = t + (pn*minion[0])
|
time = t + (pn*minion[0])
|
||||||
curr = used+[i]
|
curr = used+[i]
|
||||||
|
|
||||||
if len(curr) == len(minions):
|
if len(curr) == len(minions):
|
||||||
if len(optimal[0])>0:
|
if len(optimal[0])>0:
|
||||||
if time < optimal[1]:
|
if time < optimal[1]:
|
||||||
optimal[0] = curr
|
optimal = [ curr, time ]
|
||||||
optimal[1] = time
|
|
||||||
else:
|
else:
|
||||||
recurse_minions(curr,minions,p,time)
|
optimal = [ curr, time ]
|
||||||
|
else:
|
||||||
|
optimal = goDeeper(optimal,curr,minions,p,time)
|
||||||
|
return optimal
|
||||||
|
|
||||||
def probabilityOf(minion):
|
def probabilityOf(minion):
|
||||||
return ((1.0*minion[1])/minion[2])
|
return ((1.0*minion[1])/minion[2])
|
||||||
|
Loading…
Reference in New Issue
Block a user