python - backtracking - solution hint for generating schedule -
i'm trying solve following problem:
there 3 hours of math, 2 of phisics , 3 of informatics. need generate possible schedules day must have @ least 1 hour of these subjects , @ maximum 3 of these (it math-math-math or math-physics-informatics)
the schedule 5 days. came following solution:
let generate(k, o) 'generator' function k-th day o hours, o between 1 , 3
for every day k , hour o pick subject still in our left. keep track of having array counter[i] = takencount
, 1, 2, 3 corresponding math, physics , informatics, , takencount means how many hours of subject used.
and configuration use matrix v[k][o]
gives me configuration day k, hours o
this code i've tried far
h = ['', 'm', 'f', 'i'] counter = [0, 0, 0, 0] limit = [0, 3, 2, 3] v = [['' x in range(4)] x in range(6)] def generate(k, o): if k == 6: if counter[1] == limit[1] , counter[2] == limit[2] , counter[3] == limit[3]: showconfig() else: in range(1, 4, 1): if counter[i] != limit[i]: v[k][o] = h[i] counter[i] += 1 generate(k + 1, 1) generate(k + 1, 2) generate(k + 1, 3) v[k][o] = '' counter[i] -= 1 def showconfig(): print '-- configuration --' print counter in range(1, 6, 1): print 'day ', i, success = false j in range(1, 4, 1): print v[i][j], print '-- end --' generate(1, 1) generate(1, 2) generate(1, 3)
it's written in python think that's not problem, because it's pretty easy read
the problem here never have hours inside v[i][2]
, v[i][3]
, v[i][1]
, , condition
counter[1] == 3, counter[2] == 2 , counter[3] == 3
never gets true , not know why.
also after generate following day, k + 1
, hours 1, 2 , 3, need reset current v[k][o] to
'' , counter[i]
counter[i] - 1
this not homework or simillar. i'm asking if solution , if is, did wrong in code? because can't seem find out
output example:
( (m), (m, f), (m, f, i), (i), (i) ) ... ( (m, i), (f), (i), (m, f, i), (m) ) ( (m, f), (i), (i), (m, f, i), (m) )
where m math, f physics , informatics, , each paranthesis day in week, 5 days in 1 week
here want:
from itertools import permutations def possible_permutations(*args): length_args = len(args) args_list, permutations_list = [], [] arg in args: args_list.append(arg) permutations_list.append(tuple(permutations(args_list))) # clean ugly list of touples of touples list of touples. perms = [] perm in permutations_list: p in perm: perms.append(p) return perms def schedules(days, *classes): possible_perms = possible_permutations(*classes) perm in permutations(possible_perms, days): print(perm) schedules(5, 'math', 'science', 'english')
old answer
it not entirly clear me asking, belive solves question.
from itertools import permutations classes = {'math':3, 'physics':2, 'informatics':3} def possible_schedules(**classes_hours): k, v = [k k, v in classes_hours.items()], [v k, v in classes_hours.items()] k_perms, v_perms = permutations(k), permutations(v) perms = zip(k_perms, v_perms) k_perm, v_perm in perms: perm = tuple(zip(k_perm, v_perm)) print(perm) possible_schedules(**classes)
output:
(('physics', 2), ('math', 3), ('informatics', 3)) (('physics', 2), ('informatics', 3), ('math', 3)) (('math', 3), ('physics', 2), ('informatics', 3)) (('math', 3), ('informatics', 3), ('physics', 2)) (('informatics', 3), ('physics', 2), ('math', 3)) (('informatics', 3), ('math', 3), ('physics', 2))
Comments
Post a Comment