Created
July 21, 2010 03:56
-
-
Save elucid/484035 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # global cache removed | |
| import time | |
| start_time = time.time() | |
| # | |
| def get_eq_map(vals): | |
| # Given a list of floating point values, returns a map from result values | |
| # to an equation string returning that result. | |
| if len(vals) == 1: | |
| return {vals[0]: str(vals[0])} | |
| else: | |
| eq_map = {} | |
| for i in range(1, len(vals)): | |
| head_eq_map = get_eq_map(vals[:i]) | |
| tail_eq_map = get_eq_map(vals[i:]) | |
| for h,heq in head_eq_map.iteritems(): | |
| heq2 = "(" + heq | |
| for t,teq in tail_eq_map.iteritems(): | |
| teq2 = teq + ")" | |
| eq_map[h + t] = heq2 + "+" + teq2 | |
| eq_map[h - t] = heq2 + "-" + teq2 | |
| eq_map[h * t] = heq2 + "*" + teq2 | |
| if t != 0: | |
| eq_map[h / t] = heq2 + "/" + teq2 | |
| return eq_map | |
| # | |
| all_vals = get_eq_map([float(x) for x in range(1, 10)]) | |
| print 'Time:', time.time() - start_time | |
| print 'Total number of values', len(all_vals) | |
| for i in (float(x) for x in range(1900, 2101)): | |
| if i in all_vals: | |
| print i, all_vals[i] | |
| else: | |
| print i, 'None' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment