daily log 11.17.20

2 minute read

CTCI

Third attempt (from the book):

# 1. Create row and column arrays to store which rows and columns have zeros
# 2. loop through rows - if row has zero, add it to row array
# 3. loop through columns - if column has zero, add it to column array
# 4. loop through the rows again, checking to see if that row is "ZEROED", if so, replace subsequent values
# 5. loop through the columns again, checkint to see if that column is "ZEROED", if so, replace subsequent values

def zeroMatrix(matrix):
    row = [False] * len(matrix)
    col = [False] * len(matrix[0])
    
    for i in range(len(matrix)):
        for j in range(len(matrix[0])):
            if matrix[i][j] == 0:
                row[i] = True
                row[j] = True
    
    for i in range(len(matrix)):
        for j in range(len(matrix[0])):
            if row[i] == True or row[j] == True:
                matrix[i][j] = 0
                
    return matrix
    
    
# test = [[0,1,1],[0,1,0],[1,1,1],[2,3,2]]
test = [[0,1,1],[0,1,3],[1,1,1],[2,3,2]]
zeroMatrix(test)

Second attempt:

As a human computer, I will:

  1. make a copy of the matrix MATRIX_COPY
  2. loop through the ORIGINAL_MATRIX ROW by ROW
  3. I will check if 0 is in the ROW – the second I get to a 0 I can stop looping and set ZERO_PRESENT: True
  4. If ZERO_PRESENT, loop through each item in that ROW of MATRIX_COPY and turn it into a zero
  5. (Do this for all rows)
  6. Now, do the same thing but with COLUMNS

Working for Rows


def zeroMatrix(matrix):
    matrix_copy = matrix
    
    for i,row in enumerate(matrix):
        for num in row:
            if num == 0:
                matrix_copy[i] = [0] * len(row)
                break
            else:
                matrix_copy[i] = row
                
    return matrix_copy
    
test = [[0,1,1],[0,1,0],[1,1,1]]
zeroMatrix(test)

First attempt

As a human computer, I will:

  1. loop through the matrix ROW by ROW
  2. I will check if 0 is in the ROW – the second I get to a 0 I can stop looping and set ZERO_PRESENT: True
  3. If ZERO_PRESENT, update MATRIX_COPY
  4. turn all numbers in the ROW to 0
  5. but save this to a new matrix because we still need the old one for when we do the same thing for columns

UWD (UEL, LS)

app.post('/ingredients-multiple', async (req, res) => {

  const { name } = req.body
  const ingredients = name.split('&')
  

  function baz(results) {
    console.log('testing')
    const intersection = results.reduce((a, b) => a.filter(c => b.includes(c)));
    const merged_results = results.flat(1)
    // res.send([merged_results, merged_results.length])
    res.send([intersection, intersection.length])
  }

  async function get_recipes_using_ingredient(ingredient){
    let all_recipes_with_ingredient = await connection.collection("test_recipes_ingredients_only").find({ "name": { "$regex": ingredient, "$options": "i" } }).toArray()
    return all_recipes_with_ingredient.map(recipe => recipe.recipe_name )
  }

  async function foo(ingredients) {
    const results = [];
    for (const ingredient of ingredients) {
      
      // Good: all asynchronous operations are immediately started.
      
      results.push(get_recipes_using_ingredient(ingredient));
    }
    // Now that all the asynchronous operations are running, here we wait until they all complete.
    return baz(await Promise.all(results));
  }

  foo(ingredients)
  // var arr = [1, 2, 3, 4, 5];

  // var recipes_with_all_ingredients= await Promise.all(arr.map(async (item) => {
  //     await callAsynchronousOperation(item);
  //     return item + 1;
  // }));
  // recipe_ids = []
  // const recipes_with_all_ingredients = ingredients.map((ingredient) => {
  //   let name = ingredient;
  //   let results = await connection.collection("test_recipes_ingredients_only").find({ "name": { "$regex": name, "$options": "i" } }).toArray()
  //   // let just_ids = await results.map(result => result.recipe_id )
  //   let just_ids = await results.map(result => result.recipe_name )
  //   recipe_ids = recipe_ids + just_ids
  // })
  
  // res.send([recipe_ids, recipe_ids.length])
  
  console.log('getting to the home page!')
})