Bit Counting

Write a function that takes an (unsigned) integer as input, and returns the number of bits that are equal to one in the binary representation of that number.

Example:

The binary representation of 1234 is 10011010010, so the function should return 5 in this case


In [5]:
def countBits(n):
    count = 0
    while n > 0:
        count += (n & 1)
        n = n >> 1
        
    return count

In [6]:
print countBits(1234)


5

Remove the Min

Given an array of integers, remove the smallest value. Do not mutate the original array/list. If there are multiple elements with the same value, remove the one with a lower index. If you get an empty array/list, return an empty array/list.

Don't change the order of the elements that are left.

Examples

remove_smallest([1,2,3,4,5]) = [2,3,4,5]
remove_smallest([5,3,2,1,4]) = [5,3,2,4]
remove_smallest([2,2,1,2,1]) = [2,2,2,1]

In [7]:
def remove_smallest(numbers):
    if len(numbers) < 1: 
        return numbers
    
    idx = numbers.index(min(numbers))
    return numbers[0:idx] + numbers[idx+1:]

In [8]:
print remove_smallest([5,3,2,1,4])


[5, 3, 2, 4]

In [ ]: