Last active
November 18, 2018 17:22
-
-
Save manvillej/82333fdcb01034396e531a9af4e233ef to your computer and use it in GitHub Desktop.
This file contains hidden or 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 getArrayOfPowersOfTwos(number): | |
""" | |
breaks an integer into an array of numbers that represent the number as a series of 2 to a power in order from lowest to highest. | |
for example: 9 = 2**0 + 2**3. | |
>>> getArrayOfPowerssOfTwos(9) | |
[0, 3] | |
""" | |
array = list() | |
while number != 0: | |
# greatest power of two | |
baseTwo = int(math.log(number,2)) | |
array.append(baseTwo) | |
# remove the greatest power of 2 | |
number = number - 2**baseTwo | |
array.reverse() | |
return array |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment