# Tag: algorithm

## python – Cost of len() function

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,

## python – Check if all elements in a list are identical

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

## logic – What is the optimal algorithm for the game 2048?

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

## Efficient Algorithm for Bit Reversal (from MSB->LSB to LSB->MSB) in C

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

## c – The most efficient way to implement an integer based power function pow(int, int)

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

## Fast ceiling of an integer division in C / C++

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:

## performance – List of Big-O for PHP functions

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

## python – How to generate all permutations of a list?

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

## hash – Is my developer’s home-brew password security right or wrong, and why?

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

## algorithm – How can I pair socks from a pile efficiently?

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

## How do I check if an array includes a value in JavaScript?

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

## java – Divide and Conquer Algorithm (MergeSort Algo) calculating number of inversions

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 ππ >