Skip to the content

How many blocks you walk

Problem

The city is a grid and every corner arrives as a list of two numbers: [street, avenue].

From corner [3, 5] to corner [7, 2] you walk 4 blocks along the streets and 3 along the avenues: 7 in total. There is no diagonal shortcut, you walk on the sidewalk.

Write a function that takes the two corners and returns how many blocks you walk. It doesn't matter which corner comes first: walking never adds up to less than 0. Some streets and avenues have negative numbers, the ones on the other side of downtown.

Examples

  • Three up and four across

    [0, 0], [3, 4] → 7

  • The avenue goes backwards

    [3, 5], [7, 2] → 7

  • Same avenue, street backwards

    [8, 1], [2, 1] → 6

Besides these, the challenge has hidden tests that are revealed when you submit your solution.

You start with this

Python

def blocks(here, away):
    pass

JavaScript

function blocks(here, away) {
}
Solve this challenge

It opens in your browser, with the editor and the tests. It is free and you do not need an account to start.

More O(1) challenges

See all challenges →