Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

249
Vistas
How many objects can I construct with given number of items

I have a number of ingredients and must know the maximum number of sandwiches (Big and small sandwiches) I can make with it. If ingredients remain, the result must be false.

  • Big sandwich : 4 slices of tomatoes and 2 slices of cheese
  • Small sandwich : 2 slices of tomatoes and 1 of cheese

My code works if all ingredients can be used with only small sandwiches or maximum one big sandwich. It also returns false if ingredients are remaining. However, it returns false if we need more than one big sandwich in order to use all ingredients.

var result = []
var ispossible = function(tomatoes, cheese) {

  /* we need in any case an even number of tomatoes slices */
  if (tomatoes % 2 === 0) {
    /* the easiest way is making as many small sandwiches as possible. The minimum number of big sandwiches is the number of cheese slices remaining when we devise the total number by 2  */
    var bigSandwich = cheese % 2
    var smallSandwichTomatoes = (tomatoes - 4 * bigSandwich) / 2
    var smallSandwich = cheese - bigSandwich
    console.log("we need" + smallSandwich + "small sandwiches and " + bigSandwich + "big sandwiches")
  } else {
    console.log("false, all the ingredients cannot be used")
  }
}

ispossible(10, 3)

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Assuming that this solution helps other readers with enhancing their rudimentary algorithm, programming skills:

const bigSmallSandwiches = (t = 10, c = 5) => (
  t % 2 !== 0 || t / c !== 2 ?
  'false, all the ingredients cannot be used' :
  'we need ' + Math.floor((t - Math.floor(t / 4) * 4) / 2).toString() + ' small sandwiches and ' + Math.floor(t / 4).toString() + ' big sandwiches'
);

Explanation

  1. If number of tomato slices is not 'even' or if number of cheese is not exactly half the number of tomato, return 'false'.
  2. Determine number of 'bigSandwiches' as tomatoes / 4
  3. Determine number of 'smallSandwiches' using remaining slices of tomatoes

Code-snippet:

const bigSmallSandwiches = (t = 10, c = 5) => (
  t % 2 !== 0 || t / c !== 2 ?
  'false, all the ingredients cannot be used' :
  'we need ' + Math.floor((t - Math.floor(t / 4) * 4) / 2).toString() + ' small sandwiches and ' + Math.floor(t / 4).toString() + ' big sandwiches'
);

[
  [20, 10],
  [8, 4],
  [4, 3],
  [14, 7]
].forEach(x => console.log('tomatoes: ' + x[0] + '\tcheese slices ' + x[1] + '\n' + bigSmallSandwiches(x[0], x[1])));

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda