-
Notifications
You must be signed in to change notification settings - Fork 40.6k
feat(pascal): create exercise #447
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nik-rev
wants to merge
14
commits into
TheOdinProject:main
Choose a base branch
from
nik-contrib:feat/exercise_19
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 11 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
13c66f4
feat/ generate exercise
nik-rev fbc74a6
feat(pascal): create solution
nik-rev 0e00b59
feat(pascal): create README.md
nik-rev 8fe75f2
feat(pascal): create tests for exercise
nik-rev 8608b40
feat(pascal): remove unnecessary call to function
nik-rev 26f5679
refactor: make code more readable
nik-rev c629198
refactor: remove unnecessary line break
nik-rev b5adfce
feat(pascal): better names for variables
nik-rev ca43c00
feat(pascal): math mistake
nik-rev d64601a
fix(pascal): typo
nik-rev ad37314
feat(pascal): copy over tests from solution
nik-rev f9bbd25
feat: remove line break
nik-rev fc4bd94
feat: remove line break
nik-rev b08da1e
feat: make solution simpler (credit to @JoshDevHub)
nik-rev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Exercise 19 - pascal | ||
|
||
The pascal's triangle is modelled as follows: | ||
- The first row is `1`. | ||
- Each row can be considered to have a hidden `0` to either sides of it. So the first row could | ||
also be said to be `0, 1, 0` | ||
- To obtain the next row, we take each number and add it with its rightmost neighbor. | ||
|
||
First row: `[1]` | ||
Second row: `[0+1, 1+0]` or simply `[1, 1]` | ||
Third row: `[0+1, 1+1, 1+0]` or simply `[1, 2, 1]` | ||
Fourth row: `[0+1, 1+2, 2+1, 1+0]` or simply `[1, 3, 3, 1]` | ||
... | ||
|
||
The pattern continues forever. | ||
|
||
Your task is to create a *recursive* function, `pascal` - that will take an input `n` and output the | ||
`n`th pascal's row. | ||
nik-rev marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const pascal = function() { | ||
|
||
}; | ||
|
||
// Do not edit below this line | ||
module.exports = pascal; |
nik-rev marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const pascal = require('./pascal'); | ||
|
||
describe('pascal', () => { | ||
test('Gets the first row of pascal', () => { | ||
expect(pascal(1)).toEqual([1]); | ||
}); | ||
|
||
test.skip('Gets the second row of pascal', () => { | ||
expect(pascal(2)).toEqual([1, 1]); | ||
}); | ||
|
||
test.skip('Gets the third row of pascal', () => { | ||
expect(pascal(3)).toEqual([1, 2, 1]); | ||
}); | ||
|
||
test.skip('Gets the fourth row of pascal', () => { | ||
expect(pascal(4)).toEqual([1, 3, 3, 1]); | ||
}); | ||
|
||
test.skip('Gets the fifth row of pascal', () => { | ||
expect(pascal(5)).toEqual([1, 4, 6, 4, 1]); | ||
}); | ||
|
||
test.skip('Gets the sixth row of pascal', () => { | ||
expect(pascal(6)).toEqual([1, 5, 10, 10, 5, 1]); | ||
}); | ||
|
||
test.skip('Gets the seventh row of pascal', () => { | ||
expect(pascal(7)).toEqual([1, 6, 15, 20, 15, 6, 1]); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
const pascal = function (counter, currentLine = [1]) { | ||
if (counter === 1) { | ||
return currentLine; | ||
} | ||
|
||
const halfOfNextLine = currentLine.reduce( | ||
(numbers, currentNumber, index) => { | ||
const nextNumber = currentLine[index + 1]; | ||
if (currentNumber <= nextNumber) { | ||
return [...numbers, currentNumber + nextNumber]; | ||
} | ||
return numbers; | ||
}, | ||
[1], | ||
); | ||
|
||
// Notice that pascal triangle's lines are always palindromic in nature. | ||
// We only need the first half to obtain the information to construct the second half | ||
const joined = [...halfOfNextLine, ...halfOfNextLine.reverse()]; | ||
|
||
// If a given line of the pascal's triangle has an even amount of elements, two things are true: | ||
// - the next line will have an odd amount of elements | ||
// - the two elements in the middle will always be the same. So it's safe to remove one | ||
if (currentLine.length % 2 === 0) { | ||
joined.splice(joined.length / 2, 1); | ||
} | ||
|
||
return pascal(counter - 1, joined); | ||
}; | ||
nik-rev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Do not edit below this line | ||
module.exports = pascal; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const pascal = require('./pascal-solution'); | ||
|
||
describe('pascal', () => { | ||
test('Gets the first row of pascal', () => { | ||
expect(pascal(1)).toEqual([1]); | ||
}); | ||
|
||
test('Gets the second row of pascal', () => { | ||
expect(pascal(2)).toEqual([1, 1]); | ||
}); | ||
|
||
test('Gets the third row of pascal', () => { | ||
expect(pascal(3)).toEqual([1, 2, 1]); | ||
}); | ||
|
||
test('Gets the fourth row of pascal', () => { | ||
expect(pascal(4)).toEqual([1, 3, 3, 1]); | ||
}); | ||
|
||
test('Gets the fifth row of pascal', () => { | ||
expect(pascal(5)).toEqual([1, 4, 6, 4, 1]); | ||
}); | ||
|
||
test('Gets the sixth row of pascal', () => { | ||
expect(pascal(6)).toEqual([1, 5, 10, 10, 5, 1]); | ||
}); | ||
|
||
test('Gets the seventh row of pascal', () => { | ||
expect(pascal(7)).toEqual([1, 6, 15, 20, 15, 6, 1]); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.