News for hackers. Stuff that matters.
Re:Multiplication: Finding the Greatest Product by Strilanc on Saturday November 01, 2014 @09:17PM (#8545232)
You can cut a lot of the boilerplate in your code by using existing functions (apologies if I get the argument orders wrong):

    def best_digit_choice_to_maximize_product(n, digits):
      numer = lambda d: reduce(
          sorted(d, reverse=True),
          0,
          lambda a, e: a*10 + e)
      return max(
          itertools.combinations(digits, n),
          key = lambda e: numer(e) * numer(set(digits) - set(e)))
Re:Multiplication: Finding the Greatest Product by cousin_it on Saturday November 01, 2014 @10:35PM (#8545389)
My code runs in O(n log n) time and can deal with repeated digits. You converted it to something that takes exponential time and chokes on repeated digits.

(BTW, I just realized that my code can be made O(n), by replacing the default sort with a counting sort :-))

Re:Multiplication: Finding the Greatest Product by Strilanc on Saturday November 01, 2014 @11:49PM (#8545559)
Hm, embarrassing, I just assumed you were doing the naive brute force. You're de-interleaving the digits with an order swap when the digits first start to differ.
Re:Multiplication: Finding the Greatest Product by cousin_it on Sunday November 02, 2014 @12:30AM (#8545629)
Yeah, that's pretty much it.
Re:Multiplication: Finding the Greatest Product by thomasahle on Sunday November 02, 2014 @07:02AM (#8546378)
It's nice to have this version for testing the faster, more complicated one from OP. I can attest that they indeed produce the same answers.

Two fixes: - If you use `(Counter(digits)-Counter(e)).elements()` in the last line, you can support repeated digits. - Reduce is `reduce(function, sequence[, initial]) -> value` so you should move the lambda to the first argument.

All in all I think this is a very nice, succinct use of Python. The combinatorial parts of `itertools` are extreamly handy :)

Re:Multiplication: Finding the Greatest Product by jmduke on Saturday November 01, 2014 @10:16PM (#8545353)
I don't mean this as a criticism -- as you are absolutely correct, your code does cut out a lot of the boilerplate and makes for a more compact function -- but I vastly prefer @cousin_it's function to yours in terms of readability. I think Python is a very pretty language but I always have a hard time reading Python code when it's heavy on the functional paradigms.