-
-
Notifications
You must be signed in to change notification settings - Fork 402
New practice exercise relative-distance
#1554
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
Changes from all commits
Commits
Show all changes
5 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
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,4 @@ | ||
# Used by "mix format" | ||
[ | ||
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] | ||
] |
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,26 @@ | ||
# The directory Mix will write compiled artifacts to. | ||
/_build/ | ||
|
||
# If you run "mix test --cover", coverage assets end up here. | ||
/cover/ | ||
|
||
# The directory Mix downloads your dependencies sources to. | ||
/deps/ | ||
|
||
# Where third-party dependencies like ExDoc output generated docs. | ||
/doc/ | ||
|
||
# Ignore .fetch files in case you like to edit your project deps locally. | ||
/.fetch | ||
|
||
# If the VM crashes, it generates a dump, let's ignore it too. | ||
erl_crash.dump | ||
|
||
# Also ignore archive artifacts (built via "mix archive.build"). | ||
*.ez | ||
|
||
# Ignore package tarball (built via "mix hex.build"). | ||
relative_distance-*.tar | ||
|
||
# Temporary files, for example, from tests. | ||
/tmp/ |
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,19 @@ | ||
{ | ||
"authors": [ | ||
"jiegillet" | ||
], | ||
"files": { | ||
"solution": [ | ||
"lib/relative_distance.ex" | ||
], | ||
"test": [ | ||
"test/relative_distance_test.exs" | ||
], | ||
"example": [ | ||
".meta/example.ex" | ||
] | ||
}, | ||
"blurb": "Given a family tree, calculate the degree of separation.", | ||
"source": "vaeng", | ||
"source_url": "https://github.com/exercism/problem-specifications/pull/2537" | ||
} |
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,42 @@ | ||
defmodule RelativeDistance do | ||
@doc """ | ||
Find the degree of separation of two members given a given family tree. | ||
""" | ||
@spec degree_of_separation( | ||
family_tree :: %{String.t() => [String.t()]}, | ||
person_a :: String.t(), | ||
person_b :: String.t() | ||
) :: nil | pos_integer() | ||
def degree_of_separation(family_tree, person_a, person_b) do | ||
family_tree | ||
|> build_family_graph() | ||
|> find_separation(person_b, [{person_a, 0}], MapSet.new()) | ||
end | ||
|
||
defp build_family_graph(family_tree) do | ||
for {parent, children} <- family_tree, child <- children, reduce: %{} do | ||
graph -> | ||
siblings = children |> MapSet.new() |> MapSet.delete(child) | ||
|
||
graph | ||
|> Map.update(parent, MapSet.new([child]), &MapSet.put(&1, child)) | ||
|> Map.update(child, MapSet.new([parent]), &MapSet.put(&1, parent)) | ||
|> Map.update(child, siblings, &MapSet.union(&1, siblings)) | ||
end | ||
end | ||
|
||
defp find_separation(_graph, _goal, [], _history), do: nil | ||
|
||
defp find_separation(_graph, goal, [{goal, count} | _], _history), do: count | ||
|
||
defp find_separation(graph, goal, [{person, count} | rest], history) do | ||
history = MapSet.put(history, person) | ||
|
||
next_steps = | ||
graph[person] | ||
|> Enum.reject(fn relative -> MapSet.member?(history, relative) end) | ||
|> Enum.map(fn relative -> {relative, count + 1} end) | ||
|
||
find_separation(graph, goal, rest ++ next_steps, history) | ||
end | ||
end |
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" |
12 changes: 12 additions & 0 deletions
12
exercises/practice/relative-distance/lib/relative_distance.ex
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 @@ | ||
defmodule RelativeDistance do | ||
@doc """ | ||
Find the degree of separation of two members given a given family tree. | ||
""" | ||
@spec degree_of_separation( | ||
family_tree :: %{String.t() => [String.t()]}, | ||
person_a :: String.t(), | ||
person_b :: String.t() | ||
) :: nil | pos_integer() | ||
def degree_of_separation(family_tree, person_a, person_b) do | ||
end | ||
end |
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,27 @@ | ||
defmodule RelativeDistance.MixProject do | ||
use Mix.Project | ||
|
||
def project do | ||
[ | ||
app: :relative_distance, | ||
version: "0.1.0", | ||
start_permanent: Mix.env() == :prod, | ||
deps: deps() | ||
] | ||
end | ||
|
||
# Run "mix help compile.app" to learn about applications. | ||
def application do | ||
[ | ||
extra_applications: [:logger] | ||
] | ||
end | ||
|
||
# Run "mix help deps" to learn about dependencies. | ||
defp deps do | ||
[ | ||
# {:dep_from_hexpm, "~> 0.3.0"}, | ||
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"} | ||
] | ||
end | ||
end |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did this because mix format wouldn't run from outside the directory anymore