DEV Community

dev.to staff
dev.to staff

Posted on

Daily Challenge #5 - Ten Minute Walk

Hey there, it's time to get moving.

Today’s challenge is modified from user @JKphobic on CodeWars:

You live in a city where all roads are laid out in a perfect grid. You have the unfortunate habit of arriving too early or too late to your appointments, so you decide to create a Walk Generating App.

Any time you press the button, it will send you an array of one-letter strings representing directions to walk (eg. [‘n’,’s’,’w’,’e’]). You always walk only a single block in a direction. It takes one minute to traverse one city block.

Create a program that will return an array of one-letter strings representing the walk. The program should accept input for the amount of time the user decides to walk and should bring the user back to their starting location.

I wish the streets in my town were structured like this, it would make driving so much easier.

Good luck, happy coding!


Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge for a future post? Email yo+challenge@dev.to with your suggestions!

Top comments (21)

Collapse
 
spaciecat profile image
Spacie • Edited

My first attempt at one of these coding challenge things!

I'm not great with maths, but I'm guessing that all walks must have an even length, otherwise you can't end up back at your starting location, you'd always be one away...

If that assumption is true, then I think this works:

Basically, for each "pair" of blocks you want to walk, either add ["n", "s"] or ["e", "w"] to an array, then flatten and shuffle! Because you're always adding both a movement and it's inverse, you'll always end up where you started!

Ruby's proc / block thing still confuses me a little so I'm not sure if this is the most elegant one-liner (excluding checking for even walk length), but ayy it works!

def genWalk length
  raise "Walk length must be even!" if length % 2 != 0
  (length / 2).times.collect(&Proc.new {[%w(n s), %w(e w)].sample}).flatten.shuffle
end

genWalk 10
# -> ["n", "w", "n", "e", "w", "s", "e", "w", "e", "s"]
Collapse
 
coreyja profile image
Corey Alexander

Great solution! I believe your assumption holds up and makes this solution nice and elegant!

Great job on your first challenge too!!

Collapse
 
johncip profile image
jmc

nice 🤩

Collapse
 
jaloplo profile image
Jaime López

Here my contribution with javascript language:

function getWalkSteps(walkTime) {
  if(walkTime % 2 > 0) {
    throw new Error('You cannot come back');
  }

  const distance = walkTime/2;
  const x = Math.floor(Math.random(distance) * distance);
  const y = distance - x;

  return new Array(walkTime)
    .fill('e', 0, x)
    .fill('n', x, x+y)
    .fill('w', x+y, x+y+x)
    .fill('s', x+y+x, x+y+x+y);
}
Collapse
 
_bigblind profile image
Frederik 👨‍💻➡️🌐 Creemers • Edited

Here's a trivial solution in Python

def get_walk(minutes):
    if minutes % 2 != 0:
        # Only even minute times are possible, as you always need to walk the same distance east as west, 
        # andnortha s sound.
        raise ValueError("Only walks with an even number of minutes are possible")

    half = minutes / 2
    return half * ['n'] + half * ['s']

If you add the requirement that you can't walk the same street twice...

def get_walk(minutes):
    if minutes % 2 != 0:
        # Only even minute times are possible, as you always need to walk the same distance east as west, 
        # andnortha s sound.
        raise ValueError("Only walks with an even number of minutes are possible")

    # We'll return a rectangle with width 1, so the two short sides combined take 2 minutes
    # We'll divide the remaining minutes by 2 to know how long the other sides should be.
    length = (minutes - 2) / 2

    return ['n'] + length * ['e'] + ['s'] + length * ['w']

Collapse
 
alvaromontoro profile image
Alvaro Montoro • Edited

JS

Not a great solution, but a solution.

const createRandomPath = minutes => {
  let path = [];

  // if the minutes are even, calculate path
  if (minutes % 2 === 0) {  
    // populate the array from opposite ends with complementary values
    for (let x = 0; x < minutes/2; x++) {
      const letter = Math.floor(Math.random()*4);
      path[x] = "nsew".charAt(letter);
      path[minutes-x-1] = "snwe".charAt(letter);
    }

    // randomize path
    path.sort((a,b) => Math.floor(Math.random()*2) ? 1 : -1);
  }

  // return the generated array or an empty array if steps are odd
  return path;
}

And a one-liner but not really randomized, just going north-then-south:

const f=m=>m%2?[]:Array(m).fill("n",0,m/2).fill("s",m/2)

And the link to a demo on CodePen

Collapse
 
choroba profile image
E. Choroba

Just make sure the number of s's and n's is the same, and the same for w's and e's.

Perl solution:

use List::Util qw{ shuffle };

sub walk {
    shuffle map { int rand 2 ? ('n', 's') : ('w', 'e') } 1 .. $_[0] / 2
}
Collapse
 
martyhimmel profile image
Martin Himmel

PHP

function go_for_a_walk(int $distance) {
    if ($distance % 2 == 1) {
        throw new Exception("There's no place like home. Unfortunately, you won't return home with an odd number of steps.");
    }

    $steps = [];
    $directions = ['n', 'e', 's', 'w'];
    while ($distance > 0) {
        $index = array_rand($directions);
        $steps[] = $directions[$index];
        $steps[] = $directions[($index + 2) % 4];
        $distance -= 2;
    }
    shuffle($steps);
    return $steps;
}

echo implode(', ', go_for_a_walk(10)) . PHP_EOL;
Collapse
 
v613 profile image
Ceban Dumitru • Edited

BASH

#!/bin/bash
declare gps=(n s e w);
if [[ $((${1} % 2 )) = 0 ]]; then
    for (( i = ${1}; i >= 0; i-=2 )); do
        echo ${gps[$(($RANDOM%3))]};
    done;
else 
    echo "Sorry, you cannot generate way";
fi
Collapse
 
jasman7799 profile image
Jarod Smith
// in general we will create a sequence which just retraces itself after a certain time.
function generateWalkSequence(time) {
  if(time % 2 != 0) 
    throw new Error('Time must be even');
  let walkSequence = [];
  let oppositeDirections = [];
  let nextDirection = 'n';
  const directions = [...'nswe'];
  for(let i = 0; i < time/2; i++) {
    // loop selection after index 3
    walkSequence[i] = (nextDirection = directions[i % 4]);
    oppositeDirections[i] = (getComplement(nextDirection));
  }
  return [...walkSequence,...oppositeDirections.reverse()];
}

function getComplement(direction) {
  switch (direction) {
    case 'n':
      return 's';
    case 's':
      return 'n';
    case 'e':
      return 'w';
    case 'w':
      return 'e';
  }
}
Collapse
 
dimitrilahaye profile image
Dimitri Lahaye

During my reflexion about this challenge, I figured out that if input is an odd number, you will never be able to go back at your start position.

Here my JS implementation:

function direction(time) {
  if (time % 2 > 0) {
    throw 'You will never go back!!';
  }
  const ahead = Math.ceil(time/4),
    turn = Math.floor(time/4);
    return ('n'.repeat(ahead) + 'w'.repeat(turn) + 's'.repeat(ahead) + 'e'.repeat(turn)).split('');
}

// let's test it

try {
  console.log(direction(4)); // print ["n", "w", "s", "e"]
} catch (e) {
  console.log(e);
}

try {
  console.log(direction(1));
} catch (e) {
  console.log(e); // You will never go back!!
}

try {
  console.log(direction(3));
} catch (e) {
  console.log(e); // You will never go back!!
}
Collapse
 
johncip profile image
jmc • Edited

Spacie's algo, in Clojure:

(def pairs
  [["n" "s"] ["e" "w"]])

(defn walk [n]
  (-> (quot n 2)
      (repeatedly #(rand-nth [["n" "s"] ["e" "w"]]))
      flatten
      shuffle))

Some comments may only be visible to logged-in visitors. Sign in to view all comments.