Mercurial > hg > config
comparison python/example/possibilities.py @ 777:ef6731a4ad86
close enough
| author | Jeff Hammel <k0scist@gmail.com> |
|---|---|
| date | Sun, 03 Jul 2016 18:48:38 -0700 |
| parents | 67fa26b40dc6 |
| children |
comparison
equal
deleted
inserted
replaced
| 776:67fa26b40dc6 | 777:ef6731a4ad86 |
|---|---|
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 | |
| 3 """ | |
| 4 """ | |
| 2 | 5 |
| 3 # imports | 6 # imports |
| 4 import argparse | 7 import argparse |
| 5 import sys | 8 import sys |
| 6 import time | 9 import time |
| 9 costs = {1:1, | 12 costs = {1:1, |
| 10 7:3, | 13 7:3, |
| 11 30:25} | 14 30:25} |
| 12 | 15 |
| 13 A = [1, 2, 4, 5, 7, 29, 30] | 16 A = [1, 2, 4, 5, 7, 29, 30] |
| 14 | |
| 15 #A = [3, 7, 10] | |
| 16 | |
| 17 #A=[1,2,3] | |
| 18 | 17 |
| 19 def possibilities(A): | 18 def possibilities(A): |
| 20 possibilities = [[(1, date) for date in A]] | 19 possibilities = [[(1, date) for date in A]] |
| 21 for_consideration = [0] | 20 for_consideration = [0] |
| 22 while True: | 21 while True: |
| 44 except IndexError: | 43 except IndexError: |
| 45 pass | 44 pass |
| 46 | 45 |
| 47 return possibilities | 46 return possibilities |
| 48 | 47 |
| 48 | |
| 49 def fill_space(A, spans): | 49 def fill_space(A, spans): |
| 50 """ | 50 """ |
| 51 A -- the space to be filled | 51 A -- the space to be filled |
| 52 spans -- the discrete length of each space | 52 spans -- the discrete length of each space |
| 53 """ | 53 """ |
| 68 last_date = A[index] | 68 last_date = A[index] |
| 69 e = None | 69 e = None |
| 70 index += 1 | 70 index += 1 |
| 71 try: | 71 try: |
| 72 while last_date + last_span > A[index]: | 72 while last_date + last_span > A[index]: |
| 73 print ('index: {}'.format(index)) | 73 # print ('index: {}'.format(index)) |
| 74 index += 1 | 74 index += 1 |
| 75 except IndexError as e: | 75 except IndexError as e: |
| 76 possibilities.append(item) | 76 possibilities.append(item) |
| 77 continue | 77 continue |
| 78 | 78 |
| 87 retval = [[(A[index], span) for span, index in possibility] | 87 retval = [[(A[index], span) for span, index in possibility] |
| 88 for possibility in possibilities] | 88 for possibility in possibilities] |
| 89 return retval | 89 return retval |
| 90 | 90 |
| 91 | 91 |
| 92 def cost(A, spans=(30,)): | 92 def sorted_costs(A, costs=tuple(costs.items())): |
| 93 | |
| 94 costs = dict(costs) | |
| 93 | 95 |
| 94 # determine possibility spaces | 96 # determine possibility spaces |
| 95 spaces = fill_space(A, spans=spans) | 97 spaces = fill_space(A, spans=costs.keys()) |
| 96 | 98 |
| 97 # determine costs | 99 # determine costs |
| 100 retval = [] | |
| 101 for space in spaces: | |
| 102 cost = sum([costs[value] for date, value in space]) | |
| 103 retval.append((cost, space)) | |
| 104 | |
| 105 # sort by cost | |
| 106 retval.sort(key=lambda x: x[0]) | |
| 107 | |
| 108 return retval | |
| 98 | 109 |
| 99 if __name__ == '__main__': | 110 if __name__ == '__main__': |
| 100 | 111 |
| 101 parser = argparse | 112 parser = argparse.ArgumentParser(description=__doc__) |
| 102 spans = costs.keys() | 113 options = parser.parse_args() |
| 103 | 114 |
| 104 pprint (fill_space(A, spans)) | 115 dates = sorted(A) |
| 116 | |
| 117 print "Dates: {}".format(', '.join([str(i) for i in dates])) | |
| 118 for cost, value in sorted_costs(A, costs): | |
| 119 print ("{} : {}".format(cost, value)) |
