<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Wéri Boni</title>
    <description>The latest articles on DEV Community by Wéri Boni (@fernandels_boni).</description>
    <link>https://dev.to/fernandels_boni</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2292000%2F4f6ea855-9e6c-4274-b0ad-8e7afaa02151.jpg</url>
      <title>DEV Community: Wéri Boni</title>
      <link>https://dev.to/fernandels_boni</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/fernandels_boni"/>
    <language>en</language>
    <item>
      <title>The importance of backtracking for developpers</title>
      <dc:creator>Wéri Boni</dc:creator>
      <pubDate>Mon, 20 Jan 2025 11:02:04 +0000</pubDate>
      <link>https://dev.to/fernandels_boni/the-importance-of-backtracking-for-developpers-5a10</link>
      <guid>https://dev.to/fernandels_boni/the-importance-of-backtracking-for-developpers-5a10</guid>
      <description>&lt;p&gt;Definition&lt;/p&gt;

&lt;p&gt;Backtracking is a method used in all programming languages to explore all possible outcomes of a problems.&lt;br&gt;
It can be applied to solve problems like finding paths in a labyrinth, solving the N-Queens problem, Sudoku, and more.&lt;/p&gt;

&lt;p&gt;Why is it useful?&lt;/p&gt;

&lt;p&gt;Why is backtracking valuable for developers?&lt;br&gt;
Imagine a situation where there are numerous possible outcomes to explore. Do we have the time to check every possibility manually? Obviously not.&lt;br&gt;
We might think of creating a large loop to go through all potential solutions. But can every machine's capacity handle such extensive work? Again, no.&lt;/p&gt;

&lt;p&gt;This is where backtracking comes in handy. With this concept, we systematically try each possible solution. If a solution doesn't work, we go back and try another one, continuing until we meet the conditions for success that we’ve defined.&lt;/p&gt;

&lt;p&gt;Analogy&lt;/p&gt;

&lt;p&gt;Take Sudoku as an example:&lt;br&gt;
Each row must contain numbers from 1 to 9.&lt;br&gt;
Each column must contain numbers from 1 to 9.&lt;br&gt;
Each of the 9 sub-grids (3*3) must contain numbers from 1 to 9.&lt;/p&gt;

&lt;p&gt;When solving a Sudoku puzzle, there are empty spaces to fill. To solve it:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We create a function to check if a number meets all the rules.&lt;/li&gt;
&lt;li&gt;After verifying if we can place a number, we proceed to check the possibilities for the remaining empty spaces.&lt;/li&gt;
&lt;li&gt;If placing a number leads to an invalid solution later, we backtrack, try another number, and repeat the process.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This continues until all spaces are correctly filled and the puzzle is solved.&lt;/p&gt;

&lt;p&gt;Principal Steps of Backtracking&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Choice: Identify all possible solutions for each step and check them one by one.&lt;/li&gt;
&lt;li&gt;Constraint: Verify if a solution is valid based on the rules.&lt;/li&gt;
&lt;li&gt;Objective: Determine if a solution satisfies all conditions.&lt;/li&gt;
&lt;li&gt;Step Back: Backtrack when a solution fails and explore other options.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Example Code: Solving Sudoku with Backtracking with JavaScript&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//We start with a partially filled Sudoku board (for empty cells) and we want to find the possible numbers that can be used to fill the board


const board = [
    ["5", "3", ".", "6", "7", "8", "9", "1", "2"],
    ["6", "7", "2", "1", "9", "5", "3", "4", "8"],
    ["1", "9", "8", "3", "4", "2", "5", "6", "7"],
    ["8", "5", "9", "7", "6", "1", "4", "2", "3"],
    ["4", "2", "6", "8", ".", "3", "7", "9", "1"],
    ["7", "1", "3", "9", "2", "4", "8", "5", "6"],
    ["9", "6", "1", "5", "3", "7", "2", "8", "4"],
    ["2", "8", "7", "4", "1", "9", "6", "3", "5"],
    ["3", "4", "5", "2", "8", "6", "1", ".", "9"]
];
//'.' represents an empty cell


// Possible numbers for Sudoku
const possibleNumbers = ["1", "2", "3", "4", "5", "6", "7", "8", "9"];

// Function to check if placing a number is valid
function isValid(number, row, col, board) {
    // Check row and column
    for (let i = 0; i &amp;lt; board.length; i++) {
        if (board[row][i] === number || board[i][col] === number) {
            return false;
        }
    }

    // Check the 3x3 sub-grid
    let startRow = Math.floor(row / 3) * 3;
    let startCol = Math.floor(col / 3) * 3;

    for (let i = startRow; i &amp;lt; startRow + 3; i++) {
        for (let j = startCol; j &amp;lt; startCol + 3; j++) {
            if (board[i][j] === number) {
                return false;
            }
        }
    }

    return true; // The number is valid for this position
}

// Function to solve the Sudoku board
function solveSudoku(board) {
    const emptySpaces = [];

    // Find all empty spaces on the board
    for (let i = 0; i &amp;lt; 9; i++) {
        for (let j = 0; j &amp;lt; 9; j++) {
            if (board[i][j] === ".") {
                emptySpaces.push({ row: i, col: j });
            }
        }
    }

    // Recursive function to fill empty spaces
    function recurse(emptySpaceIndex) {
        if (emptySpaceIndex &amp;gt;= emptySpaces.length) {
            return true; // All spaces filled successfully
        }

        const { row, col } = emptySpaces[emptySpaceIndex];

        for (let i = 0; i &amp;lt; possibleNumbers.length; i++) {
            const num = possibleNumbers[i];
            if (isValid(num, row, col, board)) {
                board[row][col] = num; // Place the number

                if (recurse(emptySpaceIndex + 1)) {
                    return true; // Solution found
                }

                // Backtrack if placing the number doesn't lead to a solution
                board[row][col] = ".";
            }
        }

        return false; // No valid number found for this position
    }

    recurse(0); // Start solving from the first empty space
    return board;
}

// Solve the Sudoku puzzle
solveSudoku(board);
console.log(board);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Key Points&lt;/p&gt;

&lt;p&gt;Backtracking systematically explores all possibilities while adhering to constraints.&lt;br&gt;
It is especially useful for solving constraint-based problems like Sudoku, N-Queens, and more.&lt;br&gt;
The recursive nature of backtracking allows us to step back and try alternate paths when a solution fails.&lt;/p&gt;

&lt;p&gt;I hope you are satisfied with my article. I'll be there answering all questions you may have. &lt;br&gt;
If you're satisfied, just leave a ♥️ (It actually means a lot)&lt;/p&gt;

&lt;p&gt;For image:&lt;a href="https://www.freepik.com/free-vector/man-with-megaphone-screaming-concept-illustration_38321943.htm#fromView=keyword&amp;amp;page=2&amp;amp;position=0&amp;amp;new_detail=true&amp;amp;query=Capture+Attention" rel="noopener noreferrer"&gt;Image by storyset on Freepik&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>discuss</category>
      <category>learning</category>
    </item>
    <item>
      <title>[Boost]</title>
      <dc:creator>Wéri Boni</dc:creator>
      <pubDate>Mon, 20 Jan 2025 10:39:01 +0000</pubDate>
      <link>https://dev.to/fernandels_boni/-2o3i</link>
      <guid>https://dev.to/fernandels_boni/-2o3i</guid>
      <description></description>
      <category>career</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Hello cool devs out there. I've just starred to learn react and I'm liking the idea of reusable components</title>
      <dc:creator>Wéri Boni</dc:creator>
      <pubDate>Sun, 12 Jan 2025 14:00:35 +0000</pubDate>
      <link>https://dev.to/fernandels_boni/hello-cool-devs-out-there-ive-just-starred-to-learn-react-and-im-liking-the-idea-of-reusable-49m0</link>
      <guid>https://dev.to/fernandels_boni/hello-cool-devs-out-there-ive-just-starred-to-learn-react-and-im-liking-the-idea-of-reusable-49m0</guid>
      <description></description>
    </item>
  </channel>
</rss>
