4 ms·
Well, the Right Answer is surely to filter on a generator, rather than this ghastly mutable state - which points firmly towards what the author calls the "funct
by Smaug123 6y ago
Well, the Right Answer is surely to filter on a generator, rather than this ghastly mutable state - which points firmly towards what the author calls the "function" solution.
- throwaway_pdp09 6y agoDon't want to be rude but I'm getting really fed up with the state = bad mindset that's now becoming a dogma. State is there to be used appropriately. There's nothing wrong with state, gotos or whatever, and there's nothing right about functional or OO. It's how you use them.
- Smaug123 6y agoIn this instance, I'm pretty confident the generator version is just a better phrasing. It's shorter, contains no mutable state to get wrong, is Pythonic, and can instantly be made lazy by replacing the square brackets with round ones. `[p for p in permutations(numbers) if primePairs(p)]`
- djur 6y agoThis would have been a better original comment than just calling the solution in the blog post "ghastly".
- throwaway_pdp09 6y agoI retract what I said. You were right.
- deleted 6y ago[deleted]
- kwhitefoot 6y ago> state = bad mindset that's now becoming a dogma. You must be very young. State = bad has been a guiding principle for many decades.
- contravariant 6y agoArguably the Right Answer wouldn't filter on a generator of all permutations but would generate the permutations in a way that didn't include invalid ones to begin with. Something like: def extend(perm): for k in range(1,11): if k not in perm and isprime(k + perm[-1]): yield [*perm, k] perms = [[]] for _ in range(10): perms = [extend(perm) for perm in perms] (This is just a draft, but you get the gist)
- Smaug123 6y agoOh, absolutely, but the article is predicated on using the most naive brute force.