Last active
September 27, 2017 16:59
-
-
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 …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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!