diff --git a/config.json b/config.json index 7c8f6b933..3d1ff49af 100644 --- a/config.json +++ b/config.json @@ -1054,6 +1054,19 @@ ], "difficulty": 5 }, + { + "slug": "relative-distance", + "name": "Relative Distance", + "uuid": "a3cf95fd-c7c1-4199-a253-7bae8d1aba9a", + "practices": [ + "maps" + ], + "prerequisites": [ + "lists", + "maps" + ], + "difficulty": 5 + }, { "slug": "robot-name", "name": "Robot Name", diff --git a/exercises/practice/relative-distance/.docs/instructions.md b/exercises/practice/relative-distance/.docs/instructions.md new file mode 100644 index 000000000..9046aee7c --- /dev/null +++ b/exercises/practice/relative-distance/.docs/instructions.md @@ -0,0 +1,39 @@ +# Instructions + +Your task is to determine the degree of separation between two individuals in a family tree. +This is similar to the pop culture idea that every Hollywood actor is [within six degrees of Kevin Bacon][six-bacons]. + +- 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 2 (Tariq → Isla → Uma). +There's no known relationship between Isla and Kevin, 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 diff --git a/exercises/practice/relative-distance/.docs/introduction.md b/exercises/practice/relative-distance/.docs/introduction.md new file mode 100644 index 000000000..cb9fee6c7 --- /dev/null +++ b/exercises/practice/relative-distance/.docs/introduction.md @@ -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/ diff --git a/exercises/practice/relative-distance/.meta/config.json b/exercises/practice/relative-distance/.meta/config.json new file mode 100644 index 000000000..c6b2cc422 --- /dev/null +++ b/exercises/practice/relative-distance/.meta/config.json @@ -0,0 +1,22 @@ +{ + "authors": [ + "BNAndras" + ], + "files": { + "solution": [ + "src/main/java/RelativeDistance.java" + ], + "test": [ + "src/test/java/RelativeDistanceTest.java" + ], + "example": [ + ".meta/src/reference/java/RelativeDistance.java" + ], + "invalidator": [ + "build.gradle" + ] + }, + "blurb": "Given a family tree, calculate the degree of separation.", + "source": "vaeng", + "source_url": "https://github.com/exercism/problem-specifications/pull/2537" +} diff --git a/exercises/practice/relative-distance/.meta/src/reference/java/RelativeDistance.java b/exercises/practice/relative-distance/.meta/src/reference/java/RelativeDistance.java new file mode 100644 index 000000000..3d170879b --- /dev/null +++ b/exercises/practice/relative-distance/.meta/src/reference/java/RelativeDistance.java @@ -0,0 +1,68 @@ +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Queue; + +class RelativeDistance { + + private final Map> graph; + + RelativeDistance(Map> familyTree) { + final HashMap> connections = new HashMap<>(); + + for (Map.Entry> entry : familyTree.entrySet()) { + String parent = entry.getKey(); + List children = entry.getValue(); + + connections.putIfAbsent(parent, new HashSet<>()); + + for (String child : children) { + connections.putIfAbsent(child, new HashSet<>()); + + connections.get(parent).add(child); + connections.get(child).add(parent); + + for (String sibling : children) { + if (!sibling.equals(child)) { + connections.get(child).add(sibling); + } + } + } + } + + graph = connections; + } + + int degreeOfSeparation(String personA, String personB) { + if (!graph.containsKey(personA) || !graph.containsKey(personB)) { + return -1; + } + + Queue queue = new LinkedList<>(); + Map distances = new HashMap<>() { + { + put(personA, 0); + } + }; + queue.add(personA); + + while (!queue.isEmpty()) { + String current = queue.poll(); + int currentDistance = distances.get(current); + + for (String relative : graph.get(current)) { + if (!distances.containsKey(relative)) { + if (relative.equals(personB)) { + return currentDistance + 1; + } + distances.put(relative, currentDistance + 1); + queue.add(relative); + } + } + } + + return -1; + } +} diff --git a/exercises/practice/relative-distance/.meta/tests.toml b/exercises/practice/relative-distance/.meta/tests.toml new file mode 100644 index 000000000..66c91ba09 --- /dev/null +++ b/exercises/practice/relative-distance/.meta/tests.toml @@ -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" diff --git a/exercises/practice/relative-distance/build.gradle b/exercises/practice/relative-distance/build.gradle new file mode 100644 index 000000000..d28f35dee --- /dev/null +++ b/exercises/practice/relative-distance/build.gradle @@ -0,0 +1,25 @@ +plugins { + id "java" +} + +repositories { + mavenCentral() +} + +dependencies { + testImplementation platform("org.junit:junit-bom:5.10.0") + testImplementation "org.junit.jupiter:junit-jupiter" + testImplementation "org.assertj:assertj-core:3.25.1" + + testRuntimeOnly "org.junit.platform:junit-platform-launcher" +} + +test { + useJUnitPlatform() + + testLogging { + exceptionFormat = "full" + showStandardStreams = true + events = ["passed", "failed", "skipped"] + } +} diff --git a/exercises/practice/relative-distance/gradle/wrapper/gradle-wrapper.jar b/exercises/practice/relative-distance/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 000000000..e6441136f Binary files /dev/null and b/exercises/practice/relative-distance/gradle/wrapper/gradle-wrapper.jar differ diff --git a/exercises/practice/relative-distance/gradle/wrapper/gradle-wrapper.properties b/exercises/practice/relative-distance/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 000000000..2deab89d5 --- /dev/null +++ b/exercises/practice/relative-distance/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip +validateDistributionUrl=true +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/exercises/practice/relative-distance/gradlew b/exercises/practice/relative-distance/gradlew new file mode 100755 index 000000000..1aa94a426 --- /dev/null +++ b/exercises/practice/relative-distance/gradlew @@ -0,0 +1,249 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) +APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + if ! command -v java >/dev/null 2>&1 + then + die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Collect all arguments for the java command: +# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# and any embedded shellness will be escaped. +# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be +# treated as '${Hostname}' itself on the command line. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/exercises/practice/relative-distance/gradlew.bat b/exercises/practice/relative-distance/gradlew.bat new file mode 100644 index 000000000..25da30dbd --- /dev/null +++ b/exercises/practice/relative-distance/gradlew.bat @@ -0,0 +1,92 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 0 goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/exercises/practice/relative-distance/src/main/java/RelativeDistance.java b/exercises/practice/relative-distance/src/main/java/RelativeDistance.java new file mode 100644 index 000000000..6b74ce091 --- /dev/null +++ b/exercises/practice/relative-distance/src/main/java/RelativeDistance.java @@ -0,0 +1,13 @@ +import java.util.List; +import java.util.Map; + +class RelativeDistance { + + RelativeDistance(Map> familyTree) { + throw new UnsupportedOperationException("Delete this statement and write your own implementation."); + } + + int degreeOfSeparation(String personA, String personB) { + throw new UnsupportedOperationException("Delete this statement and write your own implementation."); + } +} diff --git a/exercises/practice/relative-distance/src/test/java/RelativeDistanceTest.java b/exercises/practice/relative-distance/src/test/java/RelativeDistanceTest.java new file mode 100644 index 000000000..3002b1a91 --- /dev/null +++ b/exercises/practice/relative-distance/src/test/java/RelativeDistanceTest.java @@ -0,0 +1,256 @@ +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; + + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; + + +public class RelativeDistanceTest { + + @Test + public void testDirectParentChildRelation() { + Map> familyTree = new HashMap<>() { + { + put("Vera", List.of("Tomoko")); + put("Tomoko", List.of("Aditi")); + } + }; + + RelativeDistance rd = new RelativeDistance(familyTree); + assertThat(rd.degreeOfSeparation("Vera", "Tomoko")).isEqualTo(1); + } + + @Disabled("Remove to run test") + @Test + public void testSiblingRelationship() { + Map> familyTree = new HashMap<>() { + { + put("Dalia", List.of("Olga", "Yassin")); + } + }; + + RelativeDistance rd = new RelativeDistance(familyTree); + assertThat(rd.degreeOfSeparation("Olga", "Yassin")).isEqualTo(1); + } + + @Disabled("Remove to run test") + @Test + public void testTwoDegreesOfSeparationGrandchild() { + Map> familyTree = new HashMap<>() { + { + put("Khadija", List.of("Mateo")); + put("Mateo", List.of("Rami")); + } + }; + + RelativeDistance rd = new RelativeDistance(familyTree); + assertThat(rd.degreeOfSeparation("Khadija", "Rami")).isEqualTo(2); + } + + @Disabled("Remove to run test") + @Test + public void testUnrelatedIndividuals() { + Map> familyTree = new HashMap<>() { + { + put("Priya", List.of("Rami")); + put("Kaito", List.of("Elif")); + } + }; + + RelativeDistance rd = new RelativeDistance(familyTree); + assertThat(rd.degreeOfSeparation("Priya", "Kaito")).isEqualTo(-1); + } + + @Disabled("Remove to run test") + @Test + public void testComplexGraphCousins() { + Map> familyTree = new HashMap<>() { + { + put("Aiko", List.of("Bao", "Carlos")); + put("Bao", List.of("Dalia", "Elias")); + put("Carlos", List.of("Fatima", "Gustavo")); + put("Dalia", List.of("Hassan", "Isla")); + put("Elias", List.of("Javier")); + put("Fatima", List.of("Khadija", "Liam")); + put("Gustavo", List.of("Mina")); + put("Hassan", List.of("Noah", "Olga")); + put("Isla", List.of("Pedro")); + put("Javier", List.of("Quynh", "Ravi")); + put("Khadija", List.of("Sofia")); + put("Liam", List.of("Tariq", "Uma")); + put("Mina", List.of("Viktor", "Wang")); + put("Noah", List.of("Xiomara")); + put("Olga", List.of("Yuki")); + put("Pedro", List.of("Zane", "Aditi")); + put("Quynh", List.of("Boris")); + put("Ravi", List.of("Celine")); + put("Sofia", List.of("Diego", "Elif")); + put("Tariq", List.of("Farah")); + put("Uma", List.of("Giorgio")); + put("Viktor", List.of("Hana", "Ian")); + put("Wang", List.of("Jing")); + put("Xiomara", List.of("Kaito")); + put("Yuki", List.of("Leila")); + put("Zane", List.of("Mateo")); + put("Aditi", List.of("Nia")); + put("Boris", List.of("Oscar")); + put("Celine", List.of("Priya")); + put("Diego", List.of("Qi")); + put("Elif", List.of("Rami")); + put("Farah", List.of("Sven")); + put("Giorgio", List.of("Tomoko")); + put("Hana", List.of("Umar")); + put("Ian", List.of("Vera")); + put("Jing", List.of("Wyatt")); + put("Kaito", List.of("Xia")); + put("Leila", List.of("Yassin")); + put("Mateo", List.of("Zara")); + put("Nia", List.of("Antonio")); + put("Oscar", List.of("Bianca")); + put("Priya", List.of("Cai")); + put("Qi", List.of("Dimitri")); + put("Rami", List.of("Ewa")); + put("Sven", List.of("Fabio")); + put("Tomoko", List.of("Gabriela")); + put("Umar", List.of("Helena")); + put("Vera", List.of("Igor")); + put("Wyatt", List.of("Jun")); + put("Xia", List.of("Kim")); + put("Yassin", List.of("Lucia")); + put("Zara", List.of("Mohammed")); + } + }; + + RelativeDistance rd = new RelativeDistance(familyTree); + assertThat(rd.degreeOfSeparation("Dimitri", "Fabio")).isEqualTo(9); + } + + @Disabled("Remove to run test") + @Test + public void testComplexGraphNoShortcutFarRemovedNephew() { + Map> familyTree = new HashMap<>() { + { + put("Aiko", List.of("Bao", "Carlos")); + put("Bao", List.of("Dalia", "Elias")); + put("Carlos", List.of("Fatima", "Gustavo")); + put("Dalia", List.of("Hassan", "Isla")); + put("Elias", List.of("Javier")); + put("Fatima", List.of("Khadija", "Liam")); + put("Gustavo", List.of("Mina")); + put("Hassan", List.of("Noah", "Olga")); + put("Isla", List.of("Pedro")); + put("Javier", List.of("Quynh", "Ravi")); + put("Khadija", List.of("Sofia")); + put("Liam", List.of("Tariq", "Uma")); + put("Mina", List.of("Viktor", "Wang")); + put("Noah", List.of("Xiomara")); + put("Olga", List.of("Yuki")); + put("Pedro", List.of("Zane", "Aditi")); + put("Quynh", List.of("Boris")); + put("Ravi", List.of("Celine")); + put("Sofia", List.of("Diego", "Elif")); + put("Tariq", List.of("Farah")); + put("Uma", List.of("Giorgio")); + put("Viktor", List.of("Hana", "Ian")); + put("Wang", List.of("Jing")); + put("Xiomara", List.of("Kaito")); + put("Yuki", List.of("Leila")); + put("Zane", List.of("Mateo")); + put("Aditi", List.of("Nia")); + put("Boris", List.of("Oscar")); + put("Celine", List.of("Priya")); + put("Diego", List.of("Qi")); + put("Elif", List.of("Rami")); + put("Farah", List.of("Sven")); + put("Giorgio", List.of("Tomoko")); + put("Hana", List.of("Umar")); + put("Ian", List.of("Vera")); + put("Jing", List.of("Wyatt")); + put("Kaito", List.of("Xia")); + put("Leila", List.of("Yassin")); + put("Mateo", List.of("Zara")); + put("Nia", List.of("Antonio")); + put("Oscar", List.of("Bianca")); + put("Priya", List.of("Cai")); + put("Qi", List.of("Dimitri")); + put("Rami", List.of("Ewa")); + put("Sven", List.of("Fabio")); + put("Tomoko", List.of("Gabriela")); + put("Umar", List.of("Helena")); + put("Vera", List.of("Igor")); + put("Wyatt", List.of("Jun")); + put("Xia", List.of("Kim")); + put("Yassin", List.of("Lucia")); + put("Zara", List.of("Mohammed")); + } + }; + RelativeDistance rd = new RelativeDistance(familyTree); + assertThat(rd.degreeOfSeparation("Lucia", "Jun")).isEqualTo(14); + } + + @Disabled("Remove to run test") + @Test + public void testComplexGraphSomeShortcutsCrossDownAndCrossUpCousinsSeveralTimesRemovedWithUnrelatedFamilyTree() { + Map> familyTree = new HashMap<>() { + { + put("Aiko", List.of("Bao", "Carlos")); + put("Bao", List.of("Dalia")); + put("Carlos", List.of("Fatima", "Gustavo")); + put("Dalia", List.of("Hassan", "Isla")); + put("Fatima", List.of("Khadija", "Liam")); + put("Gustavo", List.of("Mina")); + put("Hassan", List.of("Noah", "Olga")); + put("Isla", List.of("Pedro")); + put("Javier", List.of("Quynh", "Ravi")); + put("Khadija", List.of("Sofia")); + put("Liam", List.of("Tariq", "Uma")); + put("Mina", List.of("Viktor", "Wang")); + put("Noah", List.of("Xiomara")); + put("Olga", List.of("Yuki")); + put("Pedro", List.of("Zane", "Aditi")); + put("Quynh", List.of("Boris")); + put("Ravi", List.of("Celine")); + put("Sofia", List.of("Diego", "Elif")); + put("Tariq", List.of("Farah")); + put("Uma", List.of("Giorgio")); + put("Viktor", List.of("Hana", "Ian")); + put("Wang", List.of("Jing")); + put("Xiomara", List.of("Kaito")); + put("Yuki", List.of("Leila")); + put("Zane", List.of("Mateo")); + put("Aditi", List.of("Nia")); + put("Boris", List.of("Oscar")); + put("Celine", List.of("Priya")); + put("Diego", List.of("Qi")); + put("Elif", List.of("Rami")); + put("Farah", List.of("Sven")); + put("Giorgio", List.of("Tomoko")); + put("Hana", List.of("Umar")); + put("Ian", List.of("Vera")); + put("Jing", List.of("Wyatt")); + put("Kaito", List.of("Xia")); + put("Leila", List.of("Yassin")); + put("Mateo", List.of("Zara")); + put("Nia", List.of("Antonio")); + put("Oscar", List.of("Bianca")); + put("Priya", List.of("Cai")); + put("Qi", List.of("Dimitri")); + put("Rami", List.of("Ewa")); + put("Sven", List.of("Fabio")); + put("Tomoko", List.of("Gabriela")); + put("Umar", List.of("Helena")); + put("Vera", List.of("Igor")); + put("Wyatt", List.of("Jun")); + put("Xia", List.of("Kim")); + put("Yassin", List.of("Lucia")); + put("Zara", List.of("Mohammed")); + } + }; + RelativeDistance rd = new RelativeDistance(familyTree); + assertThat(rd.degreeOfSeparation("Wyatt", "Xia")).isEqualTo(12); + } +} diff --git a/exercises/settings.gradle b/exercises/settings.gradle index 2ff1e9a14..713a0c583 100644 --- a/exercises/settings.gradle +++ b/exercises/settings.gradle @@ -118,6 +118,7 @@ include 'practice:raindrops' include 'practice:rational-numbers' include 'practice:react' include 'practice:rectangles' +include 'practice:relative-distance' include 'practice:resistor-color' include 'practice:resistor-color-duo' include 'practice:resistor-color-trio'