Guest User

Untitled

a guest
Jun 20th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.60 KB | None | 0 0
  1. '''
  2. Algorithm:
  3.  
  4. Or & and all bit strings together.
  5. Where the digits do not match, there should be a '*' in the final result,
  6. Otherwise, the corresponding digit in the result should be the same digit.
  7.  
  8. If 2 ^ the number of differences > than the number of bit-strings inputted,
  9. there are no results, otherwise output the found result.
  10. '''
  11.  
  12. def main(index):
  13.     # Input bit-strings, ignore first one, ensure that all are distinct
  14.     input = list(set(raw_input(str(index) + ". ").split(", ")[1:]))
  15.     if(len(input) == 0): return
  16.     # End results for operations
  17.     super_and, super_or = (['1' for i in range(len(input[0]))],
  18.                            ['0' for i in range(len(input[0]))])
  19.     # The count of the number of differences
  20.     odd_count = 0
  21.     # Iterate over digits
  22.     for i in range(len(input[0])):
  23.         # Iterate over inputs
  24.         for i2 in range(len(input)):
  25.             # And it to the rolling and
  26.             super_and[i] = str(int(super_and[i]) & int(input[i2][i]))
  27.             # Or it to the rolling or
  28.             super_or[i] = str(int(super_or[i]) | int(input[i2][i]))
  29.         # If there is a diff. it is '*'
  30.         if not super_or[i] == super_and[i]: super_and[i] = "*"; odd_count += 1
  31.     # No combination spans output
  32.     if 2**odd_count > len(input): print "NONE"
  33.     # Print result
  34.     else: print str(index) + ". " + "".join(super_and)
  35.  
  36. if __name__ == "__main__":
  37.     for i in range(5):
  38.         failed = False
  39.         while True:
  40.             try:
  41.                 main(i + 1)
  42.                 break
  43.             except ValueError:
  44.                 print "Invalid Input"
Add Comment
Please, Sign In to add comment