Files
freeCodeCamp/curriculum/challenges/english/08-coding-interview-prep/project-euler/problem-15-lattice-paths.english.md
Kristofer Koishigawa 6cfd0fc503 fix: improve Project Euler descriptions, challenge seeds, and test cases (#38016)
* fix: improve Project Euler descriptions and test case

Improve formatting of Project Euler test descriptions. Also add poker hands array and new test case for problem 54

* feat: add typeof tests and gave functions proper names for first 100 challenges

* fix: continue fixing test descriptions and adding "before test" sections

* fix: address review comments

* fix: adjust grids in 18 and 67 and fix some text that reference files rather than the given arrays

* fix: implement bug fixes and improvements from review

* fix: remove console.log statements from seed and solution
2020-02-28 06:39:47 -06:00

1.6 KiB
Raw Blame History

id, challengeType, title, forumTopicId
id challengeType title forumTopicId
5900f37b1000cf542c50fe8e 5 Problem 15: Lattice paths 301780

Description

Starting in the top left corner of a 2×2 grid, and only being able to move to the right and down, there are exactly 6 routes to the bottom right corner.

a diagram of 6 2 by 2 grids showing all the routes to the bottom right corner

How many such routes are there through a given gridSize?

Instructions

Tests

tests:
  - text: <code>latticePaths(4)</code> should return a number.
    testString: assert(typeof latticePaths(4) === 'number');
  - text: <code>latticePaths(4)</code> should return 70.
    testString: assert.strictEqual(latticePaths(4), 70);
  - text: <code>latticePaths(9)</code> should return 48620.
    testString: assert.strictEqual(latticePaths(9), 48620);
  - text: <code>latticePaths(20)</code> should return 137846528820.
    testString: assert.strictEqual(latticePaths(20), 137846528820);

Challenge Seed

function latticePaths(gridSize) {
  // Good luck!
  return true;
}

latticePaths(4);

Solution

function latticePaths(gridSize) {
  let paths = 1;

  for (let i = 0; i < gridSize; i++) {
    paths *= (2 * gridSize) - i;
    paths /= i + 1;
  }
  return paths;
}