-
-
Notifications
You must be signed in to change notification settings - Fork 357
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8729d74
commit 978bf64
Showing
11 changed files
with
371 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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 @@ | ||
# Instructions | ||
|
||
Imagine you need to transmit a binary tree to a satellite approaching Alpha Centauri and you have limited bandwidth. | ||
Since the tree has no repeating items it can be uniquely represented by its [pre-order and in-order traversals][wiki]. | ||
|
||
Write the software for the satellite to rebuild the tree from the traversals. | ||
|
||
A pre-order traversal reads the value of the current node before (hence "pre") reading the left subtree in pre-order. | ||
Afterwards the right subtree is read in pre-order. | ||
|
||
An in-order traversal reads the left subtree in-order then the current node and finally the right subtree in-order. | ||
So in order from left to right. | ||
|
||
For example the pre-order traversal of this tree is [a, i, x, f, r]. | ||
The in-order traversal of this tree is [i, a, f, x, r] | ||
|
||
```text | ||
a | ||
/ \ | ||
i x | ||
/ \ | ||
f r | ||
``` | ||
|
||
Note: the first item in the pre-order traversal is always the root. | ||
|
||
[wiki]: https://en.wikipedia.org/wiki/Tree_traversal |
This file contains 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 | ||
|
||
[Satellite.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 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,30 @@ | ||
using System; | ||
using System.Linq; | ||
|
||
public record Tree(char Value, Tree Left, Tree Right); | ||
|
||
public static class Satellite | ||
{ | ||
public static Tree TreeFromTraversals(char[] preOrder, char[] inOrder) | ||
{ | ||
if (preOrder.Length != inOrder.Length) throw new ArgumentException("Traversals must have the same length"); | ||
if (!preOrder.Order().SequenceEqual(inOrder.Order())) throw new ArgumentException("Traversals must be consistent"); | ||
if (preOrder.Distinct().Count() != preOrder.Length) throw new ArgumentException("Traversals must not have repeated items"); | ||
|
||
int preOrderIndex = 0; | ||
return TreeFromTraversals(preOrder, inOrder, ref preOrderIndex, 0, preOrder.Length - 1); | ||
} | ||
|
||
private static Tree TreeFromTraversals(char[] preOrder, char[] inOrder, ref int preOrderIndex, int left, int right) | ||
{ | ||
if (left > right) return null; | ||
|
||
var preOrderValue = preOrder[preOrderIndex++]; | ||
var inOrderIndex = Array.IndexOf(inOrder, preOrderValue); | ||
|
||
return new Tree( | ||
preOrderValue, | ||
TreeFromTraversals(preOrder, inOrder, ref preOrderIndex, left, inOrderIndex - 1), | ||
TreeFromTraversals(preOrder, inOrder, ref preOrderIndex, inOrderIndex + 1, right)); | ||
} | ||
} |
This file contains 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,36 @@ | ||
{{ func to_tree | ||
if $0.empty? | ||
ret "null" | ||
end | ||
ret $"new Tree('{$0.v}', {to_tree $0.l}, {to_tree $0.r})" | ||
end }} | ||
|
||
{{ func to_arg | ||
if $0.empty? | ||
ret "[]" | ||
end | ||
ret "['" + (array.join $0 "', '") + "']" | ||
end }} | ||
|
||
using System; | ||
using Xunit; | ||
|
||
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 }}() | ||
{ | ||
{{- if test.expected.error }} | ||
Assert.Throws<ArgumentException>(() => {{ testedClass }}.{{ test.testedMethod }}({{ test.input.preorder | to_arg }}, {{ test.input.inorder | to_arg }})); | ||
{{- else if test.expected.empty? }} | ||
Assert.Null({{ testedClass }}.{{ test.testedMethod }}({{ test.input.preorder | to_arg }}, {{ test.input.inorder | to_arg }})); | ||
{{ else }} | ||
var expected = {{ test.expected | to_tree }}; | ||
Assert.Equal(expected, {{ testedClass }}.{{ test.testedMethod }}({{ test.input.preorder | to_arg }}, {{ test.input.inorder | to_arg }})); | ||
{{ end -}} | ||
} | ||
{{ end -}} | ||
} |
This file contains 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,17 @@ | ||
{ | ||
"authors": [ | ||
"erikschierboom" | ||
], | ||
"files": { | ||
"solution": [ | ||
"Satellite.cs" | ||
], | ||
"test": [ | ||
"SatelliteTests.cs" | ||
], | ||
"example": [ | ||
".meta/Example.cs" | ||
] | ||
}, | ||
"blurb": "Rebuild binary trees from pre-order and in-order traversals." | ||
} |
This file contains 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,28 @@ | ||
# 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. | ||
|
||
[8df3fa26-811a-4165-9286-ff9ac0850d19] | ||
description = "Empty tree" | ||
|
||
[f945ccfc-05e3-47d7-825b-0270559d43ad] | ||
description = "Tree with one item" | ||
|
||
[a0121d5f-37b0-48dd-9c64-cba4c4464135] | ||
description = "Tree with many items" | ||
|
||
[6074041f-4891-4d81-a128-401050c2a3b0] | ||
description = "Reject traversals of different length" | ||
|
||
[27916ce4-45f3-4d8b-8528-496fedc157ca] | ||
description = "Reject inconsistent traversals of same length" | ||
|
||
[d86a3d72-76a9-43b5-9d3a-e64cb1216035] | ||
description = "Reject traversals with repeated items" |
This file contains 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,11 @@ | ||
using System; | ||
|
||
public record Tree(char Value, Tree Left, Tree Right); | ||
|
||
public static class Satellite | ||
{ | ||
public static Tree TreeFromTraversals(char[] preOrder, char[] inOrder) | ||
{ | ||
throw new NotImplementedException("You need to implement this method."); | ||
} | ||
} |
This file contains 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,17 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net9.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Exercism.Tests" Version="0.1.0-beta1" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" /> | ||
<PackageReference Include="xunit" Version="2.8.1" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.1"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
</Project> |
Oops, something went wrong.