Last active
November 21, 2022 19:41
-
-
Save Spebby/601c7d3000ef28218ae2f39557e81cef 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. | |
This assignment required the exclusive use of While Loops | |
assumed: | |
- Atleast 1 data point | |
- Data is formated | |
""" | |
import sys | |
class DataPoint: | |
def __init__(self, date, time, data): | |
self.date = date | |
self.time = time | |
self.data = float(data) | |
def __repr__(self): | |
return f"Date: {self.date}, Time: {self.time}, Data: {self.data}" | |
dataPoints = [] | |
mean = 0 | |
maxValue = DataPoint("", "", sys.float_info.min) | |
minValue = DataPoint("", "", sys.float_info.max) | |
inData = iter(sys.stdin) | |
while True: | |
try: | |
line = next(inData) | |
dataLines = line.split() | |
value = float(dataLines[2]) | |
if value > maxValue.data: | |
maxValue = DataPoint(dataLines[0], dataLines[1], value) | |
elif value < minValue.data: | |
minValue = DataPoint(dataLines[0], dataLines[1], value) | |
dataPoints.append(value) | |
mean += value | |
except StopIteration: | |
if maxValue.data == sys.float_info.min: | |
maxValue = minValue | |
elif minValue.data == sys.float_info.max: | |
minValue = maxValue | |
break | |
dataPoints.sort() | |
mean = mean / len(dataPoints) | |
mid = len(dataPoints) // 2 | |
median = (dataPoints[mid] + dataPoints[~mid]) / 2 | |
median = int(median) if median.is_integer() else median | |
mode = minValue.data | |
modeInstances = 1 | |
currentNumber = -1000 | |
currentCount = 0 | |
iteration = iter(dataPoints) | |
while True: | |
try: | |
point = next(iteration) | |
if point != currentNumber: | |
# skips itterating through the end | |
if currentNumber == maxValue.data: | |
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 | |
except StopIteration: | |
break | |
print(f"Count: {len(dataPoints)}") | |
print(f"Max: {int(maxValue.data)} @ {maxValue.date} {maxValue.time}") | |
print(f"Min: {int(minValue.data)} @ {minValue.date} {minValue.time}") | |
print(f"Mean: {(f'{(mean):.4f}').rstrip('0').rstrip('.')}") | |
print(f"Median: {median}") | |
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