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] | |
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
Oi Masanori, descobri seu gist pela palestra Para Gostar de Python (Python Nordeste 2013). Legal, eu gosto das suas palestras.
Só por curiosidade, eu tinha feito mais ou menos como você, depois fiquei brincando de python golf e cheguei nessa solução:
https://gist.github.com/eee19/5655684
Resolvi tentar em Ruby também (não sou religioso, qualquer uma das duas linguagens me deixa feliz) e cheguei nesse:
https://gist.github.com/eee19/5655686
Se um dia você quiser usar como exemplo também (duvido, não é tão legível), fique à vontade.