-
-
Notifications
You must be signed in to change notification settings - Fork 655
Add relative-distance
#2621
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
Merged
Add relative-distance
#2621
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
39 changes: 39 additions & 0 deletions
39
exercises/practice/relative-distance/.docs/instructions.md
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,39 @@ | ||
| # Instructions | ||
|
|
||
| Your task is to determine the degree of separation between two individuals in a family tree. | ||
|
|
||
| - You will be given an input, with all parent names and their children. | ||
| - Each name is unique, a child _can_ have one or two parents. | ||
| - The degree of separation is defined as the shortest number of connections from one person to another. | ||
| - If two individuals are not connected, return a value that represents "no known relationship." | ||
| Please see the test cases for the actual implementation. | ||
|
|
||
| ## Example | ||
|
|
||
| Given the following family tree: | ||
|
|
||
| ```text | ||
| ┌──────────┐ ┌──────────┐ ┌───────────┐ | ||
| │ Helena │ │ Erdős │ │ Shusaku │ | ||
| └───┬───┬──┘ └─────┬────┘ └──────┬────┘ | ||
| ┌───┘ └───────┐ └──────┬──────┘ | ||
| ▼ ▼ ▼ | ||
| ┌──────────┐ ┌────────┐ ┌──────────┐ | ||
| │ Isla │ │ Tariq │ │ Kevin │ | ||
| └────┬─────┘ └────┬───┘ └──────────┘ | ||
| ▼ ▼ | ||
| ┌─────────┐ ┌────────┐ | ||
| │ Uma │ │ Morphy │ | ||
| └─────────┘ └────────┘ | ||
| ``` | ||
|
|
||
| The degree of separation between Tariq and Uma is 3 (Tariq → Helena → Isla → Uma). | ||
| There's no known relationship between Isla and [Kevin][six-bacons], as there is no connection in the given data. | ||
| The degree of separation between Uma and Isla is 1. | ||
|
|
||
| ```exercism/note | ||
| Isla and Tariq are siblings and have a separation of 1. | ||
| Similarly, this implementation would report a separation of 2 from you to your father's brother. | ||
| ``` | ||
|
|
||
| [six-bacons]: https://en.m.wikipedia.org/wiki/Six_Degrees_of_Kevin_Bacon |
12 changes: 12 additions & 0 deletions
12
exercises/practice/relative-distance/.docs/introduction.md
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,12 @@ | ||
| # Introduction | ||
|
|
||
| You've been hired to develop **Noble Knots**, the hottest new dating app for nobility! | ||
| With centuries of royal intermarriage, things have gotten… _complicated_. | ||
| To avoid any _oops-we're-twins_ situations, your job is to build a system that checks how closely two people are related. | ||
|
|
||
| Noble Knots is inspired by Iceland's "[Islendinga-App][islendiga-app]," which is backed up by a database that traces all known family connections between Icelanders from the time of the settlement of Iceland. | ||
| Your algorithm will determine the **degree of separation** between two individuals in the royal family tree. | ||
|
|
||
| Will your app help crown a perfect match? | ||
|
|
||
| [islendiga-app]: http://www.islendingaapp.is/information-in-english/ |
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,5 @@ | ||
| /node_modules | ||
| /bin/configlet | ||
| /bin/configlet.exe | ||
| /package-lock.json | ||
| /yarn.lock |
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,25 @@ | ||
| { | ||
| "authors": [ | ||
| "BNAndras" | ||
| ], | ||
| "files": { | ||
| "solution": [ | ||
| "relative-distance.js" | ||
| ], | ||
| "test": [ | ||
| "relative-distance.spec.js" | ||
| ], | ||
| "example": [ | ||
| ".meta/proof.ci.js" | ||
| ] | ||
| }, | ||
| "blurb": "Given a family tree, calculate the degree of separation.", | ||
| "source": "vaeng", | ||
| "source_url": "https://github.com/exercism/problem-specifications/pull/2537", | ||
| "custom": { | ||
| "version.tests.compatibility": "jest-27", | ||
| "flag.tests.task-per-describe": false, | ||
| "flag.tests.may-run-long": false, | ||
| "flag.tests.includes-optional": false | ||
| } | ||
| } | ||
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,52 @@ | ||
| export const degreesOfSeparation = (familyTree, personA, personB) => { | ||
| const neighbors = new Map(); | ||
|
|
||
| for (const [parent, children] of Object.entries(familyTree)) { | ||
| if (!neighbors.has(parent)) { | ||
| neighbors.set(parent, new Set()); | ||
| } | ||
|
|
||
| for (const child of children) { | ||
| if (!neighbors.has(child)) { | ||
| neighbors.set(child, new Set()); | ||
| } | ||
|
|
||
| neighbors.get(parent).add(child); | ||
| neighbors.get(child).add(parent); | ||
| } | ||
|
|
||
| // | ||
| for (const childA of children) { | ||
| for (const childB of children) { | ||
| if (childA !== childB) { | ||
| neighbors.get(childA).add(childB); | ||
| neighbors.get(childB).add(childA); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (!neighbors.has(personA) || !neighbors.has(personB)) { | ||
| return -1; | ||
| } | ||
|
|
||
| const queue = [[personA, 0]]; | ||
| const visited = new Set([personA]); | ||
|
|
||
| while (queue.length > 0) { | ||
| const [current, degree] = queue.shift(); | ||
|
|
||
| if (current === personB) { | ||
| return degree; | ||
| } | ||
|
|
||
| for (const neighbor of neighbors.get(current)) { | ||
| if (!visited.has(neighbor)) { | ||
| visited.add(neighbor); | ||
| queue.push([neighbor, degree + 1]); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return -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,31 @@ | ||
| # This is an auto-generated file. | ||
| # | ||
| # Regenerating this file via `configlet sync` will: | ||
| # - Recreate every `description` key/value pair | ||
| # - Recreate every `reimplements` key/value pair, where they exist in problem-specifications | ||
| # - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) | ||
| # - Preserve any other key/value pair | ||
| # | ||
| # As user-added comments (using the # character) will be removed when this file | ||
| # is regenerated, comments can be added via a `comment` key. | ||
|
|
||
| [4a1ded74-5d32-47fb-8ae5-321f51d06b5b] | ||
| description = "Direct parent-child relation" | ||
|
|
||
| [30d17269-83e9-4f82-a0d7-8ef9656d8dce] | ||
| description = "Sibling relationship" | ||
|
|
||
| [8dffa27d-a8ab-496d-80b3-2f21c77648b5] | ||
| description = "Two degrees of separation, grandchild" | ||
|
|
||
| [34e56ec1-d528-4a42-908e-020a4606ee60] | ||
| description = "Unrelated individuals" | ||
|
|
||
| [93ffe989-bad2-48c4-878f-3acb1ce2611b] | ||
| description = "Complex graph, cousins" | ||
|
|
||
| [2cc2e76b-013a-433c-9486-1dbe29bf06e5] | ||
| description = "Complex graph, no shortcut, far removed nephew" | ||
|
|
||
| [46c9fbcb-e464-455f-a718-049ea3c7400a] | ||
| description = "Complex graph, some shortcuts, cross-down and cross-up, cousins several times removed, with unrelated family tree" |
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 @@ | ||
| audit=false |
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,21 @@ | ||
| MIT License | ||
|
|
||
| Copyright (c) 2021 Exercism | ||
|
|
||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
|
|
||
| The above copyright notice and this permission notice shall be included in all | ||
| copies or substantial portions of the Software. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| SOFTWARE. |
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 = { | ||
| presets: [['@exercism/babel-preset-javascript', { corejs: '3.40' }]], | ||
| plugins: [], | ||
| }; |
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,45 @@ | ||
| // @ts-check | ||
|
|
||
| import config from '@exercism/eslint-config-javascript'; | ||
| import maintainersConfig from '@exercism/eslint-config-javascript/maintainers.mjs'; | ||
|
|
||
| import globals from 'globals'; | ||
|
|
||
| export default [ | ||
| ...config, | ||
| ...maintainersConfig, | ||
| { | ||
| files: maintainersConfig[1].files, | ||
| rules: { | ||
| 'jest/expect-expect': ['warn', { assertFunctionNames: ['expect*'] }], | ||
| }, | ||
| }, | ||
| { | ||
| files: ['scripts/**/*.mjs'], | ||
| languageOptions: { | ||
| globals: { | ||
| ...globals.node, | ||
| }, | ||
| }, | ||
| }, | ||
| // <<inject-rules-here>> | ||
| { | ||
| ignores: [ | ||
| // # Protected or generated | ||
| '/.appends/**/*', | ||
| '/.github/**/*', | ||
| '/.vscode/**/*', | ||
|
|
||
| // # Binaries | ||
| '/bin/*', | ||
|
|
||
| // # Configuration | ||
| '/config', | ||
| '/babel.config.js', | ||
|
|
||
| // # Typings | ||
| '/exercises/**/global.d.ts', | ||
| '/exercises/**/env.d.ts', | ||
| ], | ||
| }, | ||
| ]; |
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,22 @@ | ||
| module.exports = { | ||
| verbose: true, | ||
| projects: ['<rootDir>'], | ||
| testMatch: [ | ||
| '**/__tests__/**/*.[jt]s?(x)', | ||
| '**/test/**/*.[jt]s?(x)', | ||
| '**/?(*.)+(spec|test).[jt]s?(x)', | ||
| ], | ||
| testPathIgnorePatterns: [ | ||
| '/(?:production_)?node_modules/', | ||
| '.d.ts$', | ||
| '<rootDir>/test/fixtures', | ||
| '<rootDir>/test/helpers', | ||
| '__mocks__', | ||
| ], | ||
| transform: { | ||
| '^.+\\.[jt]sx?$': 'babel-jest', | ||
| }, | ||
| moduleNameMapper: { | ||
| '^(\\.\\/.+)\\.js$': '$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,34 @@ | ||
| { | ||
| "name": "@exercism/javascript-relative-distance", | ||
| "description": "Exercism exercises in Javascript.", | ||
| "author": "Katrina Owen", | ||
| "private": true, | ||
| "license": "MIT", | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "https://github.com/exercism/javascript", | ||
| "directory": "exercises/practice/relative-distance" | ||
| }, | ||
| "devDependencies": { | ||
| "@exercism/babel-preset-javascript": "^0.5.1", | ||
| "@exercism/eslint-config-javascript": "^0.8.1", | ||
| "@jest/globals": "^29.7.0", | ||
| "@types/node": "^22.10.3", | ||
| "@types/shelljs": "^0.8.15", | ||
| "babel-jest": "^29.7.0", | ||
| "core-js": "~3.40.0", | ||
| "diff": "^7.0.0", | ||
| "eslint": "^9.19.0", | ||
| "expect": "^29.7.0", | ||
| "globals": "^15.14.0", | ||
| "jest": "^29.7.0" | ||
| }, | ||
| "dependencies": {}, | ||
| "scripts": { | ||
| "lint": "corepack pnpm eslint .", | ||
| "test": "corepack pnpm jest", | ||
| "watch": "corepack pnpm jest --watch", | ||
| "format": "corepack pnpm prettier -w ." | ||
| }, | ||
| "packageManager": "pnpm@9.15.2" | ||
| } |
Oops, something went wrong.
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.