How to Solve ‘isMAC48Address’ in CodeFights

The Problem:

A media access control address (MAC address) is a unique identifier assigned to network interfaces for communications on the physical network segment.

The standard (IEEE 802) format for printing MAC-48 addresses in human-friendly form is six groups of two hexadecimal digits (0 to 9 or A to F), separated by hyphens (e.g. 01-23-45-67-89-AB).

Continue reading “How to Solve ‘isMAC48Address’ in CodeFights”

How to Solve ‘DifferentRightmostBit’ in CodeFights

The Problem

You’re given two integers, n and m. Find position of the rightmost bit in which they differ in their binary representations (it is guaranteed that such a bit exists), counting from right to left.

Return the value of 2position_of_the_found_bit (0-based).

Example

For n = 11 and m = 13, the output should be
differentRightmostBit(n, m) = 2.

1110 = 10112, 1310 = 11012, the rightmost bit in which they differ is the bit at position 1 (0-based) from the right in the binary representations.
So the answer is 21 = 2.

Input/Output

  • [time limit] 4000ms (py)
  • [input] integer nConstraints:
    0 ≤ n ≤ 230.
  • [input] integer mConstraints:
    0 ≤ m ≤ 230,
    n ≠ m.
  • [output] integer

Continue reading “How to Solve ‘DifferentRightmostBit’ in CodeFights”

How to Solve “secondRightmostZeroBit” in CodeFights

NOTE: Follow THIS link for more solutions.

The Problem:

Presented with the integer n, find the 0-based position of the second rightmost zero bit in its binary representation (it is guaranteed that such a bit exists), counting from right to left.

Return the value of 2position_of_the_found_bit.

Example

For n = 37, the output should be
secondRightmostZeroBit(n) = 8.

3710 = 1001012. The second rightmost zero bit is at position 3 (0-based) from the right in the binary representation of n.
Thus, the answer is 23 = 8.

Input/Output

  • [time limit] 4000ms (py)
  • [input] integer nConstraints:
    4 ≤ n ≤ 230.
  • [output] integer

Continue reading “How to Solve “secondRightmostZeroBit” in CodeFights”

CodeFights – Powers Of Two

Every positive integer can be uniquely represented as a sum of different positive powers of two.

Given a number n, return a sorted array of different powers of two that sum up to n.

Example

For n = 5, the output should be
powersOfTwo(n) = [1, 4].

Input/Output

[time limit] 4000ms (py)

[input] integer n

A positive integer.

Constraints:
5 ≤ n ≤ 6000.

[output] array.integer

An array of different powers of two, in ascending order.

 

Solution

def powersOfTwo(n):
  a=[]
  x=1
  while n:
    if n&1:
      a.append(x)
    x*=2
    n/=2
  return a