Hacker News new | past | comments | ask | show | jobs | submit login
Problem Solving with Algorithms and Data Structures Using Python (interactivepython.org)
294 points by samrohn on Dec 21, 2018 | hide | past | web | favorite | 30 comments



I opened 1.8.1 because I thought it'd be the first section with code and the code was not PEP-8 compliant. Having a brief scan, the authors seem to have a pretty severe aversion to putting spaces after commas, which is a bummer because it really impacts code readability. It's inconsistent even from line to line, so it's not a style thing. They're also inconsistent about using single and double quotes for strings.

I skipped ahead to "implementing a stack in python", which is a little weird because the stack class they implement is just a thin wrapper around lists with append aliased to push and "self.items[len(self.items)-1]" aliased to peek (note: in python you can just use negative indexing to access items near the end of a list)

This might seem pedantic, but, like, if the book isn't leveraging Python and it's not particularly clean code then I don't really understand what's on offer here.


I don't think that's fair. I thought the discussion of AVL trees was quite readable. The code is basic and straightforward, and that's what you want for something like that. It's about the algorithms, not about being pythonic -- it's about fundamentals. For that, this seems good to me (though I'll admit I haven't read a lot of it yet.)


"I don't want to read something because I don't like the writing style."

I personally can get behind that argument. As an author you have to incentivize the reader to engage with what you've written, that's the job of the writer, not the reader. If you, in this way, alienate some users (like the one you replied to) so be it, this is also a choice of the author. I personally can't read `def fun(a,b): ...`, I strongly prefer `def fun(a, b): ...` and hence I stopped reading as well.


the amazon comment section for any technical books are littered with comments like yours(and i have declined to purchase books based on these comments).


That you don't like the taste of no space after comma is a fair reason, but it is still a pedantic reason.

Personally I think the importance depends on the font face and particular letters used, and in your example it was entirely unimportant: I couldn't actually see the difference for a good while.


I'd usually not respond, but this is overly negative and missing a number of points.

* PEP-8: This code is trying to demonstrate some pretty basic algorithms in simple terms. Should we care about double quotes versus single quotes and spaces to separate commas? If anything, this is an editorial issue in my opinion. Perhaps for your projects and production code you want it to be strictly PEP-8 compliant, but it does not diminish what is being presented here, which are examples of basic algorithms.

* Stack class is a thin wrapper: The author was built a stack class using common conventions. Some of it is pretty redundant, such as "def pop(self): return self.items.pop()", but the point remains. This is a clear and simple representation of an algorithms, focused on a common language. The fact that you can do stack-like things to a list misses the point of making a Stack class act and look like a stack. If anything, Python's list syntax makes implementing this minimalist.

* "The books provided on interactivepython.org are free and open source. They are for educational purposes." Perhaps the editing could be better. There are many ways to solve the same problem. At the end of the day this is given out for free and is a wealth of information.


PEP-8 is a red herring. Aesthetics, however, is not. The beauty of code is often an excellent indicator of its general quality.

You bring up a good point about editing and the value of free/open-source. I've been thinking about this often, recently. It's similar to the troubles plaguing news media. The propaganda may not be as malicious, but is similarly self-promoting, or promoting one's interests. Students have always suffered from bad textbooks, but before the internet we relied on professors to select good ones. Now we just have popularity and the vagaries of Google or upvotes on Hacker News. Of course, professors haven't always been great editors either.


This is a great book. I read it and helped me a lot. I recommended it to two other people who had upcoming interviews who did not know about algorithms and they got the jobs. So yes your comment is pedantic. If you read the book you will learn the book isn’t about python at all but about problem solving and data structures.


[flagged]


It's against the HN guidelines to insinuate astroturfing without evidence. Some other users holding a different view doesn't count as evidence.

If you think you're seeing abuse, you're always welcome to email hn@ycombinator.com so we can investigate—which we always do. But please don't post like this in the threads.


There are different qualities of evidence, but the one I provided evidently didn't make the quality bar you're looking for. I wasn't insinuating but accusing. I'll email that account instead the next time, I wasn't aware of that as an option.


Accusing is even worse! The problem is that, while this sort of abuse is real, it's much less common than people imagining that they see it when it's not really there. I've written about this a ton in the hope of explaining why we have the rule we do: https://hn.algolia.com/?query=by:dang%20astroturfing&sort=by...


I suppose it's very difficult to tell the difference between astroturfing and a group of people who feel compelled to voice an appreciation for something, perhaps notified by a "Hey I posted this to Hacker News!" message on a mailing list or a Twitter profile they follow. In this case, yes, I would fall into the category of people (as you described in your comments elsewhere) with a strong opinion and confusion over why someone would think otherwise. Except, of course for the author of the article and friends.


Is there an alike book that would provide functional implementations of the algorithms alongside or instead of imperative ones? In Python preferably but I don't mind any other language. I would like to learn implementing algorithms and solving problems the functional way.


Elements of Programming Interviews in Python is excellent imo. The authors give a crash course in all of the data structures and all of the code is written extremely cleanly. Most of the problems give standard solutions and also idiomatic ones using iter/functools. I've been programming Python for years and felt like I gained a lot from it as a developer even though I bought it to interview prep.

Quick example of the "look and say" problem:

def look_and_say(n: int) -> str:

    s = '1'

    for _ in range(n - 1):

        s = ''.join(

            str(len(list(group))) + key

            for key, group in groupby(s))

    return s


I hadn't heard of this before, so I went and checked this out this morning. I haven't read too much, but I've liked what I saw. And I notice that there are java and C++ variants for those interested in those languages. Thank you for your suggestion.


I am using this book for interview prep as well. Code is pythonic and a joy to read.


+1 for EPI. Using it now myself. It's great stuff.


A lot of imperative algorithms don't really have functional analogues. Sometimes you need to mutate in place to achieve optimal algorithmic complexity.

That said, I think this might be what you're looking for: https://cs.uwaterloo.ca/~plragde/flaneries/FDS/


> A lot of imperative algorithms don't really have functional analogues.

But there are to be the ways functional programmers solve the same task?

Thank you for the link.


Definitely. But unless special care is taken, these will use (normally) more space, and potentially more time. I think Chris Osaki did a bunch of work on functional data structures for handling immutability efficiently (still less efficiently) and that some of this was implemented in Clojure.


Functional programming is not supposed to be a golden hammer. That's the pragmatic solution for this problem ;).


Nice resource, thank you for sharing. It's been a why since I've used Ocaml but this will be a good opportunity to review.


https://github.com/liuxinyu95/AlgoXY

In this book the author shows brute force, improved and functional implementations in python, c and haskell.


You can check out [1], which is used to teach 15-210 [2], the parallel data structures / algorithms course at CMU. The most recent version of the book is students-only it seems, on a private site (I took the course last semester), however this version is close and comprehensive.

[1] http://www.parallel-algorithms-book.com [2] http://www.cs.cmu.edu/~15210


Previous discussion (March 2017, with link dated 2015):

https://news.ycombinator.com/item?id=13854087


Ironically, the provided implementations for Queue and Deque are algorithmically terrible, giving O(n) performance for operations that should be O(1).

Queue and Deque could easily be developed from first principles by building on dictionaries or linked lists. It was a mistake to use list operations like s.pop(0) and s.insert(0, x).


Hey, here the some key points to know about algorithms, machine learning and python. Just check it out https://www.easythings.xyz/2018/07/machine-learning-algorith...


and for beginners to programming and Python, the same site provides this excellent course: https://interactivepython.org/runestone/static/thinkcspy/ind...


In school I was quite confused about how merge sorts worked until I got a deck of cards and walked through the algorithm a few times. This site made it dead simple though. If only I'd had it instead of my textbook


Sweet! That covers a lot of what I'm about to teach :)




Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact

Search: