From 4a9c0941446de781426a4848bd966e324d56e05e Mon Sep 17 00:00:00 2001 From: "ayoub.laamim" Date: Thu, 22 Feb 2024 17:19:12 +0100 Subject: [PATCH 1/4] added my solution --- submissions/LaamimAyoub/solution.py | 62 +++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 submissions/LaamimAyoub/solution.py diff --git a/submissions/LaamimAyoub/solution.py b/submissions/LaamimAyoub/solution.py new file mode 100644 index 00000000..96d4df63 --- /dev/null +++ b/submissions/LaamimAyoub/solution.py @@ -0,0 +1,62 @@ +from collections import defaultdict +from functools import reduce + +class Solution: + input_path = "input.txt" + output_path = "output.txt" + nb_product_to_print = 5 + + def do(self): + data, total_per_city = self.load_data() + the_cheapest_city = self.find_the_cheapest_city(total_per_city) + output = self.get_output(the_cheapest_city, data) + self.output_result(output, the_cheapest_city) + + def output_result(self, output, the_cheapest_city): + output.insert(0, the_cheapest_city) + output = list(map(lambda x: f"{x[0]} {round(x[1], 2)}", output)) + txt = "\n".join(output) + with open(self.output_path, "w") as fh: + fh.write(txt) + + + def get_output(self, the_cheapest_city, data): + the_cheapest_city_name = the_cheapest_city[0] + cc_data = data[the_cheapest_city_name] + cc_data.sort(key = lambda x : x[0]) + cc_data.sort(key = lambda x:x[1]) + cc_data = cc_data[:self.nb_product_to_print] + return cc_data + + + def find_the_cheapest_city(self, total_per_city): + cheapest_city = reduce(lambda x,y : x if x[1] <= y[1] else y, total_per_city.items()) + return cheapest_city + + + def load_data(self): + with open(self.input_path, "r") as fh: + txt = fh.read() + + lines = txt.split("\n") + default_dict_product = defaultdict(lambda : 1_000_000) + data = defaultdict(lambda : default_dict_product) + total_per_city = defaultdict(lambda : 0) + + + for line in lines: + if line == "": + continue + city, product, price = line.split(',') + price = float(price) + data[city][product] = price if price < data[city][product] + total_per_city[city] += price + + return data, total_per_city + +if __name__ == "__main__": + import time + t0 = time.time() + Solution().do() + print("This took about", time.time() - t0) + \ No newline at end of file From eeb6ae8edd7c034b0e7e506659e618fb4abd406f Mon Sep 17 00:00:00 2001 From: "ayoub.laamim" Date: Thu, 22 Feb 2024 17:19:39 +0100 Subject: [PATCH 2/4] added my solution --- submissions/LaamimAyoub/solution.py | 122 ++++++++++++++-------------- 1 file changed, 61 insertions(+), 61 deletions(-) diff --git a/submissions/LaamimAyoub/solution.py b/submissions/LaamimAyoub/solution.py index 96d4df63..4141cc37 100644 --- a/submissions/LaamimAyoub/solution.py +++ b/submissions/LaamimAyoub/solution.py @@ -1,62 +1,62 @@ -from collections import defaultdict -from functools import reduce - -class Solution: - input_path = "input.txt" - output_path = "output.txt" - nb_product_to_print = 5 - - def do(self): - data, total_per_city = self.load_data() - the_cheapest_city = self.find_the_cheapest_city(total_per_city) - output = self.get_output(the_cheapest_city, data) - self.output_result(output, the_cheapest_city) - - def output_result(self, output, the_cheapest_city): - output.insert(0, the_cheapest_city) - output = list(map(lambda x: f"{x[0]} {round(x[1], 2)}", output)) - txt = "\n".join(output) - with open(self.output_path, "w") as fh: - fh.write(txt) - - - def get_output(self, the_cheapest_city, data): - the_cheapest_city_name = the_cheapest_city[0] - cc_data = data[the_cheapest_city_name] - cc_data.sort(key = lambda x : x[0]) - cc_data.sort(key = lambda x:x[1]) - cc_data = cc_data[:self.nb_product_to_print] - return cc_data - - - def find_the_cheapest_city(self, total_per_city): - cheapest_city = reduce(lambda x,y : x if x[1] <= y[1] else y, total_per_city.items()) - return cheapest_city - - - def load_data(self): - with open(self.input_path, "r") as fh: - txt = fh.read() - - lines = txt.split("\n") - default_dict_product = defaultdict(lambda : 1_000_000) - data = defaultdict(lambda : default_dict_product) - total_per_city = defaultdict(lambda : 0) - - - for line in lines: - if line == "": - continue - city, product, price = line.split(',') - price = float(price) - data[city][product] = price if price < data[city][product] - total_per_city[city] += price - - return data, total_per_city - -if __name__ == "__main__": - import time - t0 = time.time() - Solution().do() - print("This took about", time.time() - t0) +from collections import defaultdict +from functools import reduce + +class Solution: + input_path = "input.txt" + output_path = "output.txt" + nb_product_to_print = 5 + + def do(self): + data, total_per_city = self.load_data() + the_cheapest_city = self.find_the_cheapest_city(total_per_city) + output = self.get_output(the_cheapest_city, data) + self.output_result(output, the_cheapest_city) + + def output_result(self, output, the_cheapest_city): + output.insert(0, the_cheapest_city) + output = list(map(lambda x: f"{x[0]} {round(x[1], 2)}", output)) + txt = "\n".join(output) + with open(self.output_path, "w") as fh: + fh.write(txt) + + + def get_output(self, the_cheapest_city, data): + the_cheapest_city_name = the_cheapest_city[0] + cc_data = data[the_cheapest_city_name] + cc_data.sort(key = lambda x : x[0]) + cc_data.sort(key = lambda x:x[1]) + cc_data = cc_data[:self.nb_product_to_print] + return cc_data + + + def find_the_cheapest_city(self, total_per_city): + cheapest_city = reduce(lambda x,y : x if x[1] <= y[1] else y, total_per_city.items()) + return cheapest_city + + + def load_data(self): + with open(self.input_path, "r") as fh: + txt = fh.read() + + lines = txt.split("\n") + default_dict_product = defaultdict(lambda : 1_000_000) + data = defaultdict(lambda : default_dict_product) + total_per_city = defaultdict(lambda : 0) + + + for line in lines: + if line == "": + continue + city, product, price = line.split(',') + price = float(price) + data[city][product] = price if price < data[city][product] + total_per_city[city] += price + + return data, total_per_city + +if __name__ == "__main__": + import time + t0 = time.time() + Solution().do() + print("This took about", time.time() - t0) \ No newline at end of file From 9d4b621bc71a00c335f175f3c3c93479f10951a9 Mon Sep 17 00:00:00 2001 From: "ayoub.laamim" Date: Thu, 22 Feb 2024 17:52:36 +0100 Subject: [PATCH 3/4] my solution --- submissions/LaamimAyoub/solution.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/submissions/LaamimAyoub/solution.py b/submissions/LaamimAyoub/solution.py index 4141cc37..76fc99c3 100644 --- a/submissions/LaamimAyoub/solution.py +++ b/submissions/LaamimAyoub/solution.py @@ -1,5 +1,6 @@ from collections import defaultdict from functools import reduce +from copy import deepcopy class Solution: input_path = "input.txt" @@ -22,7 +23,7 @@ def output_result(self, output, the_cheapest_city): def get_output(self, the_cheapest_city, data): the_cheapest_city_name = the_cheapest_city[0] - cc_data = data[the_cheapest_city_name] + cc_data = list(dict(data[the_cheapest_city_name]).items()) cc_data.sort(key = lambda x : x[0]) cc_data.sort(key = lambda x:x[1]) cc_data = cc_data[:self.nb_product_to_print] @@ -39,8 +40,7 @@ def load_data(self): txt = fh.read() lines = txt.split("\n") - default_dict_product = defaultdict(lambda : 1_000_000) - data = defaultdict(lambda : default_dict_product) + data = defaultdict(lambda : defaultdict(lambda : 101)) total_per_city = defaultdict(lambda : 0) @@ -49,7 +49,7 @@ def load_data(self): continue city, product, price = line.split(',') price = float(price) - data[city][product] = price if price < data[city][product] + data[city][product] = price if price < data[city][product] else data[city][product] total_per_city[city] += price return data, total_per_city From 06752fbddf3f7dea4b2622923ecb0a5892d28201 Mon Sep 17 00:00:00 2001 From: "ayoub.laamim" Date: Fri, 23 Feb 2024 09:22:38 +0100 Subject: [PATCH 4/4] changeed solution --- submissions/LaamimAyoub/solution.py | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/submissions/LaamimAyoub/solution.py b/submissions/LaamimAyoub/solution.py index 76fc99c3..e7277b48 100644 --- a/submissions/LaamimAyoub/solution.py +++ b/submissions/LaamimAyoub/solution.py @@ -1,6 +1,5 @@ from collections import defaultdict from functools import reduce -from copy import deepcopy class Solution: input_path = "input.txt" @@ -35,28 +34,22 @@ def find_the_cheapest_city(self, total_per_city): return cheapest_city - def load_data(self): - with open(self.input_path, "r") as fh: - txt = fh.read() - - lines = txt.split("\n") + def load_data(self): data = defaultdict(lambda : defaultdict(lambda : 101)) total_per_city = defaultdict(lambda : 0) - - for line in lines: - if line == "": - continue - city, product, price = line.split(',') - price = float(price) - data[city][product] = price if price < data[city][product] else data[city][product] - total_per_city[city] += price + with open(self.input_path, "r") as fh: + for line in fh: + if line == "": + continue + city, product, price = line.split(',') + price = float(price) + data[city][product] = price if price < data[city][product] else data[city][product] + total_per_city[city] += price return data, total_per_city if __name__ == "__main__": - import time - t0 = time.time() Solution().do() - print("This took about", time.time() - t0) + \ No newline at end of file