Skip to content

Instantly share code, notes, and snippets.

@VitorDiToro
Created September 19, 2016 18:38
Show Gist options
  • Save VitorDiToro/4dfd2ae53e89bc9591ae51563edb2989 to your computer and use it in GitHub Desktop.
Save VitorDiToro/4dfd2ae53e89bc9591ae51563edb2989 to your computer and use it in GitHub Desktop.
Mochila_Guloso.py
def guloso(produto,preco,peso,mochila_capacidade):
naMochila = []
lucro = 0
while True:
maiorPreco = 0
index = None
for i in range(len(produto)):
if (preco[i] > maiorPreco) & (produto[i] not in naMochila) & (mochila_capacidade >= peso[i]):
maiorPreco = preco[i]
index = i
if index != None:
naMochila.append(produto[index])
mochila_capacidade -= peso[index]
lucro += preco[index]
print("Objeto", produto[index])
print("Preco", preco[index])
else:
print("Lucro -> Guloso =", lucro)
break
def main():
produto = [1,2,3,4,5]
preco = [50,25,47,10,35]
peso = [5,3,2,9,7]
mochila_capacidade = 13
guloso(produto,preco,peso,mochila_capacidade)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment