Skip to content

Instantly share code, notes, and snippets.

@naphthalene
Created October 28, 2014 21:16
Show Gist options
  • Save naphthalene/4968ad00775c648b6b98 to your computer and use it in GitHub Desktop.
Save naphthalene/4968ad00775c648b6b98 to your computer and use it in GitHub Desktop.
#!/usr/bin/python2
from tabulate import tabulate
from random import random
from math import floor
DAYS = 7
SECTIONS = 10
MAX_TTR = 6 # minutes -> max time to read
MIN_TTR = 2
MAX_FT = 10
MIN_FT = 5
# Time to read section s
TR = [MIN_TTR + floor(random() * (MAX_TTR - MIN_TTR)) for _ in range(SECTIONS)]
T1 = [3.0, 5.0, 3.0, 5.0, 2.0, 2.0, 2.0, 2.0, 2.0, 4.0]
T = T1
# Time available on day d
AR = [MIN_FT + floor(random() * (MAX_FT - MIN_FT)) for _ in range(DAYS)]
A1 = [9.0, 7.0, 7.0, 8.0, 9.0, 7.0, 7.0]
A = A1
U = [[[None for _ in xrange(SECTIONS)]
for _ in xrange(SECTIONS)]
for _ in xrange(DAYS)]
MU = [[None for _ in xrange(SECTIONS)]
for _ in xrange(DAYS)]
for s in xrange(SECTIONS):
# Base case: on the first day, you read from 0 -> section s
time_spent_today = sum(T[:s])
print time_spent_today
U[0][0][s] = max(A[0] - time_spent_today, 0) ** 4 +\
max(time_spent_today - A[0], 0)
print "Unhappiness: {}".format(U[0][0][s])
MU[0][s] = (U[0][0][s], (0, s))
for d in range(1, DAYS):
for s in range(SECTIONS):
# Start here
for e in range(s, SECTIONS):
# End here
# s -> sections to read up to today, building on the days before
time_spent_today = sum(T[s:e])
todays_unhappiness = max(A[d] - time_spent_today, 0) ** 4 +\
max(time_spent_today - A[d], 0)
U[d][s][e] = todays_unhappiness + MU[d-1][s][0]
if not MU[d][e] or MU[d][e][0] > U[d][s][e]:
MU[d][e] = (U[d][s][e], (s, e))
print tabulate(U[d])
print tabulate(MU)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment