Created
November 17, 2018 15:28
-
-
Save manvillej/3b13a82f1e7420e490e41041bd50c619 to your computer and use it in GitHub Desktop.
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.
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 getArrayOfMultiplesOfTwos(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. | |
>>> getArrayOfMultiplesOfTwos(9) | |
[0, 3] | |
""" | |
array = list() | |
while number != 0: | |
baseTwo = int(math.log(number,2)) | |
array.append(baseTwo) | |
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