Skip to content

Instantly share code, notes, and snippets.

@jdw1996
Created July 3, 2018 21:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdw1996/1568ec00fc25e1244d16dc7559be54d3 to your computer and use it in GitHub Desktop.
Save jdw1996/1568ec00fc25e1244d16dc7559be54d3 to your computer and use it in GitHub Desktop.
Spring 2018 CO454 A4 Q2(a) Solver
#!/usr/bin/env python3
#
# Joseph Winters
# July 2018
# CO 454 Assignment 4, Question 2
#
import sys
import math
jobs = [i for i in range(1,8)]
proc_times = [0,6,10,18,12,16,17,10]
due_dates = [0,8,24,42,44,68,72,75]
weights = [0,1,4,5,2,2,4,2]
q_vals = [[None] * 21 for _ in range(8)]
def q(j,w):
if w < 0:
return math.inf
elif q_vals[j][w] is not None:
return q_vals[j][w]
ret = 0
if j == 0:
ret = 0
elif q(j-1, w) + proc_times[j] <= due_dates[j]:
ret = min(q(j-1, w) + proc_times[j], q(j-1, w - weights[j]))
else:
ret = q(j-1, w - weights[j])
q_vals[j][w] = ret
return ret
def main():
if len(sys.argv) != 3:
print("Usage: {} j w".format(sys.argv[0]))
sys.exit()
j = int(sys.argv[1])
w = int(sys.argv[2])
ans = q(j,w)
print("Answer: {}".format(ans))
for row in q_vals:
print(row)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment