Skip to content

Commit

Permalink
Add satellite exercise (#2383)
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSchierboom authored Feb 15, 2025
1 parent 8729d74 commit 978bf64
Show file tree
Hide file tree
Showing 11 changed files with 371 additions and 0 deletions.
14 changes: 14 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2675,6 +2675,20 @@
"numbers"
],
"difficulty": 7
},
{
"slug": "satellite",
"name": "Satellite",
"uuid": "4da2ca3b-2fe5-4048-985b-95a3b24aaa36",
"practices": [
"for-loops"
],
"prerequisites": [
"generic-methods",
"chars",
"for-loops"
],
"difficulty": 7
}
],
"foregone": [
Expand Down
7 changes: 7 additions & 0 deletions exercises/Exercises.sln
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KillerSudokuHelper", "pract
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SquareRoot", "practice\square-root\SquareRoot.csproj", "{5C05051E-D46C-4544-9CF8-F2A748F63172}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Satellite", "practice\satellite\Satellite.csproj", "{8E276065-1371-4CFA-BA20-95225EC6AEBC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -1045,6 +1047,10 @@ Global
{BEBBD420-075D-46F1-AE51-CC9A05FECE4A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BEBBD420-075D-46F1-AE51-CC9A05FECE4A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BEBBD420-075D-46F1-AE51-CC9A05FECE4A}.Release|Any CPU.Build.0 = Release|Any CPU
{8E276065-1371-4CFA-BA20-95225EC6AEBC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8E276065-1371-4CFA-BA20-95225EC6AEBC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8E276065-1371-4CFA-BA20-95225EC6AEBC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8E276065-1371-4CFA-BA20-95225EC6AEBC}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -1222,6 +1228,7 @@ Global
{90C70CAA-A225-4D66-8B42-6AC82AD1D5DC} = {E276EF69-669A-43E0-88AC-8ABB17A9C026}
{5C05051E-D46C-4544-9CF8-F2A748F63172} = {E276EF69-669A-43E0-88AC-8ABB17A9C026}
{BEBBD420-075D-46F1-AE51-CC9A05FECE4A} = {E276EF69-669A-43E0-88AC-8ABB17A9C026}
{8E276065-1371-4CFA-BA20-95225EC6AEBC} = {E276EF69-669A-43E0-88AC-8ABB17A9C026}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {AB4EA6C9-5461-4024-BDC7-2AE0C3A85CD1}
Expand Down
27 changes: 27 additions & 0 deletions exercises/practice/satellite/.docs/instructions.md
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
141 changes: 141 additions & 0 deletions exercises/practice/satellite/.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

[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
30 changes: 30 additions & 0 deletions exercises/practice/satellite/.meta/Example.cs
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));
}
}
36 changes: 36 additions & 0 deletions exercises/practice/satellite/.meta/Generator.tpl
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 -}}
}
17 changes: 17 additions & 0 deletions exercises/practice/satellite/.meta/config.json
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."
}
28 changes: 28 additions & 0 deletions exercises/practice/satellite/.meta/tests.toml
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"
11 changes: 11 additions & 0 deletions exercises/practice/satellite/Satellite.cs
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.");
}
}
17 changes: 17 additions & 0 deletions exercises/practice/satellite/Satellite.csproj
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>
Loading

0 comments on commit 978bf64

Please sign in to comment.