Last active
November 21, 2022 19:41
-
-
Save Spebby/248d4f4c177c6fb433741caed1194c05 to your computer and use it in GitHub Desktop.
Weather Analysis
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
#!/usr/bin/env python3 | |
""" | |
Analyses weather data from a file Santa Cruz Beach Boardwalk's weather station. | |
Data is collected once every 5 minutes. | |
assumed: | |
- Atleast 1 data point | |
- Data is formated | |
""" | |
import sys | |
dataPoints = [] | |
mean = 0 | |
maxValue = ["", "", sys.float_info.min] | |
minValue = ["", "", sys.float_info.max] | |
for line in sys.stdin: | |
dataLines = line.split() | |
value = float(dataLines[2]) | |
if value > maxValue[2]: | |
maxValue = [dataLines[0], dataLines[1], value] | |
elif value < minValue[2]: | |
minValue = [dataLines[0], dataLines[1], value] | |
dataPoints.append(value) | |
mean += value | |
if maxValue[2] == sys.float_info.min: | |
maxValue = minValue | |
elif minValue[2] == sys.float_info.max: | |
minValue = maxValue | |
dataPoints.sort() | |
mean = mean / len(dataPoints) | |
mid = len(dataPoints) // 2 | |
median = (dataPoints[mid] + dataPoints[~mid]) / 2 | |
mode = minValue[2] | |
modeInstances = 1 | |
currentNumber = -1000 | |
currentCount = 0 | |
for point in dataPoints: | |
if point != currentNumber: | |
# skips itterating through the end | |
if currentNumber == maxValue[2]: | |
currentCount = len(dataPoints) - dataPoints.index(point) | |
if currentCount > modeInstances: | |
mode = currentNumber | |
modeInstances = currentCount | |
break | |
if currentCount > modeInstances: | |
modeInstances = currentCount | |
mode = currentNumber | |
currentNumber = point | |
currentCount = 0 | |
currentCount += 1 | |
print(f"Count: {len(dataPoints)}") | |
print(f"Max: {int(maxValue[2])} @ {maxValue[0]} {maxValue[1]}") | |
print(f"Min: {int(minValue[2])} @ {minValue[0]} {minValue[1]}") | |
print(f"Mean: {(f'{(mean):.4f}').rstrip('0').rstrip('.')}") | |
print(f"Median: {(f'{(median):.4f}').rstrip('0').rstrip('.')}") | |
print(f"Mode: {int(mode)} ({modeInstances} instances)") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment