From 13802fcca154803e1cf619ddfb7f7975d0ca8f59 Mon Sep 17 00:00:00 2001 From: DanishSheikh1999 <43725095+DanishSheikh1999@users.noreply.github.com> Date: Tue, 22 Oct 2019 14:25:01 +0530 Subject: [PATCH] Create greedy.py (#1359) * Create greedy.py * Update greedy.py * Add a doctest and format with black * Update build_directory_md.py --- other/greedy.py | 63 +++++++++++++++++++++++++++++++++++ scripts/build_directory_md.py | 6 ++-- 2 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 other/greedy.py diff --git a/other/greedy.py b/other/greedy.py new file mode 100644 index 000000000..d1bc15630 --- /dev/null +++ b/other/greedy.py @@ -0,0 +1,63 @@ +class things: + def __init__(self, n, v, w): + self.name = n + self.value = v + self.weight = w + + def __repr__(self): + return f"{self.__class__.__name__}({self.name}, {self.value}, {self.weight})" + + def get_value(self): + return self.value + + def get_name(self): + return self.name + + def get_weight(self): + return self.weight + + def value_Weight(self): + return self.value / self.weight + + +def build_menu(name, value, weight): + menu = [] + for i in range(len(value)): + menu.append(things(name[i], value[i], weight[i])) + return menu + + +def greedy(item, maxCost, keyFunc): + itemsCopy = sorted(item, key=keyFunc, reverse=True) + result = [] + totalValue, total_cost = 0.0, 0.0 + for i in range(len(itemsCopy)): + if (total_cost + itemsCopy[i].get_weight()) <= maxCost: + result.append(itemsCopy[i]) + total_cost += itemsCopy[i].get_weight() + totalValue += itemsCopy[i].get_value() + return (result, totalValue) + + +def test_greedy(): + """ + >>> food = ["Burger", "Pizza", "Coca Cola", "Rice", + ... "Sambhar", "Chicken", "Fries", "Milk"] + >>> value = [80, 100, 60, 70, 50, 110, 90, 60] + >>> weight = [40, 60, 40, 70, 100, 85, 55, 70] + >>> foods = build_menu(food, value, weight) + >>> foods # doctest: +NORMALIZE_WHITESPACE + [things(Burger, 80, 40), things(Pizza, 100, 60), things(Coca Cola, 60, 40), + things(Rice, 70, 70), things(Sambhar, 50, 100), things(Chicken, 110, 85), + things(Fries, 90, 55), things(Milk, 60, 70)] + >>> greedy(foods, 500, things.get_value) # doctest: +NORMALIZE_WHITESPACE + ([things(Chicken, 110, 85), things(Pizza, 100, 60), things(Fries, 90, 55), + things(Burger, 80, 40), things(Rice, 70, 70), things(Coca Cola, 60, 40), + things(Milk, 60, 70)], 570.0) + """ + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/scripts/build_directory_md.py b/scripts/build_directory_md.py index cae38c13d..2ebd445b3 100755 --- a/scripts/build_directory_md.py +++ b/scripts/build_directory_md.py @@ -25,7 +25,7 @@ def print_path(old_path: str, new_path: str) -> str: for i, new_part in enumerate(new_path.split(os.sep)): if i + 1 > len(old_parts) or old_parts[i] != new_part: if new_part: - print(f"{md_prefix(i-1)} {new_part.replace('_', ' ').title()}") + print(f"{md_prefix(i)} {new_part.replace('_', ' ').title()}") return new_path @@ -36,9 +36,7 @@ def print_directory_md(top_dir: str = ".") -> None: if filepath != old_path: old_path = print_path(old_path, filepath) indent = (filepath.count(os.sep) + 1) if filepath else 0 - url = "/".join((URL_BASE, filepath.split(os.sep)[1], filename)).replace( - " ", "%20" - ) + url = "/".join((URL_BASE, filepath, filename)).replace(" ", "%20") filename = os.path.splitext(filename.replace("_", " "))[0] print(f"{md_prefix(indent)} [{filename}]({url})")