Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -1414,6 +1414,14 @@
"text_formatting"
]
},
{
"slug": "relative-distance",
"name": "Relative Distance",
"uuid": "c72faac5-8d41-404b-8558-759b94ea22ec",
"practices": [],
"prerequisites": [],
"difficulty": 5
},
{
"slug": "saddle-points",
"name": "Saddle Points",
Expand Down
39 changes: 39 additions & 0 deletions exercises/practice/relative-distance/.docs/instructions.md
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 exercises/practice/relative-distance/.docs/introduction.md
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/
5 changes: 5 additions & 0 deletions exercises/practice/relative-distance/.gitignore
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
25 changes: 25 additions & 0 deletions exercises/practice/relative-distance/.meta/config.json
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
}
}
52 changes: 52 additions & 0 deletions exercises/practice/relative-distance/.meta/proof.ci.js
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;
};
31 changes: 31 additions & 0 deletions exercises/practice/relative-distance/.meta/tests.toml
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"
1 change: 1 addition & 0 deletions exercises/practice/relative-distance/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
audit=false
21 changes: 21 additions & 0 deletions exercises/practice/relative-distance/LICENSE
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.
4 changes: 4 additions & 0 deletions exercises/practice/relative-distance/babel.config.js
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: [],
};
45 changes: 45 additions & 0 deletions exercises/practice/relative-distance/eslint.config.mjs
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',
],
},
];
22 changes: 22 additions & 0 deletions exercises/practice/relative-distance/jest.config.js
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',
},
};
34 changes: 34 additions & 0 deletions exercises/practice/relative-distance/package.json
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"
}
Loading