Permutation tree not making sense with the code - javascript
I watched a youtube video where this basic permutation tree was shown. If you look at this bit of code:
function recursion(input, set = [], result = []) {
if (!input.length) {
result.push([...set].join(''));
}
for (let i = 0; i < input.length; i++) {
const newArr = input.filter((n, index) => index !== i);
set.push(input[i]);
recursion(newArr, set, result);
set.pop();
}
return result.join(', ');
}
you can see that the base case (if statement) is at the top before the parameter nums is filtered. So, my whole question is how the tree and the code makes sense because for me the code would remove one too many digits from the set array. Becuase it pops an item of when returning and doesn't it return more than two times?
Does this log add clarity?
/ entering recursion with input = [1,2,3], set = [], result = []
| looping, i = 0
| adding 1 to set
| / entering recursion with input = [2,3], set = [1], result = []
| | looping, i = 0
| | adding 2 to set
| | / entering recursion with input = [3], set = [1,2], result = []
| | | looping, i = 0
| | | adding 3 to set
| | | / entering recursion with input = [], set = [1,2,3], result = []
| | | | adding 123 to result
| | | \ returning [123]
| | | removing 3 from set
| | \ returning [123]
| | removing 2 from set
| | looping, i = 1
| | adding 3 to set
| | / entering recursion with input = [2], set = [1,3], result = [123]
| | | looping, i = 0
| | | adding 2 to set
| | | / entering recursion with input = [], set = [1,3,2], result = [123]
| | | | adding 132 to result
| | | \ returning [123,132]
| | | removing 2 from set
| | \ returning [123,132]
| | removing 3 from set
| \ returning [123,132]
| removing 1 from set
| looping, i = 1
| adding 2 to set
| / entering recursion with input = [1,3], set = [2], result = [123,132]
| | looping, i = 0
| | adding 1 to set
| | / entering recursion with input = [3], set = [2,1], result = [123,132]
| | | looping, i = 0
| | | adding 3 to set
| | | / entering recursion with input = [], set = [2,1,3], result = [123,132]
| | | | adding 213 to result
| | | \ returning [123,132,213]
| | | removing 3 from set
| | \ returning [123,132,213]
| | removing 1 from set
| | looping, i = 1
| | adding 3 to set
| | / entering recursion with input = [1], set = [2,3], result = [123,132,213]
| | | looping, i = 0
| | | adding 1 to set
| | | / entering recursion with input = [], set = [2,3,1], result = [123,132,213]
| | | | adding 231 to result
| | | \ returning [123,132,213,231]
| | | removing 1 from set
| | \ returning [123,132,213,231]
| | removing 3 from set
| \ returning [123,132,213,231]
| removing 2 from set
| looping, i = 2
| adding 3 to set
| / entering recursion with input = [1,2], set = [3], result = [123,132,213,231]
| | looping, i = 0
| | adding 1 to set
| | / entering recursion with input = [2], set = [3,1], result = [123,132,213,231]
| | | looping, i = 0
| | | adding 2 to set
| | | / entering recursion with input = [], set = [3,1,2], result = [123,132,213,231]
| | | | adding 312 to result
| | | \ returning [123,132,213,231,312]
| | | removing 2 from set
| | \ returning [123,132,213,231,312]
| | removing 1 from set
| | looping, i = 1
| | adding 2 to set
| | / entering recursion with input = [1], set = [3,2], result = [123,132,213,231,312]
| | | looping, i = 0
| | | adding 1 to set
| | | / entering recursion with input = [], set = [3,2,1], result = [123,132,213,231,312]
| | | | adding 321 to result
| | | \ returning [123,132,213,231,312,321]
| | | removing 1 from set
| | \ returning [123,132,213,231,312,321]
| | removing 2 from set
| \ returning [123,132,213,231,312,321]
| removing 3 from set
\ returning [123,132,213,231,312,321]
You can see how I added the logging to your code in this snippet:
const log = (depth, message) =>
console .log ('| '.repeat (depth) + message)
function recursion(input, set = [], result = [], depth = 0) {
log (depth, `/ entering recursion with input = [${input}], set = [${set}], result = [${result}]`)
if (!input.length) {
log (depth, `| adding ${[...set].join('')} to result`)
result.push([...set].join(''));
}
for (let i = 0; i < input.length; i++) {
log (depth, `| looping, i = ${i}`)
const newArr = input.filter((n, index) => index !== i);
log (depth, `| adding ${input[i]} to set` )
set.push(input[i]);
recursion(newArr, set, result, depth + 1);
log (depth, `| removing ${input[i]} from set` )
set.pop();
}
log (depth, `\\ returning [${result}]`)
return result.join(', ');
}
console .log (recursion([1, 2, 3]))
.as-console-wrapper {min-height: 100% !important; top: 0}
(but the console output there is limited to the last 50 lines.)
Related
How does recursion work in a Countdown function
I'm learning a bit of JavaScript, but I'm having hard time understanding the lesson on FreeCodeCamp about the recursion countdown (link). In the lesson, there this initial example. But I'm confused on how it operates: function countup(n) { if (n < 1) { return []; } else { const countArray = countup(n - 1); countArray.push(n); return countArray; } } console.log(countup(5)); I see the steps that this function performs (in order), as something like this: n (5 in the beginning) is greater than 1, so go to the else statement. in the else statement I'll have a constant (countArray) assigned to do countup(n - 1), which is 4. the next operation js encounters is countArray.push(n), so it will push 4 into an array. then it encounters return countArray, which is 4. the function continues with 3,2,1 till the base condition reaches n==0, and in that case the function ends returning me an empty array. I know this is not how it works, I'm simply "describing" how my noob mind works looking at this recursive function. I looked at the solution of the exercise and other people's explanations, but I cannot understand why the function does not work like I described, and also why after reaching n==0, it starts to fill up an array counting up from 1 to 5. I would like to understand this.
Realise that every execution of countup will have its own n and countArray variables. It may help to visualise it. Each execution context is visualised as a "box". The variables in the outer boxes will still be there, when a function call returns. The outermost box is the execution context that is created by the initial call: countup(5): // n is 5 and does not change const countArray = countup(n - 1); +-------------------------------------------------------------------------------+ | // n is 4 and does not change | | const countArray = countup(n - 1); | | +-------------------------------------------------------------------------+ | | | // n is 3 and does not change | | | | const countArray = countup(n - 1); | | | | +-------------------------------------------------------------------+ | | | | | // n is 2 and does not change | | | | | | const countArray = countup(n - 1); | | | | | | +-------------------------------------------------------------+ | | | | | | | // n is 1 and does not change | | | | | | | | const countArray = countup(n - 1); | | | | | | | | +-------------------------------------------------------+ | | | | | | | | | // n is 0 and does not change | | | | | | | | | | return []; // the if-block is executed because n < 1 | | | | | | | | | +-------------------------------------------------------+ | | | | | | | | // countArray is [] | | | | | | | | countArray.push(n); // n is still 1 | | | | | | | | return countArray; // returns [1] | | | | | | | +-------------------------------------------------------------+ | | | | | | // countArray is [1] | | | | | | countArray.push(n); // n is still 2 | | | | | | return countArray; // returns [1, 2] | | | | | +-------------------------------------------------------------------+ | | | | // countArray is [1, 2] | | | | countArray.push(n); // n is still 3 | | | | return countArray; // returns [1, 2, 3] | | | +-------------------------------------------------------------------------+ | | // countArray is [1, 2, 3] | | countArray.push(n); // n is still 4 | | return countArray; // returns [1, 2, 3, 4] | +-------------------------------------------------------------------------------+ // countArray is [1, 2, 3, 4] countArray.push(n); // n is still 5 return countArray; // returns [1, 2, 3, 4, 5]
You can just add some logging to visualize what is happening: function countup(n) { if (n < 1) { console.log('n = %d, returning empty array', n); return []; } else { console.log('n = %d, calling countup(%d - 1)', n, n); const countArray = countup(n - 1); console.log('n = %d, countArray is %s', n, JSON.stringify(countArray)) console.log('n = %d, pushing n onto array', n); countArray.push(n); console.log('n = %d, returning %s', n, JSON.stringify(countArray)); return countArray; } } console.log(countup(5));
Here what the array looks like inside of each function call if this helps: Array: 1 N: 1 Array: 1,2 N: 2 Array: 1,2,3 N: 3 Array: 1,2,3,4 N: 4 Array: 1,2,3,4,5 N: 5
If you put a console log in here like this, you can visualize it better. function countup(n) { if (n < 1) { return []; } else { const countArray = countup(n - 1); countArray.push(n); console.log(`countup(${n}) returns ${countArray}`); return countArray; } } console.log(countup(5)); What happens is that as soon as you hit that first invocation of countup, execution moves there. So, invoke it with 5 and you see the first output statement is run with a 1, because several more times it basically called itself. countup (5) calls countup (4) calls countup(3) calls countup (2) calls countup (1) -- countup (1) returns 1, back to the caller of 2 which returns 2, 1, back to the caller of 3 returns 3, 2, 1...and so on back up to 5.
how to concatenate bits in javascript?
I want to concatenate the bits of two number values like this: +---------+---------+---------+-----------------+ | | val1 | val2 | concatenate val | +---------+---------+---------+-----------------+ | decimal | 18 | 16 | 592 | | hexa | 0x12 | 0x10 | 0x250 | | binary | 0b10010 | 0b10000 | 0b1001010000 | +---------+---------+---------+-----------------+ I have try to just concatenate with + like that : const test = 0b10010, test1 = 0b10000 console.log(test+test1)//return 34 It does not concatenate the values but adds them together.
You could shift the first value by the bitwise length of the second value before adding. const add = (a, b) => (a << Math.ceil(Math.log2(b)) + 1) + b; test = 0b10010, test1 = 0b10000, console.log(add(test, test1)); //
How Do I Solve This Complex Javascript Algorithm Without Overcomplicating it?
How many ways can you make the sum of a number? From wikipedia: https://en.wikipedia.org/wiki/Partition_(number_theory)# In number theory and combinatorics, a partition of a positive integer n, also called an integer partition, is a way of writing n as a sum of positive integers. Two sums that differ only in the order of their summands are considered the same partition. If order matters, the sum becomes a composition. For example, 4 can be partitioned in five distinct ways: 4 3 + 1 2 + 2 2 + 1 + 1 1 + 1 + 1 + 1 Examples Basic sum(1) // 1 sum(2) // 2 -> 1+1 , 2 sum(3) // 3 -> 1+1+1, 1+2, 3 sum(4) // 5 -> 1+1+1+1, 1+1+2, 1+3, 2+2, 4 sum(5) // 7 -> 1+1+1+1+1, 1+1+1+2, 1+1+3, 1+2+2, 1+4, 5, 2+3 sum(10) // 42 Explosive sum(50) // 204226 sum(80) // 15796476 sum(100) // 190569292 My Attempt I tried to loop through two arrays simultaneously and test them against eachother. This doesn't work (at least in the way I did it) for a few reasons. My Code: function sum(num, arr = []) { if(num == 0){ return testNumbers(arr, num); } arr.push(num); return sum(num - 1, arr); function testNumbers(arrr, n){ let arr2 = [...arrr]; let count = 0; let calculations = arrr.filter((item)=>{ return item + arr2.map((a)=>{ return a; }) == n; }) console.log(calculations); } } console.log(sum(10)); You don't need to fix my code, as I don't think its salvageable, but how do you solve the problem?
This is in fact a fairly simple recursion, if we think of the problem as counting the partitions with a given lower bound. We have two simple bases cases of 0 and 1 if the lower bound is greater than our target number or equal to it. Otherwise we recur in two ways, one for when we use that lower bound, and one for when we don't. Here's one version, where lb is the lower bound of the numbers to use: const count = (n, lb = 1) => lb > n ? 0 : lb == n ? 1 : count (n - lb, lb) + count (n, lb + 1) This counts the number of partitions with the given lower bound. So count(10, 3) would yield 5, one for each array in [[3, 3, 4], [3, 7], [4, 6], [5, 5], [10]]. Although the default value for the lower bound means that we can call it with just our target number, there are potential issues with this if we tried, say, to map over it. So it might be best to wrap this in a separate function, const countPartitions = (n) => count (n, 1) const count = (n, lb) => lb > n ? 0 : lb == n ? 1 : count (n - lb, lb) + count (n, lb + 1) const countPartitions = (n) => count (n, 1) console .log ([1, 2, 3, 4, 5, 6, 7, 8, 9, 10] .map (countPartitions)) But this will be quite slow for larger input. My test for 100 took 56.3 seconds.) If we memoize the intermediate results, we should speed things up a great deal. We can do this manually or, as I'd prefer, with a memoization helper: const memoize = (makeKey, fn) => { const memo = {} return (...args) => { const key = makeKey (...args) return memo [key] || (memo [key] = fn (...args)) } } const count = memoize ( (n, lb) => `${n}~${lb}`, (n, lb) => lb > n ? 0 : lb == n ? 1 : count (n - lb, lb) + count (n, lb + 1) ) const countPartitions = (n) => count (n, 1) console .log (countPartitions (100)) And this now takes 20 milliseconds in my test. Update: answering comment A comment asked Hey Scott, sorry to bring up an old post, but I've been going over your solution here and I'm afraid I'm having trouble understanding exactly how it works. If you don't mind, could you go a little more in-depth on why counting instances of n===lb leads to the answer? Maybe my math is just weak, but I'm not following the partitions logic. Let's imagine we're trying to partition 10, and we've already counted those whose lowest value is 1 and those whose lowest value is 2, and now we're trying to count the partitions where the lowest value is at least 3. We call count (10, 3). Since 3 > 10 is false, we don't return 0. And since 3 == 10 is false, we don't return 1. Instead we make two recursive calls and add their results together. The first one is where we choose to use 3 in our output, choose (7, 3), since we will have seven remaining when we've selected the first 3. The second one is where we choose not to use 3 in our output, choose (10, 4), since the lowest bound will be 4 if we are to skip 3, but we still have ten to partition. The call structure would look like this: (10, 3) ___________________________________/\____________________ / \ (7, 3) + (10, 4) ___________/\___________ __________/\_________ / \ / \ (4, 3) + (7, 4) (6, 4) + (10, 5) ____/\___ ________/\_______ ____/\____ _____/\_______ / \ / \ / \ / \ (1, 3) + (4, 4) (3, 4) + (7, 5) (2, 4) + (6, 5) (5, 5) + (10, 6) | | | ___/\___ | ___/\___ | _____/\_____ | | | / \ | / \ | / \ | | | (2, 5) + (7, 6) | (1, 5) + (6, 6) | (4, 6) + (10, 7) | | | | __/\__ | | | | | ____/\____ | | | | / \ | | | | | / \ | | | | (1, 6) + (7, 7) | | | | | (3, 7) + (10, 8) | | | | | | | | | | | | ___/\____ | | | | | | | | | | | | / \ | | | | | | | | | | | | (2, 8) + (10, 9) | | | | | | | | | | | | | ___/\___ | | | | | | | | | | | | | / \ | | | | | | | | | | | | | (1, 9) + (10, 10) | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | (3>1) (4=4) (4>3) (5>2) (6>1) (7=7) (4>2) (5>1) (6=6)(5=5) (6>4) (7>3) (8>2) (9>1) (10=10) | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 0 + 1 + 0 + 0 + 0 + 1 + 0 + 0 + 1 + 1 + 0 + 0 + 0 + 0 + 1 | | | | | | | | | | [[3, 3, 4], [3, 7], [4, 6],[5, 5], [10]]
We can get a speed up over Scott's solution by using the recurrence relation for the partition function that uses pentagonal numbers: function _p(k, memo){ if (k == 0) return 1; if (memo[k]) return memo[k]; let result = 0; let i = 1; const ms = [1, 1, -1, -1]; while (true){ const n = i & 1 ? (i + 1) / 2 : -i / 2; const pentagonal = (3*n*n - n) / 2; if (pentagonal > k) break; result = result + ms[(i-1) % 4] * _p(k - pentagonal, memo); i = i + 1; } return memo[k] = result; } function p(k){ return _p(k, {}); } var ks = [1, 2, 3, 4, 5, 6, 10, 50, 80, 100]; for (let k of ks){ const start = new Date; console.log(`${ k }: ${ p(k) }`); console.log(`${ new Date - start } ms`); console.log(''); }
Google Apps Script - Find Last Empty Row of a Column in Spreadsheet
I have currently a script like this: (Which will run every minute and fetch any values) // function 01 sheet2.getRange(sheet2.getLastRow() + 1, 1, 1, 6).setValues(values); // function 02 sheet2.getRange(sheet2.getLastRow() + 1, 10, 1, 6).setValues(values); It finds the last row and set the values in next row. Both are in separate functions. But currently it outputs something like this. Current Output : NOT GOOD // function 1 output here // function 2 output here ------------------------------------------------------------ | A | B | C || | | | ------------------------------------------------------------ | | | || D | E | F | ------------------------------------------------------------ | | | || G | H | I | ------------------------------------------------------------ | J | K | L || | | | ------------------------------------------------------------ I want that to be displayed like this: EXPECTED RESULT ------------------------------------------------------------ | A | B | C || D | E | F | ------------------------------------------------------------ | J | K | L || G | H | I | ------------------------------------------------------------ | | | || | | | ------------------------------------------------------------ Hope I'm clear.
Try the below function, very slightly modified to pass the column from the solution Mogsdad supplied on Nov 27, 2014 for the New Sheets in response to the thread Faster way to find the first empty row // Don's array approach - checks first column only // With added stopping condition & correct result. // From answer https://stackoverflow.com/a/9102463/1677912 // Added the passing of myColumn which needs to be a column range // example use: var emptyRow = getFirstEmptyRowByColumnArray('B:B'); function getFirstEmptyRowByColumnArray(myColumn) { var spr = SpreadsheetApp.getActiveSpreadsheet(); var column = spr.getRange(myColumn); var values = column.getValues(); // get all data in one call var ct = 0; while ( values[ct] && values[ct][0] != "" ) { ct++; } return (ct+1); } It could, of course, be written instead to just pass the column and create the range if you like.
calculate values from row and column then output to mulltiple cell
i have a table with these columns: 1st col 2nd col 3rd col 4th col ------------------------------------------------------ | | | 1st Inst | 2nd Inst | | Athletic | 100 | | | | Socio-cultural | 100 | | | | Library | 150 | | | | Medical | 50 | | | | School Organ | 20 | | | | Dev't Fee | 100 | | | ------------------------------------------------------ | Buttons: (ADD) (DEL) | | | ------------------------------------------------------ | | |___Input____|___Input____| ------------------------------------------------------ the values on the 1st and 2nd column are generated using php from the database. this table is a dynamic table wherein the number of the columns can increase or decrease by pressing the buttons. and at the bottom of the table i have input fields that can also increase as the columns of the table increases. what i want is that when i input a value on the input fields, it will be process in a calculation together with the values each row in the 2nd column and that output will be written on the corresponding row of the 3rd/4th column depending on which input field contains the initial value. example is like these: 1st col 2nd col 3rd col 4th col ------------------------------------------------------ | | | 1st Inst | 2nd Inst | | Athletic | 100 | 200 | | | Socio-cultural | 200 | 400 | | | Library | 300 | 600 | | | Medical | 400 | 800 | | | School Organ | 500 | 1000 | | | Dev't Fee | 600 | 1200 | | ------------------------------------------------------ | Buttons: (ADD) (DEL) | | | ------------------------------------------------------ | | | 2 | | i have no problem in the calculation part...what my problem is how can i output the values on the corresponding columns. here is the php code for retrieving the values for both 1st and 2nd col. $stmt = $pdo->prepare('SELECT * FROM miscfeelist'); $stmt->setFetchMode(PDO::FETCH_ASSOC); $stmt->execute(); $result = $stmt->fetchAll(); if (count($result)){ foreach ($result as $r){ echo '<tbody id="tb"><tr> <td align="center">'.$r['MiscFee_Name'].'</td> <td align="center" valign="middle" class="value">'.$r['MiscFee_Value'].'</td> <td> </td> <td class="dwn"> </td> <td class="dwn"> </td> </tbody></tr>'; } } else { echo "No Record in the Database."; } here is the input field: <tr> <td align="center"> </td> <td align="center" valign="middle" bgcolor="#FF9900"><input class="inst" type="text" style="border:thick" size="12" onkeyup="MiscFeeColl()" placeholder="1st" value="0"></td> <td align="center" valign="middle" bgcolor="#FF9900"><input class="inst" type="text" style="border:thick" size="12" onkeyup="MiscFeeColl()" placeholder="2nd" value="0"></td> </tr> here is my code for the calculation: for(var i = 0; i < theOddOnes.length; i++){ //rows for(var l = 0; l < res.length; l++){ inst = theOddOnes[l].value; fund165 = (MiscFee/GrandTot()*inst); fund165 = +fund165.toFixed(2); //columns Miscfee123 = (parseFloat(val[l].innerHTML)/MiscFee*fund165); Miscfee123 = +Miscfee123.toFixed(2); document.getElementsByClassName("dwn")[l].firstChild.nodeValue = Miscfee123; } } the output of these calculation is these: 1st col 2nd col 3rd col 4th col ------------------------------------------------------ | | | 1st Inst | 2nd Inst | | Athletic | 100 | 200 | 400 | | Socio-cultural | 200 | 600 | 800 | | Library | 300 | 1000 | 1200 | | Medical | 400 | | | | School Organ | 500 | | | | Dev't Fee | 600 | | | ------------------------------------------------------ | Buttons: (ADD) (DEL) | | | ------------------------------------------------------ | | |___2________|____________| ------------------------------------------------------ my code is displaying it horizontally.and i've been working on this for the las two weeks..i cant seem to make it to work..pls help.. EDITED I have tried this one and it make sense somehow but i cant make it work with the other inputs... var myTable = document.getElementById('billingForm'); for(var i= 0; i < theOddOnes.length; i++){ //rows for(var l = 0; l < res.length; l++){ inst = theOddOnes[i].value; fund165 = (MiscFee/GrandTot()*inst); fund165 = +fund165.toFixed(2); Miscfee123 = (parseFloat(val[l].innerHTML)/MiscFee*fund165); Miscfee123 = +Miscfee123.toFixed(2); // document.getElementsByClassName("dwn")[l].firstChild.nodeValue = Miscfee123; myTable.rows[l].cells[i].innerHTML = Miscfee123; } } here is the output: 200 2nd col 3rd col 4th col ------------------------------------------------------ | 400 | | 1st Inst | 2nd Inst | | 600 | 100 | | | | 800 | 200 | | | | 1000 | 300 | | | | 1200 | 400 | | | | School Organ | 500 | | | | Dev't Fee | 600 | | | ------------------------------------------------------ | Buttons: (ADD) (DEL) | | | ------------------------------------------------------ | | | 2 | | i tried to edit the code to other index but it outputs 0 values. for(var i= 2; i < theOddOnes.length; i++){ //rows for(var l = 3; l < res.length; l++){ here is the output: 1st col 2nd col 3rd col 4th col ------------------------------------------------------ | | | 1st Inst | 2nd Inst | | Athletic | 100 | 0 | | | Socio-cultural | 200 | 0 | | | Library | 300 | 0 | | | Medical | 400 | 0 | | | School Organ | 500 | 0 | | | Dev't Fee | 600 | 0 | | ------------------------------------------------------ | Buttons: (ADD) (DEL) | | | ------------------------------------------------------ | | | 2 | |