Skip to content

Commit

Permalink
Avoid boolean inversion & reduce dict operations
Browse files Browse the repository at this point in the history
  • Loading branch information
boidolr committed Jul 8, 2024
1 parent edfa973 commit bf75d72
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions calculateAverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ def _process_file_chunk(
break
location, measurement = line.split(b";")
measurement = float(measurement)
try:
_result = result[location]
_result = result.get(location)
if _result:
if measurement < _result[0]:
_result[0] = measurement
if measurement > _result[1]:
_result[1] = measurement
_result[2] += measurement
_result[3] += 1
except KeyError:
else:
result[location] = [
measurement,
measurement,
Expand Down Expand Up @@ -107,17 +107,16 @@ def process_file(
result = dict()
for chunk_result in chunk_results:
for location, measurements in chunk_result.items():
if location not in result:
result[location] = measurements
else:
_result = result[location]
_result = result.get(location)
if _result:
if measurements[0] < _result[0]:
_result[0] = measurements[0]
if measurements[1] > _result[1]:
_result[1] = measurements[1]
_result[2] += measurements[2]
_result[3] += measurements[3]

else:
result[location] = measurements
# Print final results
print("{", end="")
for location, measurements in sorted(result.items()):
Expand Down

0 comments on commit bf75d72

Please sign in to comment.