Skip to content

Commit 978bf64

Browse files
Add satellite exercise (#2383)
1 parent 8729d74 commit 978bf64

File tree

11 files changed

+371
-0
lines changed

11 files changed

+371
-0
lines changed

config.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2675,6 +2675,20 @@
26752675
"numbers"
26762676
],
26772677
"difficulty": 7
2678+
},
2679+
{
2680+
"slug": "satellite",
2681+
"name": "Satellite",
2682+
"uuid": "4da2ca3b-2fe5-4048-985b-95a3b24aaa36",
2683+
"practices": [
2684+
"for-loops"
2685+
],
2686+
"prerequisites": [
2687+
"generic-methods",
2688+
"chars",
2689+
"for-loops"
2690+
],
2691+
"difficulty": 7
26782692
}
26792693
],
26802694
"foregone": [

exercises/Exercises.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KillerSudokuHelper", "pract
351351
EndProject
352352
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SquareRoot", "practice\square-root\SquareRoot.csproj", "{5C05051E-D46C-4544-9CF8-F2A748F63172}"
353353
EndProject
354+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Satellite", "practice\satellite\Satellite.csproj", "{8E276065-1371-4CFA-BA20-95225EC6AEBC}"
355+
EndProject
354356
Global
355357
GlobalSection(SolutionConfigurationPlatforms) = preSolution
356358
Debug|Any CPU = Debug|Any CPU
@@ -1045,6 +1047,10 @@ Global
10451047
{BEBBD420-075D-46F1-AE51-CC9A05FECE4A}.Debug|Any CPU.Build.0 = Debug|Any CPU
10461048
{BEBBD420-075D-46F1-AE51-CC9A05FECE4A}.Release|Any CPU.ActiveCfg = Release|Any CPU
10471049
{BEBBD420-075D-46F1-AE51-CC9A05FECE4A}.Release|Any CPU.Build.0 = Release|Any CPU
1050+
{8E276065-1371-4CFA-BA20-95225EC6AEBC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
1051+
{8E276065-1371-4CFA-BA20-95225EC6AEBC}.Debug|Any CPU.Build.0 = Debug|Any CPU
1052+
{8E276065-1371-4CFA-BA20-95225EC6AEBC}.Release|Any CPU.ActiveCfg = Release|Any CPU
1053+
{8E276065-1371-4CFA-BA20-95225EC6AEBC}.Release|Any CPU.Build.0 = Release|Any CPU
10481054
EndGlobalSection
10491055
GlobalSection(SolutionProperties) = preSolution
10501056
HideSolutionNode = FALSE
@@ -1222,6 +1228,7 @@ Global
12221228
{90C70CAA-A225-4D66-8B42-6AC82AD1D5DC} = {E276EF69-669A-43E0-88AC-8ABB17A9C026}
12231229
{5C05051E-D46C-4544-9CF8-F2A748F63172} = {E276EF69-669A-43E0-88AC-8ABB17A9C026}
12241230
{BEBBD420-075D-46F1-AE51-CC9A05FECE4A} = {E276EF69-669A-43E0-88AC-8ABB17A9C026}
1231+
{8E276065-1371-4CFA-BA20-95225EC6AEBC} = {E276EF69-669A-43E0-88AC-8ABB17A9C026}
12251232
EndGlobalSection
12261233
GlobalSection(ExtensibilityGlobals) = postSolution
12271234
SolutionGuid = {AB4EA6C9-5461-4024-BDC7-2AE0C3A85CD1}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Instructions
2+
3+
Imagine you need to transmit a binary tree to a satellite approaching Alpha Centauri and you have limited bandwidth.
4+
Since the tree has no repeating items it can be uniquely represented by its [pre-order and in-order traversals][wiki].
5+
6+
Write the software for the satellite to rebuild the tree from the traversals.
7+
8+
A pre-order traversal reads the value of the current node before (hence "pre") reading the left subtree in pre-order.
9+
Afterwards the right subtree is read in pre-order.
10+
11+
An in-order traversal reads the left subtree in-order then the current node and finally the right subtree in-order.
12+
So in order from left to right.
13+
14+
For example the pre-order traversal of this tree is [a, i, x, f, r].
15+
The in-order traversal of this tree is [i, a, f, x, r]
16+
17+
```text
18+
a
19+
/ \
20+
i x
21+
/ \
22+
f r
23+
```
24+
25+
Note: the first item in the pre-order traversal is always the root.
26+
27+
[wiki]: https://en.wikipedia.org/wiki/Tree_traversal
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
###############################
2+
# Core EditorConfig Options #
3+
###############################
4+
5+
; This file is for unifying the coding style for different editors and IDEs.
6+
; More information at:
7+
; https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference?view=vs-2017
8+
; https://docs.microsoft.com/en-us/visualstudio/ide/create-portable-custom-editor-options?view=vs-2017
9+
10+
root = true
11+
12+
[*]
13+
indent_style = space
14+
15+
[Satellite.cs]
16+
indent_size = 4
17+
18+
###############################
19+
# .NET Coding Conventions #
20+
###############################
21+
22+
# Organize usings
23+
dotnet_sort_system_directives_first = true
24+
dotnet_separate_import_directive_groups = true
25+
26+
# this. preferences
27+
dotnet_style_qualification_for_field = false:suggestion
28+
dotnet_style_qualification_for_property = false:suggestion
29+
dotnet_style_qualification_for_method = false:suggestion
30+
dotnet_style_qualification_for_event = false:suggestion
31+
32+
# Language keywords vs BCL types preferences
33+
dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion
34+
dotnet_style_predefined_type_for_member_access = true:suggestion
35+
36+
# Parentheses preferences
37+
dotnet_style_parentheses_in_arithmetic_binary_operators = never_if_unnecessary:none
38+
dotnet_style_parentheses_in_relational_binary_operators = never_if_unnecessary:none
39+
dotnet_style_parentheses_in_other_binary_operators = never_if_unnecessary:none
40+
dotnet_style_parentheses_in_other_operators = never_if_unnecessary:suggestion
41+
42+
# Modifier preferences
43+
dotnet_style_require_accessibility_modifiers = always:suggestion
44+
dotnet_style_readonly_field = true:suggestion
45+
46+
# Expression-level preferences
47+
dotnet_style_object_initializer = true:suggestion
48+
dotnet_style_collection_initializer = true:suggestion
49+
dotnet_style_explicit_tuple_names = true:suggestion
50+
dotnet_style_prefer_inferred_tuple_names = true:suggestion
51+
dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion
52+
dotnet_style_prefer_auto_properties = true:suggestion
53+
dotnet_style_prefer_is_null_check_over_reference_equality_method = true:suggestion
54+
dotnet_style_prefer_conditional_expression_over_assignment = true:suggestion
55+
dotnet_style_prefer_conditional_expression_over_return = true:suggestion
56+
dotnet_style_coalesce_expression = true:suggestion
57+
dotnet_style_null_propagation = true:suggestion
58+
59+
###############################
60+
# Naming Conventions #
61+
###############################
62+
63+
# Style Definitions
64+
dotnet_naming_style.pascal_case_style.capitalization = pascal_case
65+
66+
# Use PascalCase for constant fields
67+
dotnet_naming_rule.constant_fields_should_be_pascal_case.severity = suggestion
68+
dotnet_naming_rule.constant_fields_should_be_pascal_case.symbols = constant_fields
69+
dotnet_naming_rule.constant_fields_should_be_pascal_case.style = pascal_case_style
70+
dotnet_naming_symbols.constant_fields.applicable_kinds = field
71+
dotnet_naming_symbols.constant_fields.applicable_accessibilities = *
72+
dotnet_naming_symbols.constant_fields.required_modifiers = const
73+
74+
###############################
75+
# C# Code Style Rules #
76+
###############################
77+
78+
# var preferences
79+
csharp_style_var_for_built_in_types = true:none
80+
csharp_style_var_when_type_is_apparent = true:none
81+
csharp_style_var_elsewhere = true:none
82+
83+
# Expression-bodied members
84+
csharp_style_expression_bodied_methods = true:suggestion
85+
csharp_style_expression_bodied_constructors = true:suggestion
86+
csharp_style_expression_bodied_operators = true:suggestion
87+
csharp_style_expression_bodied_properties = true:suggestion
88+
csharp_style_expression_bodied_indexers = true:suggestion
89+
csharp_style_expression_bodied_accessors = true:suggestion
90+
91+
# Pattern-matching preferences
92+
csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion
93+
csharp_style_pattern_matching_over_as_with_null_check = true:suggestion
94+
95+
# Null-checking preferences
96+
csharp_style_throw_expression = true:suggestion
97+
csharp_style_conditional_delegate_call = true:suggestion
98+
99+
# Modifier preferences
100+
csharp_preferred_modifier_order = public,private,protected,internal,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,volatile,async:suggestion
101+
102+
# Expression-level preferences
103+
csharp_prefer_braces = true:none
104+
csharp_prefer_simple_default_expression = true:suggestion
105+
csharp_style_deconstructed_variable_declaration = true:suggestion
106+
csharp_style_pattern_local_over_anonymous_function = true:suggestion
107+
csharp_style_inlined_variable_declaration = true:suggestion
108+
109+
###############################
110+
# C# Formatting Rules #
111+
###############################
112+
113+
# New line preferences
114+
csharp_new_line_before_open_brace = all
115+
csharp_new_line_before_else = true
116+
csharp_new_line_before_catch = true
117+
csharp_new_line_before_finally = true
118+
csharp_new_line_before_members_in_object_initializers = false
119+
csharp_new_line_before_members_in_anonymous_types = false
120+
csharp_new_line_between_query_expression_clauses = true
121+
122+
# Indentation preferences
123+
csharp_indent_case_contents = true
124+
csharp_indent_switch_labels = true
125+
csharp_indent_labels = flush_left
126+
127+
# Space preferences
128+
csharp_space_after_cast = false
129+
csharp_space_after_keywords_in_control_flow_statements = true
130+
csharp_space_between_method_declaration_parameter_list_parentheses = false
131+
csharp_space_between_method_call_parameter_list_parentheses = false
132+
csharp_space_before_colon_in_inheritance_clause = true
133+
csharp_space_after_colon_in_inheritance_clause = true
134+
csharp_space_around_binary_operators = before_and_after
135+
csharp_space_between_method_declaration_empty_parameter_list_parentheses = false
136+
csharp_space_between_method_call_name_and_opening_parenthesis = false
137+
csharp_space_between_method_call_empty_parameter_list_parentheses = false
138+
139+
# Wrapping preferences
140+
csharp_preserve_single_line_blocks = true
141+
csharp_preserve_single_line_statements = true
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Linq;
3+
4+
public record Tree(char Value, Tree Left, Tree Right);
5+
6+
public static class Satellite
7+
{
8+
public static Tree TreeFromTraversals(char[] preOrder, char[] inOrder)
9+
{
10+
if (preOrder.Length != inOrder.Length) throw new ArgumentException("Traversals must have the same length");
11+
if (!preOrder.Order().SequenceEqual(inOrder.Order())) throw new ArgumentException("Traversals must be consistent");
12+
if (preOrder.Distinct().Count() != preOrder.Length) throw new ArgumentException("Traversals must not have repeated items");
13+
14+
int preOrderIndex = 0;
15+
return TreeFromTraversals(preOrder, inOrder, ref preOrderIndex, 0, preOrder.Length - 1);
16+
}
17+
18+
private static Tree TreeFromTraversals(char[] preOrder, char[] inOrder, ref int preOrderIndex, int left, int right)
19+
{
20+
if (left > right) return null;
21+
22+
var preOrderValue = preOrder[preOrderIndex++];
23+
var inOrderIndex = Array.IndexOf(inOrder, preOrderValue);
24+
25+
return new Tree(
26+
preOrderValue,
27+
TreeFromTraversals(preOrder, inOrder, ref preOrderIndex, left, inOrderIndex - 1),
28+
TreeFromTraversals(preOrder, inOrder, ref preOrderIndex, inOrderIndex + 1, right));
29+
}
30+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{{ func to_tree
2+
if $0.empty?
3+
ret "null"
4+
end
5+
6+
ret $"new Tree('{$0.v}', {to_tree $0.l}, {to_tree $0.r})"
7+
end }}
8+
9+
{{ func to_arg
10+
if $0.empty?
11+
ret "[]"
12+
end
13+
14+
ret "['" + (array.join $0 "', '") + "']"
15+
end }}
16+
17+
using System;
18+
using Xunit;
19+
20+
public class {{ testClass }}
21+
{
22+
{{- for test in tests }}
23+
[Fact{{ if !for.first }}(Skip = "Remove this Skip property to run this test"){{ end }}]
24+
public void {{ test.testMethod }}()
25+
{
26+
{{- if test.expected.error }}
27+
Assert.Throws<ArgumentException>(() => {{ testedClass }}.{{ test.testedMethod }}({{ test.input.preorder | to_arg }}, {{ test.input.inorder | to_arg }}));
28+
{{- else if test.expected.empty? }}
29+
Assert.Null({{ testedClass }}.{{ test.testedMethod }}({{ test.input.preorder | to_arg }}, {{ test.input.inorder | to_arg }}));
30+
{{ else }}
31+
var expected = {{ test.expected | to_tree }};
32+
Assert.Equal(expected, {{ testedClass }}.{{ test.testedMethod }}({{ test.input.preorder | to_arg }}, {{ test.input.inorder | to_arg }}));
33+
{{ end -}}
34+
}
35+
{{ end -}}
36+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"authors": [
3+
"erikschierboom"
4+
],
5+
"files": {
6+
"solution": [
7+
"Satellite.cs"
8+
],
9+
"test": [
10+
"SatelliteTests.cs"
11+
],
12+
"example": [
13+
".meta/Example.cs"
14+
]
15+
},
16+
"blurb": "Rebuild binary trees from pre-order and in-order traversals."
17+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[8df3fa26-811a-4165-9286-ff9ac0850d19]
13+
description = "Empty tree"
14+
15+
[f945ccfc-05e3-47d7-825b-0270559d43ad]
16+
description = "Tree with one item"
17+
18+
[a0121d5f-37b0-48dd-9c64-cba4c4464135]
19+
description = "Tree with many items"
20+
21+
[6074041f-4891-4d81-a128-401050c2a3b0]
22+
description = "Reject traversals of different length"
23+
24+
[27916ce4-45f3-4d8b-8528-496fedc157ca]
25+
description = "Reject inconsistent traversals of same length"
26+
27+
[d86a3d72-76a9-43b5-9d3a-e64cb1216035]
28+
description = "Reject traversals with repeated items"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
3+
public record Tree(char Value, Tree Left, Tree Right);
4+
5+
public static class Satellite
6+
{
7+
public static Tree TreeFromTraversals(char[] preOrder, char[] inOrder)
8+
{
9+
throw new NotImplementedException("You need to implement this method.");
10+
}
11+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<PackageReference Include="Exercism.Tests" Version="0.1.0-beta1" />
9+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
10+
<PackageReference Include="xunit" Version="2.8.1" />
11+
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.1">
12+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
13+
<PrivateAssets>all</PrivateAssets>
14+
</PackageReference>
15+
</ItemGroup>
16+
17+
</Project>

0 commit comments

Comments
 (0)