import re
# Check if a string contains only '0' and '1'
def is_binary(s: str) -> bool:
binary = re.compile(r'^[01]+$')
return bool(binary.match(s))
"""
In decimal: reading "123" means
remainder = remainder * 10 + digit
In binary: reading "1011" means
remainder = remainder * 2 + bit
"""
"""
0 (0*2 + 0) % 7 0
0 (0*2 + 0) % 7 0
0 (0*2 + 0) % 7 0
1 (0*2 + 1) % 7 1
1 (1*2 + 1) % 7 3
1 (3*2 + 1) % 7 0
0 (0*2 + 0) % 7 0
0 (0*2 + 0) % 7 0
"""
# Compute binary string modulo 7 without overflow
def divisible_by_7(s: str) -> bool:
remainder = 0
for c in s:
remainder = (remainder * 2 + (ord(c) - ord('0'))) % 7
return remainder == 0
def main():
s = "00011100" # 28
if not is_binary(s):
print("Not a binary string")
return
if divisible_by_7(s):
print("Divisible by 7")
else:
print("Not divisible by 7")
if __name__ == "__main__":
main()
"""
run:
Divisible by 7
"""