Skip to content

Instantly share code, notes, and snippets.

@fmasanori
Last active September 27, 2017 16:59
Show Gist options
  • Save fmasanori/5620075 to your computer and use it in GitHub Desktop.
Save fmasanori/5620075 to your computer and use it in GitHub Desktop.
Selection Test 2013 Facebook Hackaton Given two positive integers n and k, generate all binary integer between 0 and 2 ** n-1, inclusive. These binaries will be drawn in descending order according to the number of existing 1s. If there is a tie choose the lowest numerical value. Return the k-th element from the selected list. Eg n = 3 and k = 5 …
def hack1(n, k):
def f(s):
return s.count('1')
binaries = []
for x in range(2**n):
binaries.append(bin(x))
binaries.sort(key=f, reverse = True)
return binaries[k - 1]
def hack(n, k):
return sorted([bin(x) for x in range(2**n)],
key=lambda s: s.count('1'),
reverse = True)[k-1]
@cyberplant
Copy link

Seems that the links provided above changed to:

https://gist.github.com/douglasdrumond/5655684

and

https://gist.github.com/douglasdrumond/5655686

Thanks Fernando for those gists!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment