Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # For X you are giving the input for 2**X
- # For solution, you provide your solution
- # and the algorithim is suppose to decide if its correct
- X = int(input('enter your exponent for X: '))
- solution = int(input('enter your solution for 2**X: '))
- # Converting solution into binary
- binary = "{0:b}".format(int(solution))
- # Powers of 2 should only have ONE 1-bit and the rest 0-bits.
- # Using a slice to make sure that all bits are 0 after the first Index.
- zero_bits = binary[1:]
- if zero_bits.count(str('1')) != 0:
- print(zero_bits.count(str('1')) == 0)
- # returns false if solution is not a power of 2
- quit()
- # Below the statement checks the length of 0-bits comparing to the value X
- # if both equal then it returns True.
- if len(zero_bits) == X:
- print(len(zero_bits) == X)
- # Returns True if solution is correct
- else:
- print(len(zero_bits) == X)
- # Returns False if solution is wrong
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement