Cookie Consent by Free Privacy Policy Generator 📌 Dry running the code to understand the flow


✅ Dry running the code to understand the flow


💡 Newskategorie: Programmierung
🔗 Quelle: dev.to

So I am trying to solve a problem in recursion and backtracking using js as my language. the question is :

` Subsets ( Backtracking Algorithm using Recursion )
// Given an integer array nums of unique elements, return all possible subsets (the power set).
// The solution set must not contain duplicate subsets. Return the solution in any order.

// Input: [1,2,3] ----->>>>> Output: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
// Input: [0] ----->>>>> Output: [[],[0]]`

My Solution

function subsets(prob) {
  let result = [];
  let temp = [];

  function getPowSet(nums, i) {
    if (i === nums.length) {
      return result.push([...temp]);
    }
    console.log(temp, i, "0");
    temp.push(nums[i]);
    getPowSet(nums, i + 1);
    console.log(temp, i, "1");
    temp.pop();
    getPowSet(nums, i + 1);
    console.log(temp, i, "2");
  }
  getPowSet(prob, 0);
  return result;
}

and I have put certain logs in the code to understand the flow

the logs looks like:-

[ 1 ] 0 0
[ 1, 2 ] 1 0
[ 1, 2, 3 ] 2 0
[ 1, 2 ] 2 1
[ 1, 2 ] 2 2
[ 1 ] 1 1
[ 1, 3 ] 2 0
[ 1 ] 2 1
[ 1 ] 2 2
[ 1 ] 1 2
[] 0 1
[ 2 ] 1 0
[ 2, 3 ] 2 0
[ 2 ] 2 1
[ 2 ] 2 2
[] 1 1
[ 3 ] 2 0
[] 2 1
[] 2 2
[] 1 2
[] 0 2

My concern:

So when the code completes its 1st recursion loop and hits the base case when i become 3 and pushes the temp array to result and returns.. why does the i still logs as 2 in the next log, because there is no i-- going on anywhere inside the base case so it should still log i = 3

here :-

[ 1 ] 0 0
[ 1, 2 ] 1 0
[ 1, 2, 3 ] 2 0
[ 1, 2 ] 2 1 <--------

...

✅ Dry running the code to understand the flow


📈 55.53 Punkte

✅ Ansible Dry Run: Running Playbook in Check Mode


📈 27.17 Punkte

✅ Duped into running bogus virus scans at Office Depot? Dry your eyes with a small check from $35m settlement


📈 27.17 Punkte

✅ Understand & Implement Masked AutoRegressive Flow with TensorFlow


📈 24.8 Punkte

✅ Intel CSME/TXE Control Flow Management control flow [CVE-2020-8745]


📈 23.21 Punkte

✅ Antec P82 Flow: Flow heißt nicht Mesh, sondern Lüfter


📈 23.21 Punkte

✅ The Right Flow For The Job: Which OAuth 2.0 Flow Should I Use?


📈 23.21 Punkte

✅ Insta360 Flow & Flow Pro: Neue intelligente Tracking-Gimbals mit KI vorgestellt


📈 23.21 Punkte

✅ Flow-Flow Social Stream <= 3.0.71 - Unauthenticated Cross-Site Scripting (XSS)


📈 23.21 Punkte

✅ CVE-2024-37622 | Xinhu RockOA 2.6.3 /flow/flow.php num cross site scripting


📈 23.21 Punkte

✅ NZXT zur Computex: Neues H7 Flow, H7 Flow RGB, Lüfter und Netzteil vorgestellt


📈 23.21 Punkte

✅ Going with the Flow for CI/CD: Heroku Flow with Gitflow


📈 23.21 Punkte

✅ Visualizing trade flow in Python maps — Part I: Bi-directional trade flow maps


📈 23.21 Punkte

✅ ASUS announces ROG Flow X16, its 'most powerful' ROG Flow laptop yet


📈 23.21 Punkte

✅ Intel Graphics Drivers prior 15.45.32.5145 Control Flow Management control flow


📈 23.21 Punkte

✅ Insertion Sort in Java (With Intuition + Dry run + Code)


📈 22.21 Punkte

✅ Selection Sort in Java (With Intuition + Dry run + Code)


📈 22.21 Punkte

✅ Bubble Sort in Java (With Intuition + Dry run + Code)


📈 22.21 Punkte

✅ Merge Sort in Java (With Intuition + Dry run + Code)


📈 22.21 Punkte

✅ DRY, KISS and YAGNI - Make Your Code Simple


📈 22.21 Punkte

✅ Mastering Higher-Order Components in React: A Guide to DRY Code


📈 22.21 Punkte

✅ Why Code with Code Smells is Harder to Understand


📈 20.31 Punkte

✅ 🔴 Code in the Flow with VS Code’s GitHub Copilot Chat | Part 2


📈 18.71 Punkte

✅ 🔴 Code in the Flow with VS Code’s GitHub Copilot Chat


📈 18.71 Punkte

✅ Cordless vacuums are going smart, and this Narwal wet/dry vacuum is the latest proof


📈 18.65 Punkte

✅ Leisure Suit Larry - Wet Dreams Don't Dry: Schwerenöter Larry Laffer treibt auch auf Xbox One sein Unwesen


📈 18.65 Punkte

✅ Leisure Suit Larry: Wet Dreams Don't Dry im Test - Der Schwerenöter aus den ...


📈 18.65 Punkte

✅ [Reflective Coding Journal] The DRY Principle: A Lazy Programmer's Best Friend


📈 18.65 Punkte

✅ Delicious Non-Boozy Beer, Wine, and Spirits for Dry January


📈 18.65 Punkte

✅ Drought Is Declared in Parts of a Hot, Dry Britain


📈 18.65 Punkte

✅ LHB Linux Digest #24.09: Docker Course, Ansible Dry Run, System Resource History and More


📈 18.65 Punkte











matomo

Datei nicht gefunden!