-
Notifications
You must be signed in to change notification settings - Fork 934
chore: new exercise #83
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
Merged
+47
−0
Merged
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions
jest-all.config.js
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,4 @@ | ||
| module.exports = { | ||
| name: 'dsa.js', | ||
| // testPathIgnorePatterns: ['/node_modules/', '/dist/', '/lab/', '/benchmarks/', '/coverage/'], | ||
| }; |
23 changes: 23 additions & 0 deletions
lab/exercises/10-mixed/document-distance.js
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,23 @@ | ||
|
|
||
| // npx jest lab/exercises/10-mixed/document-distance.spec.js --watch -c 'jest-all.config.js' | ||
|
|
||
| /** | ||
| * Find the distance between two documents. | ||
| * | ||
| * Convert files into vectors of words where the value is the frequency. | ||
| * Calculate the angle of the two vectors: cos α = v1 · v2 / |v1| * |v2| | ||
| * @param {string} file1 - String of words separated by whitespace | ||
| * @param {string} file2 - String of words separated by whitespace | ||
| */ | ||
| function documentDistance(file1, file2) { | ||
| // 0. slip words | ||
| // 1. calculate freq of each word per file | ||
| const byCounter = (map, w) => map.set(w, 1 + (map.get(w) || 0)); | ||
| const f1 = file1.split(' ').reduce(byCounter, new Map()); | ||
| const f2 = file2.split(' ').reduce(byCounter, new Map()); | ||
| // 2. multiply each occurence and divide it | ||
| const dotProd = (m1, m2) => [...new Set([...m1.keys(), ...m2.keys()])].reduce((sum, w) => sum + (m1.get(w) || 0) * (m2.get(w) || 0), 0); | ||
| return Math.acos(dotProd(f1, f2) / Math.sqrt(dotProd(f1, f1) * dotProd(f2, f2))); | ||
| } | ||
|
|
||
| module.exports = { documentDistance }; |
20 changes: 20 additions & 0 deletions
lab/exercises/10-mixed/document-distance.spec.js
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,20 @@ | ||
| const { documentDistance } = require('./document-distance'); | ||
|
|
||
| describe('documentDistance', () => { | ||
| it('should work with different files', () => { | ||
| const file1 = 'This is a cat.'; | ||
| const file2 = 'This is a dog.'; | ||
| expect(documentDistance(file1, file2)).toBeCloseTo(0.722); | ||
| }); | ||
|
|
||
| it('should work with different files', () => { | ||
| const file1 = 'This is a cat.'; | ||
| const file2 = 'Occaecat irure enim sint cupidatat id cillum cupidatat ipsum officia ea reprehenderit eiusmod voluptate. Est in laboris esse anim tempor sit in labore eiusmod consectetur aliqua. Quis nulla sunt incididunt magna velit in reprehenderit officia ut esse. Duis proident aute sint laborum consectetur eu reprehenderit amet et esse esse deserunt.'; | ||
| expect(documentDistance(file1, file2)).toBeCloseTo(1.57); | ||
| }); | ||
|
|
||
| it('should work with equal files', () => { | ||
| const file1 = 'This is a cat.'; | ||
| expect(documentDistance(file1, file1)).toEqual(0); | ||
| }); | ||
| }); |
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.