From 43241be2f3a4c90e2540817ea4f665945435842e Mon Sep 17 00:00:00 2001 From: Orien Vandenbergh Date: Tue, 30 Aug 2016 10:56:01 -0600 Subject: [PATCH] Optimize interrogation, add max_eq solution --- python/foobar/three/interrogation.bad.py | 44 ++++++++++++++++++++++++ python/foobar/three/interrogation.py | 34 ++++-------------- python/foobar/two/max_eq.py | 12 +++++++ 3 files changed, 62 insertions(+), 28 deletions(-) create mode 100644 python/foobar/three/interrogation.bad.py create mode 100644 python/foobar/two/max_eq.py diff --git a/python/foobar/three/interrogation.bad.py b/python/foobar/three/interrogation.bad.py new file mode 100644 index 0000000..be8a2d0 --- /dev/null +++ b/python/foobar/three/interrogation.bad.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python + +# Foreach minion, x[0] = time, x[1]/x[2] = probability of response. +inputs = [ [[5, 1, 5], [10, 1, 2]], [[390, 185, 624], [686, 351, 947], [276, 1023, 1024], [199, 148, 250]] ] +solutions = [ [1,0], [2,3,0,1] ] + +minions = inputs + +def answer(minions): + """Calculate the optimal interrogation order, providing the answer in the shortest time""" + """ T[1] + (1-P[1])*T[2] + (1-P[1])(1-P[2])*T[3]""" + + optimal = [ [], 0 ] + + for i,minion in enumerate(minions): + optimal = goDeeper(optimal,[i],minions,1-probabilityOf(minion),minion[0]) + + return optimal[0] + +def goDeeper(optimal,used,minions,pn,t): + """My recursion function to evaluate all the combinations of interrogation orders""" + for i, minion in enumerate(minions): + if i not in used: + p = pn*(1-probabilityOf(minion)) + time = t + (pn*minion[0]) + curr = used+[i] + + if len(curr) == len(minions): + if len(optimal[0])>0: + if time < optimal[1]: + optimal = [ curr, time ] + else: + optimal = [ curr, time ] + else: + optimal = goDeeper(optimal,curr,minions,p,time) + return optimal + +def probabilityOf(minion): + return ((1.0*minion[1])/minion[2]) + +for i in range(0,len(solutions)): + print answer(inputs[i]) + print solutions[i] + diff --git a/python/foobar/three/interrogation.py b/python/foobar/three/interrogation.py index 232c760..b4dff83 100644 --- a/python/foobar/three/interrogation.py +++ b/python/foobar/three/interrogation.py @@ -7,36 +7,14 @@ solutions = [ [1,0], [2,3,0,1] ] minions = inputs def answer(minions): - """Calculate the optimal interrogation order, providing the answer in the shortest time""" - """ T[1] + (1-P[1])*T[2] + (1-P[1])(1-P[2])*T[3]""" + orderable = map(order,range(0,len(minions)),minions) + return map(index,sorted(orderable,key=lambda minion: minion[1])) - optimal = [ [], 0 ] +def order(i,minion): + return [i,minion[0]/(1.0*minion[1]/minion[2])] - for i,minion in enumerate(minions): - optimal = goDeeper(optimal,[i],minions,1-probabilityOf(minion),minion[0]) - - return optimal[0] - -def goDeeper(optimal,used,minions,pn,t): - """My recursion function to evaluate all the combinations of interrogation orders""" - for i, minion in enumerate(minions): - if i not in used: - p = pn*(1-probabilityOf(minion)) - time = t + (pn*minion[0]) - curr = used+[i] - - if len(curr) == len(minions): - if len(optimal[0])>0: - if time < optimal[1]: - optimal = [ curr, time ] - else: - optimal = [ curr, time ] - else: - optimal = goDeeper(optimal,curr,minions,p,time) - return optimal - -def probabilityOf(minion): - return ((1.0*minion[1])/minion[2]) +def index(minion): + return minion[0] for i in range(0,len(solutions)): print answer(inputs[i]) diff --git a/python/foobar/two/max_eq.py b/python/foobar/two/max_eq.py new file mode 100644 index 0000000..3d0f6e2 --- /dev/null +++ b/python/foobar/two/max_eq.py @@ -0,0 +1,12 @@ +#!/usr/bin/env python + +samples = [ [[1,4,1],3], [[1,2],1] ] + +def answer(x): + """Find the number of cars that can be made an equal weight via bunny redistribution between cars""" + remainder = sum(x)%(len(x)) + return len(x) if (remainder==0) else len(x)-1 + +for sample in samples: + print answer(sample[0]) + print sample[1]