Created
May 23, 2024 00:15
-
-
Save tlhakhan/1e1df7e0f503d7fdea23741e98c3ccff to your computer and use it in GitHub Desktop.
Python zero counter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
def find_largest_zero_sequence(filename): | |
max_zeros = 0 | |
current_zeros = 0 | |
with open(filename, 'rb') as file: | |
while True: | |
byte = file.read(1) | |
if not byte: # End of file | |
break | |
if byte == b'\x00': | |
current_zeros += 1 | |
else: | |
if current_zeros > max_zeros: | |
max_zeros = current_zeros | |
current_zeros = 0 | |
# Check last sequence if file ends with zero bytes | |
if current_zeros > max_zeros: | |
max_zeros = current_zeros | |
return max_zeros | |
# Check if the script is given the necessary arguments | |
if len(sys.argv) != 2: | |
print("Usage: python script.py filename") | |
sys.exit(1) | |
filename = sys.argv[1] | |
largest_zeros = find_largest_zero_sequence(filename) | |
print(f'The largest contiguous zero bytes in the file is: {largest_zeros}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment