DEV Community

dev.to staff
dev.to staff

Posted on

6

Daily Challenge #289 - Manhattan Distance

The distance formula can be used to find the distance between two points. What if we were trying to walk from point A to point B, but there were buildings in the way? We would need some other formula, but which?

Manhattan Distance
Manhattan distance is the distance between two points in a grid (like the grid-like street geography of the New York borough of Manhattan) calculated by only taking a vertical and/or horizontal path.

Write a function manhattanDistance that accepts two points, pointA and pointB, and returns the Manhattan Distance between the two points.

pointA and pointB are arrays containing the x and y coordinate in the grid. You can think of x as the row in the grid, and y as the column.

Examples:
manhattanDistance( [1, 1], [1, 1] ) // => returns 0
manhattanDistance( [5, 4], [3, 2] ) // => returns 4
manhattanDistance( [1, 1], [0, 3] ) // => returns 3

Tests:
manhattanDistance( [1,2], [1,3] )
manhattanDistance( [5,2], [7,3] )
manhattanDistance( [1,2], [4,4] )

Good luck!


This challenge comes from xDranik on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

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

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

Top comments (6)

Collapse
 
qm3ster profile image
Mihail Malo

JavaScript

const manhattan = ([x1, y1], [x2, y2]) => Math.abs(x2-x1) + Math.abs(y2-y1)
Collapse
 
patricktingen profile image
Patrick Tingen • Edited

Progress 4GL (aka OpenEdge ABL)

FUNCTION manhattan RETURNS INTEGER
  ( x1 AS INTEGER, y1 AS INTEGER
  , x2 AS INTEGER, y2 AS INTEGER):

  RETURN ABS(x2 - x1) + ABS(y2 - y1).

END FUNCTION. 

MESSAGE manhattan(5,2,7,3)
  VIEW-AS ALERT-BOX INFORMATION BUTTONS OK.
Collapse
 
qm3ster profile image
Mihail Malo • Edited

Rust

lol generics

use core::ops::{Add, Sub};
fn abs_difference<T>(x: T, y: T) -> T
where
    T: Sub<Output = T> + Ord,
{
    if x < y {
        y - x
    } else {
        x - y
    }
}

struct Point<T>(T, T);

fn manhattan<T>(Point(x1, y1): Point<T>, Point(x2, y2): Point<T>) -> T
where
    T: Sub<Output = T> + Add<Output = T> + Ord,
{
    abs_difference(x2, x1) + abs_difference(y2, y1)
}

fn main() {
    assert_eq!(manhattan(Point(1, 1), Point(1, 1)), 0);
    assert_eq!(manhattan(Point(5, 4), Point(3, 2)), 4);
    assert_eq!(manhattan(Point(1, 1), Point(0, 3)), 3);

    dbg!(manhattan(Point(1, 2), Point(1, 3)));
    dbg!(manhattan(Point(5, 2), Point(7, 3)));
    dbg!(manhattan(Point(1, 2), Point(4, 4)));
}
Collapse
 
peter279k profile image
peter279k

Here is the simple solution with Python:

def manhattan_distance(pointA, pointB):
    return abs(pointA[0] - pointB[0]) + abs(pointA[1] - pointB[1])
Collapse
 
jeremiebardon profile image
Jérémie Bardon

I'm sure a most simpler solution exist but:

function calculateDistance(a, b) {
    return a.map((d1, index) => {
        return Math.abs(d1 - b[index])
    }).reduce((acc, curr) => acc + curr);
}
Collapse
 
erivera23 profile image
Efrain Rivera

Python

def manhattan_distance(x1, y1, x2, y2):
      distance = abs(x2 - x1) + abs(y2 - y1)
      return distance

print(manhattan_distance(5,2,7,3))

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay