Solutions: Assemble furniture

Run Settings
LanguageJavaScript
Language Version
Run Command
// Here's the explanation of the challenge -- when you're done reading it, // click on `main.js` above to get started with the challenge! // After a trip down to the furniture store, you've come back with... well, // a random collection of pieces of wood and bits of metal, it seems. // Apparently you have to assemble it yourself. Let's use code to see if we // can make a dining space out of this. // ## Description // In this challenge, you will write a `maximumGuests` function that receives // two arguments. The first argument is the number of wooden boards, and the // second argument is the number of wooden legs. The arguments represent the // pieces we can use to assemble dining tables and chairs. // For example, if we have four wooden boards and ten wooden legs, the // function could be invoked like: // maximumGuests(4, 10) // Your function should return the number of people that will be able to sit // around the tables, once the furniture is assembled in the way that allows // the most people to sit in it. // For someone to be able to sit around a table, there has to be a **chair** // for them to sit on, and a **space** in the table for them to sit in front // of. Here's what you need to know about the furniture: // To assemble a **table**, you will need **four wooden boards** and **four // wooden legs**. Each table provides spaces to seat **four people** around // them, provided there's a chair. // To assemble a **chair**, you will need **one wooden board** and **three // wooden legs**. Each chair can sit **one person**, provided there's space // for them in a table. // ## Example // For example, if you have eight wooden boards and twelve wooden legs, you could // assemble four chairs (with four boards to spare) but then no one would be able // to sit around the table, because there wouldn't be any tables. You could also // assemble two tables (with four legs to spare) but then no one would be able // to sit around those tables either, because there wouldn't be any chairs. // But with the same boards and legs, you can instead assemble a table and two // chairs (with two boards and two legs to spare) and sit two people around a // table. This is the most people you can sit around a table with that number // of boards and legs, and therefore your function should return `2`.
/* Check out README.md for instructions! */ // Prodedural-style solution function proceduralMaximumGuests(boards, legs) { let spaces = 0; let guests = 0; while (true) { // If we're out of spaces around the table // to seat people around, let's build a table. if (!spaces) { // If we can't build a table, we're done. if (boards < 4 || legs < 4) { break; } // Building a table uses four boards // and four legs, and provides four spaces // to seat people around. boards -= 4 legs -= 4 spaces += 4 } // If we can't build a chair, we're done. if (boards < 1 || legs < 3) { break; } // Building a chair uses one board, three // legs, and one space around the table. // It allows us to seat one guest. boards -= 1 legs -= 3 spaces -= 1 guests += 1 } // Return the number of guests seated. return guests } // Generator-style solution function generatorMaximumGuests(boards, legs) { // Generate a sequence of increasing amounts of // boards, legs, and the guests that you can seat // if you meet those amounts. function* piecesToGuests() { let boards = 0 let legs = 0 let guests = 0 while (true) { // Account for needing to build a table. boards += 4 legs += 4 // We can build four chairs around a table. for (i=0; i<4; i++) { // The materials to build one more chair // allow us to seat one more guest. yield [boards += 1, legs += 3, guests += 1] } } } // Iterate through the generator. for ( const [neededBoards, neededLegs, guests] of piecesToGuests() ) { // If we don't have enough boards or legs, we cannot // seat these many guests -- but we can seat one less. if (boards < neededBoards || legs < neededLegs) { return guests - 1 } } } for (const solution of [ proceduralMaximumGuests, generatorMaximumGuests ]) { /* Test runner (do not modify this line!) */ const expect = require("./expect")(solution) /* Test cases (remember to add more!) */ // no pieces, no furniture, no seats! expect(0, 0, 0) // with six boards and ten legs we can build // a table and two chairs, seating two people expect(2, 6, 10) // with one less, we can only seat one person expect(1, 6, 9) // accounts for needing a second table for the // fifth guest expect(5, 13, 23) // your test case here! // expect(expectedGuests, boards, legs) }
/* Test runner (do not modify this!) */ module.exports = (run) => function expect(expected, ...inputs) { const stringify = (value) => { const serialised = JSON.stringify(value, undefined, 2) || "undefined" const lines = serialised.split('\n') for (i = 1; i < lines.length; i++) { lines[i] = "\t" + lines[i] } return lines.join('\n') } const serialisedInputs = inputs .map(stringify) .join(", ") const actual = run(...inputs) if (expected !== actual) { console.log( ` ❌ ${run.name}(${serialisedInputs})\n\n` + `\texpected: ${stringify(expected)}\n` + `\tbut instead got: ${stringify(actual)}\n\n` ) } else { console.log( ` ✅ ${run.name}(${serialisedInputs}) === ${stringify(actual)}\n` ) } }
Editor Settings
Theme
Key bindings
Full width
Lines