Earlier quoted context omitted.
It's a bit overly clever. I am a huge fan of Peter Norvig (and he was once my skip-level boss), but I'd call this code out in a code review for being obtuse. Notice that the input, `nums`, is a set. So he's taking the intersection of two sets. One set always has one item, so the result will be a collection with either 0 or 1 items. It could have also been written as: first(x * (2020 - x) for x in nums if (2020 - x) i…
I am not sure I would advocate for this but, from Python 3.8, this is an instance where assignment expressions could allow the first code without duplication. first(x * y for x in nums if (y := 2020 - x) in nums and x != y)
Peter Norvig's “pytudes” for Advent of Code 2020
91–96 of 96 posts
Re: Peter Norvig's “pytudes” for Advent of Code 2020
#92Re: Peter Norvig's “pytudes” for Advent of Code 2020
#93This is very well-written code. I don't think anyone would bring into question Norvig's technical skills, but I wouldn't think someone like him would spend much time at his job writing code, or have much free time to program as a hobby. As someone who aspires to a position similar to Norvig's (directing research at an organization that is frequently applied in real life), I think it's really cool to see him having ti…
If you haven't, you should check out the free course he gave at Udacity.
https://www.udacity.com/course/design-of-computer-programs--...
I have been meaning to brush up on my interview skills, and I've found the lack of structure in LeetCode's Monthly problem sets to be quite frustrating. Every time I'd get stuck on a certain problem, I'd dive deep into the techniques behind the solution just to discard that knowledge the next day, as the selection of problems was seemingly random.
Re: Peter Norvig's “pytudes” for Advent of Code 2020
#94Earlier quoted context omitted.
Given that the comment says "Find 2 distinct numbers that sum to 2020", I expect duplicate entries are not allowed.
Yes, but I cannot find the same in the problem definition: "they need you to find the two entries that sum to 2020 and then multiply those two numbers together" https://adventofcode.com/2020/day/1
Re: Peter Norvig's “pytudes” for Advent of Code 2020
#95Earlier quoted context omitted.
Full quote > Family holiday preparations kept me from doing Part 2 on the night it was released, and unfortunately I didn't feel like coming back to it later: it seemed too tedious for too little reward. I thought it was inelegant that a solid block of # pixels would be considered a sea monster with waves. Phew - I'm not alone.
My computers are full of half finished projects if that makes you feel better. :)
Re: Peter Norvig's “pytudes” for Advent of Code 2020
#96I thought I spotted a bug on the very first day, part 2. def day1_2(nums): "Find 3 distinct numbers that sum to 2020, and return their product." return first(x * y * z for x, y in combinations(nums, 2) for z in nums & {2020 - x - y} if x != z != y) The last line (if x != z != y) returns true when x and y are equal and z is different. But x and y are already constrained to be different since nums is a set and itertool…