3 ms·
Author here. If I may, could I ask you to write the equivalent code as you have suggested? I can see one way to implement with generators but it's not particula
by brilee 10y ago
Author here. If I may, could I ask you to write the equivalent code as you have suggested? I can see one way to implement with generators but it's not particularly concise. The benefit of the list comprehension approach is that its mechanics are laid bare and there's almost nothing to debug.
The normal benefit of generators is that they don't store the intermediate result in memory, but that isn't particularly necessary in this case
- sametmax 10y agoI actually tried, and my generator version ended up less readable than yours. I was not expecting this, but for such an algo the way you write it gives this impression the data flow from one end to the other, which reads better than splitting it in several functions including nested blocks. The only drawbacks I can see: - if you want to debug that with PDB, you are out of luck. - anybody not wanting to spend the time understanding what it does will freak out. Unrelated, but you can do: def get_ranges(i): return product(*(range(1, 27) for _ in range(i))) To replaced stuff like: for th in range(1, 27) for o in range(1, 27) for r in range(1, 27) With: for th, o, r in get_ranges(3) But again, no sure it makes things better. Shorter yes, but the benefit of your version is that each step is very clear. I still have no idea what's this code is for though :)
- hcrisp 10y agoHere's a generator version. It is not as terse, but seems to run faster. [Edit:] Or not faster in entirety, but finds solution more quickly during the iteration. from fractions import Fraction as F def thor(): for th in range(1, 27): for o in range(1, 27): for r in range(1, 27): if 2*(th**2) + o**2 + r**2 == 1000: yield th, o, r def sz(thors): for th, o, r in thors: for s in range(1, 27): for z in range(1, 27): if (th**2 * z**2 * (s - z) + s**2) == 7225: yield th, o, r, s, z def gdxi(szs): for th, o, r, s, z in szs: for g in range(1, 27): for d in range(1, 27): for xi in range(1, 27): if (g-1)**2 + d**2 + xi**2 - xi == 600: yield th, o, r, s, z, g, d, xi def eti(gdxis): for th, o, r, s, z, g, d, xi in gdxis: for (et, i) in ( (xi-7, xi-11), (xi-11,xi-7), (xi+7, xi+11), (xi+11, xi+7)): if 0 < et <= 26 and 0 < i <= 26: yield th, o, r, s, z, g, d, xi, et, i def amupi(etis): for th, o, r, s, z, g, d, xi, et, i in etis: for (a, mu) in ((4, 1), (2, 2), (1, 4)): for pi in range(1, 27): if a*(a+pi) == 4 * g: yield th, o, r, s, z, g, d, xi, et, i, a, mu, pi def kben(amupis): for th, o, r, s, z, g, d, xi, et, i, a, mu, pi in amupis: for k in (3,4): for b in range(1, 27): for e in range(1, 27): for n in range(1, 27): if b**3 + z**3 + n**3 + o**9 == 1997: if (F(a, k)**2 + F(b, n)**2 + F(d, xi)**2 + F(e, pi)**2 + F(et, mu)**2 + F(i, s)**2 == 6): yield (a, b, g, d, e, z, et, th, i, k, th, mu, n, xi, o, pi, r, s) thors = thor() szs = sz(thors) gdxis = gdxi(szs) etis = eti(gdxis) amupis = amupi(etis) results = kben(amupis) print('\t'.join(('a', 'b', 'g', 'd', 'e', 'z', 'et', 'th', 'i', 'k', 'l', 'mu', 'n', 'xi', 'o', 'pi', 'r', 's'))) for r in results: print('\t'.join(map(str, r)))