-
-
Notifications
You must be signed in to change notification settings - Fork 365
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
BNAndras
wants to merge
1
commit into
exercism:main
Choose a base branch
from
BNAndras:add-relative-distance
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Add relative-distance
#2424
Changes from all commits
Commits
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,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 |
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,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; | ||
} | ||
} |
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,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 -}} | ||
} |
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": [ | ||
"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" | ||
} |
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" |
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 @@ | ||
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
18
exercises/practice/relative-distance/RelativeDistance.csproj
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,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> |
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.
Could you add some prerequisites?