Advertisement
gruntfutuk

is_binary_and_spaces_only

Mar 30th, 2019
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.41 KB | None | 0 0
  1. def isbin(num:str) ->bool:
  2.     'check string contains only binary digits and spaces'
  3.     nums = num.split()
  4.     if not nums:  # treat empty as false
  5.         return False
  6.     for num in nums:
  7.         if not set(num) <= {'0', '1'}:
  8.             return False
  9.     return True
  10.  
  11. tests = ['', '0', '1', '101 111', '0x101 111 10001', '00001 000001 11111111']
  12. for test in tests:
  13.     print(f'{test} gives {isbin(test)}')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement