Last active
December 31, 2021 05:43
-
-
Save audrow/8434a90695b00ea8ec9a9be46895ccf2 to your computer and use it in GitHub Desktop.
Recipes with a common ingredient #teaching
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Test with deno test | |
import { getRecipesWithIngredients, isIngredientsInRecipe, recipes } from "./index.ts" | |
import { assert, assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts"; | |
Deno.test('getRecipesWithNumbers', () => { | |
{ | |
const results = getRecipesWithIngredients(recipes, [1, 2, 3, 4]); | |
assert(results.length === 0); | |
} | |
{ | |
const results = getRecipesWithIngredients(recipes, [1, 2, 3]); | |
assert(results.length === 1); | |
assertEquals(results, ["pizza"]) | |
} | |
{ | |
const results = getRecipesWithIngredients(recipes, [1, 2]); | |
assert(results.length === 2); | |
assertEquals(results, ["pizza", "bread"]) | |
} | |
}) | |
Deno.test('ingredients in a recipe', () => { | |
assert(isIngredientsInRecipe([], [1, 2, 3])) | |
assert(isIngredientsInRecipe([1], [1, 2, 3])) | |
assert(isIngredientsInRecipe([1, 2], [1, 2, 3])) | |
assert(isIngredientsInRecipe([1, 2, 3], [1, 2, 3])) | |
assert(!isIngredientsInRecipe([1], [3, 4, 5])) | |
assert(!isIngredientsInRecipe([1, 2], [3, 4, 5])) | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Run with deno run index.ts | |
type Recipes = { [key: string]: number[] }; | |
export const recipes: Recipes = { | |
'pizza': [1, 2, 3], | |
'bread': [1, 2, 4], | |
'cake': [4, 5, 6], | |
'pastry': [3, 4], | |
'salad': [7, 8, 9], | |
'worse salad': [7, 8], | |
'worst salad': [7], | |
}; | |
export function getRecipesWithIngredients( | |
recipes: Recipes, | |
desiredIngredients: number[], | |
): string[] { | |
const recipesWithDesiredIngredients: string[] = [] | |
for (const [recipeName, recipeIngredients] of Object.entries(recipes)) { | |
if (isIngredientsInRecipe(desiredIngredients, recipeIngredients)) { | |
recipesWithDesiredIngredients.push(recipeName) | |
} | |
} | |
return recipesWithDesiredIngredients | |
} | |
export function isIngredientsInRecipe(desiredIngredients: number[], recipeIngredients: number[]) { | |
for (const desiredIngredient of desiredIngredients) { | |
if(! (recipeIngredients.includes(desiredIngredient)) ) { | |
return false | |
} | |
} | |
return true | |
} | |
console.log(getRecipesWithIngredients(recipes, [2, 4])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment