DEV Community

Cover image for Take a Ten Minute Walk: A Codewars kata solution By Papan Sarkar
Papan Sarkar
Papan Sarkar

Posted on

Take a Ten Minute Walk: A Codewars kata solution By Papan Sarkar

Take a Ten Minute Walk: A Codewars kata solution 
In this article, I'll explain a kata solution from Codewars. This kata(problem) is authored by JKphobic. And here is the kata
link:https://www.codewars.com/kata/54da539698b8a2ad76000228/train/python

Problem:
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.

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!).

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.

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 :

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

Let's convert these instructions into code and make small segments of the whole solution code:
So,

if len(walk) == 10 //True
else // False
Enter fullscreen mode Exit fullscreen mode

We'll create two variable and initialize to 0 to track our walk, ns (North-South) and ew (East-West) -

ns = 0
ew = 0
Enter fullscreen mode Exit fullscreen mode

For every single block we walk, respectively we'll increment and decrement with 1

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
Enter fullscreen mode Exit fullscreen mode

And then finally we will check we returned to our same position or not

if ns == 0 and ew == 0 //True
else //False
Enter fullscreen mode Exit fullscreen mode

Let's combine all this:

# 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
Enter fullscreen mode Exit fullscreen mode
//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 && ew === 0; 
  }
Enter fullscreen mode Exit fullscreen mode

Now it's time to some other best and clever solutions:
Most upvoted:

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
Enter fullscreen mode Exit fullscreen mode

I hope it was helpful and useful. Please let me know your thoughts. Thank You for reading. 

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." - Martin Fowler

Top comments (1)

Collapse
 
papansarkar101 profile image
Papan Sarkar

I feel the same..no doubt on that