Created
May 15, 2016 12:08
-
-
Save rsrini7/656a60464d66524583a161beb1ddd2aa 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
| # you can write to stdout for debugging purposes, e.g. | |
| # print "this is a debug message" | |
| def solution(inputNum): | |
| # write your code in Python 2.7 | |
| powOfTwoVal = 1 | |
| powOfTwoCnt = 0 | |
| prevPowOfTwoCnt = 0 | |
| binaryZeroCnt = 0 | |
| while inputNum != 0: | |
| while powOfTwoVal * 2 <= inputNum: | |
| powOfTwoVal = powOfTwoVal * 2 | |
| powOfTwoCnt = powOfTwoCnt + 1 | |
| if prevPowOfTwoCnt - powOfTwoCnt - 1 > binaryZeroCnt: | |
| binaryZeroCnt = prevPowOfTwoCnt - powOfTwoCnt - 1 | |
| prevPowOfTwoCnt = powOfTwoCnt | |
| inputNum = inputNum - powOfTwoVal | |
| powOfTwoVal = 1 | |
| powOfTwoCnt = 0 | |
| return binaryZeroCnt | |
| print solution(1041) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment