Skip to content

Create greedy.py #1359

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions other/greedy.py
Original file line number Diff line number Diff line change
@@ -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()
6 changes: 2 additions & 4 deletions scripts/build_directory_md.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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})")

Expand Down