mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-27 15:01:08 +00:00
Create greedy.py (#1359)
* Create greedy.py * Update greedy.py * Add a doctest and format with black * Update build_directory_md.py
This commit is contained in:
parent
54830644a2
commit
13802fcca1
63
other/greedy.py
Normal file
63
other/greedy.py
Normal file
|
@ -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()
|
|
@ -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})")
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user