did a couple more over lunch

This commit is contained in:
Ben Harris 2021-11-12 14:58:27 -05:00
parent d4d5f1e4d0
commit b8b45b106c
49 changed files with 2330 additions and 0 deletions

View File

@ -95,6 +95,18 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RationalNumbers", "csharp\r
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PerfectNumbers", "csharp\perfect-numbers\PerfectNumbers.csproj", "{C424BB3E-AADC-42A0-889E-BE1C5A53C2F3}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ProteinTranslation", "csharp\protein-translation\ProteinTranslation.csproj", "{FCB4EC34-86A9-4E7A-BF83-AC618505ACE9}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DifferenceOfSquares", "csharp\difference-of-squares\DifferenceOfSquares.csproj", "{022A3C14-C825-4797-B4C1-80CE4BB1AD4B}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Triangle", "csharp\triangle\Triangle.csproj", "{53575277-DC47-4597-92FB-C3D28344B88F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Darts", "csharp\darts\Darts.csproj", "{4A6A7678-F939-4EBD-B50E-A68943F4016B}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FlattenArray", "csharp\flatten-array\FlattenArray.csproj", "{4C529743-AE36-42E5-A63F-9054ABD2E80B}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Proverb", "csharp\proverb\Proverb.csproj", "{CEC032AD-5DF7-4FDD-B389-764B96323A8E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -285,6 +297,30 @@ Global
{C424BB3E-AADC-42A0-889E-BE1C5A53C2F3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C424BB3E-AADC-42A0-889E-BE1C5A53C2F3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C424BB3E-AADC-42A0-889E-BE1C5A53C2F3}.Release|Any CPU.Build.0 = Release|Any CPU
{FCB4EC34-86A9-4E7A-BF83-AC618505ACE9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FCB4EC34-86A9-4E7A-BF83-AC618505ACE9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FCB4EC34-86A9-4E7A-BF83-AC618505ACE9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FCB4EC34-86A9-4E7A-BF83-AC618505ACE9}.Release|Any CPU.Build.0 = Release|Any CPU
{022A3C14-C825-4797-B4C1-80CE4BB1AD4B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{022A3C14-C825-4797-B4C1-80CE4BB1AD4B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{022A3C14-C825-4797-B4C1-80CE4BB1AD4B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{022A3C14-C825-4797-B4C1-80CE4BB1AD4B}.Release|Any CPU.Build.0 = Release|Any CPU
{53575277-DC47-4597-92FB-C3D28344B88F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{53575277-DC47-4597-92FB-C3D28344B88F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{53575277-DC47-4597-92FB-C3D28344B88F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{53575277-DC47-4597-92FB-C3D28344B88F}.Release|Any CPU.Build.0 = Release|Any CPU
{4A6A7678-F939-4EBD-B50E-A68943F4016B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4A6A7678-F939-4EBD-B50E-A68943F4016B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4A6A7678-F939-4EBD-B50E-A68943F4016B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4A6A7678-F939-4EBD-B50E-A68943F4016B}.Release|Any CPU.Build.0 = Release|Any CPU
{4C529743-AE36-42E5-A63F-9054ABD2E80B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4C529743-AE36-42E5-A63F-9054ABD2E80B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4C529743-AE36-42E5-A63F-9054ABD2E80B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4C529743-AE36-42E5-A63F-9054ABD2E80B}.Release|Any CPU.Build.0 = Release|Any CPU
{CEC032AD-5DF7-4FDD-B389-764B96323A8E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CEC032AD-5DF7-4FDD-B389-764B96323A8E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CEC032AD-5DF7-4FDD-B389-764B96323A8E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CEC032AD-5DF7-4FDD-B389-764B96323A8E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

141
csharp/darts/.editorconfig Normal file
View File

@ -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
[Darts.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

View File

@ -0,0 +1,25 @@
{
"blurb": "Write a function that returns the earned points in a single toss of a Darts game",
"authors": [
"bmeverett"
],
"contributors": [
"ErikSchierboom",
"j2jensen",
"robkeim",
"sjwarner",
"wolf99"
],
"files": {
"solution": [
"Darts.cs"
],
"test": [
"DartsTests.cs"
],
"example": [
".meta/Example.cs"
]
},
"source": "Inspired by an exercise created by a professor Della Paolera in Argentina"
}

View File

@ -0,0 +1 @@
{"track":"csharp","exercise":"darts","id":"ed58a1226c504827a927b77954158cad","url":"https://exercism.org/tracks/csharp/exercises/darts","handle":"benharri","is_requester":true,"auto_approve":false}

13
csharp/darts/Darts.cs Normal file
View File

@ -0,0 +1,13 @@
using System;
public static class Darts
{
public static int Score(double x, double y) =>
Math.Sqrt(x * x + y * y) switch
{
<= 1 => 10,
<= 5 => 5,
<= 10 => 1,
_ => 0
};
}

14
csharp/darts/Darts.csproj Normal file
View File

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" />
<PackageReference Include="Exercism.Tests" Version="0.1.0-beta1" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,82 @@
using Xunit;
public class DartsTests
{
[Fact]
public void Missed_target()
{
Assert.Equal(0, Darts.Score(-9, 9));
}
[Fact]
public void On_the_outer_circle()
{
Assert.Equal(1, Darts.Score(0, 10));
}
[Fact]
public void On_the_middle_circle()
{
Assert.Equal(5, Darts.Score(-5, 0));
}
[Fact]
public void On_the_inner_circle()
{
Assert.Equal(10, Darts.Score(0, -1));
}
[Fact]
public void Exactly_on_centre()
{
Assert.Equal(10, Darts.Score(0, 0));
}
[Fact]
public void Near_the_centre()
{
Assert.Equal(10, Darts.Score(-0.1, -0.1));
}
[Fact]
public void Just_within_the_inner_circle()
{
Assert.Equal(10, Darts.Score(0.7, 0.7));
}
[Fact]
public void Just_outside_the_inner_circle()
{
Assert.Equal(5, Darts.Score(0.8, -0.8));
}
[Fact]
public void Just_within_the_middle_circle()
{
Assert.Equal(5, Darts.Score(-3.5, 3.5));
}
[Fact]
public void Just_outside_the_middle_circle()
{
Assert.Equal(1, Darts.Score(-3.6, -3.6));
}
[Fact]
public void Just_within_the_outer_circle()
{
Assert.Equal(1, Darts.Score(-7, 7));
}
[Fact]
public void Just_outside_the_outer_circle()
{
Assert.Equal(0, Darts.Score(7.1, -7.1));
}
[Fact]
public void Asymmetric_position_between_the_inner_and_middle_circles()
{
Assert.Equal(5, Darts.Score(0.5, -4));
}
}

39
csharp/darts/HELP.md Normal file
View File

@ -0,0 +1,39 @@
# Help
## Running the tests
You can run the tests by opening a command prompt in the exercise's directory, and then running the [`dotnet test` command](https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-test)
Alternatively, most IDE's have built-in support for running tests, including [Visual Studio](https://docs.microsoft.com/en-us/visualstudio/test/run-unit-tests-with-test-explorer), [Rider](https://www.jetbrains.com/help/rider/Unit_Testing_in_Solution.html) and [Visual Studio code](https://github.com/OmniSharp/omnisharp-vscode/wiki/How-to-run-and-debug-unit-tests).
See the [tests page](https://exercism.io/tracks/csharp/tests) for more information.
## Skipped tests
Initially, only the first test will be enabled.
This is to encourage you to solve the exercise one step at a time.
Once you get the first test passing, remove the `Skip` property from the next test and work on getting that test passing.
## Submitting your solution
You can submit your solution using the `exercism submit Darts.cs` command.
This command will upload your solution to the Exercism website and print the solution page's URL.
It's possible to submit an incomplete solution which allows you to:
- See how others have completed the exercise
- Request help from a mentor
## Need to get help?
If you'd like help solving the exercise, check the following pages:
- The [C# track's documentation](https://exercism.org/docs/tracks/csharp)
- [Exercism's support channel on gitter](https://gitter.im/exercism/support)
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
To get help if you're having trouble, you can use one of the following resources:
- [Gitter](https://gitter.im/exercism/xcsharp) is Exercism C# track's Gitter room; go here to get support and ask questions related to the C# track.
- [/r/csharp](https://www.reddit.com/r/csharp) is the C# subreddit.
- [StackOverflow](http://stackoverflow.com/questions/tagged/c%23) can be used to search for your problem and see if it has been answered already. You can also ask and answer questions.

40
csharp/darts/README.md Normal file
View File

@ -0,0 +1,40 @@
# Darts
Welcome to Darts on Exercism's C# Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
## Instructions
Write a function that returns the earned points in a single toss of a Darts game.
[Darts](https://en.wikipedia.org/wiki/Darts) is a game where players
throw darts to a [target](https://en.wikipedia.org/wiki/Darts#/media/File:Darts_in_a_dartboard.jpg).
In our particular instance of the game, the target rewards with 4 different amounts of points, depending on where the dart lands:
* If the dart lands outside the target, player earns no points (0 points).
* If the dart lands in the outer circle of the target, player earns 1 point.
* If the dart lands in the middle circle of the target, player earns 5 points.
* If the dart lands in the inner circle of the target, player earns 10 points.
The outer circle has a radius of 10 units (This is equivalent to the total radius for the entire target), the middle circle a radius of 5 units, and the inner circle a radius of 1. Of course, they are all centered to the same point (That is, the circles are [concentric](http://mathworld.wolfram.com/ConcentricCircles.html)) defined by the coordinates (0, 0).
Write a function that given a point in the target (defined by its `real` cartesian coordinates `x` and `y`), returns the correct amount earned by a dart landing in that point.
## Source
### Created by
- @bmeverett
### Contributed to by
- @ErikSchierboom
- @j2jensen
- @robkeim
- @sjwarner
- @wolf99
### Based on
Inspired by an exercise created by a professor Della Paolera in Argentina

View File

@ -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
[DifferenceOfSquares.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

View File

@ -0,0 +1,29 @@
{
"blurb": "Find the difference between the square of the sum and the sum of the squares of the first N natural numbers.",
"authors": [
"mikecoop"
],
"contributors": [
"balazsbotond",
"ErikSchierboom",
"felix91gr",
"j2jensen",
"jwood803",
"kytrinyx",
"robkeim",
"wolf99"
],
"files": {
"solution": [
"DifferenceOfSquares.cs"
],
"test": [
"DifferenceOfSquaresTests.cs"
],
"example": [
".meta/Example.cs"
]
},
"source": "Problem 6 at Project Euler",
"source_url": "http://projecteuler.net/problem=6"
}

View File

@ -0,0 +1 @@
{"track":"csharp","exercise":"difference-of-squares","id":"1b64bac5568349a7ab3fe1767ccee214","url":"https://exercism.org/tracks/csharp/exercises/difference-of-squares","handle":"benharri","is_requester":true,"auto_approve":false}

View File

@ -0,0 +1,16 @@
using System.Linq;
public static class DifferenceOfSquares
{
public static int CalculateSquareOfSum(int max)
{
var sum = Enumerable.Range(1, max).Sum();
return sum * sum;
}
public static int CalculateSumOfSquares(int max) =>
Enumerable.Range(1, max).Sum(i => i * i);
public static int CalculateDifferenceOfSquares(int max) =>
CalculateSquareOfSum(max) - CalculateSumOfSquares(max);
}

View File

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" />
<PackageReference Include="Exercism.Tests" Version="0.1.0-beta1" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,58 @@
using Xunit;
public class DifferenceOfSquaresTests
{
[Fact]
public void Square_of_sum_1()
{
Assert.Equal(1, DifferenceOfSquares.CalculateSquareOfSum(1));
}
[Fact]
public void Square_of_sum_5()
{
Assert.Equal(225, DifferenceOfSquares.CalculateSquareOfSum(5));
}
[Fact]
public void Square_of_sum_100()
{
Assert.Equal(25502500, DifferenceOfSquares.CalculateSquareOfSum(100));
}
[Fact]
public void Sum_of_squares_1()
{
Assert.Equal(1, DifferenceOfSquares.CalculateSumOfSquares(1));
}
[Fact]
public void Sum_of_squares_5()
{
Assert.Equal(55, DifferenceOfSquares.CalculateSumOfSquares(5));
}
[Fact]
public void Sum_of_squares_100()
{
Assert.Equal(338350, DifferenceOfSquares.CalculateSumOfSquares(100));
}
[Fact]
public void Difference_of_squares_1()
{
Assert.Equal(0, DifferenceOfSquares.CalculateDifferenceOfSquares(1));
}
[Fact]
public void Difference_of_squares_5()
{
Assert.Equal(170, DifferenceOfSquares.CalculateDifferenceOfSquares(5));
}
[Fact]
public void Difference_of_squares_100()
{
Assert.Equal(25164150, DifferenceOfSquares.CalculateDifferenceOfSquares(100));
}
}

View File

@ -0,0 +1,39 @@
# Help
## Running the tests
You can run the tests by opening a command prompt in the exercise's directory, and then running the [`dotnet test` command](https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-test)
Alternatively, most IDE's have built-in support for running tests, including [Visual Studio](https://docs.microsoft.com/en-us/visualstudio/test/run-unit-tests-with-test-explorer), [Rider](https://www.jetbrains.com/help/rider/Unit_Testing_in_Solution.html) and [Visual Studio code](https://github.com/OmniSharp/omnisharp-vscode/wiki/How-to-run-and-debug-unit-tests).
See the [tests page](https://exercism.io/tracks/csharp/tests) for more information.
## Skipped tests
Initially, only the first test will be enabled.
This is to encourage you to solve the exercise one step at a time.
Once you get the first test passing, remove the `Skip` property from the next test and work on getting that test passing.
## Submitting your solution
You can submit your solution using the `exercism submit DifferenceOfSquares.cs` command.
This command will upload your solution to the Exercism website and print the solution page's URL.
It's possible to submit an incomplete solution which allows you to:
- See how others have completed the exercise
- Request help from a mentor
## Need to get help?
If you'd like help solving the exercise, check the following pages:
- The [C# track's documentation](https://exercism.org/docs/tracks/csharp)
- [Exercism's support channel on gitter](https://gitter.im/exercism/support)
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
To get help if you're having trouble, you can use one of the following resources:
- [Gitter](https://gitter.im/exercism/xcsharp) is Exercism C# track's Gitter room; go here to get support and ask questions related to the C# track.
- [/r/csharp](https://www.reddit.com/r/csharp) is the C# subreddit.
- [StackOverflow](http://stackoverflow.com/questions/tagged/c%23) can be used to search for your problem and see if it has been answered already. You can also ask and answer questions.

View File

@ -0,0 +1,47 @@
# Difference Of Squares
Welcome to Difference Of Squares on Exercism's C# Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
## Instructions
Find the difference between the square of the sum and the sum of the squares of the first N natural numbers.
The square of the sum of the first ten natural numbers is
(1 + 2 + ... + 10)² = 55² = 3025.
The sum of the squares of the first ten natural numbers is
1² + 2² + ... + 10² = 385.
Hence the difference between the square of the sum of the first
ten natural numbers and the sum of the squares of the first ten
natural numbers is 3025 - 385 = 2640.
You are not expected to discover an efficient solution to this yourself from
first principles; research is allowed, indeed, encouraged. Finding the best
algorithm for the problem is a key skill in software engineering.
This exercise requires you to process a collection of data. You can simplify your code by using LINQ (Language Integrated Query).
For more information, see [this page]
(https://docs.microsoft.com/en-us/dotnet/articles/standard/using-linq).
## Source
### Created by
- @mikecoop
### Contributed to by
- @balazsbotond
- @ErikSchierboom
- @felix91gr
- @j2jensen
- @jwood803
- @kytrinyx
- @robkeim
- @wolf99
### Based on
Problem 6 at Project Euler - http://projecteuler.net/problem=6

View File

@ -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
[FlattenArray.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

View File

@ -0,0 +1,25 @@
{
"blurb": "Take a nested list and return a single list with all values except nil/null",
"authors": [
"ErikSchierboom"
],
"contributors": [
"felix91gr",
"j2jensen",
"robkeim",
"wolf99"
],
"files": {
"solution": [
"FlattenArray.cs"
],
"test": [
"FlattenArrayTests.cs"
],
"example": [
".meta/Example.cs"
]
},
"source": "Interview Question",
"source_url": "https://reference.wolfram.com/language/ref/Flatten.html"
}

View File

@ -0,0 +1 @@
{"track":"csharp","exercise":"flatten-array","id":"f9c4a3fffbab4276bc9e1638983045a9","url":"https://exercism.org/tracks/csharp/exercises/flatten-array","handle":"benharri","is_requester":true,"auto_approve":false}

View File

@ -0,0 +1,17 @@
using System.Collections;
public static class FlattenArray
{
public static IEnumerable Flatten(IEnumerable input)
{
foreach (var element in input)
{
if (element is null) continue;
if (element is IEnumerable)
foreach (var nestedElement in Flatten(element as IEnumerable))
yield return nestedElement;
else
yield return element;
}
}
}

View File

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" />
<PackageReference Include="Exercism.Tests" Version="0.1.0-beta1" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,86 @@
using Xunit;
public class FlattenArrayTests
{
[Fact]
public void No_nesting()
{
var array = new object[]
{
0,
1,
2
};
var expected = new[] { 0, 1, 2 };
Assert.Equal(expected, FlattenArray.Flatten(array));
}
[Fact]
public void Flattens_array_with_just_integers_present()
{
var array = new object[]
{
1,
new object[] { 2, 3, 4, 5, 6, 7 },
8
};
var expected = new[] { 1, 2, 3, 4, 5, 6, 7, 8 };
Assert.Equal(expected, FlattenArray.Flatten(array));
}
[Fact]
public void Number_5_level_nesting()
{
var array = new object[]
{
0,
2,
new object[] { new object[] { 2, 3 }, 8, 100, 4, new object[] { new object[] { new object[] { 50 } } } },
-2
};
var expected = new[] { 0, 2, 2, 3, 8, 100, 4, 50, -2 };
Assert.Equal(expected, FlattenArray.Flatten(array));
}
[Fact]
public void Number_6_level_nesting()
{
var array = new object[]
{
1,
new object[] { 2, new object[] { new object[] { 3 } }, new object[] { 4, new object[] { new object[] { 5 } } }, 6, 7 },
8
};
var expected = new[] { 1, 2, 3, 4, 5, 6, 7, 8 };
Assert.Equal(expected, FlattenArray.Flatten(array));
}
[Fact]
public void Number_6_level_nest_list_with_null_values()
{
var array = new object[]
{
0,
2,
new object[] { new object[] { 2, 3 }, 8, new object[] { new object[] { 100 } }, null, new object[] { new object[] { null } } },
-2
};
var expected = new[] { 0, 2, 2, 3, 8, 100, -2 };
Assert.Equal(expected, FlattenArray.Flatten(array));
}
[Fact]
public void All_values_in_nested_list_are_null()
{
var array = new object[]
{
null,
new object[] { new object[] { new object[] { null } } },
null,
null,
new object[] { new object[] { null, null }, null },
null
};
Assert.Empty(FlattenArray.Flatten(array));
}
}

View File

@ -0,0 +1,39 @@
# Help
## Running the tests
You can run the tests by opening a command prompt in the exercise's directory, and then running the [`dotnet test` command](https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-test)
Alternatively, most IDE's have built-in support for running tests, including [Visual Studio](https://docs.microsoft.com/en-us/visualstudio/test/run-unit-tests-with-test-explorer), [Rider](https://www.jetbrains.com/help/rider/Unit_Testing_in_Solution.html) and [Visual Studio code](https://github.com/OmniSharp/omnisharp-vscode/wiki/How-to-run-and-debug-unit-tests).
See the [tests page](https://exercism.io/tracks/csharp/tests) for more information.
## Skipped tests
Initially, only the first test will be enabled.
This is to encourage you to solve the exercise one step at a time.
Once you get the first test passing, remove the `Skip` property from the next test and work on getting that test passing.
## Submitting your solution
You can submit your solution using the `exercism submit FlattenArray.cs` command.
This command will upload your solution to the Exercism website and print the solution page's URL.
It's possible to submit an incomplete solution which allows you to:
- See how others have completed the exercise
- Request help from a mentor
## Need to get help?
If you'd like help solving the exercise, check the following pages:
- The [C# track's documentation](https://exercism.org/docs/tracks/csharp)
- [Exercism's support channel on gitter](https://gitter.im/exercism/support)
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
To get help if you're having trouble, you can use one of the following resources:
- [Gitter](https://gitter.im/exercism/xcsharp) is Exercism C# track's Gitter room; go here to get support and ask questions related to the C# track.
- [/r/csharp](https://www.reddit.com/r/csharp) is the C# subreddit.
- [StackOverflow](http://stackoverflow.com/questions/tagged/c%23) can be used to search for your problem and see if it has been answered already. You can also ask and answer questions.

View File

@ -0,0 +1,33 @@
# Flatten Array
Welcome to Flatten Array on Exercism's C# Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
## Instructions
Take a nested list and return a single flattened list with all values except nil/null.
The challenge is to write a function that accepts an arbitrarily-deep nested list-like structure and returns a flattened structure without any nil/null values.
For Example
input: [1,[2,3,null,4],[null],5]
output: [1,2,3,4,5]
## Source
### Created by
- @ErikSchierboom
### Contributed to by
- @felix91gr
- @j2jensen
- @robkeim
- @wolf99
### Based on
Interview Question - https://reference.wolfram.com/language/ref/Flatten.html

View File

@ -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
[ProteinTranslation.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

View File

@ -0,0 +1,23 @@
{
"blurb": "Translate RNA sequences into proteins.",
"authors": [
"robkeim"
],
"contributors": [
"ErikSchierboom",
"j2jensen",
"wolf99"
],
"files": {
"solution": [
"ProteinTranslation.cs"
],
"test": [
"ProteinTranslationTests.cs"
],
"example": [
".meta/Example.cs"
]
},
"source": "Tyler Long"
}

View File

@ -0,0 +1 @@
{"track":"csharp","exercise":"protein-translation","id":"998e522961fd47f9880d643ee8216abc","url":"https://exercism.org/tracks/csharp/exercises/protein-translation","handle":"benharri","is_requester":true,"auto_approve":false}

View File

@ -0,0 +1,39 @@
# Help
## Running the tests
You can run the tests by opening a command prompt in the exercise's directory, and then running the [`dotnet test` command](https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-test)
Alternatively, most IDE's have built-in support for running tests, including [Visual Studio](https://docs.microsoft.com/en-us/visualstudio/test/run-unit-tests-with-test-explorer), [Rider](https://www.jetbrains.com/help/rider/Unit_Testing_in_Solution.html) and [Visual Studio code](https://github.com/OmniSharp/omnisharp-vscode/wiki/How-to-run-and-debug-unit-tests).
See the [tests page](https://exercism.io/tracks/csharp/tests) for more information.
## Skipped tests
Initially, only the first test will be enabled.
This is to encourage you to solve the exercise one step at a time.
Once you get the first test passing, remove the `Skip` property from the next test and work on getting that test passing.
## Submitting your solution
You can submit your solution using the `exercism submit ProteinTranslation.cs` command.
This command will upload your solution to the Exercism website and print the solution page's URL.
It's possible to submit an incomplete solution which allows you to:
- See how others have completed the exercise
- Request help from a mentor
## Need to get help?
If you'd like help solving the exercise, check the following pages:
- The [C# track's documentation](https://exercism.org/docs/tracks/csharp)
- [Exercism's support channel on gitter](https://gitter.im/exercism/support)
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
To get help if you're having trouble, you can use one of the following resources:
- [Gitter](https://gitter.im/exercism/xcsharp) is Exercism C# track's Gitter room; go here to get support and ask questions related to the C# track.
- [/r/csharp](https://www.reddit.com/r/csharp) is the C# subreddit.
- [StackOverflow](http://stackoverflow.com/questions/tagged/c%23) can be used to search for your problem and see if it has been answered already. You can also ask and answer questions.

View File

@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
public static class ProteinTranslation
{
public static string[] Proteins(string strand) =>
strand.Chunk(3).Select(p => p switch
{
"AUG" => "Methionine",
"UUU" or "UUC" => "Phenylalanine",
"UUA" or "UUG" => "Leucine",
"UCU" or "UCC" or "UCA" or "UCG" => "Serine",
"UAU" or "UAC" => "Tyrosine",
"UGU" or "UGC" => "Cysteine",
"UGG" => "Tryptophan",
"UAA" or "UAG" or "UGA" => "",
_ => throw new ArgumentOutOfRangeException(nameof(strand))
}).TakeWhile(p => p != "").ToArray();
public static IEnumerable<string> Chunk(this string s, int chunkSize) =>
Enumerable.Range(0, s.Length / chunkSize)
.Select(i => s.Substring(i * chunkSize, chunkSize));
}

View File

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" />
<PackageReference Include="Exercism.Tests" Version="0.1.0-beta1" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,142 @@
using Xunit;
public class ProteinTranslationTests
{
[Fact]
public void Methionine_rna_sequence()
{
Assert.Equal(new[] { "Methionine" }, ProteinTranslation.Proteins("AUG"));
}
[Fact]
public void Phenylalanine_rna_sequence_1()
{
Assert.Equal(new[] { "Phenylalanine" }, ProteinTranslation.Proteins("UUU"));
}
[Fact]
public void Phenylalanine_rna_sequence_2()
{
Assert.Equal(new[] { "Phenylalanine" }, ProteinTranslation.Proteins("UUC"));
}
[Fact]
public void Leucine_rna_sequence_1()
{
Assert.Equal(new[] { "Leucine" }, ProteinTranslation.Proteins("UUA"));
}
[Fact]
public void Leucine_rna_sequence_2()
{
Assert.Equal(new[] { "Leucine" }, ProteinTranslation.Proteins("UUG"));
}
[Fact]
public void Serine_rna_sequence_1()
{
Assert.Equal(new[] { "Serine" }, ProteinTranslation.Proteins("UCU"));
}
[Fact]
public void Serine_rna_sequence_2()
{
Assert.Equal(new[] { "Serine" }, ProteinTranslation.Proteins("UCC"));
}
[Fact]
public void Serine_rna_sequence_3()
{
Assert.Equal(new[] { "Serine" }, ProteinTranslation.Proteins("UCA"));
}
[Fact]
public void Serine_rna_sequence_4()
{
Assert.Equal(new[] { "Serine" }, ProteinTranslation.Proteins("UCG"));
}
[Fact]
public void Tyrosine_rna_sequence_1()
{
Assert.Equal(new[] { "Tyrosine" }, ProteinTranslation.Proteins("UAU"));
}
[Fact]
public void Tyrosine_rna_sequence_2()
{
Assert.Equal(new[] { "Tyrosine" }, ProteinTranslation.Proteins("UAC"));
}
[Fact]
public void Cysteine_rna_sequence_1()
{
Assert.Equal(new[] { "Cysteine" }, ProteinTranslation.Proteins("UGU"));
}
[Fact]
public void Cysteine_rna_sequence_2()
{
Assert.Equal(new[] { "Cysteine" }, ProteinTranslation.Proteins("UGC"));
}
[Fact]
public void Tryptophan_rna_sequence()
{
Assert.Equal(new[] { "Tryptophan" }, ProteinTranslation.Proteins("UGG"));
}
[Fact]
public void Stop_codon_rna_sequence_1()
{
Assert.Empty(ProteinTranslation.Proteins("UAA"));
}
[Fact]
public void Stop_codon_rna_sequence_2()
{
Assert.Empty(ProteinTranslation.Proteins("UAG"));
}
[Fact]
public void Stop_codon_rna_sequence_3()
{
Assert.Empty(ProteinTranslation.Proteins("UGA"));
}
[Fact]
public void Translate_rna_strand_into_correct_protein_list()
{
Assert.Equal(new[] { "Methionine", "Phenylalanine", "Tryptophan" }, ProteinTranslation.Proteins("AUGUUUUGG"));
}
[Fact]
public void Translation_stops_if_stop_codon_at_beginning_of_sequence()
{
Assert.Empty(ProteinTranslation.Proteins("UAGUGG"));
}
[Fact]
public void Translation_stops_if_stop_codon_at_end_of_two_codon_sequence()
{
Assert.Equal(new[] { "Tryptophan" }, ProteinTranslation.Proteins("UGGUAG"));
}
[Fact]
public void Translation_stops_if_stop_codon_at_end_of_three_codon_sequence()
{
Assert.Equal(new[] { "Methionine", "Phenylalanine" }, ProteinTranslation.Proteins("AUGUUUUAA"));
}
[Fact]
public void Translation_stops_if_stop_codon_in_middle_of_three_codon_sequence()
{
Assert.Equal(new[] { "Tryptophan" }, ProteinTranslation.Proteins("UGGUAGUGG"));
}
[Fact]
public void Translation_stops_if_stop_codon_in_middle_of_six_codon_sequence()
{
Assert.Equal(new[] { "Tryptophan", "Cysteine", "Tyrosine" }, ProteinTranslation.Proteins("UGGUGUUAUUAAUGGUUU"));
}
}

View File

@ -0,0 +1,63 @@
# Protein Translation
Welcome to Protein Translation on Exercism's C# Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
## Instructions
Translate RNA sequences into proteins.
RNA can be broken into three nucleotide sequences called codons, and then translated to a polypeptide like so:
RNA: `"AUGUUUUCU"` => translates to
Codons: `"AUG", "UUU", "UCU"`
=> which become a polypeptide with the following sequence =>
Protein: `"Methionine", "Phenylalanine", "Serine"`
There are 64 codons which in turn correspond to 20 amino acids; however, all of the codon sequences and resulting amino acids are not important in this exercise. If it works for one codon, the program should work for all of them.
However, feel free to expand the list in the test suite to include them all.
There are also three terminating codons (also known as 'STOP' codons); if any of these codons are encountered (by the ribosome), all translation ends and the protein is terminated.
All subsequent codons after are ignored, like this:
RNA: `"AUGUUUUCUUAAAUG"` =>
Codons: `"AUG", "UUU", "UCU", "UAA", "AUG"` =>
Protein: `"Methionine", "Phenylalanine", "Serine"`
Note the stop codon `"UAA"` terminates the translation and the final methionine is not translated into the protein sequence.
Below are the codons and resulting Amino Acids needed for the exercise.
Codon | Protein
:--- | :---
AUG | Methionine
UUU, UUC | Phenylalanine
UUA, UUG | Leucine
UCU, UCC, UCA, UCG | Serine
UAU, UAC | Tyrosine
UGU, UGC | Cysteine
UGG | Tryptophan
UAA, UAG, UGA | STOP
Learn more about [protein translation on Wikipedia](http://en.wikipedia.org/wiki/Translation_(biology))
## Source
### Created by
- @robkeim
### Contributed to by
- @ErikSchierboom
- @j2jensen
- @wolf99
### Based on
Tyler Long

View File

@ -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
[Proverb.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

View File

@ -0,0 +1,26 @@
{
"blurb": "For want of a horseshoe nail, a kingdom was lost, or so the saying goes. Output the full text of this proverbial rhyme.",
"authors": [
"ErikSchierboom"
],
"contributors": [
"j2jensen",
"jmbradnan",
"robkeim",
"ShamilS",
"wolf99"
],
"files": {
"solution": [
"Proverb.cs"
],
"test": [
"ProverbTests.cs"
],
"example": [
".meta/Example.cs"
]
},
"source": "Wikipedia",
"source_url": "http://en.wikipedia.org/wiki/For_Want_of_a_Nail"
}

View File

@ -0,0 +1 @@
{"track":"csharp","exercise":"proverb","id":"2fb26d386d05442aaea34958e319d67c","url":"https://exercism.org/tracks/csharp/exercises/proverb","handle":"benharri","is_requester":true,"auto_approve":false}

39
csharp/proverb/HELP.md Normal file
View File

@ -0,0 +1,39 @@
# Help
## Running the tests
You can run the tests by opening a command prompt in the exercise's directory, and then running the [`dotnet test` command](https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-test)
Alternatively, most IDE's have built-in support for running tests, including [Visual Studio](https://docs.microsoft.com/en-us/visualstudio/test/run-unit-tests-with-test-explorer), [Rider](https://www.jetbrains.com/help/rider/Unit_Testing_in_Solution.html) and [Visual Studio code](https://github.com/OmniSharp/omnisharp-vscode/wiki/How-to-run-and-debug-unit-tests).
See the [tests page](https://exercism.io/tracks/csharp/tests) for more information.
## Skipped tests
Initially, only the first test will be enabled.
This is to encourage you to solve the exercise one step at a time.
Once you get the first test passing, remove the `Skip` property from the next test and work on getting that test passing.
## Submitting your solution
You can submit your solution using the `exercism submit Proverb.cs` command.
This command will upload your solution to the Exercism website and print the solution page's URL.
It's possible to submit an incomplete solution which allows you to:
- See how others have completed the exercise
- Request help from a mentor
## Need to get help?
If you'd like help solving the exercise, check the following pages:
- The [C# track's documentation](https://exercism.org/docs/tracks/csharp)
- [Exercism's support channel on gitter](https://gitter.im/exercism/support)
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
To get help if you're having trouble, you can use one of the following resources:
- [Gitter](https://gitter.im/exercism/xcsharp) is Exercism C# track's Gitter room; go here to get support and ask questions related to the C# track.
- [/r/csharp](https://www.reddit.com/r/csharp) is the C# subreddit.
- [StackOverflow](http://stackoverflow.com/questions/tagged/c%23) can be used to search for your problem and see if it has been answered already. You can also ask and answer questions.

14
csharp/proverb/Proverb.cs Normal file
View File

@ -0,0 +1,14 @@
using System;
public static class Proverb
{
public static string[] Recite(string[] subjects)
{
if (subjects.Length == 0) return Array.Empty<string>();
var result = new string[subjects.Length];
for (int i = 0; i < subjects.Length - 1; i++)
result[i] = $"For want of a {subjects[i]} the {subjects[i + 1]} was lost.";
result[subjects.Length - 1] = $"And all for the want of a {subjects[0]}.";
return result;
}
}

View File

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" />
<PackageReference Include="Exercism.Tests" Version="0.1.0-beta1" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,107 @@
using System;
using Xunit;
public class ProverbTests
{
[Fact]
public void Zero_pieces()
{
var strings = Array.Empty<string>();
var expected = Array.Empty<string>();
Assert.Equal(expected, Proverb.Recite(strings));
}
[Fact]
public void One_piece()
{
var strings = new[]
{
"nail"
};
var expected = new[]
{
"And all for the want of a nail."
};
Assert.Equal(expected, Proverb.Recite(strings));
}
[Fact]
public void Two_pieces()
{
var strings = new[]
{
"nail",
"shoe"
};
var expected = new[]
{
"For want of a nail the shoe was lost.",
"And all for the want of a nail."
};
Assert.Equal(expected, Proverb.Recite(strings));
}
[Fact]
public void Three_pieces()
{
var strings = new[]
{
"nail",
"shoe",
"horse"
};
var expected = new[]
{
"For want of a nail the shoe was lost.",
"For want of a shoe the horse was lost.",
"And all for the want of a nail."
};
Assert.Equal(expected, Proverb.Recite(strings));
}
[Fact]
public void Full_proverb()
{
var strings = new[]
{
"nail",
"shoe",
"horse",
"rider",
"message",
"battle",
"kingdom"
};
var expected = new[]
{
"For want of a nail the shoe was lost.",
"For want of a shoe the horse was lost.",
"For want of a horse the rider was lost.",
"For want of a rider the message was lost.",
"For want of a message the battle was lost.",
"For want of a battle the kingdom was lost.",
"And all for the want of a nail."
};
Assert.Equal(expected, Proverb.Recite(strings));
}
[Fact]
public void Four_pieces_modernized()
{
var strings = new[]
{
"pin",
"gun",
"soldier",
"battle"
};
var expected = new[]
{
"For want of a pin the gun was lost.",
"For want of a gun the soldier was lost.",
"For want of a soldier the battle was lost.",
"And all for the want of a pin."
};
Assert.Equal(expected, Proverb.Recite(strings));
}
}

42
csharp/proverb/README.md Normal file
View File

@ -0,0 +1,42 @@
# Proverb
Welcome to Proverb on Exercism's C# Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
## Instructions
For want of a horseshoe nail, a kingdom was lost, or so the saying goes.
Given a list of inputs, generate the relevant proverb. For example, given the list `["nail", "shoe", "horse", "rider", "message", "battle", "kingdom"]`, you will output the full text of this proverbial rhyme:
```text
For want of a nail the shoe was lost.
For want of a shoe the horse was lost.
For want of a horse the rider was lost.
For want of a rider the message was lost.
For want of a message the battle was lost.
For want of a battle the kingdom was lost.
And all for the want of a nail.
```
Note that the list of inputs may vary; your solution should be able to handle lists of arbitrary length and content. No line of the output text should be a static, unchanging string; all should vary according to the input given.
Try to capture the structure of the song in your code, where you build up the song by composing its parts.
## Source
### Created by
- @ErikSchierboom
### Contributed to by
- @j2jensen
- @jmbradnan
- @robkeim
- @ShamilS
- @wolf99
### Based on
Wikipedia - http://en.wikipedia.org/wiki/For_Want_of_a_Nail

View File

@ -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
[Triangle.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

View File

@ -0,0 +1,28 @@
{
"blurb": "Determine if a triangle is equilateral, isosceles, or scalene.",
"authors": [
"bressain"
],
"contributors": [
"bmeverett",
"ErikSchierboom",
"j2jensen",
"jwood803",
"robkeim",
"tushartyagi",
"wolf99"
],
"files": {
"solution": [
"Triangle.cs"
],
"test": [
"TriangleTests.cs"
],
"example": [
".meta/Example.cs"
]
},
"source": "The Ruby Koans triangle project, parts 1 & 2",
"source_url": "http://rubykoans.com"
}

View File

@ -0,0 +1 @@
{"track":"csharp","exercise":"triangle","id":"03abff63966644beb3cfbb5c2417ccd6","url":"https://exercism.org/tracks/csharp/exercises/triangle","handle":"benharri","is_requester":true,"auto_approve":false}

39
csharp/triangle/HELP.md Normal file
View File

@ -0,0 +1,39 @@
# Help
## Running the tests
You can run the tests by opening a command prompt in the exercise's directory, and then running the [`dotnet test` command](https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-test)
Alternatively, most IDE's have built-in support for running tests, including [Visual Studio](https://docs.microsoft.com/en-us/visualstudio/test/run-unit-tests-with-test-explorer), [Rider](https://www.jetbrains.com/help/rider/Unit_Testing_in_Solution.html) and [Visual Studio code](https://github.com/OmniSharp/omnisharp-vscode/wiki/How-to-run-and-debug-unit-tests).
See the [tests page](https://exercism.io/tracks/csharp/tests) for more information.
## Skipped tests
Initially, only the first test will be enabled.
This is to encourage you to solve the exercise one step at a time.
Once you get the first test passing, remove the `Skip` property from the next test and work on getting that test passing.
## Submitting your solution
You can submit your solution using the `exercism submit Triangle.cs` command.
This command will upload your solution to the Exercism website and print the solution page's URL.
It's possible to submit an incomplete solution which allows you to:
- See how others have completed the exercise
- Request help from a mentor
## Need to get help?
If you'd like help solving the exercise, check the following pages:
- The [C# track's documentation](https://exercism.org/docs/tracks/csharp)
- [Exercism's support channel on gitter](https://gitter.im/exercism/support)
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
To get help if you're having trouble, you can use one of the following resources:
- [Gitter](https://gitter.im/exercism/xcsharp) is Exercism C# track's Gitter room; go here to get support and ask questions related to the C# track.
- [/r/csharp](https://www.reddit.com/r/csharp) is the C# subreddit.
- [StackOverflow](http://stackoverflow.com/questions/tagged/c%23) can be used to search for your problem and see if it has been answered already. You can also ask and answer questions.

48
csharp/triangle/README.md Normal file
View File

@ -0,0 +1,48 @@
# Triangle
Welcome to Triangle on Exercism's C# Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
## Instructions
Determine if a triangle is equilateral, isosceles, or scalene.
An _equilateral_ triangle has all three sides the same length.
An _isosceles_ triangle has at least two sides the same length. (It is sometimes
specified as having exactly two sides the same length, but for the purposes of
this exercise we'll say at least two.)
A _scalene_ triangle has all sides of different lengths.
## Note
For a shape to be a triangle at all, all sides have to be of length > 0, and
the sum of the lengths of any two sides must be greater than or equal to the
length of the third side. See [Triangle Inequality](https://en.wikipedia.org/wiki/Triangle_inequality).
## Dig Deeper
The case where the sum of the lengths of two sides _equals_ that of the
third is known as a _degenerate_ triangle - it has zero area and looks like
a single line. Feel free to add your own code/tests to check for degenerate triangles.
## Source
### Created by
- @bressain
### Contributed to by
- @bmeverett
- @ErikSchierboom
- @j2jensen
- @jwood803
- @robkeim
- @tushartyagi
- @wolf99
### Based on
The Ruby Koans triangle project, parts 1 & 2 - http://rubykoans.com

View File

@ -0,0 +1,18 @@
public static class Triangle
{
public static bool IsScalene(double side1, double side2, double side3) =>
side1 != side2 && side2 != side3 && side1 != side3
&& !IsDegenerate(side1, side2, side3);
public static bool IsIsosceles(double side1, double side2, double side3) =>
(side1 == side2 || side2 == side3 || side1 == side3)
&& !IsDegenerate(side1, side2, side3);
public static bool IsEquilateral(double side1, double side2, double side3) =>
side1 == side2 && side2 == side3
&& !IsDegenerate(side1, side2, side3);
private static bool IsDegenerate(double side1, double side2, double side3) =>
side1 == 0 || side2 == 0 || side3 == 0
|| (side1 >= side2 + side3) || (side2 >= side1 + side3) || (side3 >= side1 + side2);
}

View File

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" />
<PackageReference Include="Exercism.Tests" Version="0.1.0-beta1" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,118 @@
using Xunit;
public class TriangleTests
{
[Fact]
public void Equilateral_triangle_all_sides_are_equal()
{
Assert.True(Triangle.IsEquilateral(2, 2, 2));
}
[Fact]
public void Equilateral_triangle_any_side_is_unequal()
{
Assert.False(Triangle.IsEquilateral(2, 3, 2));
}
[Fact]
public void Equilateral_triangle_no_sides_are_equal()
{
Assert.False(Triangle.IsEquilateral(5, 4, 6));
}
[Fact]
public void Equilateral_triangle_all_zero_sides_is_not_a_triangle()
{
Assert.False(Triangle.IsEquilateral(0, 0, 0));
}
[Fact]
public void Equilateral_triangle_sides_may_be_floats()
{
Assert.True(Triangle.IsEquilateral(0.5, 0.5, 0.5));
}
[Fact]
public void Isosceles_triangle_last_two_sides_are_equal()
{
Assert.True(Triangle.IsIsosceles(3, 4, 4));
}
[Fact]
public void Isosceles_triangle_first_two_sides_are_equal()
{
Assert.True(Triangle.IsIsosceles(4, 4, 3));
}
[Fact]
public void Isosceles_triangle_first_and_last_sides_are_equal()
{
Assert.True(Triangle.IsIsosceles(4, 3, 4));
}
[Fact]
public void Isosceles_triangle_equilateral_triangles_are_also_isosceles()
{
Assert.True(Triangle.IsIsosceles(4, 4, 4));
}
[Fact]
public void Isosceles_triangle_no_sides_are_equal()
{
Assert.False(Triangle.IsIsosceles(2, 3, 4));
}
[Fact]
public void Isosceles_triangle_first_triangle_inequality_violation()
{
Assert.False(Triangle.IsIsosceles(1, 1, 3));
}
[Fact]
public void Isosceles_triangle_second_triangle_inequality_violation()
{
Assert.False(Triangle.IsIsosceles(1, 3, 1));
}
[Fact]
public void Isosceles_triangle_third_triangle_inequality_violation()
{
Assert.False(Triangle.IsIsosceles(3, 1, 1));
}
[Fact]
public void Isosceles_triangle_sides_may_be_floats()
{
Assert.True(Triangle.IsIsosceles(0.5, 0.4, 0.5));
}
[Fact]
public void Scalene_triangle_no_sides_are_equal()
{
Assert.True(Triangle.IsScalene(5, 4, 6));
}
[Fact]
public void Scalene_triangle_all_sides_are_equal()
{
Assert.False(Triangle.IsScalene(4, 4, 4));
}
[Fact]
public void Scalene_triangle_two_sides_are_equal()
{
Assert.False(Triangle.IsScalene(4, 4, 3));
}
[Fact]
public void Scalene_triangle_may_not_violate_triangle_inequality()
{
Assert.False(Triangle.IsScalene(7, 3, 2));
}
[Fact]
public void Scalene_triangle_sides_may_be_floats()
{
Assert.True(Triangle.IsScalene(0.5, 0.4, 0.6));
}
}