Peter Norvig's “pytudes” for Advent of Code 2020(github.com) |
Peter Norvig's “pytudes” for Advent of Code 2020(github.com) |
Too bad he punted on 20... one of the few difficult problems this year. Usually the hard problems are where you really see the contrast between his solutions and the rest of us plebs. Do I sense a hit of frustration in his comment? ;)
"too tedious for too little reward...." "sea monster as characters inelegant"
> 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.
I'm still looking forward to reviewing how Norvig did most of the problems.
Asking because for example most solutions to Day 1 I've seen are O(n^2)
I sometimes go back and clean up my solutions, though I aim for a combination of clarity and performance if I do that, clarity first.
Yeah, I too smiled at his comment last week.
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 time to do stuff like this.
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.
The guy created very clean python solutions, and very insigthful walkthrough (although a bit wordy some times). It was my go to repo after validating my solutions on AoC (he managed to publish daily during the whole challenge !).
In this case I think they work really well, and aren't stuck in places where they're not needed.
[0]: https://github.com/norvig/pytudes/blob/master/requirements.t...
> but the outermost edges won't line up with any other tiles.
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 itertools combinations picks distinct elements from the iterable. If the test had been x != y != z it would have been a bug.
Love it. There's so many bits of Python I've written where I should have had something like this.
> cat = ''.join
0 = 0 + 0
then would a pythonista be someone who knows that ''==''+''
?His "poker" course (on YouTube) is also golden standard.
Why? The background in math, basic data structures and logic, so one know the range and boundaries of what is possible. Plus careful attention to details, like it is in any art.
Also, I can't help but wonder. Why does the Director of Google Research and a computer scientist use his personal time to code, what looks like exclusively, in Python rather than other interesting languages? Is it a bit of marketing for Google and/or his book, or does he just like Python?
[0] https://www.udacity.com/course/design-of-computer-programs--...
Those are things that really help in algorithmic code by increasing accuracy of the names of things to the application (almost creating a domain specific language).
For Scheme I think my thinking came from working through SICP many years ago while the other languages in the list force you basically to first think about a solution before you start typing.
I am not saying, obviously, people cannot think up these things straight in rust, python, c# etc, however, for me personally, it works much better if I first work it out in one of the others; it is way too tempting to grab for my plumbing wrench otherwise.
After that, it's just practice interspersed with data structure and algorithm research.
I agree with you completely here. I also feel like I fit the description of a plumber presented here. The most challenging algorithmic problems that I've had to confront in my career, which came when I was working in DSP for an RF company, were things that were tackled by the team -- a group of very talented and very experienced engineers -- and we worked on these problems collaboratively and over a period of time that is not measurable in minutes or hours.
But, having said that, isn't it also undeniable these things, these leetcode-style algorithms, are used to gatekeep entry to many of the "better" jobs in our field, despite the fact that we know that these things are not strictly necessary on the job? At the very least, this has been my experience and I read the same anecdotes on HN frequently.
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) in nums and x != (2020 - x))
But that has a lot of duplication, so it'd probably be best to just use an imperative approach here and do something like: def day1_1(nums):
"Find 2 distinct numbers that sum to 2020, and return their product."
for x in nums:
y = 2020 - x
if x == y:
continue
if y in nums:
return x * yBut he defines it above in his commonly used functions:
def first(iterable, default=None) -> object:
"Return first item in iterable, or default."
return next(iter(iterable), default) first(x * y
for x in nums
if (y := 2020 - x) in nums and x != y)Completely agree, your solution is more clear.
def day1_1(nums):
"Find 2 distinct numbers that sum to 2020, and return their product."
return first(x * y
for x in nums
for y in nums & {2020 - x}
if x != y)
`nums` is a set of integers
`nums & {2020 - x}` Finds all the numbers that are in the set of `nums` and the set of `{2020 - x}` - this basically extracts a number, `y`, for which `x+y = 2020`.So for each x in nums, he extracts a number from nums that, added to x, equals 2020. If there is such a number y, he checks if it is the same as x, and only if it is different, does he yield in his generator, as the product of y and x.
It's succint, but not exactly readable, imho.
probably because he cares more about the problems and less about the language and python is a pleasant general purpose language (and ubiquitous in AI research)
I never understood why many software developers are so focused on languages rather than on problems, the language at the end of the day doesn't really matter, unless the problem is in a very peculiar domain.
My favorite essay is http://norvig.com/21-days.html after reading this essay you might change your opinion about he finding only python interesting.
We don't have to all enjoy the same things.
In this particular case also, if you read his code, I would be surprised if you don't learn something.
I'm sure I would learn something if I read through the code in more detail, but that's true of a ton of things and time is what it is. I merely commented on what I personally find interesting and uninteresting. Also, even I, a non-Director of Research at Google and non-computer scientist, don't particular enjoy using the language I use on a day-to-day basis for fun in my personal time.
The added benefit of Python is the ease of using something like Jupyter notebooks, which make it trivial to iterate.
(disclaimer: I coded more in Ruby than in Python, but I still enjoy both)
Although, other languages do have better REPLs, allowing easier iteration than Python's REPL. Some languages, have both a better REPL and notebook-style programming, such as F#.
But I was really more curious rather than saying which language would be "better".
As an adult, I have to say that for me doing AoC brought back a little of that magic. It helped that the problems unlocked at 6am in my timezone so I'd wake up, hack on the problem for an hour or so then walk my dog and get ready for work. It was also nice having a collective experience, knowing that not only 5-6 of my colleagues but thousands of other people across the globe were all going through the same problem at the same time. I really liked that feeling.
So yeah Project Euler has very compact, well-written, well-designed, challenging problems in spades ... but AoC is a very specific sort of event that I must admit I look forward to more and more each year :-)
But my question was trying to understand if Python has some hidden mojo that Norvig really likes or if he uses it like this publicly because he’s a top leader at a company that also uses it heavily. People listen to people like that in positions like his, and if he did all his solutions in Julia, F#, or whatever, that’d probably route a good amount of attention to those other languages that Google and his book doesn’t really use or hire for.
That's absolutely true. But once you've been through the language paradigms, written Forth and Prolog and Haskell and APL and Lisp, you don't gain much by immersing in a new language.
At that point you start looking for languages where the things you loathe about them intersect minimally with what you work on. Python has relatively little to loathe and is very convenient. I still reach for it by default as well, and I was at one point best known for my Haskell work.
It has nothing to do with Google marketing. Norvig's established enough where he lends luster to Google, not vice versa.
I think you may be somewhat confused there. What book are you talking about?
Edit: I also wonder why his remarks ten or twenty years ago about Python being close to pseudocode "are not really relevant in today's world".
In the HN comment he says "the Lisp code that Russell and I had online" so I don't think it's difficult to guess it's indeed AIMA he's talking about.
I don't think that the book, in any of the four editions, "primarily uses or encourages Python". I have not checked them all, but I expect the book to still use pseudocode exclusively. Elsewhere, they provide implementations in different languages: https://github.com/aimacode
Why are these comments so hostile?
"they need you to find the two entries that sum to 2020 and then multiply those two numbers together"
I still think it's fair to say the book encourages the use of Python if one of the primary authors publishes solutions in that language over existing solutions in another language and the author promotes Python. Of course, it seems over time other languages have grown into providing solutions. I don't have the book (because of its exorbitant price) but do have PAIP, so I can't say for sure. Even the GitHub repo for the Lisp implementation says its out of date and was used back in 1995. This latter point is relevant and was what I was curious about. Were Norvig's comments referring to a time before the first edition was published, or was it after the first edition was published and sometime in between the first and second editions? I'm just generally curious about the context in which he's referring to.
So you can say that I was confused but I cannot say that you were confused? Ok then.
What do you find unclear, really?
The 1st edition of AIMA is from February 1995.
The "Python for Lisp Programmers" page is from May 2000.
The second paragraph starts: "I looked into Python because I was considering translating the Lisp code for the Russell & Norvig AI textbook into Java."
Why aren't you satisfied with his own explanations that you already knew about?
"I looked around for a language that was closer to the pseudocode in the book, and discovered Python was the closest. [...] Python is an excellent language for my intended use. It is easy to use (interactive with no compile-link-load-run cycle), which is important for my pedagogical purposes."
"I looked for the language that was most like our pseudocode, and found that Python was the best match. Then I had to teach myself enough Python to implement the examples from the textbook. I found that Python was very nice for certain types of small problems, and had the libraries I needed to integrate with lots of other stuff, at Google and elsewhere on the net."