Is bit shifting O(1) or O(n)?

Some instruction sets are limited to one bit shift per instruction. And some instruction sets allow you to specify any number of bits to shift in one instruction, which usually takes one clock cycle on modern processors (modern being an intentionally vague word). See dan04’s answer about a barrel shifter, a circuit that shifts more … Read more

Optimizing Conway’s ‘Game of Life’

I am going to quote my answer from the other question, because the chapters I mention have some very interesting and fine-tuned solutions. Some of the implementation details are in c and/or assembly, yes, but for the most part the algorithms can work in any language: Chapters 17 and 18 of Michael Abrash’s Graphics Programmer’s … Read more

When a method has too many parameters? [closed]

There is no clear limit, but I am uncomfortable with more than 3-4 parameters. AFAIR Uncle Bob Martin in Clean Code recommends max 3. There are several refactorings to reduce the number of method parameters (see Working Effectively with Legacy Code, by Michael Feathers for details). These come to my mind: encapsulate many related parameters … Read more

The type system in Scala is Turing complete. Proof? Example? Benefits?

There is a blog post somewhere with a type-level implementation of the SKI combinator calculus, which is known to be Turing-complete. Turing-complete type systems have basically the same benefits and drawbacks that Turing-complete languages have: you can do anything, but you can prove very little. In particular, you cannot prove that you will actually eventually … Read more

Is “Out Of Memory” A Recoverable Error?

It really depends on what you’re building. It’s not entirely unreasonable for a webserver to fail one request/response pair but then keep on going for further requests. You’d have to be sure that the single failure didn’t have detrimental effects on the global state, however – that would be the tricky bit. Given that a … Read more

What are some algorithms for comparing how similar two strings are?

What you’re looking for are called String Metric algorithms. There a significant number of them, many with similar characteristics. Among the more popular: Levenshtein Distance : The minimum number of single-character edits required to change one word into the other. Strings do not have to be the same length Hamming Distance : The number of … Read more