3 (jupiter)
def mapper(line):
parts = line.strip().split(',')
return parts if len(parts) == 2 else None
def reducer(conditions):
return max(set(conditions), key=conditions.count)
def main(file):
with open(file, encoding='utf-8') as f:
data = [mapper(line) for line in f if mapper(line)]
grouped = {}
for date, cond in data:
grouped.setdefault(date, []).append(cond)
for date in grouped:
print(f"Weather on {date}: {reducer(grouped[date])}")
# Example usage
if __name__ == "__main__":
main('weather.txt')
..................................................................................
def mapper(l): return tuple(l.strip().split(','))
def reducer(c): return max(set(c), key=c.count)
def main(data):
g = {}
for d, c in map(mapper, data): g.setdefault(d, []).append(c)
for d in g: print(f"Weather on {d}: {reducer(g[d])}")
if __name__ == "__main__":
lines = [
"2023-01-01,Sunny",
"2023-01-01,Rainy",
"2023-01-01,Sunny",
"2023-01-02,Cloudy",
"2023-01-02,Cloudy",
"2023-01-02,Rainy"
]
main(lines)
Comments
Post a Comment