What is an NP-complete in computer science?

What is NP? NP is the set of all decision problems (questions with a yes-or-no answer) for which the ‘yes’-answers can be verified in polynomial time (O(nk) where n is the problem size, and k is a constant) by a deterministic Turing machine. Polynomial time is sometimes used as the definition of fast or quickly. … Read more

Check if all elements in a list are identical

Use itertools.groupby (see the itertools recipes): from itertools import groupby def all_equal(iterable): g = groupby(iterable) return next(g, True) and not next(g, False) or without groupby: def all_equal(iterator): iterator = iter(iterator) try: first = next(iterator) except StopIteration: return True return all(first == x for x in iterator) There are a number of alternative one-liners you might … Read more

Why does Java’s hashCode() in String use 31 as a multiplier?

According to Joshua Bloch’s Effective Java (a book that can’t be recommended enough, and which I bought thanks to continual mentions on stackoverflow): The value 31 was chosen because it is an odd prime. If it were even and the multiplication overflowed, information would be lost, as multiplication by 2 is equivalent to shifting. The … Read more

What is the most efficient/elegant way to parse a flat table into a tree?

Now that MySQL 8.0 supports recursive queries, we can say that all popular SQL databases support recursive queries in standard syntax. WITH RECURSIVE MyTree AS ( SELECT * FROM MyTable WHERE ParentId IS NULL UNION ALL SELECT m.* FROM MyTABLE AS m JOIN MyTree AS t ON m.ParentId = t.Id ) SELECT * FROM MyTree; … Read more

What algorithms compute directions from point A to point B on a map?

Speaking as someone who spent 18 months working at a mapping company, which included working on the routing algorithm… yes, Dijkstra’s does work, with a couple of modifications: Instead of doing Dijkstra’s once from source to dest, you start at each end, and expand both sides until they meet in the middle. This eliminates roughly … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)