Skip to content

feat: add relative distance exercise #966

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@
"thread": "cpp",
"cinttypes": "cpp",
"typeinfo": "cpp",
"variant": "cpp"
"variant": "cpp",
"charconv": "cpp",
"span": "cpp",
"format": "cpp"
},
"cSpell.words": [
"stationprogress"
Expand Down
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -1244,6 +1244,14 @@
"strings"
],
"difficulty": 4
},
{
"slug": "relative-distance",
"name": "Relative Distance",
"uuid": "58877a11-7dc8-4caf-9d84-c4137a624791",
"practices": [],
"prerequisites": [],
"difficulty": 1
}
],
"foregone": [
Expand Down
34 changes: 34 additions & 0 deletions exercises/practice/relative-distance/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# 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.

[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/
21 changes: 21 additions & 0 deletions exercises/practice/relative-distance/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"authors": [
"vaeng"
],
"files": {
"solution": [
"relative_distance.cpp",
"relative_distance.h"
],
"test": [
"relative_distance_test.cpp"
],
"example": [
".meta/example.cpp",
".meta/example.h"
]
},
"blurb": "Given a family tree, calculate the degree of separation.",
"source": "vaeng",
"source_url": "https://github.com/exercism/problem-specifications/pull/2537"
}
5 changes: 5 additions & 0 deletions exercises/practice/relative-distance/.meta/example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "relative_distance.h"

namespace relative_distance {

} // namespace relative_distance
5 changes: 5 additions & 0 deletions exercises/practice/relative-distance/.meta/example.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

namespace relative_distance {

} // namespace relative_distance
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 three times removed"
64 changes: 64 additions & 0 deletions exercises/practice/relative-distance/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Get the exercise name from the current directory
get_filename_component(exercise ${CMAKE_CURRENT_SOURCE_DIR} NAME)

# Basic CMake project
cmake_minimum_required(VERSION 3.5.1)

# Name the project after the exercise
project(${exercise} CXX)

# Get a source filename from the exercise name by replacing -'s with _'s
string(REPLACE "-" "_" file ${exercise})

# Implementation could be only a header
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${file}.cpp)
set(exercise_cpp ${file}.cpp)
else()
set(exercise_cpp "")
endif()

# Use the common Catch library?
if(EXERCISM_COMMON_CATCH)
# For Exercism track development only
add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h $<TARGET_OBJECTS:catchlib>)
elseif(EXERCISM_TEST_SUITE)
# The Exercism test suite is being run, the Docker image already
# includes a pre-built version of Catch.
find_package(Catch2 REQUIRED)
add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h)
target_link_libraries(${exercise} PRIVATE Catch2::Catch2WithMain)
# When Catch is installed system wide we need to include a different
# header, we need this define to use the correct one.
target_compile_definitions(${exercise} PRIVATE EXERCISM_TEST_SUITE)
else()
# Build executable from sources and headers
add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h test/tests-main.cpp)
endif()

set_target_properties(${exercise} PROPERTIES
CXX_STANDARD 17
CXX_STANDARD_REQUIRED OFF
CXX_EXTENSIONS OFF
)

set(CMAKE_BUILD_TYPE Debug)

if("${CMAKE_CXX_COMPILER_ID}" MATCHES "(GNU|Clang)")
set_target_properties(${exercise} PROPERTIES
COMPILE_FLAGS "-Wall -Wextra -Wpedantic -Werror"
)
endif()

# Configure to run all the tests?
if(${EXERCISM_RUN_ALL_TESTS})
target_compile_definitions(${exercise} PRIVATE EXERCISM_RUN_ALL_TESTS)
endif()

# Tell MSVC not to warn us about unchecked iterators in debug builds
if(${MSVC})
set_target_properties(${exercise} PROPERTIES
COMPILE_DEFINITIONS_DEBUG _SCL_SECURE_NO_WARNINGS)
endif()

# Run the tests on every build
add_custom_target(test_${exercise} ALL DEPENDS ${exercise} COMMAND ${exercise})
5 changes: 5 additions & 0 deletions exercises/practice/relative-distance/relative_distance.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "relative_distance.h"

namespace relative_distance {

} // namespace relative_distance
5 changes: 5 additions & 0 deletions exercises/practice/relative-distance/relative_distance.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

namespace relative_distance {

} // namespace relative_distance
39 changes: 39 additions & 0 deletions exercises/practice/relative-distance/relative_distance_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "relative_distance.h"
#ifdef EXERCISM_TEST_SUITE
#include <catch2/catch.hpp>
#else
#include "test/catch.hpp"
#endif


TEST_CASE("Direct parent-child relation", "[4a1ded74-5d32-47fb-8ae5-321f51d06b5b]") {
REQUIRE(1 == relative_distance::degreeOfSeparation("{'Vera': ['Tomoko'], 'Tomoko': ['Aditi']}", "Vera", "Tomoko"));
}

#if defined(EXERCISM_RUN_ALL_TESTS)

TEST_CASE("Sibling relationship", "[30d17269-83e9-4f82-a0d7-8ef9656d8dce]") {
REQUIRE(1 == relative_distance::degreeOfSeparation("{'Dalia': ['Olga', 'Yassin']}", "Olga", "Yassin"));
}

TEST_CASE("Two degrees of separation, grandchild", "[8dffa27d-a8ab-496d-80b3-2f21c77648b5]") {
REQUIRE(2 == relative_distance::degreeOfSeparation("{'Khadija': ['Mateo'], 'Mateo': ['Rami']}", "Khadija", "Rami"));
}

TEST_CASE("Unrelated individuals", "[34e56ec1-d528-4a42-908e-020a4606ee60]") {
REQUIRE("None" == relative_distance::degreeOfSeparation("{'Priya': ['Rami'], 'Kaito': ['Elif']}", "Priya", "Kaito"));
}

TEST_CASE("Complex graph, cousins", "[93ffe989-bad2-48c4-878f-3acb1ce2611b]") {
REQUIRE(4 == relative_distance::degreeOfSeparation("{'Aiko': ['Bao', 'Carlos'], 'Bao': ['Dalia', 'Elias'], 'Carlos': ['Fatima', 'Gustavo'], 'Dalia': ['Hassan', 'Isla'], 'Elias': ['Javier'], 'Fatima': ['Khadija', 'Liam'], 'Gustavo': ['Mina'], 'Hassan': ['Noah', 'Olga'], 'Isla': ['Pedro'], 'Javier': ['Quynh', 'Ravi'], 'Khadija': ['Sofia'], 'Liam': ['Tariq', 'Uma'], 'Mina': ['Viktor', 'Wang'], 'Noah': ['Xiomara'], 'Olga': ['Yuki'], 'Pedro': ['Zane', 'Aditi'], 'Quynh': ['Boris'], 'Ravi': ['Celine'], 'Sofia': ['Diego', 'Elif'], 'Tariq': ['Farah'], 'Uma': ['Giorgio'], 'Viktor': ['Hana', 'Ian'], 'Wang': ['Jing'], 'Xiomara': ['Kaito'], 'Yuki': ['Leila'], 'Zane': ['Mateo'], 'Aditi': ['Nia'], 'Boris': ['Oscar'], 'Celine': ['Priya'], 'Diego': ['Qi'], 'Elif': ['Rami'], 'Farah': ['Sven'], 'Giorgio': ['Tomoko'], 'Hana': ['Umar'], 'Ian': ['Vera'], 'Jing': ['Wyatt'], 'Kaito': ['Xia'], 'Leila': ['Yassin'], 'Mateo': ['Zara'], 'Nia': ['Antonio'], 'Oscar': ['Bianca'], 'Priya': ['Cai'], 'Qi': ['Dimitri'], 'Rami': ['Ewa'], 'Sven': ['Fabio'], 'Tomoko': ['Gabriela'], 'Umar': ['Helena'], 'Vera': ['Igor'], 'Wyatt': ['Jun'], 'Xia': ['Kim'], 'Yassin': ['Lucia'], 'Zara': ['Mohammed']}", "Dimitri", "Fabio"));
}

TEST_CASE("Complex graph, no shortcut, far removed nephew", "[2cc2e76b-013a-433c-9486-1dbe29bf06e5]") {
REQUIRE(15 == relative_distance::degreeOfSeparation("{'Aiko': ['Bao', 'Carlos'], 'Bao': ['Dalia', 'Elias'], 'Carlos': ['Fatima', 'Gustavo'], 'Dalia': ['Hassan', 'Isla'], 'Elias': ['Javier'], 'Fatima': ['Khadija', 'Liam'], 'Gustavo': ['Mina'], 'Hassan': ['Noah', 'Olga'], 'Isla': ['Pedro'], 'Javier': ['Quynh', 'Ravi'], 'Khadija': ['Sofia'], 'Liam': ['Tariq', 'Uma'], 'Mina': ['Viktor', 'Wang'], 'Noah': ['Xiomara'], 'Olga': ['Yuki'], 'Pedro': ['Zane', 'Aditi'], 'Quynh': ['Boris'], 'Ravi': ['Celine'], 'Sofia': ['Diego', 'Elif'], 'Tariq': ['Farah'], 'Uma': ['Giorgio'], 'Viktor': ['Hana', 'Ian'], 'Wang': ['Jing'], 'Xiomara': ['Kaito'], 'Yuki': ['Leila'], 'Zane': ['Mateo'], 'Aditi': ['Nia'], 'Boris': ['Oscar'], 'Celine': ['Priya'], 'Diego': ['Qi'], 'Elif': ['Rami'], 'Farah': ['Sven'], 'Giorgio': ['Tomoko'], 'Hana': ['Umar'], 'Ian': ['Vera'], 'Jing': ['Wyatt'], 'Kaito': ['Xia'], 'Leila': ['Yassin'], 'Mateo': ['Zara'], 'Nia': ['Antonio'], 'Oscar': ['Bianca'], 'Priya': ['Cai'], 'Qi': ['Dimitri'], 'Rami': ['Ewa'], 'Sven': ['Fabio'], 'Tomoko': ['Gabriela'], 'Umar': ['Helena'], 'Vera': ['Igor'], 'Wyatt': ['Jun'], 'Xia': ['Kim'], 'Yassin': ['Lucia'], 'Zara': ['Mohammed']}", "Lucia", "Jun"));
}

TEST_CASE("Complex graph, some shortcuts, cross-down and cross-up, cousins three times removed", "[46c9fbcb-e464-455f-a718-049ea3c7400a]") {
REQUIRE(8 == relative_distance::degreeOfSeparation("{'Aiko': ['Bao', 'Carlos'], 'Bao': ['Dalia', 'Elias'], 'Carlos': ['Fatima', 'Gustavo'], 'Dalia': ['Hassan', 'Isla'], 'Elias': ['Javier'], 'Fatima': ['Khadija', 'Liam'], 'Gustavo': ['Mina'], 'Hassan': ['Noah', 'Olga'], 'Isla': ['Pedro'], 'Javier': ['Quynh', 'Ravi'], 'Khadija': ['Viktor'], 'Liam': ['Tariq', 'Uma'], 'Mina': ['Viktor', 'Wang'], 'Noah': ['Xiomara'], 'Olga': ['Yuki'], 'Pedro': ['Zane', 'Aditi'], 'Quynh': ['Boris'], 'Ravi': ['Celine'], 'Sofia': ['Diego', 'Elif'], 'Tariq': ['Farah'], 'Uma': ['Giorgio'], 'Viktor': ['Hana', 'Ian'], 'Wang': ['Jing'], 'Xiomara': ['Kaito'], 'Yuki': ['Leila'], 'Zane': ['Mateo'], 'Aditi': ['Nia'], 'Boris': ['Oscar'], 'Celine': ['Priya'], 'Diego': ['Qi'], 'Elif': ['Rami'], 'Farah': ['Sven'], 'Giorgio': ['Tomoko'], 'Hana': ['Umar'], 'Ian': ['Vera'], 'Jing': ['Wyatt'], 'Kaito': ['Xia'], 'Leila': ['Yassin'], 'Mateo': ['Zara'], 'Nia': ['Antonio'], 'Oscar': ['Bianca'], 'Priya': ['Cai'], 'Qi': ['Dimitri'], 'Rami': ['Ewa'], 'Sven': ['Fabio'], 'Tomoko': ['Gabriela'], 'Umar': ['Helena'], 'Vera': ['Igor'], 'Wyatt': ['Jun'], 'Xia': ['Kim'], 'Yassin': ['Lucia'], 'Zara': ['Mohammed']}", "Tomoko", "Qi"));
}

#endif
Loading
Loading