Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Algorithm:
- Or & and all bit strings together.
- Where the digits do not match, there should be a '*' in the final result,
- Otherwise, the corresponding digit in the result should be the same digit.
- If 2 ^ the number of differences > than the number of bit-strings inputted,
- there are no results, otherwise output the found result.
- '''
- def main(index):
- # Input bit-strings, ignore first one, ensure that all are distinct
- input = list(set(raw_input(str(index) + ". ").split(", ")[1:]))
- if(len(input) == 0): return
- # End results for operations
- super_and, super_or = (['1' for i in range(len(input[0]))],
- ['0' for i in range(len(input[0]))])
- # The count of the number of differences
- odd_count = 0
- # Iterate over digits
- for i in range(len(input[0])):
- # Iterate over inputs
- for i2 in range(len(input)):
- # And it to the rolling and
- super_and[i] = str(int(super_and[i]) & int(input[i2][i]))
- # Or it to the rolling or
- super_or[i] = str(int(super_or[i]) | int(input[i2][i]))
- # If there is a diff. it is '*'
- if not super_or[i] == super_and[i]: super_and[i] = "*"; odd_count += 1
- # No combination spans output
- if 2**odd_count > len(input): print "NONE"
- # Print result
- else: print str(index) + ". " + "".join(super_and)
- if __name__ == "__main__":
- for i in range(5):
- failed = False
- while True:
- try:
- main(i + 1)
- break
- except ValueError:
- print "Invalid Input"
Add Comment
Please, Sign In to add comment