Mercurial > hg > config
comparison python/example/possibilities.py @ 776:67fa26b40dc6
add example program for time-spacing
| author | Jeff Hammel <k0scist@gmail.com> |
|---|---|
| date | Sun, 03 Jul 2016 18:33:09 -0700 |
| parents | |
| children | ef6731a4ad86 |
comparison
equal
deleted
inserted
replaced
| 775:13dd0ce4fa9f | 776:67fa26b40dc6 |
|---|---|
| 1 #!/usr/bin/env python | |
| 2 | |
| 3 # imports | |
| 4 import argparse | |
| 5 import sys | |
| 6 import time | |
| 7 from pprint import pprint | |
| 8 | |
| 9 costs = {1:1, | |
| 10 7:3, | |
| 11 30:25} | |
| 12 | |
| 13 A = [1, 2, 4, 5, 7, 29, 30] | |
| 14 | |
| 15 #A = [3, 7, 10] | |
| 16 | |
| 17 #A=[1,2,3] | |
| 18 | |
| 19 def possibilities(A): | |
| 20 possibilities = [[(1, date) for date in A]] | |
| 21 for_consideration = [0] | |
| 22 while True: | |
| 23 if not for_consideration: | |
| 24 break | |
| 25 _for_consideration = [] | |
| 26 for item in for_consideration: | |
| 27 basis = possibilities[item][:] | |
| 28 tickets = [i[0] for i in basis] | |
| 29 if set(tickets) == set([7]): | |
| 30 continue | |
| 31 index = 0 | |
| 32 potential = [] | |
| 33 while True: | |
| 34 try: | |
| 35 pos = tickets.find(1, index) | |
| 36 except ValueError: | |
| 37 break | |
| 38 potential = basis[:] | |
| 39 potential[pos] = (7, basis[pos][-1]) | |
| 40 | |
| 41 try: | |
| 42 while A[index] < date+6: | |
| 43 index += 1 | |
| 44 except IndexError: | |
| 45 pass | |
| 46 | |
| 47 return possibilities | |
| 48 | |
| 49 def fill_space(A, spans): | |
| 50 """ | |
| 51 A -- the space to be filled | |
| 52 spans -- the discrete length of each space | |
| 53 """ | |
| 54 if not A: | |
| 55 return [] | |
| 56 | |
| 57 A = sorted(A) | |
| 58 | |
| 59 possibilities = [] | |
| 60 | |
| 61 # [(ticket_value, index_into_A)] | |
| 62 working_set = [[(value, 0)] for value in spans] | |
| 63 # print "working set" | |
| 64 # pprint(working_set) | |
| 65 while working_set: | |
| 66 item = working_set.pop() | |
| 67 last_span, index = item[-1] | |
| 68 last_date = A[index] | |
| 69 e = None | |
| 70 index += 1 | |
| 71 try: | |
| 72 while last_date + last_span > A[index]: | |
| 73 print ('index: {}'.format(index)) | |
| 74 index += 1 | |
| 75 except IndexError as e: | |
| 76 possibilities.append(item) | |
| 77 continue | |
| 78 | |
| 79 # print "item:" | |
| 80 # pprint(item) | |
| 81 extension = [item[:] + [(span, index)] | |
| 82 for span in spans] | |
| 83 | |
| 84 working_set.extend(extension) | |
| 85 | |
| 86 # fill possibility indices with dates | |
| 87 retval = [[(A[index], span) for span, index in possibility] | |
| 88 for possibility in possibilities] | |
| 89 return retval | |
| 90 | |
| 91 | |
| 92 def cost(A, spans=(30,)): | |
| 93 | |
| 94 # determine possibility spaces | |
| 95 spaces = fill_space(A, spans=spans) | |
| 96 | |
| 97 # determine costs | |
| 98 | |
| 99 if __name__ == '__main__': | |
| 100 | |
| 101 parser = argparse | |
| 102 spans = costs.keys() | |
| 103 | |
| 104 pprint (fill_space(A, spans)) |
