<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Axel Lee on Medium]]></title>
        <description><![CDATA[Stories by Axel Lee on Medium]]></description>
        <link>https://medium.com/@axellee1994?source=rss-21622b47f8f4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*EoWmB0-iE9WP2Cd3x0TKHA.png</url>
            <title>Stories by Axel Lee on Medium</title>
            <link>https://medium.com/@axellee1994?source=rss-21622b47f8f4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 14:03:29 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@axellee1994/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[LeetCode 605: Can Place Flowers]]></title>
            <link>https://medium.com/@axellee1994/leetcode-605-can-place-flowers-dde8ef015691?source=rss-21622b47f8f4------2</link>
            <guid isPermaLink="false">https://medium.com/p/dde8ef015691</guid>
            <category><![CDATA[greedy-algorithms]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[leetcode-easy]]></category>
            <dc:creator><![CDATA[Axel Lee]]></dc:creator>
            <pubDate>Wed, 08 Oct 2025 06:59:34 GMT</pubDate>
            <atom:updated>2025-10-08T06:59:34.111Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oBPbwg73Fi-DBMeOK-Hh7g.jpeg" /></figure><p>This is the first question from the practice set of the Greedy Algorithim chapter.</p><h3>Problem Statement — What Does the Problem Say?</h3><p>You have a long flowerbed in which some plots are planted and some are empty. However, flowers cannot be planted in adjacent plots — they would compete for water and both would die.</p><p>Given an integer array flowerbed containing 0&#39;s (empty) and 1&#39;s (planted), and an integer n, return whether n new flowers can be planted in the flowerbed without violating the no-adjacent-flowers rule.</p><h3>Input/Output Example:</h3><pre>Input: flowerbed = [1,0,0,0,1], n = 1<br>Output: True</pre><p><strong>Explanation:</strong> We can plant one flower in the middle empty plot without adjacent flowers</p><h3>Intuition and Approach (Optimal — Greedy)</h3><p>Finding whether we can plant n flowers is equivalent <strong>to maximizing the number of flowers we can plant greedily at the earliest possible valid position</strong>.</p><p>When deciding where to plant, the key insight is: <strong>if a plot is empty and both its neighbors are empty, we should plant a flower there immediately. </strong>This greedy choice works because:</p><ul><li>Planting early never reduces future opportunities — it only affects immediate neighbors</li><li>By planting as soon as possible, we utilize empty space efficiently without looking ahead</li></ul><p>The specific implementation involves:</p><ul><li>Iterating through each plot in the flowerbed</li><li>For each empty plot, checking if both adjacent plots are also empty (or non-existent at boundaries)</li><li>Planting a flower immediately when a valid spot is found</li><li>Counting successfully planted flowers until we reach n</li></ul><p><strong>In the example</strong> flowerbed = [1,0,0,0,1], n = 1:</p><ul><li>Index 0: Already planted → skip</li><li>Index 1: Empty, but left neighbor is planted → skip</li><li>Index 2: Empty, both neighbors empty → plant flower → count = 1</li><li>We’ve planted 1 flower, which satisfies n = 1 → return True</li></ul><h3>Python Implementation</h3><pre>class Solution:<br>    def canPlaceFlowers(self, flowerbed: List[int], n: int) -&gt; bool:<br>        m = len(flowerbed)<br>        count = 0<br>        for i in range(m):<br>            if flowerbed[i] == 0:<br>                empty_left_plot = (i == 0) or (flowerbed[i - 1] == 0)<br>                empty_right_plot = (i == m - 1 ) or (flowerbed[i + 1] == 0)<br><br>                if empty_left_plot and empty_right_plot:<br>                    flowerbed[i] = 1<br>                    count += 1<br>                    if count &gt;= n:<br>                        return True<br>        return count &gt;= n</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dde8ef015691" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Leetcode 435: Non-Overlapping Intervals]]></title>
            <link>https://medium.com/@axellee1994/leetcode-435-non-overlapping-intervals-1f18c524d5a1?source=rss-21622b47f8f4------2</link>
            <guid isPermaLink="false">https://medium.com/p/1f18c524d5a1</guid>
            <category><![CDATA[greedy-algorithms]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[leetcode-medium]]></category>
            <category><![CDATA[leetcode]]></category>
            <dc:creator><![CDATA[Axel Lee]]></dc:creator>
            <pubDate>Fri, 03 Oct 2025 06:38:17 GMT</pubDate>
            <atom:updated>2025-10-03T06:38:17.518Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*lyA11WJdAKmWk5k0Ra0PeQ.png" /></figure><p>Continuing on from the second question of the series, this is the third question of the Greed Algorithm series: LeetCode 435 Non-overlapping Intervals</p><h3>Problem Statement — What Does the Problem Say?</h3><p>Given multiple intervals, calculate the minimum number of intervals that need to be removed so that the remaining intervals do not overlap.</p><p>Note: Intervals that are connected end-to-end are not considered overlapping.</p><h4>Input/Output Example:</h4><pre>Input: [[1,2], [2,4], [1,3]]<br>Output: 1</pre><p><strong>Explanation:</strong> In this example, we can remove the interval [1,3], so that the remaining intervals [[1,2], [2,4]] do not overlap.</p><h3>Intuition and Approach (Optimal — Greedy)</h3><p>Finding the minimum number of intervals to remove is equivalent to retaining as many non-overlapping intervals as possible. When selecting which intervals to keep, the end point of an interval is crucial: the smaller the end point of the selected interval, the more space is left for other intervals, and the more intervals can be retained. Therefore, the greedy strategy adopted is to prioritize intervals with smaller end points that do not overlap with previously selected intervals.</p><p>The specific implementation involves:</p><ul><li>Sorting the intervals in <strong>ascending order based on their end points</strong> (using a lambda function)</li><li>Iteratively selecting the interval with the smallest end point that does not overlap with the previously selected interval</li></ul><p>In the example, the sorted array is [[1,2], [1,3], [2,4]]. Following the greedy strategy:</p><ul><li>Start by initialising with the interval: [1,2]</li><li>Since [1,3] overlaps with [1,2] , skip it</li><li>Since [2,4] does not overlap with [1,2] , retain it</li></ul><p>Thus, the final retained intervals are [[1,2], [2,4]].</p><p><strong>Note:</strong> Depending on the problem, it may be necessary to decide whether to sort by the start or the end of the intervals</p><h3>Python Implementation</h3><pre>class Solution:<br>    def eraseOverlapIntervals(self, intervals: List[List[int]]) -&gt; int:<br>        intervals.sort(key=lambda x: x[1])<br>        print(intervals)<br>        removed, previous_end = 0, intervals[0][1]<br>        for i in range(1, len(intervals)):<br>            if previous_end &gt; intervals[i][0]:<br>                removed += 1<br>            else:<br>                previous_end = intervals[i][1]<br>        return removed</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1f18c524d5a1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Leetcode 135: Candy]]></title>
            <link>https://medium.com/@axellee1994/leetcode-135-candy-b0efb8d59342?source=rss-21622b47f8f4------2</link>
            <guid isPermaLink="false">https://medium.com/p/b0efb8d59342</guid>
            <category><![CDATA[leetcode-hard]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[greedy-algorithms]]></category>
            <category><![CDATA[leetcode]]></category>
            <dc:creator><![CDATA[Axel Lee]]></dc:creator>
            <pubDate>Thu, 02 Oct 2025 02:25:27 GMT</pubDate>
            <atom:updated>2025-10-02T02:25:27.987Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/686/1*8zgIiO1QAFGhlH3ZQoC95Q.jpeg" /></figure><p>Continuing on from the first question of the series, this is the second question of the Greed Algorithm series: LeetCode 135 Candy</p><h3>Problem Statement — What Does the Problem Say?</h3><p>A group of children are standing in a line, each with their own rating. We need to distribute candies to them under the following rules:</p><ol><li>Each child must receive <strong>at least one candy</strong>.</li><li>If a child’s rating is <strong>higher</strong> than that of their immediate neighbor, then this child must receive <strong>more candies</strong> than that neighbor.</li></ol><p><strong>Task:</strong> Find the minimum number of candies needed.</p><p>Example 1:</p><pre>Input: [1,0,2]  <br>Output: 5</pre><p><strong>Explanation:</strong> The minimum distribution is [2,1,2]</p><h3>Intuition and Approach (Optimal — Greedy)</h3><p>At each step, only consider and update the relationship with the neighbor on one side:</p><ul><li>First pass → check right neighbor.</li><li>Second pass → check left neighbor</li></ul><p><strong>Initialization</strong></p><ul><li>Give each child 1 candy: [1,1,1].</li></ul><p><strong>First pass (left to right):</strong></p><ul><li>If the right child’s rating is higher than the left child’s, then the right child’s candy count = left child’s candy count + 1.</li><li>After this pass: [1,1,2].</li></ul><p><strong>Second pass (right to left):</strong></p><ul><li>If the left child’s rating is higher than the right child’s <strong>and</strong> the left child’s candy count ≤ the right child’s, then update left child’s candy = right child’s candy + 1.</li><li>After this pass: [2,1,2].</li></ul><p>Final result: total = 2 + 1 + 2 = 5.</p><h3>Python Implementation</h3><pre>class Solution:<br>    def candy(self, ratings: List[int]) -&gt; int:<br>        n = len(ratings) # To find the length of the array<br>        candies = [1] * n # This is to give all kids 1 candies<br><br>        # Left -&gt; Right Pass<br>        # If a child has a higher rating than the left neighbor<br>        # They must get more candies than the left neighbour<br>        for i in range(1, n): # Start from 2nd child -&gt; last child<br>            if ratings[i] &gt; ratings[i - 1]:<br>                candies[i] = candies[i - 1] + 1<br><br>        # Right -&lt; Left Pass<br>        # If a child has a higher rating than the right neighbour<br>        # They must also get more candies than the right neighout<br>        for i in range(n - 1, 0, -1): # Start from last child -&gt; 2nd child<br>            if ratings[i] &lt; ratings[i - 1]:<br>                # Ensure left child has at least 1 more candy than the right child<br>                candies[i - 1] = max(candies[i - 1], candies[i] + 1)<br>        return sum(candies)</pre><h3>Visualization of code workflow</h3><h3>Step 1: Initialization</h3><ul><li>Start by giving <strong>1 candy to every child</strong>, since that’s the minimum requirement.</li><li>For example, with ratings [1, 0, 2], the initial candy distribution is:</li></ul><pre>[1, 1, 1]</pre><ul><li>This satisfies rule (1), but not necessarily rule (2).</li></ul><h3>Step 2: First Pass (Left → Right)</h3><ul><li>Move from <strong>left to right</strong>, comparing each child with the one <strong>before</strong> them.</li><li>Rule: If the <strong>current child’s rating is higher</strong> than the left neighbor’s, then give the current child <strong>one more candy than the left neighbor</strong>.</li></ul><pre>Ratings = [1, 0, 2]<br><br>Candies start as [1, 1, 1].<br><br>Compare rating[1] (0) with rating[0] (1):<br><br>  0 &lt; 1 → no update, candies remain [1, 1, 1].<br><br>Compare rating[2] (2) with rating[1] (0):<br><br>  2 &gt; 0 → child[2] should get candies[1] + 1 = 2.<br><br>Update: [1, 1, 2].</pre><h3>Step 3: Second Pass (Right → Left)</h3><ul><li>Now move from <strong>right to left</strong>, comparing each child with the one <strong>after</strong> them.</li><li>Rule: If the <strong>current child’s rating is higher</strong> than the right neighbor’s, and their candy count is <strong>not greater</strong>, then update the current child’s candy to be right child’s candy + 1.</li></ul><pre>Ratings = [1, 0, 2]<br><br>Candies after first pass = [1, 1, 2].<br><br>Compare rating[1] (0) with rating[2] (2):<br><br>  0 &lt; 2 → no update, candies stay [1, 1, 2].<br><br>Compare rating[0] (1) with rating[1] (0):<br><br>  1 &gt; 0 → left child must have more candies than right child.<br><br>Currently, candies[0] = 1, candies[1] = 1 → not valid.<br><br>Update: candies[0] = candies[1] + 1 = 2.<br><br>New distribution: [2, 1, 2].</pre><ul><li>After this pass: [1, 1, 2]</li></ul><h3>Step 3: Second Pass (Right → Left)</h3><ul><li>The final candy distribution is:</li></ul><pre>Candies = [2, 1, 2]</pre><ul><li>Child 0 (rating 1) → 2 candies.</li><li>Child 1 (rating 0) → 1 candy.</li><li>Child 2 (rating 2) → 2 candies.</li></ul><p><strong>Total = 2 + 1 + 2 = 5 candies.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b0efb8d59342" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Leetcode 455: Assign Cookies]]></title>
            <link>https://medium.com/@axellee1994/leetcode-455-assign-cookies-2787d9fa4df9?source=rss-21622b47f8f4------2</link>
            <guid isPermaLink="false">https://medium.com/p/2787d9fa4df9</guid>
            <category><![CDATA[leetcode-easy]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[greedy-algorithms]]></category>
            <dc:creator><![CDATA[Axel Lee]]></dc:creator>
            <pubDate>Wed, 01 Oct 2025 08:47:08 GMT</pubDate>
            <atom:updated>2025-10-01T08:47:08.822Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/686/1*GuRu3EZGMlHU8iKfMYbPKw.jpeg" /><figcaption>How to solve a greedy child’s appetite</figcaption></figure><p>While spending some time in Beijing, I came across an app called <em>The Little Red Note (小红书)</em>. Even though I’ve been busy preparing for my new role as a Data Scientist, I still make it a point to keep sharpening my software engineering skills — especially through <strong>LeetCode practice</strong>.</p><p>That’s when I stumbled upon a resource by <a href="https://github.com/changgyhub">Chang Gao</a>, who wrote an excellent guidebook called <a href="https://github.com/changgyhub/leetcode_101">LeetCode 101 — A Grinding Guide</a>. The structure of the book is clear and systematic, making it easier to see the thought process behind each problem. While some parts (like function headers) may be a little outdated, it’s still a fantastic learning resource.</p><p>So, let’s dive into the very first problem from the book and break it down step by step.</p><h3>1.1 Greed Alogorithim (Greedy Strategy)</h3><p>As the name suggests, a <strong>Greedy Algorithm</strong> takes the best possible choice at each step (a local optimum) in the hope of reaching the global optimum.</p><p>A simple analogy:</p><ul><li>Xiao Ming can eat 5 apples, and Xiao Wang can eat 3 apples.</li><li>The orchard has an unlimited supply.</li><li>The greedy solution is simply: let each eat as much as they can.</li></ul><p>Since their choices are independent, the global solution is just the sum of local optima — 8 apples in total.</p><p>Greedy strategies usually apply when:</p><ul><li>You can clearly see a local-to-global relationship, or</li><li>Mathematical induction suggests that local choices lead to the best global outcome.</li></ul><h3>1.2 Distribution Problem</h3><h3>Problem Statement — What Does the Problem Say?</h3><p>There are a group of children and a pile of cookies. Each child has a <em>greed factor</em> (hunger level), and each cookie has a <em>size</em> (satiation value). Each child can only eat <strong>one</strong> cookie, and a child can only be satisfied if the cookie’s size is greater than or equal to the child’s greed factor.</p><p>The task: Find out the maximum number of children that can be satisfied.</p><p>Example 1:</p><pre>Input: g = [1,2,3], s = [1,1]<br>Output: 1<br>Explanation: You have 3 children and 2 cookies. The greed factors of 3 children are 1, 2, 3. <br>And even though you have 2 cookies, since their size is both 1, you could only make the child whose greed factor is 1 content.<br>You need to output 1.</pre><p>Example 2:</p><pre>Input: g = [1,2], s = [1,2,3]<br>Output: 2<br>Explanation: You have 2 children and 3 cookies. The greed factors of 2 children are 1, 2. <br>You have 3 cookies and their sizes are big enough to gratify all of the children, <br>You need to output 2.</pre><p>So the challenge is: How do we maximize the number of content children using the available cookies?</p><h3>Intuition and Approach (Optimal — Greedy)</h3><p>The key idea here is:</p><ul><li>A smaller cookie should be given to the least greedy child (the one with the smallest greed factor).</li><li>If we waste a big cookie on a child who only needs a small one, we might not have enough cookies left for greedier children.</li></ul><h3>Solution Idea:</h3><ul><li>The key insight:<br> 👉 <strong>Give the smallest possible cookie that can satisfy the least greedy child.</strong></li></ul><p>Why?</p><ul><li>The least greedy child is easiest to satisfy.</li><li>If you waste a big cookie on them, you might not have enough for greedier children later.</li></ul><p>So the process becomes:</p><ol><li>Sort both the greed factors and cookie sizes.</li><li>Start from the smallest child and smallest cookie.</li><li>If the cookie satisfies the child → assign it.</li><li>Otherwise, try the next bigger cookie.</li><li>Repeat until you run out of cookies or children.</li></ol><p><strong>Implementation Note</strong><br> Since comparisons rely on ordering, a convenient way is to <strong>sort both children’s greed factors and cookie sizes</strong>.</p><ul><li>Start from the smallest child and the smallest cookie.</li><li>Count how many matches satisfy the condition.</li></ul><p>⚠️ Note: Sorting arrays or strings is very common for greedy solutions. Unless otherwise stated, sorting is in ascending order.<br> Also, when dealing with variables in a continuous space, we won’t strictly distinguish between arrays and strings (since strings can also be seen as ordered collections). For example, the string &quot;abc&quot; can be treated as the array [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;].</p><h3>Python Implentation</h3><pre>class Solution:<br>    def findContentChildren(self, g: List[int], s: List[int]) -&gt; int:<br>        n = len(g)<br>        m = len(s)<br>        <br>        # Sort cookie size and greed<br>        g.sort()<br>        s.sort()<br><br>        left = 0 # Pointer for Children<br>        right = 0 # Pointer for Cookies<br>        count = 0 # Number of satisfied children<br><br>        while left &lt; n and right &lt; m: # To prevent out of index bound<br>            if g[left] &lt;= s[right]: # If Cookie satisfies the children greed<br>                count += 1 # Children is content<br>                left += 1 # Move to the next child<br>            right += 1 # Move to the next cookie<br>        <br>        return count # Return how many children is content</pre><h3>5. Key Takeaways ✨</h3><ul><li>Greedy algorithms work best when local choices lead to the global solution.</li><li>Sorting simplifies comparisons and ensures we always allocate resources efficiently.</li></ul><p>In this problem:</p><ul><li>Children = demand side.</li><li>Cookies = supply side.</li><li>Match the smallest available supply to the smallest demand first.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2787d9fa4df9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[42 Piscine — How to tackle C00(EX02)]]></title>
            <link>https://medium.com/@axellee1994/42-piscine-how-to-tackle-c00-ex02-7dbd2d1c2db9?source=rss-21622b47f8f4------2</link>
            <guid isPermaLink="false">https://medium.com/p/7dbd2d1c2db9</guid>
            <category><![CDATA[ecole-42]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[42-piscine]]></category>
            <category><![CDATA[c-programming]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Axel Lee]]></dc:creator>
            <pubDate>Thu, 12 Dec 2024 07:21:01 GMT</pubDate>
            <atom:updated>2024-12-12T07:21:01.792Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*TYsogmIhc7pZXXlQooj9kw.png" /></figure><h3>42 Piscine — How to tackle C00(EX02)</h3><h4>Print the Alphabet in Reverse— z to a</h4><p>Leading on from ft_print_alphabet, the idea is to write a function that prints the lowercase of A-Z <strong>IN REVERSE</strong>. Since we know how to print the alphabets already, all we need to do is simply:</p><h3>Pseudo-Code</h3><p>To solve this exercise properly:</p><ol><li><strong>Understand the Task:</strong><br>I will need to print all lowercase letters (a to z) in reverse.</li><li><strong>Plan the Logic:</strong></li></ol><ul><li>Use a loop to iterate through the ASCII values of lowercase letters (122 to 97).</li><li>Print each character using the function ft_putchar you wrote earlier.</li><li>Every time we print a character, we shift down the ASCII table by one digit. 122 -&gt; 121 -&gt; 120 etc.</li></ul><pre>1. Include &lt;unistd.h&gt; for the write function.<br><br>2. Define a function `ft_print_reverse_alphabet` with a `void` return type:<br>   //This function will print the lowercase alphabet.<br><br>3. Declare a variable `char letter` to hold the current character.<br><br>4. Initialize `letter` to 122(&#39;z&#39;).<br><br>5. Create a `while` loop:<br>   //Loop while `letter` is more than or equal to 97(&#39;a&#39;).<br><br>6. Inside the loop:<br>   //Print the current character using `write` or `ft_putchar`.<br>   //Decrement `letter` to move to the next character.<br><br>7. Close the function `ft_print_reverse_alphabet`.<br><br>8. Write the `main` function:<br>   //Call `ft_print_reverse_alphabet()` to test your function.<br>   //Return 0 to indicate successful execution.</pre><h4>How does the code function:</h4><pre>First Iteration:<br><br>    letter = &#39;z&#39; → Condition is true.<br>    write(1, &amp;letter, 1) prints &#39;z&#39;.<br>    letter-- makes letter = &#39;y&#39;.<br><br>Output So Far: z<br><br>Second Iteration:<br><br>    letter = &#39;y&#39; → Condition is true.<br>    write(1, &amp;letter, 1) prints &#39;y&#39;.<br>    letter-- makes letter = &#39;x&#39;.<br><br>Output So Far: zy<br><br>Subsequent Iterations:<br><br>    This continues, printing each letter and decrementing letter.<br><br>Examples:<br><br>    &#39;x&#39; → &#39;w&#39; → &#39;v&#39; → ... → &#39;b&#39;<br><br>Output So Far: zyxwvutsrqponmlkjihgfedcb<br><br>Final Iteration:<br><br>    letter = &#39;a&#39; → Condition is true.<br>    write(1, &amp;letter, 1) prints &#39;a&#39;.<br>    letter-- makes letter = &#39;&#39;` (ASCII 96).<br><br>Output So Far: zyxwvutsrqponmlkjihgfedcba<br><br>Exit Loop:<br><br>    letter = &#39;&#39;→ Conditionletter &gt;= &#39;a&#39;` is false.<br>    The loop exits.</pre><p>So now after writing the code, how do we compile it?</p><p><strong>To compile:</strong></p><ul><li><strong>Write the Code</strong>: Write the ft_print_reverse_alphabet.c file with your function.</li><li><strong>Compile</strong>: You’ll compile it using a C compiler (like gcc):</li></ul><pre>gcc -Wall -Werror -Wextra ft_print_alphabet.c -o ft_print_alphabet</pre><ul><li>-Wall: Enables all warnings.</li><li>-Werror: Treats warnings as errors.</li><li>-Wextra: Enables additional warnings.</li><li>ft_putchar.c: Input source file.</li><li>-o ft_putchar: Output executable named ft_putchar.</li></ul><p>And to run the program:</p><pre>./ft_print_reverse_alphabet</pre><p><strong>Final Output:</strong></p><pre>zyxwvutsrqponmlkjihgfedcba</pre><p>And that is basically it! Congratulations! You have cleared C00-EX02! Make sure to comment out the main before submitting.</p><h3>Key Takeaways:</h3><p><strong>Loops</strong>:</p><p>Use loops to automate repetitive tasks, such as iterating through a sequence (e.g., printing characters from &#39;z&#39; to &#39;a&#39; in reverse).</p><ul><li>They improve scalability and make your code cleaner and easier to manage.</li></ul><p><strong>ASCII Table</strong>:</p><ul><li>Understand ASCII values for characters (&#39;a&#39; = 97, &#39;z&#39; = 122) to work efficiently with character sequences.</li><li>Using ASCII ranges can help simplify conditions in loops.</li></ul><p><strong>Reusable Functions</strong>:</p><ul><li>Write small, focused functions (like ft_print_reverse_alphabet) to encapsulate specific tasks.</li><li>This makes your main program concise and easier to debug or extend.</li></ul><h4>Closing Note</h4><p>Thank you for reading! Feel free to check out my other projects on GitHub:<br><a href="https://github.com/axellee1994">GitHub: axellee1994</a></p><p>Till next time, always remember: <strong>RTFM — READ THE F***KING MANUAL</strong>🚀</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7dbd2d1c2db9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[42 Piscine — How to tackle C00(EX01)]]></title>
            <link>https://medium.com/@axellee1994/42-piscine-how-to-tackle-c00-ex01-57e9bdcd6138?source=rss-21622b47f8f4------2</link>
            <guid isPermaLink="false">https://medium.com/p/57e9bdcd6138</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[42-piscine]]></category>
            <category><![CDATA[c-programming]]></category>
            <category><![CDATA[ecole-42]]></category>
            <dc:creator><![CDATA[Axel Lee]]></dc:creator>
            <pubDate>Fri, 06 Dec 2024 03:18:22 GMT</pubDate>
            <atom:updated>2024-12-06T03:18:22.679Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*HGUO61QqHrP2_rkBdul9lQ.png" /></figure><h3>42 Piscine — How to tackle C00(EX01)</h3><h4>Print the Alphabet — a to z</h4><p>So for the next exercise, it is relatively simple. The idea is to write a function that prints the lowercase of A-Z. After learning ft_putchar from the previous exercise, you might have an indication of writing it like this:</p><pre>int main()<br>{<br>  write(1,&quot;abcdefghijklmnopqrstuvexyz&quot;,26);<br>  return (0);<br>}</pre><p>While this technically works for printing lowercase letters, it is:</p><ol><li><strong>Not scalable</strong>: If requirements change (e.g., print uppercase letters or include special characters), you’d need to rewrite the string.</li><li><strong>Not acceptable for the 42 curriculum</strong>: The exercise is meant to teach loops and ASCII manipulation.</li></ol><p>So without further ado, here is how I tackled the problem:</p><h3>WHAT IS ASCII TABLE?</h3><p>To be able to print the characters needed for this exercise, you will need to know the fundamentals of the ASCII table. You might be asking, what is an ASCII table?</p><p>The ASCII table is a chart that maps characters (like letters, numbers, and symbols) to specific numbers. Computers only understand numbers, so ASCII translates characters into numerical values.</p><h4>For example:</h4><pre>A = 65<br>a = 97<br>3 = 51<br>Space ( ) = 32</pre><p>So to look what is the character we would like to print, you will need to look for the Decimal number of it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*4AJUzsCKFDbPeGWH.png" /><figcaption>ASCII Table</figcaption></figure><h4>How it’s Organized:</h4><ul><li><strong>0–31</strong>: Special control characters (e.g., newline, tab).</li><li><strong>32–47</strong>: Punctuation and symbols (e.g., space, !, @).</li><li><strong>48–57</strong>: Numbers (0 to 9).</li><li><strong>65–90</strong>: Uppercase letters (A to Z).</li><li><strong>97–122</strong>: Lowercase letters (a to z).</li></ul><h4>How Computers Understand Characters:</h4><ul><li>When you press the letter A on your keyboard, the computer sees the number 65.</li><li>When you print the number 65 using ASCII, it shows the letter A.</li></ul><p>Now that we know how to look for the characters in the ASCII Table, let’s head to the pseudo coding section.</p><h3>Pseudo-Code</h3><p>To solve this exercise properly:</p><ol><li><strong>Understand the Task:</strong><br>I will need to print all lowercase letters (a to z).</li><li><strong>Plan the Logic:</strong></li></ol><ul><li>Use a loop to iterate through the ASCII values of lowercase letters (97 to 122).</li><li>Print each character using the function ft_putchar you wrote earlier.</li></ul><pre>1. Include &lt;unistd.h&gt; for the write function.<br><br>2. Define a function `ft_print_alphabet` with a `void` return type:<br>   //This function will print the lowercase alphabet.<br><br>3. Declare a variable `char letter` to hold the current character.<br><br>4. Initialize `letter` to 97 (&#39;a&#39;).<br><br>5. Create a `while` loop:<br>   //Loop while `letter` is less than or equal to 122 (&#39;z&#39;).<br><br>6. Inside the loop:<br>   //Print the current character using `write` or `ft_putchar`.<br>   //Increment `letter` to move to the next character.<br><br>7. Close the function `ft_print_alphabet`.<br><br>8. Write the `main` function:<br>   //Call `ft_print_alphabet()` to test your function.<br>   //Return 0 to indicate successful execution.</pre><p>So now after writing the code, how do we compile it?</p><p><strong>To compile:</strong></p><ul><li><strong>Write the Code</strong>: Write the ft_print_alphabet.c file with your function.</li><li><strong>Compile</strong>: You’ll compile it using a C compiler (like gcc):</li></ul><pre>gcc -Wall -Werror -Wextra ft_print_alphabet.c -o ft_print_alphabet</pre><ul><li>-Wall: Enables all warnings.</li><li>-Werror: Treats warnings as errors.</li><li>-Wextra: Enables additional warnings.</li><li>ft_putchar.c: Input source file.</li><li>-o ft_putchar: Output executable named ft_putchar.</li></ul><p>And to run the program:</p><pre>./ft_print_alphabet</pre><p>And that is basically it! Congratulations! You have cleared C00-EX01! Make sure to comment out the main before submitting.</p><h3>Key Takeaways:</h3><ol><li><strong>Loops:</strong> Enable scalability by automating repetitive tasks (e.g., printing a series of characters).</li><li><strong>ASCII Table:</strong> Understand ASCII values for characters and use them in your code.</li><li><strong>Reusable Functions:</strong> Use small, focused functions like ft_putchar to simplify your main code.</li></ol><p>Thank you for reading! If you would like to give me a follow, here the link to my Github:</p><p><a href="https://github.com/axellee1994">https://github.com/axellee1994</a></p><p>Till then, always remember: <strong>RTFM — READ THE F***KING MANUAL</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=57e9bdcd6138" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[42 Piscine — How to tackle C00(EX00)]]></title>
            <link>https://medium.com/@axellee1994/42-piscine-how-to-tackle-c00-ex00-dc35d49994d3?source=rss-21622b47f8f4------2</link>
            <guid isPermaLink="false">https://medium.com/p/dc35d49994d3</guid>
            <category><![CDATA[42-piscine]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[c-programming]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Axel Lee]]></dc:creator>
            <pubDate>Thu, 05 Dec 2024 08:03:39 GMT</pubDate>
            <atom:updated>2024-12-06T03:15:04.774Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*BMD0EQVaQtSww94EmwG1sQ.png" /></figure><h3>42 Piscine — How to tackle C00(EX00)</h3><h4>What is ft_putchar.c ?</h4><p>Looking back at my progress in 42Singapore, it has already been 18 months since I last transitioned from Architecture to Coding. Having been part of the pioneer batch for 42 Singapore Piscine and Core, I figured it would be a good time for me to look back at the progress I made and how I tackled the problems during Piscine.</p><p>So in this article, we will be discussing the code on how to pass C00-Ex00 for the 42 Piscine. The article will be written in pseudo-code to explain how to tackle the question</p><h4>Topic:</h4><p>Print a Character</p><ul><li>You are creating a function to write a character when called upon in the main using — ft_putchar(‘ ‘); <br> any character in the ‘ ‘ will be written when the main runs.</li></ul><pre>#include &lt;unistd.h&gt; // allows the write function from the C library <br><br>void ft_putchar(char c) // creating a function to output characters<br>{ // This is the start of the function<br>   write(1, &amp;c, 1); // this allows for a character to be written with &amp;c<br>} // This is the end of the function<br>/* Must comment the main, unless compiling<br>int main(void)  // starts the main<br>{ <br> ft_putchar(&#39;a&#39;); // This writes the character &#39;a&#39;<br>} <br>*/ // Must add a comment at the end to close it</pre><p>For those, who started off without coding knowledge like myself, this will be a mind-boggling question. Essentially, how this works is that we have a function, what the function is doing and a main.</p><h4>Function:</h4><ul><li>A <strong>function</strong> is like a small machine that performs a specific task. In your case, the function we created is called ft_putchar, and its job is to print a single character when called.</li><li>Functions help us <strong>organize</strong> code, <strong>reuse</strong> it, and <strong>simplify</strong> complex tasks. For example, rather than writing the same code to print a character every time, we can just call ft_putchar(&#39;a&#39;); wherever we need to print &#39;a&#39;.</li></ul><h4>The write() System Call:</h4><p>The write() function is used to send output to the screen (or any other output destination).</p><ul><li>1 is the file descriptor for standard output, meaning &quot;the screen&quot; in most cases.</li><li>&amp;c is the <strong>address</strong> of the character variable c. We use &amp;c because write() requires the memory location of the data (not the actual value).</li><li>1 specifies how many bytes to write. Since we&#39;re printing only one character, we tell write() to write 1 byte.</li></ul><h4>Main:</h4><ul><li>The <strong>main function</strong> is where the program starts running. This is the entry point of your program.</li><li>Inside main, we <strong>call</strong> the function ft_putchar(&#39;a&#39;). This will trigger the function to write the character &#39;a&#39; on the screen.</li><li>After calling the function, we use return (0); to indicate that the program ran successfully.</li></ul><h4>Pseudo Code Explanation:</h4><pre>// Include the necessary library for system calls<br>Include &lt;unistd.h&gt;<br><br>// Start of the program<br>Begin main<br><br>   // Call the function to print a character<br>   Call ft_putchar(&#39;a&#39;)<br><br>   // End the program successfully<br>   Return 0<br><br>End main</pre><p>So now after writing the code, how do we compile it?</p><p><strong>To compile:</strong></p><ul><li><strong>Write the Code</strong>: Write the ft_putchar.c file with your function.</li><li><strong>Compile</strong>: You’ll compile it using a C compiler (like gcc):</li></ul><pre>gcc -Wall -Werror -Wextra ft_putchar.c -o ft_putchar</pre><ul><li>-Wall: Enables all warnings.</li><li>-Werror: Treats warnings as errors.</li><li>-Wextra: Enables additional warnings.</li><li>ft_putchar.c: Input source file.</li><li>-o ft_putchar: Output executable named ft_putchar.</li></ul><p>And to run the program:</p><pre>./ft_putchar</pre><p>And that is basically it! Congratulations! You have cleared C00-EX01! Make sure to comment out the main before submitting.</p><h3>Key Concepts to Remember</h3><ul><li><strong>Functions</strong>: Used to organize and simplify your code. You can create them to do a specific task, like printing characters.</li><li><strong>Main Function</strong>: The starting point of your program.</li><li><strong>System Calls</strong>: write() allows you to send output to the screen.</li><li><strong>Compile and Run</strong>: After writing your code, you need to compile it into an executable and then run it.</li></ul><p>By breaking it down like this, it becomes clear that the main task here is to call the function that writes a character to the screen.</p><p>Thank you for reading! If you would like to give me a follow, here the link to my Github:</p><p><a href="https://github.com/axellee1994">https://github.com/axellee1994</a></p><p>Till then, always remember: <strong>RTFM — READ THE F***KING MANUAL</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dc35d49994d3" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>