Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/util/knapsack.test.ts
Views: 687
import knapsack from "@cocalc/util/knapsack";12describe("knapsack", () => {3const input = {4banana: { cost: 5, benefit: 33 },5apple: { cost: 1, benefit: 12 },6kiwi: { cost: 1, benefit: 7 },7};89it("should return the correct result for the first example", () => {10const maxCost = 7;11const expectedOutput = {12items: ["kiwi", "apple", "banana"],13cost: 7,14benefit: 52,15};16const output = knapsack(input, maxCost);17expect(output).toEqual(expectedOutput);18});1920it("should return the correct result for the second example", () => {21const maxCost = 2;22const expectedOutput = {23items: ["kiwi", "apple"],24cost: 2,25benefit: 19,26};27const output = knapsack(input, maxCost);28expect(output).toEqual(expectedOutput);29});3031it("should return the correct result for the third example", () => {32const maxCost = 7;33const expectedOutput = {34items: ["kiwi", "apple", "banana"],35cost: 7,36benefit: 52,37};38const output = knapsack(input, maxCost);39expect(output).toEqual(expectedOutput);40});4142it("should return the correct result for the fourth example", () => {43const maxCost = 20;44const expectedOutput = {45items: ["kiwi", "apple", "banana"],46cost: 7,47benefit: 52,48};49const output = knapsack(input, maxCost);50expect(output).toEqual(expectedOutput);51});5253it("should return the correct result for the fifth example", () => {54const maxCost = 6;55const expectedOutput = {56items: ["apple", "banana"],57cost: 6,58benefit: 45,59};60const output = knapsack(input, maxCost);61expect(output).toEqual(expectedOutput);62});63});646566