lint and analysis

This commit is contained in:
2023-12-15 22:46:01 +01:00
parent 55799cbd22
commit b3b1e40b4e
12 changed files with 441 additions and 181 deletions

View File

@@ -7,12 +7,13 @@ def hash_algorithm(step):
current_value %= 256 # Modulo 256
return current_value
def process_file(file_path):
"""Process each step in the file and return the sum of HASH values."""
try:
with open(file_path, 'r') as file:
data = file.read().replace('\n', '') # Remove newline characters
steps = data.split(',') # Split steps by comma
with open(file_path, "r") as file:
data = file.read().replace("\n", "") # Remove newline characters
steps = data.split(",") # Split steps by comma
print(f"Processing {len(steps)} steps from {file_path}")
total_sum = 0
@@ -27,12 +28,14 @@ def process_file(file_path):
except Exception as e:
print(f"Unexpected error occurred: {e}")
def test_algorithm():
"""Test the algorithm with the test file."""
test_result = process_file("../test.txt")
print(f"Test Result: {test_result}")
assert test_result == 1320, "Test failed. Expected result is 1320."
def main():
"""Main function to run the HASH algorithm on the input file."""
try:
@@ -48,5 +51,6 @@ def main():
except Exception as e:
print(f"An error occurred in main: {e}")
if __name__ == "__main__":
main()

View File

@@ -1,6 +1,7 @@
import re
from collections import defaultdict
def hash_label(label):
"""Compute hash value for a given label."""
value = 0
@@ -8,11 +9,12 @@ def hash_label(label):
value = (value + ord(char)) * 17 % 256
return value
def process_file(file_path):
"""Process the file and return the total focusing power."""
try:
with open(file_path, 'r') as file:
line = file.read().replace('\n', '')
with open(file_path, "r") as file:
line = file.read().replace("\n", "")
boxes = defaultdict(list)
focal_lengths = {}
@@ -44,11 +46,15 @@ def process_file(file_path):
except Exception as e:
print(f"Unexpected error occurred: {e}")
def test_algorithm():
"""Test the algorithm with the test file."""
test_result = process_file("../test.txt")
print(f"Test Result: {test_result}")
assert test_result == 145, f"Test failed. Expected result is 145, got {test_result}."
assert (
test_result == 145
), f"Test failed. Expected result is 145, got {test_result}."
def main():
"""Main function to run the algorithm on the input file."""
@@ -65,5 +71,6 @@ def main():
except Exception as e:
print(f"An error occurred in main: {e}")
if __name__ == "__main__":
main()