From 387fc8ecf892d80f08b3066a9e6a51a10baa0fba Mon Sep 17 00:00:00 2001 From: Zakaria Chelhaoui Date: Sat, 24 Feb 2024 20:21:34 +0100 Subject: [PATCH 1/2] Create main.py First commit --- submissions/chelzakaria/main.py | 34 +++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 submissions/chelzakaria/main.py diff --git a/submissions/chelzakaria/main.py b/submissions/chelzakaria/main.py new file mode 100644 index 00000000..ac2a9c62 --- /dev/null +++ b/submissions/chelzakaria/main.py @@ -0,0 +1,34 @@ +import polars as pl + + +def fun(): + df = pl.scan_csv("input.txt", new_columns=["City", "Product", "Price"]) + + cheapest_city = ( + df.group_by("City") + .agg(pl.sum("Price").alias("Total_Price")) + .sort("Total_Price") + .select(["City", "Total_Price"]) + .collect(streaming=True) + .head(1) + ) + + cheapest_city_df = df.filter(pl.col("City") == cheapest_city["City"][0]) + result = ( + cheapest_city_df.group_by("Product") + .agg(pl.min("Price").alias("Min_Price")) + .sort("Min_Price") + .select(["Product", "Min_Price"]) + .limit(5) + .collect(streaming=True) + ) + + cheapest_city.drop_in_place("Total_Price") + with open("output.txt", "w") as f: + cheapest_city.write_csv(f, include_header=False, separator=" ") + result.write_csv(f, include_header=False, separator=" ") + + + +if __name__ == "__main__": + fun() From 09457ff103d5a02a6b1737715a5f052a5963eb4b Mon Sep 17 00:00:00 2001 From: Zakaria Chelhaoui Date: Sat, 24 Feb 2024 20:41:07 +0100 Subject: [PATCH 2/2] Update main.py Fix output structure --- submissions/chelzakaria/main.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/submissions/chelzakaria/main.py b/submissions/chelzakaria/main.py index ac2a9c62..861021d2 100644 --- a/submissions/chelzakaria/main.py +++ b/submissions/chelzakaria/main.py @@ -17,17 +17,15 @@ def fun(): result = ( cheapest_city_df.group_by("Product") .agg(pl.min("Price").alias("Min_Price")) - .sort("Min_Price") + .sort(["Min_Price", "Product"]) .select(["Product", "Min_Price"]) .limit(5) .collect(streaming=True) ) - cheapest_city.drop_in_place("Total_Price") with open("output.txt", "w") as f: - cheapest_city.write_csv(f, include_header=False, separator=" ") - result.write_csv(f, include_header=False, separator=" ") - + cheapest_city.write_csv(f, has_header=False, separator=" ") + result.write_csv(f, has_header=False, separator=" ") if __name__ == "__main__":