The Question : 313 people think this question is useful What is the cost of len() function for Python built-ins? (list/tuple/string/dictionary) The Question Comments : The Answer 1 379 people think this answer is useful It’s O(1) (constant time, not depending of actual length of the element – very fast) on every type you’ve mentioned,
The Question : 427 people think this question is useful I need a function which takes in a list and outputs True if all elements in the input list evaluate as equal to each other using the standard equality operator and False otherwise. I feel it would be best to iterate through the list comparing
The Question : 1951 people think this question is useful I have recently stumbled upon the game 2048. You merge similar tiles by moving them in any of the four directions to make “bigger” tiles. After each move, a new tile appears at random empty position with a value of either 2 or 4. The
The Question : 249 people think this question is useful What is the most efficient algorithm to achieve the following: 0010 0000 => 0000 0100 The conversion is from MSB->LSB to LSB->MSB. All bits must be reversed; that is, this is not endianness-swapping. The Question Comments : I think the appropriate name is a bitwise
The Question : 255 people think this question is useful What is the most efficient way given to raise an integer to the power of another integer in C? The Question Comments : When you say “efficiency,” you need to specify efficient in relation to what. Speed? Memory usage? Code size? Maintainability? Doesn’t C have
The Question : 273 people think this question is useful Given integer values x and y, C and C++ both return as the quotient q = x/y the floor of the floating point equivalent. I’m interested in a method of returning the ceiling instead. For example, ceil(10/5)=2 and ceil(11/5)=3. The obvious approach involves something like:
The Question : 350 people think this question is useful After using PHP for a while now, I’ve noticed that not all built-in PHP functions are as fast as expected. Consider these two possible implementations of a function that finds if a number is prime using a cached array of primes. This is because in_array
The Question : 651 people think this question is useful How do you generate all the permutations of a list in Python, independently of the type of elements in that list? For example: The Question Comments : I agree with the recursive, accepted answer – TODAY. However, this still hangs out there as a huge
The Question : 533 people think this question is useful A developer, let’s call him ‘Dave’, insists on using home-brew scripts for password security. See Dave’s proposal below. His team spent months adopting an industry standard protocol using Bcrypt. The software and methods in that protocol are not new, and are based on tried and
The Question : 3959 people think this question is useful Yesterday I was pairing the socks from the clean laundry and figured out the way I was doing it is not very efficient. I was doing a naive searchΒ β picking one sock and “iterating” the pile in order to find its pair. This requires iterating
The Question : 4176 people think this question is useful What is the most concise and efficient way to find out if a JavaScript array contains a value? This is the only way I know to do it: Is there a better and more concise way to accomplish this? This is very closely related to
The Question : 0 people think this question is useful I am trying to solve the problem of calculating the number of inversions where the problem is: `An inversion of a sequence π0, π1, . . . , ππβ1 is a pair of indices 0 β€ π < π < π such that ππ >