Created
September 1, 2013 14:32
-
-
Save azakordonets/6404798 to your computer and use it in GitHub Desktop.
I faced with one task while learning python where i had to work with bytes and get total number of differences between this two numbers
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 Denary2Binary(list): | |
'''convert denary integer n to binary string bStr''' | |
result = [] | |
bStr = '' | |
for n in list: | |
print "n is %s" %n | |
if n < 0: raise ValueError, "must be a positive integer" | |
if n == 0: return '0' | |
while n > 0: | |
bStr = str(n % 2) + bStr | |
n = n >> 1 | |
print "bStr is %s" %bStr | |
result.append(bStr) | |
bStr = '' | |
print result | |
return result[0],result[1] | |
def checkio(data): | |
a,b = Denary2Binary(data) | |
difference = abs(len(a)-len(b)) | |
return a,b | |
#These "asserts" using only for self-checking and not necessary for auto-testing | |
if __name__ == '__main__': | |
assert checkio([117, 17]) == 3, "First example" | |
assert checkio([1, 2]) == 2, "Second example" | |
assert checkio([16, 15]) == 5, "Third example" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment