<?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>Forem: Papan Sarkar</title>
    <description>The latest articles on Forem by Papan Sarkar (@papansarkar101).</description>
    <link>https://forem.com/papansarkar101</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%2F484187%2F52354a7a-52d7-417c-87f5-8c7e9bcd8083.png</url>
      <title>Forem: Papan Sarkar</title>
      <link>https://forem.com/papansarkar101</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/papansarkar101"/>
    <language>en</language>
    <item>
      <title>Prototyping just went from "Hours" to "Minutes." ⚡️</title>
      <dc:creator>Papan Sarkar</dc:creator>
      <pubDate>Fri, 05 Dec 2025 17:19:32 +0000</pubDate>
      <link>https://forem.com/papansarkar101/prototyping-just-went-from-hours-to-minutes-2e72</link>
      <guid>https://forem.com/papansarkar101/prototyping-just-went-from-hours-to-minutes-2e72</guid>
      <description>&lt;p&gt;I remember when setting up a clean, responsive dashboard meant wrestling with boilerplate code for half a day.&lt;/p&gt;

&lt;p&gt;Today, I challenged Google AI Studio to build a fully functional Personal Finance Dashboard using React, Tailwind CSS, and Recharts.&lt;/p&gt;

&lt;p&gt;The result? A stunning, dark-mode UI with "Bento Grid" layout and glassmorphism effects—generated in under 120 seconds. 🤯&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmd5cy9ctj54g3obj21bc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmd5cy9ctj54g3obj21bc.png" alt="Dashboard UI" width="800" height="397"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Why this matters for developers: It's not about AI replacing us. It's about "Velocity to V1." Instead of spending 4 hours on the UI shell, you spend 2 minutes generating it, and the next 3 hours and 58 minutes on the actual logic and backend architecture.&lt;/p&gt;

&lt;p&gt;The Reality Check: Yes, this is a frontend prototype. It still needs a secure backend, auth, and database. But the visual heavy lifting? Done.&lt;/p&gt;

&lt;p&gt;I shared the full breakdown and code in my latest YouTube Short.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fygr0aqam94khscp7zvq2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fygr0aqam94khscp7zvq2.png" alt="Prompt" width="480" height="208"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What you see here is a Frontend Prototype (the interface). It looks great, but to turn this into a real, working app, you still need: A Backend (Logic to handle transactions). A Database (To actually save user data). Authentication (Secure login/signup). &lt;/p&gt;

&lt;p&gt;The Reality: AI is an amazing tool to speed up the design and boilerplate code, but you still need to understand programming to connect the pieces and make it secure! Use this to prototype fast, not to skip learning the basics.&lt;/p&gt;

&lt;p&gt;🚀 Need to turn your idea into a working product fast? Check out how we build high-performance MVPs at &lt;a href="https://yestoideas.com/" rel="noopener noreferrer"&gt;Yestoideas&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>webdev</category>
      <category>programming</category>
      <category>productivity</category>
    </item>
    <item>
      <title>🚀 Supabase Connection Scaling: The Essential Guide for FastAPI Developers</title>
      <dc:creator>Papan Sarkar</dc:creator>
      <pubDate>Fri, 14 Nov 2025 18:24:55 +0000</pubDate>
      <link>https://forem.com/papansarkar101/supabase-connection-scaling-the-essential-guide-for-fastapi-developers-348o</link>
      <guid>https://forem.com/papansarkar101/supabase-connection-scaling-the-essential-guide-for-fastapi-developers-348o</guid>
      <description>&lt;p&gt;Building a scalable FastAPI backend requires mastering database connections. When using Supabase (PostgreSQL), choosing the wrong connection method is the fastest way to crash your server under load. This guide clarifies the critical difference between Transaction Mode and Session Mode to help you choose the right path for high performance.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9xzntn4uqgj6ddebnkqh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9xzntn4uqgj6ddebnkqh.png" alt=" " width="800" height="446"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  🔒 The Connection Pooler: Your Database’s Gatekeeper
&lt;/h2&gt;

&lt;p&gt;Every Supabase project includes a Connection Pooler (Supavisor/PgBouncer). Its job is to efficiently manage traffic between your many client applications and your limited database resources.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The “Lobby” (Max Client Connections): This is the high limit (e.g., 200 on Nano). It’s the total number of clients that can connect to the pooler.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The “Tellers” (Pool Size / Backend Connections): This is the low, resource-intensive limit (e.g., 15–20 on Nano). It’s the number of actual PostgreSQL processes available to run queries.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The core challenge is enabling hundreds of clients in the Lobby to efficiently share just 20 Tellers.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Transaction Mode: Lightning-Fast Exchange (Port 6543) ⚡
&lt;/h2&gt;

&lt;p&gt;Transaction Mode is the mandatory choice for scalable, high-concurrency web APIs.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How it Works: A client (your FastAPI endpoint) borrows a Teller only for the duration of a single transaction or query. The moment the transaction commits, the Teller is immediately released back to the pool for the next waiting client.&lt;/li&gt;
&lt;li&gt;    Analogy: The Express Lane at a bank. You quickly finish your one task, and the Teller is free instantly.&lt;/li&gt;
&lt;li&gt;    Advantages: Maximizes throughput and concurrency. A few connections can handle a high volume of requests because no connection is ever held idle.&lt;/li&gt;
&lt;li&gt;    When to Use: Default mode for all general-purpose web traffic, serverless functions, and short-lived tasks.&lt;/li&gt;
&lt;li&gt;    Supabase Port: 6543.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  2. Session Mode: The Dedicated Teller (Port 5432) 🔑
&lt;/h2&gt;

&lt;p&gt;Session Mode is a stateful mode designed for compatibility and long-running tasks.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How it Works: A client connection permanently holds a specific backend connection (Teller) for the entire duration of the client’s session, even when the client is idle. The connection is only returned when the client disconnects.&lt;/li&gt;
&lt;li&gt;    Analogy: The Personal Banker. You keep the banker until your entire long meeting is over, making them unavailable to others.&lt;/li&gt;
&lt;li&gt;    Advantages: Provides Full PostgreSQL Compatibility for features that require persistent state.&lt;/li&gt;
&lt;li&gt;    When to Use: Database migrations, admin tools, debugging, or complex background jobs that rely on setting session variables (SET TIME ZONE) or using temporary tables.&lt;/li&gt;
&lt;li&gt;    Supabase Port: 5432 (the default PostgreSQL port).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  🛑 The Critical Pitfall: Running FastAPI in Session Mode
&lt;/h2&gt;

&lt;p&gt;If your FastAPI backend connects on Port 5432, your entire application operates in Session Mode, leading to three major stability risks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Rapid Limit Exhaustion: Your application effectively ignores the 200 client limit and is capped by the 20 Pool Size. If 21 instances or users connect, the 21st is blocked, even if the first 20 are idle.&lt;/li&gt;
&lt;li&gt;    Memory Overload: Holding 20+ persistent connections on a small instance (like Nano) quickly exhausts the server’s limited RAM, forcing the database into a read-only state to prevent a crash.&lt;/li&gt;
&lt;li&gt;    Connection Leakage: Any connection not cleanly closed by FastAPI will remain stuck, eventually using up all 20 backend connections permanently.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  🛠️ The Essential Code Fix: Disable Prepared Statements
&lt;/h2&gt;

&lt;p&gt;The migration to Transaction Mode (Port 6543) requires a vital code change because it breaks a common optimization feature: Prepared Statements.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;    The Conflict: Prepared statements are an efficiency feature that involves storing a pre-compiled query plan on a specific session (backend connection). When Transaction Mode swaps the connection, the new connection cannot find the stored plan, causing the frequent and fatal prepared statement “…” does not exist error.&lt;/li&gt;
&lt;li&gt;    The Solution: You must configure your Python database driver or ORM (like SQLAlchemy with asyncpg) to explicitly disable prepared statements by setting the cache size to zero.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Python/FastAPI Configuration (SQLAlchemy + Asyncpg)&lt;/p&gt;

&lt;p&gt;This code snippet is the necessary modification when pointing your FastAPI backend to Port 6543.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from sqlalchemy.ext.asyncio import create_async_engine
from sqlalchemy.pool import NullPool 

# ⚠️ Change port to 6543
DATABASE_URL = "postgresql+asyncpg://user:pass@host.supabase.co:6543/postgres" 

engine = create_async_engine(
    DATABASE_URL,
    # Use NullPool: Let the external pooler (Supavisor) manage connections, not the app.
    poolclass=NullPool, 
    connect_args={
        # CRITICAL: Disables statement caching, fixing the Transaction Mode conflict
        "statement_cache_size": 0, 

        # Ensures asyncpg also respects the cache being disabled
        "prepared_statement_cache_size": 0, 
    },
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note: If using the pure psycopg3 driver, you would typically pass prepare_threshold=None to the connection pool configuration.&lt;br&gt;
Conclusion&lt;/p&gt;

&lt;p&gt;For a scalable FastAPI application on Supabase, always start with Transaction Mode (Port 6543) as your default for all API traffic. This choice maximizes concurrency and efficiency. Reserve Session Mode (Port 5432) only for specialized, stateful tasks like migrations or admin scripts. The main takeaway: Enable Port 6543 and disable prepared statements to unlock optimal scalability and prevent stability issues.&lt;/p&gt;

</description>
      <category>fastapi</category>
      <category>python</category>
      <category>supabase</category>
      <category>postgres</category>
    </item>
    <item>
      <title>Dynamic Serializers in Django</title>
      <dc:creator>Papan Sarkar</dc:creator>
      <pubDate>Wed, 17 Apr 2024 18:24:08 +0000</pubDate>
      <link>https://forem.com/papansarkar101/dynamic-serializers-in-django-e7b</link>
      <guid>https://forem.com/papansarkar101/dynamic-serializers-in-django-e7b</guid>
      <description>&lt;p&gt;Dynamic serializers are extremely useful to make flexible APIs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why do we need dynamic serializers&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Flexibility&lt;/strong&gt;: It provides flexibility. We often run into issues, where we need a serializer but not all the fields from that serializer. In that case dynamic serializers are especially useful to use subsets of data. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Reusability&lt;/strong&gt;: No need to write multiple serializers anymore, a single dynamic serializer can achieve various needs. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Performance&lt;/strong&gt;: If we only pick the fields we need, it will immensely improve the overall performance. &lt;/p&gt;

&lt;p&gt;While it has good advantages, it comes with few complexities as well. It can make debugging a little difficult and we can come across with errors if we do not follow good practices and test all kind of edge cases. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Implementation&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class DynamicSerializer(ModelSerializer): 

    """
    A ModelSerializer that takes an additional 'fields' 
    argument that controls which fields should be displayed. 
    """


    def __init__(self, *args, **kwargs): 

        fields = kwargs.pop('fields', None) 

        # Instantiate the superclass normally 

        super(DynamicFieldsModelSerializer, self).__init__(*args, **kwargs) 

        if fields is not None: 

            # Drop any fields that are not specified in the `fields` argument. 

            allowed = set(fields) 
            existing = set(self.fields.keys()) 
            for field_name in existing - allowed: 
                self.fields.pop(field_name)


# Example usage in a view 
class UserSerializer(DynamicFieldsModelSerializer): 
    class Meta: 
        model = User 
        fields = '__all__' `


# In your view 
serializer = UserSerializer(user, fields=('id', 'username', 'email')) 

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

&lt;/div&gt;



&lt;p&gt;Conclusion: &lt;/p&gt;

&lt;p&gt;Dynamic serializer is powerful tool in Django, especially for large and complex projects.  &lt;/p&gt;

&lt;p&gt;Proper implementation and thorough testing are important to achieve good and optimised results.  &lt;/p&gt;

</description>
      <category>django</category>
      <category>webdev</category>
      <category>programmers</category>
      <category>developers</category>
    </item>
    <item>
      <title>Don't write queries inside serializers in Django!</title>
      <dc:creator>Papan Sarkar</dc:creator>
      <pubDate>Sat, 13 Apr 2024 14:36:06 +0000</pubDate>
      <link>https://forem.com/papansarkar101/dont-write-queries-inside-serializers-in-django-5d25</link>
      <guid>https://forem.com/papansarkar101/dont-write-queries-inside-serializers-in-django-5d25</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7nmsuy89phhworxbdjdc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7nmsuy89phhworxbdjdc.png" alt="Don't Do it" width="498" height="280"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Why?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;It should focus data conversion and validations only. Don't write database logic on this.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Not efficient and bad performance. It will create lots of queries&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You can not reuse it&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Very hard to test&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Readability is gone&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I can share the alternatives if you want! Let me know&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Do's and Don'ts in Django</title>
      <dc:creator>Papan Sarkar</dc:creator>
      <pubDate>Fri, 12 Apr 2024 17:34:42 +0000</pubDate>
      <link>https://forem.com/papansarkar101/dos-and-donts-in-django-59ii</link>
      <guid>https://forem.com/papansarkar101/dos-and-donts-in-django-59ii</guid>
      <description>&lt;h2&gt;
  
  
  Do’s
&lt;/h2&gt;

&lt;p&gt;Use meaningful variables: We should use clear, concise, and self-explanatory variables, functions, and classes name. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DRY:&lt;/strong&gt; Don’t Repeat Yourself. We should reuse code as much as possible. Once you are done with the code, take 10 minutes to think what the possibilities are to improve it further and make it optimal. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use Django features:&lt;/strong&gt; On daily basis, we receive unique requirements which is consist of unique business logic for those cases we often write flexible code to solve a problem, which is fine but again it might not be the optimal solution. So again, take 10 minutes extra to research how could you have done this differently keeping a standard approach and solution.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quick related note:&lt;/strong&gt; It is very normal to think of a custom solution first, however it helps or comes to your head. No one solves a problem in the first go with the best possible solution and standards. It is always the next set of iteration which makes the difference. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Do a security check:&lt;/strong&gt; Always do a security check once you are done with a task.  Think of edge cases which can eventually create a problem. If needed get on a call with someone and share your thoughts and understanding, you will get more clarity with another set of eyes. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Follow PEP8:&lt;/strong&gt; Since so many people are working on the same code, and we are trying to make it more readable and understandable. It is especially important that we make use PEP 8 style guide for Python code. &lt;/p&gt;

&lt;h2&gt;
  
  
  Don’ts
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Do not write Complex Queries on serializers:&lt;/strong&gt; Avoid writing complex queries on serializers or any serializer methods. Prefetch data in views instead. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Don’t write generic views all the time:&lt;/strong&gt; Generic views are super useful and powerful but do not use it when it is not required. If the requirement is simple and straightforward write Functional views instead. &lt;/p&gt;

&lt;p&gt;**Don’t keep unlimited access to endpoints (Rate Limiting): **Implement rate limiting to protect APIs.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Don’t write nested serializers:&lt;/strong&gt; Nested serializers can produce inefficient queries and effect performance. Create different endpoints for these cases or change the data structure. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Don’t write business logic inside views or serializers:&lt;/strong&gt; It is one of most important things to consider when building a huge project consists of lots of business logic. Business logic should be written in service layer, model property methods etc. &lt;/p&gt;

</description>
      <category>django</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Take a Ten Minute Walk: A Codewars kata solution By Papan Sarkar</title>
      <dc:creator>Papan Sarkar</dc:creator>
      <pubDate>Wed, 07 Oct 2020 08:41:24 +0000</pubDate>
      <link>https://forem.com/papansarkar101/take-a-ten-minute-walk-a-codewars-kata-solution-by-papan-sarkar-4mf0</link>
      <guid>https://forem.com/papansarkar101/take-a-ten-minute-walk-a-codewars-kata-solution-by-papan-sarkar-4mf0</guid>
      <description>&lt;p&gt;Take a Ten Minute Walk: A Codewars kata solution &lt;br&gt;
In this article, I'll explain a kata solution from Codewars. This kata(problem) is authored by JKphobic. And here is the kata &lt;br&gt;
link:&lt;a href="https://www.codewars.com/kata/54da539698b8a2ad76000228/train/python"&gt;https://www.codewars.com/kata/54da539698b8a2ad76000228/train/python&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Problem:&lt;br&gt;
You live in the city of Cartesian where all roads are laid out in a perfect grid. You arrived ten minutes too early to an appointment, so you decided to take the opportunity to go for a short walk. The city provides its citizens with a Walk Generating App on their phones -- every time you press the button it sends you an array of one-letter strings representing directions to walk (eg. ['n', 's', 'w', 'e']). You always walk only a single block for each letter (direction) and you know it takes you one minute to traverse one city block, so create a function that will return true if the walk the app gives you will take you exactly ten minutes (you don't want to be early or late!) and will, of course, return you to your starting point. Return false otherwise.&lt;/p&gt;

&lt;p&gt;Note: you will always receive a valid array containing a random assortment of direction letters ('n', 's', 'e', or 'w' only). It will never give you an empty array (that's not a walk, that's standing still!).&lt;/p&gt;

&lt;p&gt;First of all, we need to understand the problem. Then we will see what it's input and of course what kind of output it wants from us.&lt;/p&gt;

&lt;p&gt;If you read the problem twice or thrice, you'll see a few things like what things we need to keep in our minds. Like :&lt;/p&gt;

&lt;p&gt;The walk has to be for 10 minutes.&lt;br&gt;
You need to return to the starting point &lt;br&gt;
Every list item (each letter) or single block represents one minute &lt;/p&gt;

&lt;p&gt;Let's convert these instructions into code and make small segments of the whole solution code:&lt;br&gt;
So,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if len(walk) == 10 //True
else // False
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We'll create two variable and initialize to 0 to track our walk, ns (North-South) and ew (East-West) -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ns = 0
ew = 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For every single block we walk, respectively we'll increment and decrement with 1&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if we move in n direction then ns += 1
if we move in s direction (coming back) then ns -= 1
if we move in e direction then ew += 1
if we move in w direction (coming back) then ew -= 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And then finally we will check we returned to our same position or not&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if ns == 0 and ew == 0 //True
else //False
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's combine all this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Python Solution
def is_valid_walk(walk):
    ns, ew = 0, 0
    if len(walk) == 10:
        for i in walk:
            if i == 'n': ns+=1
            if i == 's': ns-=1
            if i == 'w': ew+=1
            if i == 'e': ew-=1
    else:
        return False
    return ns == 0 and ew == 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Javascript solution
function isValidWalk(walk) {
    let ns = 0, ew = 0; 
    if(walk.length === 10){
      for (let i of walk) { 
        if (i == 'n') ns += 1; 
        if (i == 's') ns -= 1; 
        if (i == 'e') ew += 1; 
        if (i == 'w') ew -= 1; 
      } 
    }
    else
      return false
      return ns === 0 &amp;amp;&amp;amp; ew === 0; 
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now it's time to some other best and clever solutions:&lt;br&gt;
Most upvoted:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def isValidWalk(walk):
    return len(walk) == 10 and walk.count('n') == walk.count('s') and walk.count('e') == walk.count('w')

Second one:
def isValidWalk(walk):
    if (walk.count('n') == walk.count('s') and 
        walk.count('e') == walk.count('w') and
        len(walk) == 10):
            return True
    return False
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I hope it was helpful and useful. Please let me know your thoughts. Thank You for reading. &lt;/p&gt;

&lt;p&gt;"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." - Martin Fowler&lt;/p&gt;

</description>
      <category>python</category>
      <category>javascript</category>
      <category>codewars</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
