Skip to content

Add relative-distance #2424

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
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2731,6 +2731,14 @@
],
"difficulty": 6
},
{
"slug": "relative-distance",
"name": "Relative Distance",
"uuid": "4f4229ce-5a2a-415c-a682-6a56cf3846ac",
"practices": [],
"prerequisites": [],
Comment on lines +2738 to +2739
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add some prerequisites?

"difficulty": 5
},
{
"slug": "split-second-stopwatch",
"name": "Split-Second Stopwatch",
Expand Down
15 changes: 15 additions & 0 deletions exercises/Exercises.sln
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "swift-scheduling", "swift-s
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SwiftScheduling", "practice\swift-scheduling\SwiftScheduling.csproj", "{9A073412-4B64-48EB-A346-A56DE6847CCC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RelativeDistance", "practice\relative-distance\RelativeDistance.csproj", "{2C37454E-8624-47B8-A09B-ADE201C2B04C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -2525,6 +2527,18 @@ Global
{9A073412-4B64-48EB-A346-A56DE6847CCC}.Release|x64.Build.0 = Release|Any CPU
{9A073412-4B64-48EB-A346-A56DE6847CCC}.Release|x86.ActiveCfg = Release|Any CPU
{9A073412-4B64-48EB-A346-A56DE6847CCC}.Release|x86.Build.0 = Release|Any CPU
{2C37454E-8624-47B8-A09B-ADE201C2B04C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2C37454E-8624-47B8-A09B-ADE201C2B04C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2C37454E-8624-47B8-A09B-ADE201C2B04C}.Debug|x64.ActiveCfg = Debug|Any CPU
{2C37454E-8624-47B8-A09B-ADE201C2B04C}.Debug|x64.Build.0 = Debug|Any CPU
{2C37454E-8624-47B8-A09B-ADE201C2B04C}.Debug|x86.ActiveCfg = Debug|Any CPU
{2C37454E-8624-47B8-A09B-ADE201C2B04C}.Debug|x86.Build.0 = Debug|Any CPU
{2C37454E-8624-47B8-A09B-ADE201C2B04C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2C37454E-8624-47B8-A09B-ADE201C2B04C}.Release|Any CPU.Build.0 = Release|Any CPU
{2C37454E-8624-47B8-A09B-ADE201C2B04C}.Release|x64.ActiveCfg = Release|Any CPU
{2C37454E-8624-47B8-A09B-ADE201C2B04C}.Release|x64.Build.0 = Release|Any CPU
{2C37454E-8624-47B8-A09B-ADE201C2B04C}.Release|x86.ActiveCfg = Release|Any CPU
{2C37454E-8624-47B8-A09B-ADE201C2B04C}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -2710,6 +2724,7 @@ Global
{1BA5F72A-ED8E-48A8-8FD6-0F567BDCBD9E} = {E276EF69-669A-43E0-88AC-8ABB17A9C026}
{FA726133-BC1C-D753-28C5-C9ACB44C0776} = {E276EF69-669A-43E0-88AC-8ABB17A9C026}
{9A073412-4B64-48EB-A346-A56DE6847CCC} = {FA726133-BC1C-D753-28C5-C9ACB44C0776}
{2C37454E-8624-47B8-A09B-ADE201C2B04C} = {E276EF69-669A-43E0-88AC-8ABB17A9C026}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {AB4EA6C9-5461-4024-BDC7-2AE0C3A85CD1}
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/
141 changes: 141 additions & 0 deletions exercises/practice/relative-distance/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
###############################
# Core EditorConfig Options #
###############################

; This file is for unifying the coding style for different editors and IDEs.
; More information at:
; https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference?view=vs-2017
; https://docs.microsoft.com/en-us/visualstudio/ide/create-portable-custom-editor-options?view=vs-2017

root = true

[*]
indent_style = space

[RelativeDistance.cs]
indent_size = 4

###############################
# .NET Coding Conventions #
###############################

# Organize usings
dotnet_sort_system_directives_first = true
dotnet_separate_import_directive_groups = true

# this. preferences
dotnet_style_qualification_for_field = false:suggestion
dotnet_style_qualification_for_property = false:suggestion
dotnet_style_qualification_for_method = false:suggestion
dotnet_style_qualification_for_event = false:suggestion

# Language keywords vs BCL types preferences
dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion
dotnet_style_predefined_type_for_member_access = true:suggestion

# Parentheses preferences
dotnet_style_parentheses_in_arithmetic_binary_operators = never_if_unnecessary:none
dotnet_style_parentheses_in_relational_binary_operators = never_if_unnecessary:none
dotnet_style_parentheses_in_other_binary_operators = never_if_unnecessary:none
dotnet_style_parentheses_in_other_operators = never_if_unnecessary:suggestion

# Modifier preferences
dotnet_style_require_accessibility_modifiers = always:suggestion
dotnet_style_readonly_field = true:suggestion

# Expression-level preferences
dotnet_style_object_initializer = true:suggestion
dotnet_style_collection_initializer = true:suggestion
dotnet_style_explicit_tuple_names = true:suggestion
dotnet_style_prefer_inferred_tuple_names = true:suggestion
dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion
dotnet_style_prefer_auto_properties = true:suggestion
dotnet_style_prefer_is_null_check_over_reference_equality_method = true:suggestion
dotnet_style_prefer_conditional_expression_over_assignment = true:suggestion
dotnet_style_prefer_conditional_expression_over_return = true:suggestion
dotnet_style_coalesce_expression = true:suggestion
dotnet_style_null_propagation = true:suggestion

###############################
# Naming Conventions #
###############################

# Style Definitions
dotnet_naming_style.pascal_case_style.capitalization = pascal_case

# Use PascalCase for constant fields
dotnet_naming_rule.constant_fields_should_be_pascal_case.severity = suggestion
dotnet_naming_rule.constant_fields_should_be_pascal_case.symbols = constant_fields
dotnet_naming_rule.constant_fields_should_be_pascal_case.style = pascal_case_style
dotnet_naming_symbols.constant_fields.applicable_kinds = field
dotnet_naming_symbols.constant_fields.applicable_accessibilities = *
dotnet_naming_symbols.constant_fields.required_modifiers = const

###############################
# C# Code Style Rules #
###############################

# var preferences
csharp_style_var_for_built_in_types = true:none
csharp_style_var_when_type_is_apparent = true:none
csharp_style_var_elsewhere = true:none

# Expression-bodied members
csharp_style_expression_bodied_methods = true:suggestion
csharp_style_expression_bodied_constructors = true:suggestion
csharp_style_expression_bodied_operators = true:suggestion
csharp_style_expression_bodied_properties = true:suggestion
csharp_style_expression_bodied_indexers = true:suggestion
csharp_style_expression_bodied_accessors = true:suggestion

# Pattern-matching preferences
csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion
csharp_style_pattern_matching_over_as_with_null_check = true:suggestion

# Null-checking preferences
csharp_style_throw_expression = true:suggestion
csharp_style_conditional_delegate_call = true:suggestion

# Modifier preferences
csharp_preferred_modifier_order = public,private,protected,internal,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,volatile,async:suggestion

# Expression-level preferences
csharp_prefer_braces = true:none
csharp_prefer_simple_default_expression = true:suggestion
csharp_style_deconstructed_variable_declaration = true:suggestion
csharp_style_pattern_local_over_anonymous_function = true:suggestion
csharp_style_inlined_variable_declaration = true:suggestion

###############################
# C# Formatting Rules #
###############################

# New line preferences
csharp_new_line_before_open_brace = all
csharp_new_line_before_else = true
csharp_new_line_before_catch = true
csharp_new_line_before_finally = true
csharp_new_line_before_members_in_object_initializers = false
csharp_new_line_before_members_in_anonymous_types = false
csharp_new_line_between_query_expression_clauses = true

# Indentation preferences
csharp_indent_case_contents = true
csharp_indent_switch_labels = true
csharp_indent_labels = flush_left

# Space preferences
csharp_space_after_cast = false
csharp_space_after_keywords_in_control_flow_statements = true
csharp_space_between_method_declaration_parameter_list_parentheses = false
csharp_space_between_method_call_parameter_list_parentheses = false
csharp_space_before_colon_in_inheritance_clause = true
csharp_space_after_colon_in_inheritance_clause = true
csharp_space_around_binary_operators = before_and_after
csharp_space_between_method_declaration_empty_parameter_list_parentheses = false
csharp_space_between_method_call_name_and_opening_parenthesis = false
csharp_space_between_method_call_empty_parameter_list_parentheses = false

# Wrapping preferences
csharp_preserve_single_line_blocks = true
csharp_preserve_single_line_statements = true
55 changes: 55 additions & 0 deletions exercises/practice/relative-distance/.meta/Example.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
public class RelativeDistance
{
private readonly Dictionary<string, HashSet<string>> relatives;

public RelativeDistance(Dictionary<string, List<string>> familyTree)
{
Dictionary<string, HashSet<string>> parsed = new();
foreach (var (parent, children) in familyTree)
{
var parentConnections = parsed.GetValueOrDefault(parent, []);
foreach (string child in children)
{
parentConnections.Add(child);
var childConnections = parsed.GetValueOrDefault(child, [])
.Union(children.Where(sibling => sibling != child)).ToHashSet();
childConnections.Add(parent);
parsed[child] = childConnections;
}

parsed[parent] = parentConnections;
}
relatives = parsed;
}

public int DegreeOfSeparation(string personA, string personB)
{
if (!relatives.ContainsKey(personA) || !relatives.ContainsKey(personB))
{
return -1;
}

Queue<(string Person, int Degree)> queue = new();
queue.Enqueue((personA, 0));
HashSet<string> visited = [personA];

while (queue.Count > 0)
{
var (currentPerson, degree) = queue.Dequeue();

if (currentPerson == personB)
{
return degree;
}

var unvisited = relatives[currentPerson].Except(visited).ToHashSet();
foreach (string neighbor in unvisited)
{
queue.Enqueue((neighbor, degree + 1));
}
visited.UnionWith(unvisited);
}

return -1;
}
}
10 changes: 10 additions & 0 deletions exercises/practice/relative-distance/.meta/Generator.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
public class {{ testClass }}
{
{{- for test in tests }}
[Fact{{ if !for.first }}(Skip = "Remove this Skip property to run this test"){{ end }}]
public void {{ test.testMethod }}()
{
Assert.Equal({{ test.expected }}, {{ testedClass }}.{{ test.testedMethod }}({{ test.input.familyTree }}, {{ test.input.personA | string.literal }}, {{ test.input.personB | string.literal }}));
}
{{ end -}}
}
19 changes: 19 additions & 0 deletions exercises/practice/relative-distance/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"BNAndras"
],
"files": {
"solution": [
"RelativeDistance.cs"
],
"test": [
"RelativeDistanceTests.cs"
],
"example": [
".meta/Example.cs"
]
},
"blurb": "Given a family tree, calculate the degree of separation.",
"source": "vaeng",
"source_url": "https://github.com/exercism/problem-specifications/pull/2537"
}
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"
12 changes: 12 additions & 0 deletions exercises/practice/relative-distance/RelativeDistance.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
public class RelativeDistance
{
public RelativeDistance(Dictionary<string, List<string>> familyTree)
{
throw new NotImplementedException("You need to implement this method.");
}

public int DegreeOfSeparation(string personA, string personB)
{
throw new NotImplementedException("You need to implement this method.");
}
}
18 changes: 18 additions & 0 deletions exercises/practice/relative-distance/RelativeDistance.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
</PropertyGroup>
<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Exercism.Tests.xunit.v3" Version="0.1.0-beta1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="xunit.v3" Version="1.1.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.1" />
</ItemGroup>
</Project>
Loading
Loading