some new ones i've been working on
This commit is contained in:
parent
908a4abb0e
commit
6de2165785
|
@ -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
|
||||
|
||||
[Allergies.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
|
|
@ -0,0 +1,27 @@
|
|||
{
|
||||
"blurb": "Given a person's allergy score, determine whether or not they're allergic to a given item, and their full list of allergies.",
|
||||
"authors": [
|
||||
"bressain"
|
||||
],
|
||||
"contributors": [
|
||||
"ErikSchierboom",
|
||||
"j2jensen",
|
||||
"jwood803",
|
||||
"NextNebula",
|
||||
"robkeim",
|
||||
"wolf99"
|
||||
],
|
||||
"files": {
|
||||
"solution": [
|
||||
"Allergies.cs"
|
||||
],
|
||||
"test": [
|
||||
"AllergiesTests.cs"
|
||||
],
|
||||
"example": [
|
||||
".meta/Example.cs"
|
||||
]
|
||||
},
|
||||
"source": "Jumpstart Lab Warm-up",
|
||||
"source_url": "http://jumpstartlab.com"
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
{"track":"csharp","exercise":"allergies","id":"7d5ad97240a2483785a4c8ee3e7ae09f","url":"https://exercism.org/tracks/csharp/exercises/allergies","handle":"benharri","is_requester":true,"auto_approve":false}
|
|
@ -0,0 +1,30 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
[Flags]
|
||||
public enum Allergen
|
||||
{
|
||||
Eggs = 1,
|
||||
Peanuts = 2,
|
||||
Shellfish = 4,
|
||||
Strawberries = 8,
|
||||
Tomatoes = 16,
|
||||
Chocolate = 32,
|
||||
Pollen = 64,
|
||||
Cats = 128,
|
||||
}
|
||||
|
||||
public class Allergies
|
||||
{
|
||||
private readonly Allergen _allergen;
|
||||
|
||||
public Allergies(int mask) =>
|
||||
_allergen = (Allergen)mask;
|
||||
|
||||
public bool IsAllergicTo(Allergen allergen) =>
|
||||
(_allergen & allergen) == allergen;
|
||||
|
||||
public Allergen[] List() =>
|
||||
Enum.GetValues(typeof(Allergen)).Cast<Allergen>().Where(a => (_allergen & a) == a).ToArray();
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net5.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>
|
|
@ -0,0 +1,355 @@
|
|||
using Xunit;
|
||||
|
||||
public class AllergiesTests
|
||||
{
|
||||
[Fact]
|
||||
public void Testing_for_eggs_allergy_not_allergic_to_anything()
|
||||
{
|
||||
var sut = new Allergies(0);
|
||||
Assert.False(sut.IsAllergicTo(Allergen.Eggs));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Testing_for_eggs_allergy_allergic_only_to_eggs()
|
||||
{
|
||||
var sut = new Allergies(1);
|
||||
Assert.True(sut.IsAllergicTo(Allergen.Eggs));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Testing_for_eggs_allergy_allergic_to_eggs_and_something_else()
|
||||
{
|
||||
var sut = new Allergies(3);
|
||||
Assert.True(sut.IsAllergicTo(Allergen.Eggs));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Testing_for_eggs_allergy_allergic_to_something_but_not_eggs()
|
||||
{
|
||||
var sut = new Allergies(2);
|
||||
Assert.False(sut.IsAllergicTo(Allergen.Eggs));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Testing_for_eggs_allergy_allergic_to_everything()
|
||||
{
|
||||
var sut = new Allergies(255);
|
||||
Assert.True(sut.IsAllergicTo(Allergen.Eggs));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Testing_for_peanuts_allergy_not_allergic_to_anything()
|
||||
{
|
||||
var sut = new Allergies(0);
|
||||
Assert.False(sut.IsAllergicTo(Allergen.Peanuts));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Testing_for_peanuts_allergy_allergic_only_to_peanuts()
|
||||
{
|
||||
var sut = new Allergies(2);
|
||||
Assert.True(sut.IsAllergicTo(Allergen.Peanuts));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Testing_for_peanuts_allergy_allergic_to_peanuts_and_something_else()
|
||||
{
|
||||
var sut = new Allergies(7);
|
||||
Assert.True(sut.IsAllergicTo(Allergen.Peanuts));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Testing_for_peanuts_allergy_allergic_to_something_but_not_peanuts()
|
||||
{
|
||||
var sut = new Allergies(5);
|
||||
Assert.False(sut.IsAllergicTo(Allergen.Peanuts));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Testing_for_peanuts_allergy_allergic_to_everything()
|
||||
{
|
||||
var sut = new Allergies(255);
|
||||
Assert.True(sut.IsAllergicTo(Allergen.Peanuts));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Testing_for_shellfish_allergy_not_allergic_to_anything()
|
||||
{
|
||||
var sut = new Allergies(0);
|
||||
Assert.False(sut.IsAllergicTo(Allergen.Shellfish));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Testing_for_shellfish_allergy_allergic_only_to_shellfish()
|
||||
{
|
||||
var sut = new Allergies(4);
|
||||
Assert.True(sut.IsAllergicTo(Allergen.Shellfish));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Testing_for_shellfish_allergy_allergic_to_shellfish_and_something_else()
|
||||
{
|
||||
var sut = new Allergies(14);
|
||||
Assert.True(sut.IsAllergicTo(Allergen.Shellfish));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Testing_for_shellfish_allergy_allergic_to_something_but_not_shellfish()
|
||||
{
|
||||
var sut = new Allergies(10);
|
||||
Assert.False(sut.IsAllergicTo(Allergen.Shellfish));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Testing_for_shellfish_allergy_allergic_to_everything()
|
||||
{
|
||||
var sut = new Allergies(255);
|
||||
Assert.True(sut.IsAllergicTo(Allergen.Shellfish));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Testing_for_strawberries_allergy_not_allergic_to_anything()
|
||||
{
|
||||
var sut = new Allergies(0);
|
||||
Assert.False(sut.IsAllergicTo(Allergen.Strawberries));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Testing_for_strawberries_allergy_allergic_only_to_strawberries()
|
||||
{
|
||||
var sut = new Allergies(8);
|
||||
Assert.True(sut.IsAllergicTo(Allergen.Strawberries));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Testing_for_strawberries_allergy_allergic_to_strawberries_and_something_else()
|
||||
{
|
||||
var sut = new Allergies(28);
|
||||
Assert.True(sut.IsAllergicTo(Allergen.Strawberries));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Testing_for_strawberries_allergy_allergic_to_something_but_not_strawberries()
|
||||
{
|
||||
var sut = new Allergies(20);
|
||||
Assert.False(sut.IsAllergicTo(Allergen.Strawberries));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Testing_for_strawberries_allergy_allergic_to_everything()
|
||||
{
|
||||
var sut = new Allergies(255);
|
||||
Assert.True(sut.IsAllergicTo(Allergen.Strawberries));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Testing_for_tomatoes_allergy_not_allergic_to_anything()
|
||||
{
|
||||
var sut = new Allergies(0);
|
||||
Assert.False(sut.IsAllergicTo(Allergen.Tomatoes));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Testing_for_tomatoes_allergy_allergic_only_to_tomatoes()
|
||||
{
|
||||
var sut = new Allergies(16);
|
||||
Assert.True(sut.IsAllergicTo(Allergen.Tomatoes));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Testing_for_tomatoes_allergy_allergic_to_tomatoes_and_something_else()
|
||||
{
|
||||
var sut = new Allergies(56);
|
||||
Assert.True(sut.IsAllergicTo(Allergen.Tomatoes));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Testing_for_tomatoes_allergy_allergic_to_something_but_not_tomatoes()
|
||||
{
|
||||
var sut = new Allergies(40);
|
||||
Assert.False(sut.IsAllergicTo(Allergen.Tomatoes));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Testing_for_tomatoes_allergy_allergic_to_everything()
|
||||
{
|
||||
var sut = new Allergies(255);
|
||||
Assert.True(sut.IsAllergicTo(Allergen.Tomatoes));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Testing_for_chocolate_allergy_not_allergic_to_anything()
|
||||
{
|
||||
var sut = new Allergies(0);
|
||||
Assert.False(sut.IsAllergicTo(Allergen.Chocolate));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Testing_for_chocolate_allergy_allergic_only_to_chocolate()
|
||||
{
|
||||
var sut = new Allergies(32);
|
||||
Assert.True(sut.IsAllergicTo(Allergen.Chocolate));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Testing_for_chocolate_allergy_allergic_to_chocolate_and_something_else()
|
||||
{
|
||||
var sut = new Allergies(112);
|
||||
Assert.True(sut.IsAllergicTo(Allergen.Chocolate));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Testing_for_chocolate_allergy_allergic_to_something_but_not_chocolate()
|
||||
{
|
||||
var sut = new Allergies(80);
|
||||
Assert.False(sut.IsAllergicTo(Allergen.Chocolate));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Testing_for_chocolate_allergy_allergic_to_everything()
|
||||
{
|
||||
var sut = new Allergies(255);
|
||||
Assert.True(sut.IsAllergicTo(Allergen.Chocolate));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Testing_for_pollen_allergy_not_allergic_to_anything()
|
||||
{
|
||||
var sut = new Allergies(0);
|
||||
Assert.False(sut.IsAllergicTo(Allergen.Pollen));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Testing_for_pollen_allergy_allergic_only_to_pollen()
|
||||
{
|
||||
var sut = new Allergies(64);
|
||||
Assert.True(sut.IsAllergicTo(Allergen.Pollen));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Testing_for_pollen_allergy_allergic_to_pollen_and_something_else()
|
||||
{
|
||||
var sut = new Allergies(224);
|
||||
Assert.True(sut.IsAllergicTo(Allergen.Pollen));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Testing_for_pollen_allergy_allergic_to_something_but_not_pollen()
|
||||
{
|
||||
var sut = new Allergies(160);
|
||||
Assert.False(sut.IsAllergicTo(Allergen.Pollen));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Testing_for_pollen_allergy_allergic_to_everything()
|
||||
{
|
||||
var sut = new Allergies(255);
|
||||
Assert.True(sut.IsAllergicTo(Allergen.Pollen));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Testing_for_cats_allergy_not_allergic_to_anything()
|
||||
{
|
||||
var sut = new Allergies(0);
|
||||
Assert.False(sut.IsAllergicTo(Allergen.Cats));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Testing_for_cats_allergy_allergic_only_to_cats()
|
||||
{
|
||||
var sut = new Allergies(128);
|
||||
Assert.True(sut.IsAllergicTo(Allergen.Cats));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Testing_for_cats_allergy_allergic_to_cats_and_something_else()
|
||||
{
|
||||
var sut = new Allergies(192);
|
||||
Assert.True(sut.IsAllergicTo(Allergen.Cats));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Testing_for_cats_allergy_allergic_to_something_but_not_cats()
|
||||
{
|
||||
var sut = new Allergies(64);
|
||||
Assert.False(sut.IsAllergicTo(Allergen.Cats));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Testing_for_cats_allergy_allergic_to_everything()
|
||||
{
|
||||
var sut = new Allergies(255);
|
||||
Assert.True(sut.IsAllergicTo(Allergen.Cats));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void No_allergies()
|
||||
{
|
||||
var sut = new Allergies(0);
|
||||
Assert.Empty(sut.List());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Just_eggs()
|
||||
{
|
||||
var sut = new Allergies(1);
|
||||
var expected = new[] { Allergen.Eggs };
|
||||
Assert.Equal(expected, sut.List());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Just_peanuts()
|
||||
{
|
||||
var sut = new Allergies(2);
|
||||
var expected = new[] { Allergen.Peanuts };
|
||||
Assert.Equal(expected, sut.List());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Just_strawberries()
|
||||
{
|
||||
var sut = new Allergies(8);
|
||||
var expected = new[] { Allergen.Strawberries };
|
||||
Assert.Equal(expected, sut.List());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Eggs_and_peanuts()
|
||||
{
|
||||
var sut = new Allergies(3);
|
||||
var expected = new[] { Allergen.Eggs, Allergen.Peanuts };
|
||||
Assert.Equal(expected, sut.List());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void More_than_eggs_but_not_peanuts()
|
||||
{
|
||||
var sut = new Allergies(5);
|
||||
var expected = new[] { Allergen.Eggs, Allergen.Shellfish };
|
||||
Assert.Equal(expected, sut.List());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Lots_of_stuff()
|
||||
{
|
||||
var sut = new Allergies(248);
|
||||
var expected = new[] { Allergen.Strawberries, Allergen.Tomatoes, Allergen.Chocolate, Allergen.Pollen, Allergen.Cats };
|
||||
Assert.Equal(expected, sut.List());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Everything()
|
||||
{
|
||||
var sut = new Allergies(255);
|
||||
var expected = new[] { Allergen.Eggs, Allergen.Peanuts, Allergen.Shellfish, Allergen.Strawberries, Allergen.Tomatoes, Allergen.Chocolate, Allergen.Pollen, Allergen.Cats };
|
||||
Assert.Equal(expected, sut.List());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void No_allergen_score_parts()
|
||||
{
|
||||
var sut = new Allergies(509);
|
||||
var expected = new[] { Allergen.Eggs, Allergen.Shellfish, Allergen.Strawberries, Allergen.Tomatoes, Allergen.Chocolate, Allergen.Pollen, Allergen.Cats };
|
||||
Assert.Equal(expected, sut.List());
|
||||
}
|
||||
}
|
|
@ -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 Allergies.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.
|
|
@ -0,0 +1,56 @@
|
|||
# Allergies
|
||||
|
||||
Welcome to Allergies on Exercism's C# Track.
|
||||
If you need help running the tests or submitting your code, check out `HELP.md`.
|
||||
|
||||
## Instructions
|
||||
|
||||
Given a person's allergy score, determine whether or not they're allergic to a given item, and their full list of allergies.
|
||||
|
||||
An allergy test produces a single numeric score which contains the
|
||||
information about all the allergies the person has (that they were
|
||||
tested for).
|
||||
|
||||
The list of items (and their value) that were tested are:
|
||||
|
||||
* eggs (1)
|
||||
* peanuts (2)
|
||||
* shellfish (4)
|
||||
* strawberries (8)
|
||||
* tomatoes (16)
|
||||
* chocolate (32)
|
||||
* pollen (64)
|
||||
* cats (128)
|
||||
|
||||
So if Tom is allergic to peanuts and chocolate, he gets a score of 34.
|
||||
|
||||
Now, given just that score of 34, your program should be able to say:
|
||||
|
||||
- Whether Tom is allergic to any one of those allergens listed above.
|
||||
- All the allergens Tom is allergic to.
|
||||
|
||||
Note: a given score may include allergens **not** listed above (i.e.
|
||||
allergens that score 256, 512, 1024, etc.). Your program should
|
||||
ignore those components of the score. For example, if the allergy
|
||||
score is 257, your program should only report the eggs (1) allergy.
|
||||
|
||||
This exercise requires you to use bitwise operations. For more information, see [this page](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/bitwise-and-shift-operators).
|
||||
|
||||
## Source
|
||||
|
||||
### Created by
|
||||
|
||||
- @bressain
|
||||
|
||||
### Contributed to by
|
||||
|
||||
- @ErikSchierboom
|
||||
- @j2jensen
|
||||
- @jwood803
|
||||
- @NextNebula
|
||||
- @robkeim
|
||||
- @wolf99
|
||||
|
||||
### Based on
|
||||
|
||||
Jumpstart Lab Warm-up - http://jumpstartlab.com
|
|
@ -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
|
||||
|
||||
[CircularBuffer.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
|
|
@ -0,0 +1,24 @@
|
|||
{
|
||||
"blurb": "A data structure that uses a single, fixed-size buffer as if it were connected end-to-end.",
|
||||
"authors": [
|
||||
"ErikSchierboom"
|
||||
],
|
||||
"contributors": [
|
||||
"j2jensen",
|
||||
"robkeim",
|
||||
"wolf99"
|
||||
],
|
||||
"files": {
|
||||
"solution": [
|
||||
"CircularBuffer.cs"
|
||||
],
|
||||
"test": [
|
||||
"CircularBufferTests.cs"
|
||||
],
|
||||
"example": [
|
||||
".meta/Example.cs"
|
||||
]
|
||||
},
|
||||
"source": "Wikipedia",
|
||||
"source_url": "http://en.wikipedia.org/wiki/Circular_buffer"
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
{"track":"csharp","exercise":"circular-buffer","id":"4479726f0cec42ff8d66f033e67a0cea","url":"https://exercism.org/tracks/csharp/exercises/circular-buffer","handle":"benharri","is_requester":true,"auto_approve":false}
|
|
@ -0,0 +1,34 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class CircularBuffer<T>
|
||||
{
|
||||
private readonly int _size;
|
||||
private readonly Queue<T> _buffer;
|
||||
|
||||
public CircularBuffer(int capacity)
|
||||
{
|
||||
_size = capacity;
|
||||
_buffer = new Queue<T>(capacity);
|
||||
}
|
||||
|
||||
public T Read() => _buffer.Dequeue();
|
||||
|
||||
public void Write(T value)
|
||||
{
|
||||
if (_buffer.Count >= _size)
|
||||
throw new InvalidOperationException();
|
||||
|
||||
_buffer.Enqueue(value);
|
||||
}
|
||||
|
||||
public void Overwrite(T value)
|
||||
{
|
||||
if (_buffer.Count >= _size)
|
||||
_buffer.Dequeue();
|
||||
|
||||
_buffer.Enqueue(value);
|
||||
}
|
||||
|
||||
public void Clear() => _buffer.Clear();
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net5.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>
|
|
@ -0,0 +1,147 @@
|
|||
using System;
|
||||
using Xunit;
|
||||
|
||||
public class CircularBufferTests
|
||||
{
|
||||
[Fact]
|
||||
public void Reading_empty_buffer_should_fail()
|
||||
{
|
||||
var buffer = new CircularBuffer<int>(capacity: 1);
|
||||
Assert.Throws<InvalidOperationException>(() => buffer.Read());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Can_read_an_item_just_written()
|
||||
{
|
||||
var buffer = new CircularBuffer<int>(capacity: 1);
|
||||
buffer.Write(1);
|
||||
Assert.Equal(1, buffer.Read());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Each_item_may_only_be_read_once()
|
||||
{
|
||||
var buffer = new CircularBuffer<int>(capacity: 1);
|
||||
buffer.Write(1);
|
||||
Assert.Equal(1, buffer.Read());
|
||||
Assert.Throws<InvalidOperationException>(() => buffer.Read());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Items_are_read_in_the_order_they_are_written()
|
||||
{
|
||||
var buffer = new CircularBuffer<int>(capacity: 2);
|
||||
buffer.Write(1);
|
||||
buffer.Write(2);
|
||||
Assert.Equal(1, buffer.Read());
|
||||
Assert.Equal(2, buffer.Read());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Full_buffer_cant_be_written_to()
|
||||
{
|
||||
var buffer = new CircularBuffer<int>(capacity: 1);
|
||||
buffer.Write(1);
|
||||
Assert.Throws<InvalidOperationException>(() => buffer.Write(2));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_read_frees_up_capacity_for_another_write()
|
||||
{
|
||||
var buffer = new CircularBuffer<int>(capacity: 1);
|
||||
buffer.Write(1);
|
||||
Assert.Equal(1, buffer.Read());
|
||||
buffer.Write(2);
|
||||
Assert.Equal(2, buffer.Read());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Read_position_is_maintained_even_across_multiple_writes()
|
||||
{
|
||||
var buffer = new CircularBuffer<int>(capacity: 3);
|
||||
buffer.Write(1);
|
||||
buffer.Write(2);
|
||||
Assert.Equal(1, buffer.Read());
|
||||
buffer.Write(3);
|
||||
Assert.Equal(2, buffer.Read());
|
||||
Assert.Equal(3, buffer.Read());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Items_cleared_out_of_buffer_cant_be_read()
|
||||
{
|
||||
var buffer = new CircularBuffer<int>(capacity: 1);
|
||||
buffer.Write(1);
|
||||
buffer.Clear();
|
||||
Assert.Throws<InvalidOperationException>(() => buffer.Read());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Clear_frees_up_capacity_for_another_write()
|
||||
{
|
||||
var buffer = new CircularBuffer<int>(capacity: 1);
|
||||
buffer.Write(1);
|
||||
buffer.Clear();
|
||||
buffer.Write(2);
|
||||
Assert.Equal(2, buffer.Read());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Clear_does_nothing_on_empty_buffer()
|
||||
{
|
||||
var buffer = new CircularBuffer<int>(capacity: 1);
|
||||
buffer.Clear();
|
||||
buffer.Write(1);
|
||||
Assert.Equal(1, buffer.Read());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Overwrite_acts_like_write_on_non_full_buffer()
|
||||
{
|
||||
var buffer = new CircularBuffer<int>(capacity: 2);
|
||||
buffer.Write(1);
|
||||
buffer.Overwrite(2);
|
||||
Assert.Equal(1, buffer.Read());
|
||||
Assert.Equal(2, buffer.Read());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Overwrite_replaces_the_oldest_item_on_full_buffer()
|
||||
{
|
||||
var buffer = new CircularBuffer<int>(capacity: 2);
|
||||
buffer.Write(1);
|
||||
buffer.Write(2);
|
||||
buffer.Overwrite(3);
|
||||
Assert.Equal(2, buffer.Read());
|
||||
Assert.Equal(3, buffer.Read());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Overwrite_replaces_the_oldest_item_remaining_in_buffer_following_a_read()
|
||||
{
|
||||
var buffer = new CircularBuffer<int>(capacity: 3);
|
||||
buffer.Write(1);
|
||||
buffer.Write(2);
|
||||
buffer.Write(3);
|
||||
Assert.Equal(1, buffer.Read());
|
||||
buffer.Write(4);
|
||||
buffer.Overwrite(5);
|
||||
Assert.Equal(3, buffer.Read());
|
||||
Assert.Equal(4, buffer.Read());
|
||||
Assert.Equal(5, buffer.Read());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Initial_clear_does_not_affect_wrapping_around()
|
||||
{
|
||||
var buffer = new CircularBuffer<int>(capacity: 2);
|
||||
buffer.Clear();
|
||||
buffer.Write(1);
|
||||
buffer.Write(2);
|
||||
buffer.Overwrite(3);
|
||||
buffer.Overwrite(4);
|
||||
Assert.Equal(3, buffer.Read());
|
||||
Assert.Equal(4, buffer.Read());
|
||||
Assert.Throws<InvalidOperationException>(() => buffer.Read());
|
||||
}
|
||||
}
|
|
@ -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 CircularBuffer.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.
|
|
@ -0,0 +1,72 @@
|
|||
# Circular Buffer
|
||||
|
||||
Welcome to Circular Buffer on Exercism's C# Track.
|
||||
If you need help running the tests or submitting your code, check out `HELP.md`.
|
||||
|
||||
## Instructions
|
||||
|
||||
A circular buffer, cyclic buffer or ring buffer is a data structure that
|
||||
uses a single, fixed-size buffer as if it were connected end-to-end.
|
||||
|
||||
A circular buffer first starts empty and of some predefined length. For
|
||||
example, this is a 7-element buffer:
|
||||
<!-- prettier-ignore -->
|
||||
[ ][ ][ ][ ][ ][ ][ ]
|
||||
|
||||
Assume that a 1 is written into the middle of the buffer (exact starting
|
||||
location does not matter in a circular buffer):
|
||||
<!-- prettier-ignore -->
|
||||
[ ][ ][ ][1][ ][ ][ ]
|
||||
|
||||
Then assume that two more elements are added — 2 & 3 — which get
|
||||
appended after the 1:
|
||||
<!-- prettier-ignore -->
|
||||
[ ][ ][ ][1][2][3][ ]
|
||||
|
||||
If two elements are then removed from the buffer, the oldest values
|
||||
inside the buffer are removed. The two elements removed, in this case,
|
||||
are 1 & 2, leaving the buffer with just a 3:
|
||||
<!-- prettier-ignore -->
|
||||
[ ][ ][ ][ ][ ][3][ ]
|
||||
|
||||
If the buffer has 7 elements then it is completely full:
|
||||
<!-- prettier-ignore -->
|
||||
[5][6][7][8][9][3][4]
|
||||
|
||||
When the buffer is full an error will be raised, alerting the client
|
||||
that further writes are blocked until a slot becomes free.
|
||||
|
||||
When the buffer is full, the client can opt to overwrite the oldest
|
||||
data with a forced write. In this case, two more elements — A & B —
|
||||
are added and they overwrite the 3 & 4:
|
||||
<!-- prettier-ignore -->
|
||||
[5][6][7][8][9][A][B]
|
||||
|
||||
3 & 4 have been replaced by A & B making 5 now the oldest data in the
|
||||
buffer. Finally, if two elements are removed then what would be
|
||||
returned is 5 & 6 yielding the buffer:
|
||||
<!-- prettier-ignore -->
|
||||
[ ][ ][7][8][9][A][B]
|
||||
|
||||
Because there is space available, if the client again uses overwrite
|
||||
to store C & D then the space where 5 & 6 were stored previously will
|
||||
be used not the location of 7 & 8. 7 is still the oldest element and
|
||||
the buffer is once again full.
|
||||
<!-- prettier-ignore -->
|
||||
[C][D][7][8][9][A][B]
|
||||
|
||||
## Source
|
||||
|
||||
### Created by
|
||||
|
||||
- @ErikSchierboom
|
||||
|
||||
### Contributed to by
|
||||
|
||||
- @j2jensen
|
||||
- @robkeim
|
||||
- @wolf99
|
||||
|
||||
### Based on
|
||||
|
||||
Wikipedia - http://en.wikipedia.org/wiki/Circular_buffer
|
|
@ -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
|
||||
|
||||
[Clock.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
|
|
@ -0,0 +1,34 @@
|
|||
{
|
||||
"blurb": "Implement a clock that handles times without dates.",
|
||||
"authors": [
|
||||
"rprouse"
|
||||
],
|
||||
"contributors": [
|
||||
"avjgit",
|
||||
"bmeverett",
|
||||
"bressain",
|
||||
"ErikSchierboom",
|
||||
"j2jensen",
|
||||
"jwood803",
|
||||
"kytrinyx",
|
||||
"mattcbaker",
|
||||
"robkeim",
|
||||
"Thorocaine",
|
||||
"vamcs",
|
||||
"vgrigoriu",
|
||||
"wolf99"
|
||||
],
|
||||
"files": {
|
||||
"solution": [
|
||||
"Clock.cs"
|
||||
],
|
||||
"test": [
|
||||
"ClockTests.cs"
|
||||
],
|
||||
"example": [
|
||||
".meta/Example.cs"
|
||||
]
|
||||
},
|
||||
"source": "Pairing session with Erin Drummond",
|
||||
"source_url": "https://twitter.com/ebdrummond"
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
{"track":"csharp","exercise":"clock","id":"6e5f0678ad9446e38a071bde4b88a98c","url":"https://exercism.org/tracks/csharp/exercises/clock","handle":"benharri","is_requester":true,"auto_approve":false}
|
|
@ -0,0 +1,22 @@
|
|||
using System;
|
||||
|
||||
public class Clock
|
||||
{
|
||||
private const int MinutesInDay = 24 * 60;
|
||||
private readonly int DisplayHours, DisplayMinutes, TotalMins;
|
||||
|
||||
public Clock(int h, int m)
|
||||
{
|
||||
TotalMins = (h * 60 + m) % MinutesInDay;
|
||||
if (TotalMins < 0)
|
||||
TotalMins += MinutesInDay;
|
||||
DisplayHours = Math.DivRem(TotalMins, 60, out DisplayMinutes);
|
||||
}
|
||||
|
||||
public Clock Add(int m) => new(0, TotalMins + m);
|
||||
public Clock Subtract(int m) => Add(-m);
|
||||
|
||||
public override string ToString() => $"{DisplayHours:00}:{DisplayMinutes:00}";
|
||||
public override bool Equals(object obj) => ToString().Equals(obj.ToString());
|
||||
public override int GetHashCode() => TotalMins;
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net5.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>
|
|
@ -0,0 +1,376 @@
|
|||
using Xunit;
|
||||
|
||||
public class ClockTests
|
||||
{
|
||||
[Fact]
|
||||
public void On_the_hour()
|
||||
{
|
||||
var sut = new Clock(8, 0);
|
||||
Assert.Equal("08:00", sut.ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Past_the_hour()
|
||||
{
|
||||
var sut = new Clock(11, 9);
|
||||
Assert.Equal("11:09", sut.ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Midnight_is_zero_hours()
|
||||
{
|
||||
var sut = new Clock(24, 0);
|
||||
Assert.Equal("00:00", sut.ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Hour_rolls_over()
|
||||
{
|
||||
var sut = new Clock(25, 0);
|
||||
Assert.Equal("01:00", sut.ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Hour_rolls_over_continuously()
|
||||
{
|
||||
var sut = new Clock(100, 0);
|
||||
Assert.Equal("04:00", sut.ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Sixty_minutes_is_next_hour()
|
||||
{
|
||||
var sut = new Clock(1, 60);
|
||||
Assert.Equal("02:00", sut.ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Minutes_roll_over()
|
||||
{
|
||||
var sut = new Clock(0, 160);
|
||||
Assert.Equal("02:40", sut.ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Minutes_roll_over_continuously()
|
||||
{
|
||||
var sut = new Clock(0, 1723);
|
||||
Assert.Equal("04:43", sut.ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Hour_and_minutes_roll_over()
|
||||
{
|
||||
var sut = new Clock(25, 160);
|
||||
Assert.Equal("03:40", sut.ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Hour_and_minutes_roll_over_continuously()
|
||||
{
|
||||
var sut = new Clock(201, 3001);
|
||||
Assert.Equal("11:01", sut.ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Hour_and_minutes_roll_over_to_exactly_midnight()
|
||||
{
|
||||
var sut = new Clock(72, 8640);
|
||||
Assert.Equal("00:00", sut.ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Negative_hour()
|
||||
{
|
||||
var sut = new Clock(-1, 15);
|
||||
Assert.Equal("23:15", sut.ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Negative_hour_rolls_over()
|
||||
{
|
||||
var sut = new Clock(-25, 0);
|
||||
Assert.Equal("23:00", sut.ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Negative_hour_rolls_over_continuously()
|
||||
{
|
||||
var sut = new Clock(-91, 0);
|
||||
Assert.Equal("05:00", sut.ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Negative_minutes()
|
||||
{
|
||||
var sut = new Clock(1, -40);
|
||||
Assert.Equal("00:20", sut.ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Negative_minutes_roll_over()
|
||||
{
|
||||
var sut = new Clock(1, -160);
|
||||
Assert.Equal("22:20", sut.ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Negative_minutes_roll_over_continuously()
|
||||
{
|
||||
var sut = new Clock(1, -4820);
|
||||
Assert.Equal("16:40", sut.ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Negative_sixty_minutes_is_previous_hour()
|
||||
{
|
||||
var sut = new Clock(2, -60);
|
||||
Assert.Equal("01:00", sut.ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Negative_hour_and_minutes_both_roll_over()
|
||||
{
|
||||
var sut = new Clock(-25, -160);
|
||||
Assert.Equal("20:20", sut.ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Negative_hour_and_minutes_both_roll_over_continuously()
|
||||
{
|
||||
var sut = new Clock(-121, -5810);
|
||||
Assert.Equal("22:10", sut.ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Add_minutes()
|
||||
{
|
||||
var sut = new Clock(10, 0);
|
||||
Assert.Equal("10:03", sut.Add(3).ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Add_no_minutes()
|
||||
{
|
||||
var sut = new Clock(6, 41);
|
||||
Assert.Equal("06:41", sut.Add(0).ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Add_to_next_hour()
|
||||
{
|
||||
var sut = new Clock(0, 45);
|
||||
Assert.Equal("01:25", sut.Add(40).ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Add_more_than_one_hour()
|
||||
{
|
||||
var sut = new Clock(10, 0);
|
||||
Assert.Equal("11:01", sut.Add(61).ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Add_more_than_two_hours_with_carry()
|
||||
{
|
||||
var sut = new Clock(0, 45);
|
||||
Assert.Equal("03:25", sut.Add(160).ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Add_across_midnight()
|
||||
{
|
||||
var sut = new Clock(23, 59);
|
||||
Assert.Equal("00:01", sut.Add(2).ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Add_more_than_one_day_1500_min_25_hrs_()
|
||||
{
|
||||
var sut = new Clock(5, 32);
|
||||
Assert.Equal("06:32", sut.Add(1500).ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Add_more_than_two_days()
|
||||
{
|
||||
var sut = new Clock(1, 1);
|
||||
Assert.Equal("11:21", sut.Add(3500).ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Subtract_minutes()
|
||||
{
|
||||
var sut = new Clock(10, 3);
|
||||
Assert.Equal("10:00", sut.Subtract(3).ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Subtract_to_previous_hour()
|
||||
{
|
||||
var sut = new Clock(10, 3);
|
||||
Assert.Equal("09:33", sut.Subtract(30).ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Subtract_more_than_an_hour()
|
||||
{
|
||||
var sut = new Clock(10, 3);
|
||||
Assert.Equal("08:53", sut.Subtract(70).ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Subtract_across_midnight()
|
||||
{
|
||||
var sut = new Clock(0, 3);
|
||||
Assert.Equal("23:59", sut.Subtract(4).ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Subtract_more_than_two_hours()
|
||||
{
|
||||
var sut = new Clock(0, 0);
|
||||
Assert.Equal("21:20", sut.Subtract(160).ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Subtract_more_than_two_hours_with_borrow()
|
||||
{
|
||||
var sut = new Clock(6, 15);
|
||||
Assert.Equal("03:35", sut.Subtract(160).ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Subtract_more_than_one_day_1500_min_25_hrs_()
|
||||
{
|
||||
var sut = new Clock(5, 32);
|
||||
Assert.Equal("04:32", sut.Subtract(1500).ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Subtract_more_than_two_days()
|
||||
{
|
||||
var sut = new Clock(2, 20);
|
||||
Assert.Equal("00:20", sut.Subtract(3000).ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Clocks_with_same_time()
|
||||
{
|
||||
var sut = new Clock(15, 37);
|
||||
Assert.Equal(new Clock(15, 37), sut);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Clocks_a_minute_apart()
|
||||
{
|
||||
var sut = new Clock(15, 37);
|
||||
Assert.NotEqual(new Clock(15, 36), sut);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Clocks_an_hour_apart()
|
||||
{
|
||||
var sut = new Clock(15, 37);
|
||||
Assert.NotEqual(new Clock(14, 37), sut);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Clocks_with_hour_overflow()
|
||||
{
|
||||
var sut = new Clock(34, 37);
|
||||
Assert.Equal(new Clock(10, 37), sut);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Clocks_with_hour_overflow_by_several_days()
|
||||
{
|
||||
var sut = new Clock(99, 11);
|
||||
Assert.Equal(new Clock(3, 11), sut);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Clocks_with_negative_hour()
|
||||
{
|
||||
var sut = new Clock(-2, 40);
|
||||
Assert.Equal(new Clock(22, 40), sut);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Clocks_with_negative_hour_that_wraps()
|
||||
{
|
||||
var sut = new Clock(-31, 3);
|
||||
Assert.Equal(new Clock(17, 3), sut);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Clocks_with_negative_hour_that_wraps_multiple_times()
|
||||
{
|
||||
var sut = new Clock(-83, 49);
|
||||
Assert.Equal(new Clock(13, 49), sut);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Clocks_with_minute_overflow()
|
||||
{
|
||||
var sut = new Clock(0, 1441);
|
||||
Assert.Equal(new Clock(0, 1), sut);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Clocks_with_minute_overflow_by_several_days()
|
||||
{
|
||||
var sut = new Clock(2, 4322);
|
||||
Assert.Equal(new Clock(2, 2), sut);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Clocks_with_negative_minute()
|
||||
{
|
||||
var sut = new Clock(3, -20);
|
||||
Assert.Equal(new Clock(2, 40), sut);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Clocks_with_negative_minute_that_wraps()
|
||||
{
|
||||
var sut = new Clock(5, -1490);
|
||||
Assert.Equal(new Clock(4, 10), sut);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Clocks_with_negative_minute_that_wraps_multiple_times()
|
||||
{
|
||||
var sut = new Clock(6, -4305);
|
||||
Assert.Equal(new Clock(6, 15), sut);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Clocks_with_negative_hours_and_minutes()
|
||||
{
|
||||
var sut = new Clock(-12, -268);
|
||||
Assert.Equal(new Clock(7, 32), sut);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Clocks_with_negative_hours_and_minutes_that_wrap()
|
||||
{
|
||||
var sut = new Clock(-54, -11513);
|
||||
Assert.Equal(new Clock(18, 7), sut);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Full_clock_and_zeroed_clock()
|
||||
{
|
||||
var sut = new Clock(0, 0);
|
||||
Assert.Equal(new Clock(24, 0), sut);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Clocks_are_immutable()
|
||||
{
|
||||
var sut = new Clock(0, 0);
|
||||
var sutPlus1 = sut.Add(1);
|
||||
Assert.NotEqual(sutPlus1, sut);
|
||||
}
|
||||
}
|
|
@ -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 Clock.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.
|
|
@ -0,0 +1,41 @@
|
|||
# Clock
|
||||
|
||||
Welcome to Clock on Exercism's C# Track.
|
||||
If you need help running the tests or submitting your code, check out `HELP.md`.
|
||||
|
||||
## Instructions
|
||||
|
||||
Implement a clock that handles times without dates.
|
||||
|
||||
You should be able to add and subtract minutes to it.
|
||||
|
||||
Two clocks that represent the same time should be equal to each other.
|
||||
|
||||
This exercise requires you to implement a type-specific method for determining equality of instances.
|
||||
For more information, see [this page](https://docs.microsoft.com/en-us/dotnet/api/System.IEquatable-1?view=netcore-2.1).
|
||||
|
||||
## Source
|
||||
|
||||
### Created by
|
||||
|
||||
- @rprouse
|
||||
|
||||
### Contributed to by
|
||||
|
||||
- @avjgit
|
||||
- @bmeverett
|
||||
- @bressain
|
||||
- @ErikSchierboom
|
||||
- @j2jensen
|
||||
- @jwood803
|
||||
- @kytrinyx
|
||||
- @mattcbaker
|
||||
- @robkeim
|
||||
- @Thorocaine
|
||||
- @vamcs
|
||||
- @vgrigoriu
|
||||
- @wolf99
|
||||
|
||||
### Based on
|
||||
|
||||
Pairing session with Erin Drummond - https://twitter.com/ebdrummond
|
|
@ -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
|
||||
|
||||
[Hamming.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
|
|
@ -0,0 +1,27 @@
|
|||
{
|
||||
"blurb": "Calculate the Hamming difference between two DNA strands.",
|
||||
"authors": [],
|
||||
"contributors": [
|
||||
"bressain",
|
||||
"ErikSchierboom",
|
||||
"j2jensen",
|
||||
"jwood803",
|
||||
"robkeim",
|
||||
"vgrigoriu",
|
||||
"wolf99",
|
||||
"Zureka"
|
||||
],
|
||||
"files": {
|
||||
"solution": [
|
||||
"Hamming.cs"
|
||||
],
|
||||
"test": [
|
||||
"HammingTests.cs"
|
||||
],
|
||||
"example": [
|
||||
".meta/Example.cs"
|
||||
]
|
||||
},
|
||||
"source": "The Calculating Point Mutations problem at Rosalind",
|
||||
"source_url": "http://rosalind.info/problems/hamm/"
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
{"track":"csharp","exercise":"hamming","id":"1045de70c2d34b18b60099a291e18d05","url":"https://exercism.org/tracks/csharp/exercises/hamming","handle":"benharri","is_requester":true,"auto_approve":false}
|
|
@ -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 Hamming.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.
|
|
@ -0,0 +1,10 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
|
||||
public static class Hamming
|
||||
{
|
||||
public static int Distance(string firstStrand, string secondStrand) =>
|
||||
firstStrand.Length != secondStrand.Length
|
||||
? throw new ArgumentException("must be the same length")
|
||||
: firstStrand.Where((t, i) => t != secondStrand[i]).Count();
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net5.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>
|
|
@ -0,0 +1,59 @@
|
|||
using System;
|
||||
using Xunit;
|
||||
|
||||
public class HammingTests
|
||||
{
|
||||
[Fact]
|
||||
public void Empty_strands()
|
||||
{
|
||||
Assert.Equal(0, Hamming.Distance("", ""));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Single_letter_identical_strands()
|
||||
{
|
||||
Assert.Equal(0, Hamming.Distance("A", "A"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Single_letter_different_strands()
|
||||
{
|
||||
Assert.Equal(1, Hamming.Distance("G", "T"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Long_identical_strands()
|
||||
{
|
||||
Assert.Equal(0, Hamming.Distance("GGACTGAAATCTG", "GGACTGAAATCTG"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Long_different_strands()
|
||||
{
|
||||
Assert.Equal(9, Hamming.Distance("GGACGGATTCTG", "AGGACGGATTCT"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Disallow_first_strand_longer()
|
||||
{
|
||||
Assert.Throws<ArgumentException>(() => Hamming.Distance("AATG", "AAA"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Disallow_second_strand_longer()
|
||||
{
|
||||
Assert.Throws<ArgumentException>(() => Hamming.Distance("ATA", "AGTG"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Disallow_left_empty_strand()
|
||||
{
|
||||
Assert.Throws<ArgumentException>(() => Hamming.Distance("", "G"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Disallow_right_empty_strand()
|
||||
{
|
||||
Assert.Throws<ArgumentException>(() => Hamming.Distance("G", ""));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
# Hamming
|
||||
|
||||
Welcome to Hamming on Exercism's C# Track.
|
||||
If you need help running the tests or submitting your code, check out `HELP.md`.
|
||||
|
||||
## Instructions
|
||||
|
||||
Calculate the Hamming Distance between two DNA strands.
|
||||
|
||||
Your body is made up of cells that contain DNA. Those cells regularly wear out and need replacing, which they achieve by dividing into daughter cells. In fact, the average human body experiences about 10 quadrillion cell divisions in a lifetime!
|
||||
|
||||
When cells divide, their DNA replicates too. Sometimes during this process mistakes happen and single pieces of DNA get encoded with the incorrect information. If we compare two strands of DNA and count the differences between them we can see how many mistakes occurred. This is known as the "Hamming Distance".
|
||||
|
||||
We read DNA using the letters C,A,G and T. Two strands might look like this:
|
||||
|
||||
GAGCCTACTAACGGGAT
|
||||
CATCGTAATGACGGCCT
|
||||
^ ^ ^ ^ ^ ^^
|
||||
|
||||
They have 7 differences, and therefore the Hamming Distance is 7.
|
||||
|
||||
The Hamming Distance is useful for lots of things in science, not just biology, so it's a nice phrase to be familiar with :)
|
||||
|
||||
The Hamming distance is only defined for sequences of equal length, so
|
||||
an attempt to calculate it between sequences of different lengths should
|
||||
not work. The general handling of this situation (e.g., raising an
|
||||
exception vs returning a special value) may differ between languages.
|
||||
|
||||
## Source
|
||||
|
||||
### Contributed to by
|
||||
|
||||
- @bressain
|
||||
- @ErikSchierboom
|
||||
- @j2jensen
|
||||
- @jwood803
|
||||
- @robkeim
|
||||
- @vgrigoriu
|
||||
- @wolf99
|
||||
- @Zureka
|
||||
|
||||
### Based on
|
||||
|
||||
The Calculating Point Mutations problem at Rosalind - http://rosalind.info/problems/hamm/
|
|
@ -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
|
||||
|
||||
[HighScores.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
|
|
@ -0,0 +1,24 @@
|
|||
{
|
||||
"blurb": "Manage a player's High Score list",
|
||||
"authors": [
|
||||
"ErikSchierboom"
|
||||
],
|
||||
"contributors": [
|
||||
"pjangam",
|
||||
"robkeim",
|
||||
"valentin-p",
|
||||
"wolf99"
|
||||
],
|
||||
"files": {
|
||||
"solution": [
|
||||
"HighScores.cs"
|
||||
],
|
||||
"test": [
|
||||
"HighScoresTests.cs"
|
||||
],
|
||||
"example": [
|
||||
".meta/Example.cs"
|
||||
]
|
||||
},
|
||||
"source": "Tribute to the eighties' arcade game Frogger"
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
{"track":"csharp","exercise":"high-scores","id":"700f7d0b102d4512a2ec15d71884fb44","url":"https://exercism.org/tracks/csharp/exercises/high-scores","handle":"benharri","is_requester":true,"auto_approve":false}
|
|
@ -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 HighScores.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.
|
|
@ -0,0 +1,18 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
public class HighScores
|
||||
{
|
||||
private readonly List<int> ScoreList;
|
||||
|
||||
public HighScores(List<int> list) => ScoreList = list;
|
||||
|
||||
public List<int> Scores() => ScoreList;
|
||||
|
||||
public int Latest() => ScoreList.Last();
|
||||
|
||||
public int PersonalBest() => ScoreList.Max();
|
||||
|
||||
public List<int> PersonalTopThree() => ScoreList.OrderByDescending(x => x).Take(3).ToList();
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net5.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>
|
|
@ -0,0 +1,77 @@
|
|||
using System.Collections.Generic;
|
||||
using Xunit;
|
||||
|
||||
public class HighScoresTests
|
||||
{
|
||||
[Fact]
|
||||
public void List_of_scores()
|
||||
{
|
||||
var sut = new HighScores(new List<int> { 30, 50, 20, 70 });
|
||||
Assert.Equal(new List<int> { 30, 50, 20, 70 }, sut.Scores());
|
||||
}
|
||||
|
||||
[Fact(Skip = "Remove this Skip property to run this test")]
|
||||
public void Latest_score()
|
||||
{
|
||||
var sut = new HighScores(new List<int> { 100, 0, 90, 30 });
|
||||
Assert.Equal(30, sut.Latest());
|
||||
}
|
||||
|
||||
[Fact(Skip = "Remove this Skip property to run this test")]
|
||||
public void Personal_best()
|
||||
{
|
||||
var sut = new HighScores(new List<int> { 40, 100, 70 });
|
||||
Assert.Equal(100, sut.PersonalBest());
|
||||
}
|
||||
|
||||
[Fact(Skip = "Remove this Skip property to run this test")]
|
||||
public void Personal_top_three_from_a_list_of_scores()
|
||||
{
|
||||
var sut = new HighScores(new List<int> { 10, 30, 90, 30, 100, 20, 10, 0, 30, 40, 40, 70, 70 });
|
||||
Assert.Equal(new List<int> { 100, 90, 70 }, sut.PersonalTopThree());
|
||||
}
|
||||
|
||||
[Fact(Skip = "Remove this Skip property to run this test")]
|
||||
public void Personal_top_highest_to_lowest()
|
||||
{
|
||||
var sut = new HighScores(new List<int> { 20, 10, 30 });
|
||||
Assert.Equal(new List<int> { 30, 20, 10 }, sut.PersonalTopThree());
|
||||
}
|
||||
|
||||
[Fact(Skip = "Remove this Skip property to run this test")]
|
||||
public void Personal_top_when_there_is_a_tie()
|
||||
{
|
||||
var sut = new HighScores(new List<int> { 40, 20, 40, 30 });
|
||||
Assert.Equal(new List<int> { 40, 40, 30 }, sut.PersonalTopThree());
|
||||
}
|
||||
|
||||
[Fact(Skip = "Remove this Skip property to run this test")]
|
||||
public void Personal_top_when_there_are_less_than_3()
|
||||
{
|
||||
var sut = new HighScores(new List<int> { 30, 70 });
|
||||
Assert.Equal(new List<int> { 70, 30 }, sut.PersonalTopThree());
|
||||
}
|
||||
|
||||
[Fact(Skip = "Remove this Skip property to run this test")]
|
||||
public void Personal_top_when_there_is_only_one()
|
||||
{
|
||||
var sut = new HighScores(new List<int> { 40 });
|
||||
Assert.Equal(new List<int> { 40 }, sut.PersonalTopThree());
|
||||
}
|
||||
|
||||
[Fact(Skip = "Remove this Skip property to run this test")]
|
||||
public void Latest_score_should_not_change_after_calling_personal_best()
|
||||
{
|
||||
var sut = new HighScores(new List<int> { 20, 10, 30, 3, 2, 1 });
|
||||
Assert.Equal(30, sut.PersonalBest());
|
||||
Assert.Equal(1, sut.Latest());
|
||||
}
|
||||
|
||||
[Fact(Skip = "Remove this Skip property to run this test")]
|
||||
public void Latest_score_should_not_change_after_calling_personal_top_three()
|
||||
{
|
||||
var sut = new HighScores(new List<int> { 20, 100, 30, 90, 2, 70 });
|
||||
Assert.Equal(new List<int> { 100, 90, 70 }, sut.PersonalTopThree());
|
||||
Assert.Equal(70, sut.Latest());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
# High Scores
|
||||
|
||||
Welcome to High Scores on Exercism's C# Track.
|
||||
If you need help running the tests or submitting your code, check out `HELP.md`.
|
||||
|
||||
## Instructions
|
||||
|
||||
Manage a game player's High Score list.
|
||||
|
||||
Your task is to build a high-score component of the classic Frogger game, one of the highest selling and addictive games of all time, and a classic of the arcade era. Your task is to write methods that return the highest score from the list, the last added score and the three highest scores.
|
||||
|
||||
## Source
|
||||
|
||||
### Created by
|
||||
|
||||
- @ErikSchierboom
|
||||
|
||||
### Contributed to by
|
||||
|
||||
- @pjangam
|
||||
- @robkeim
|
||||
- @valentin-p
|
||||
- @wolf99
|
||||
|
||||
### Based on
|
||||
|
||||
Tribute to the eighties' arcade game Frogger
|
|
@ -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
|
||||
|
||||
[MatchingBrackets.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
|
|
@ -0,0 +1,26 @@
|
|||
{
|
||||
"blurb": "Make sure the brackets and braces all match.",
|
||||
"authors": [
|
||||
"ErikSchierboom"
|
||||
],
|
||||
"contributors": [
|
||||
"j2jensen",
|
||||
"martinfreedman",
|
||||
"robkeim",
|
||||
"SleeplessByte",
|
||||
"vgrigoriu",
|
||||
"wolf99"
|
||||
],
|
||||
"files": {
|
||||
"solution": [
|
||||
"MatchingBrackets.cs"
|
||||
],
|
||||
"test": [
|
||||
"MatchingBracketsTests.cs"
|
||||
],
|
||||
"example": [
|
||||
".meta/Example.cs"
|
||||
]
|
||||
},
|
||||
"source": "Ginna Baker"
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
{"track":"csharp","exercise":"matching-brackets","id":"a5b024ac2916402994fb0a245848f7a1","url":"https://exercism.org/tracks/csharp/exercises/matching-brackets","handle":"benharri","is_requester":true,"auto_approve":false}
|
|
@ -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 MatchingBrackets.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.
|
|
@ -0,0 +1,30 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public static class MatchingBrackets
|
||||
{
|
||||
private static readonly Dictionary<char, char> Brackets = new()
|
||||
{
|
||||
{ '(', ')' },
|
||||
{ '[', ']' },
|
||||
{ '{', '}' }
|
||||
};
|
||||
|
||||
public static bool IsPaired(string input)
|
||||
{
|
||||
var open = new Stack<char>();
|
||||
|
||||
foreach (var c in input)
|
||||
if (Brackets.ContainsKey(c))
|
||||
open.Push(c);
|
||||
else if (Brackets.ContainsValue(c))
|
||||
{
|
||||
if (open.Count == 0 || c != Brackets[open.Peek()])
|
||||
return false;
|
||||
|
||||
open.Pop();
|
||||
}
|
||||
|
||||
return open.Count == 0;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net5.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>
|
|
@ -0,0 +1,123 @@
|
|||
using Xunit;
|
||||
|
||||
public class MatchingBracketsTests
|
||||
{
|
||||
[Fact]
|
||||
public void Paired_square_brackets()
|
||||
{
|
||||
var value = "[]";
|
||||
Assert.True(MatchingBrackets.IsPaired(value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Empty_string()
|
||||
{
|
||||
var value = "";
|
||||
Assert.True(MatchingBrackets.IsPaired(value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Unpaired_brackets()
|
||||
{
|
||||
var value = "[[";
|
||||
Assert.False(MatchingBrackets.IsPaired(value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Wrong_ordered_brackets()
|
||||
{
|
||||
var value = "}{";
|
||||
Assert.False(MatchingBrackets.IsPaired(value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Wrong_closing_bracket()
|
||||
{
|
||||
var value = "{]";
|
||||
Assert.False(MatchingBrackets.IsPaired(value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Paired_with_whitespace()
|
||||
{
|
||||
var value = "{ }";
|
||||
Assert.True(MatchingBrackets.IsPaired(value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Partially_paired_brackets()
|
||||
{
|
||||
var value = "{[])";
|
||||
Assert.False(MatchingBrackets.IsPaired(value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Simple_nested_brackets()
|
||||
{
|
||||
var value = "{[]}";
|
||||
Assert.True(MatchingBrackets.IsPaired(value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Several_paired_brackets()
|
||||
{
|
||||
var value = "{}[]";
|
||||
Assert.True(MatchingBrackets.IsPaired(value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Paired_and_nested_brackets()
|
||||
{
|
||||
var value = "([{}({}[])])";
|
||||
Assert.True(MatchingBrackets.IsPaired(value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Unopened_closing_brackets()
|
||||
{
|
||||
var value = "{[)][]}";
|
||||
Assert.False(MatchingBrackets.IsPaired(value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Unpaired_and_nested_brackets()
|
||||
{
|
||||
var value = "([{])";
|
||||
Assert.False(MatchingBrackets.IsPaired(value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Paired_and_wrong_nested_brackets()
|
||||
{
|
||||
var value = "[({]})";
|
||||
Assert.False(MatchingBrackets.IsPaired(value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Paired_and_incomplete_brackets()
|
||||
{
|
||||
var value = "{}[";
|
||||
Assert.False(MatchingBrackets.IsPaired(value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Too_many_closing_brackets()
|
||||
{
|
||||
var value = "[]]";
|
||||
Assert.False(MatchingBrackets.IsPaired(value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Math_expression()
|
||||
{
|
||||
var value = "(((185 + 223.85) * 15) - 543)/2";
|
||||
Assert.True(MatchingBrackets.IsPaired(value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Complex_latex_expression()
|
||||
{
|
||||
var value = "\\left(\\begin{array}{cc} \\frac{1}{3} & x\\\\ \\mathrm{e}^{x} &... x^2 \\end{array}\\right)";
|
||||
Assert.True(MatchingBrackets.IsPaired(value));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
# Matching Brackets
|
||||
|
||||
Welcome to Matching Brackets on Exercism's C# Track.
|
||||
If you need help running the tests or submitting your code, check out `HELP.md`.
|
||||
|
||||
## Instructions
|
||||
|
||||
Given a string containing brackets `[]`, braces `{}`, parentheses `()`,
|
||||
or any combination thereof, verify that any and all pairs are matched
|
||||
and nested correctly.
|
||||
|
||||
## Source
|
||||
|
||||
### Created by
|
||||
|
||||
- @ErikSchierboom
|
||||
|
||||
### Contributed to by
|
||||
|
||||
- @j2jensen
|
||||
- @martinfreedman
|
||||
- @robkeim
|
||||
- @SleeplessByte
|
||||
- @vgrigoriu
|
||||
- @wolf99
|
||||
|
||||
### Based on
|
||||
|
||||
Ginna Baker
|
|
@ -0,0 +1,20 @@
|
|||
{
|
||||
"blurb": "Learn about classes by creating cars.",
|
||||
"contributors": [
|
||||
"yzAlvin"
|
||||
],
|
||||
"authors": [
|
||||
"ErikSchierboom"
|
||||
],
|
||||
"files": {
|
||||
"solution": [
|
||||
"NeedForSpeed.cs"
|
||||
],
|
||||
"test": [
|
||||
"NeedForSpeedTests.cs"
|
||||
],
|
||||
"exemplar": [
|
||||
".meta/Exemplar.cs"
|
||||
]
|
||||
}
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
{"track":"csharp","exercise":"need-for-speed","id":"4a5e2786604a4a538f3327ad9f113193","url":"https://exercism.org/tracks/csharp/exercises/need-for-speed","handle":"benharri","is_requester":true,"auto_approve":false}
|
|
@ -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 NeedForSpeed.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.
|
|
@ -0,0 +1,38 @@
|
|||
# Hints
|
||||
|
||||
## 1. Creating a remote controlled car
|
||||
|
||||
- [Define a constructor][constructor-syntax] that has two `int` parameters.
|
||||
- Store the two parameters as [fields][fields] to access them from the classes' methods.
|
||||
|
||||
## 2. Creating a race track
|
||||
|
||||
- [Define a constructor][constructor-syntax] that has one `int` parameter.
|
||||
- Store the parameter as a [field][fields] to access it from the class' method.
|
||||
|
||||
## 3. Drive the car
|
||||
|
||||
- Add a [field][fields] to keep track of the distance driven.
|
||||
- Add the car's speed to the [field][fields] that keeps track of the distance driven.
|
||||
|
||||
## 4. Check for a drained battery
|
||||
|
||||
- Add a [field][fields] to keep track of the remaining battery charge percentage (starts at 100%).
|
||||
- Remove the car's battery drain from the [field][fields] to keep track of the battery charge.
|
||||
- Don't update the distance driven if the battery is drained.
|
||||
- Remember that if the battery charge is less than the battery drain percentage, it is considered drained.
|
||||
|
||||
## 5. Create the Nitro remote control car
|
||||
|
||||
- [Instantiate][instance-constructors] an instance of the `RemoteControlCar` with the correct arguments.
|
||||
|
||||
## 6. Check if a remote control car can finish a race
|
||||
|
||||
- Solving this is probably best done by [repeatedly driving the car][while].
|
||||
- Remember that the car has a method to retrieve the distance it has driven.
|
||||
- Consider what to do when the battery has been drained before reaching the finish line.
|
||||
|
||||
[constructor-syntax]: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/constructors#constructor-syntax
|
||||
[instance-constructors]: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/instance-constructors
|
||||
[while]: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/while
|
||||
[fields]: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/fields
|
|
@ -0,0 +1,48 @@
|
|||
class RemoteControlCar
|
||||
{
|
||||
private readonly int Speed;
|
||||
private readonly int BatteryDrain;
|
||||
private int Battery;
|
||||
private int Driven;
|
||||
|
||||
public RemoteControlCar(int speed, int batteryDrain)
|
||||
{
|
||||
Speed = speed;
|
||||
BatteryDrain = batteryDrain;
|
||||
Battery = 100;
|
||||
Driven = 0;
|
||||
}
|
||||
|
||||
public bool BatteryDrained() => Battery < BatteryDrain;
|
||||
|
||||
public int DistanceDriven() => Driven;
|
||||
|
||||
public void Drive()
|
||||
{
|
||||
if (BatteryDrained()) return;
|
||||
Battery -= BatteryDrain;
|
||||
Driven += Speed;
|
||||
}
|
||||
|
||||
public static RemoteControlCar Nitro() => new RemoteControlCar(50, 4);
|
||||
}
|
||||
|
||||
class RaceTrack
|
||||
{
|
||||
private readonly int Distance;
|
||||
|
||||
public RaceTrack(int distance)
|
||||
{
|
||||
Distance = distance;
|
||||
}
|
||||
|
||||
public bool CarCanFinish(RemoteControlCar car)
|
||||
{
|
||||
while (car.DistanceDriven() < Distance)
|
||||
{
|
||||
if (car.BatteryDrained()) return false;
|
||||
car.Drive();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net5.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>
|
|
@ -0,0 +1,217 @@
|
|||
using Xunit;
|
||||
using Exercism.Tests;
|
||||
|
||||
public class NeedForSpeedTests
|
||||
{
|
||||
[Fact]
|
||||
[Task(3)]
|
||||
public void New_remote_control_car_has_not_driven_any_distance()
|
||||
{
|
||||
int speed = 10;
|
||||
int batteryDrain = 2;
|
||||
var car = new RemoteControlCar(speed, batteryDrain);
|
||||
|
||||
Assert.Equal(0, car.DistanceDriven());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Task(3)]
|
||||
public void Drive_increases_distance_driven_with_speed()
|
||||
{
|
||||
int speed = 5;
|
||||
int batteryDrain = 1;
|
||||
var car = new RemoteControlCar(speed, batteryDrain);
|
||||
|
||||
car.Drive();
|
||||
|
||||
Assert.Equal(5, car.DistanceDriven());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Task(4)]
|
||||
public void Drive_does_not_increase_distance_driven_when_battery_drained()
|
||||
{
|
||||
int speed = 9;
|
||||
int batteryDrain = 50;
|
||||
var car = new RemoteControlCar(speed, batteryDrain);
|
||||
|
||||
// Drain the battery
|
||||
car.Drive();
|
||||
car.Drive();
|
||||
|
||||
// One extra drive attempt (should not succeed)
|
||||
car.Drive();
|
||||
|
||||
Assert.Equal(18, car.DistanceDriven());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Task(4)]
|
||||
public void New_remote_control_car_battery_is_not_drained()
|
||||
{
|
||||
int speed = 15;
|
||||
int batteryDrain = 3;
|
||||
var car = new RemoteControlCar(speed, batteryDrain);
|
||||
|
||||
Assert.False(car.BatteryDrained());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Task(4)]
|
||||
public void Drive_to_almost_drain_battery()
|
||||
{
|
||||
int speed = 2;
|
||||
int batteryDrain = 1;
|
||||
var car = new RemoteControlCar(speed, batteryDrain);
|
||||
|
||||
// Almost drain the battery
|
||||
for (var i = 0; i < 99; i++)
|
||||
{
|
||||
car.Drive();
|
||||
}
|
||||
|
||||
Assert.False(car.BatteryDrained());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Task(4)]
|
||||
public void Drive_until_battery_is_drained()
|
||||
{
|
||||
int speed = 2;
|
||||
int batteryDrain = 1;
|
||||
var car = new RemoteControlCar(speed, batteryDrain);
|
||||
|
||||
// Drain the battery
|
||||
for (var i = 0; i < 100; i++)
|
||||
{
|
||||
car.Drive();
|
||||
}
|
||||
|
||||
Assert.True(car.BatteryDrained());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Task(4)]
|
||||
public void Super_hungry_car_after_one_drive_is_drained()
|
||||
{
|
||||
int speed = 100;
|
||||
int batteryDrain = 60;
|
||||
var car = new RemoteControlCar(speed, batteryDrain);
|
||||
car.Drive();
|
||||
Assert.True(car.BatteryDrained());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Task(4)]
|
||||
public void Super_hungry_car_can_try_driving_but_is_drained()
|
||||
{
|
||||
int speed = 100;
|
||||
int batteryDrain = 60;
|
||||
var car = new RemoteControlCar(speed, batteryDrain);
|
||||
car.Drive();
|
||||
car.Drive();
|
||||
Assert.True(car.BatteryDrained());
|
||||
Assert.Equal(100, car.DistanceDriven());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Task(5)]
|
||||
public void Nitro_car_has_not_driven_any_distance()
|
||||
{
|
||||
var car = RemoteControlCar.Nitro();
|
||||
Assert.Equal(0, car.DistanceDriven());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Task(5)]
|
||||
public void Nitro_car_has_battery_not_drained()
|
||||
{
|
||||
var car = RemoteControlCar.Nitro();
|
||||
Assert.False(car.BatteryDrained());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Task(5)]
|
||||
public void Nitro_car_has_correct_speed()
|
||||
{
|
||||
var car = RemoteControlCar.Nitro();
|
||||
car.Drive();
|
||||
Assert.Equal(50, car.DistanceDriven());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Task(5)]
|
||||
public void Nitro_car_has_correct_battery_drain()
|
||||
{
|
||||
var car = RemoteControlCar.Nitro();
|
||||
|
||||
// The battery is almost drained
|
||||
for (var i = 0; i < 24; i++)
|
||||
{
|
||||
car.Drive();
|
||||
}
|
||||
|
||||
Assert.False(car.BatteryDrained());
|
||||
|
||||
// Drain the battery
|
||||
car.Drive();
|
||||
|
||||
Assert.True(car.BatteryDrained());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Task(6)]
|
||||
public void Car_can_finish_with_car_that_can_easily_finish()
|
||||
{
|
||||
int speed = 10;
|
||||
int batteryDrain = 2;
|
||||
var car = new RemoteControlCar(speed, batteryDrain);
|
||||
|
||||
int distance = 100;
|
||||
var race = new RaceTrack(distance);
|
||||
|
||||
Assert.True(race.CarCanFinish(car));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Task(6)]
|
||||
public void Car_can_finish_with_car_that_can_just_finish()
|
||||
{
|
||||
int speed = 2;
|
||||
int batteryDrain = 10;
|
||||
var car = new RemoteControlCar(speed, batteryDrain);
|
||||
|
||||
int distance = 20;
|
||||
var race = new RaceTrack(distance);
|
||||
|
||||
Assert.True(race.CarCanFinish(car));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Task(6)]
|
||||
public void Car_can_finish_with_car_that_just_cannot_finish()
|
||||
{
|
||||
int speed = 3;
|
||||
int batteryDrain = 20;
|
||||
var car = new RemoteControlCar(speed, batteryDrain);
|
||||
|
||||
int distance = 16;
|
||||
var race = new RaceTrack(distance);
|
||||
|
||||
Assert.False(race.CarCanFinish(car));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Task(6)]
|
||||
public void Car_can_finish_with_car_that_cannot_finish()
|
||||
{
|
||||
int speed = 1;
|
||||
int batteryDrain = 20;
|
||||
var car = new RemoteControlCar(speed, batteryDrain);
|
||||
|
||||
int distance = 678;
|
||||
var race = new RaceTrack(distance);
|
||||
|
||||
Assert.False(race.CarCanFinish(car));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,139 @@
|
|||
# Need for Speed
|
||||
|
||||
Welcome to Need for Speed on Exercism's C# Track.
|
||||
If you need help running the tests or submitting your code, check out `HELP.md`.
|
||||
If you get stuck on the exercise, check out `HINTS.md`, but try and solve it without using those first :)
|
||||
|
||||
## Introduction
|
||||
|
||||
Creating an instance of a _class_ is done by calling its _constructor_ through the `new` operator. A constructor is a special type of method whose goal is to initialize a newly created instance. Constructors look like regular methods, but without a return type and with a name that matches the classes' name.
|
||||
|
||||
```csharp
|
||||
class Library
|
||||
{
|
||||
private books;
|
||||
|
||||
public Library()
|
||||
{
|
||||
// Initialize the books field
|
||||
this.books = 10;
|
||||
}
|
||||
}
|
||||
|
||||
// This will call the constructor
|
||||
var library = new Library();
|
||||
```
|
||||
|
||||
Like regular methods, constructors can have parameters. Constructor parameters are usually stored as (private) fields to be accessed later, or else used in some one-off calculation. Arguments can be passed to constructors just like passing arguments to regular methods.
|
||||
|
||||
```csharp
|
||||
class Building
|
||||
{
|
||||
private int numberOfStories;
|
||||
private int totalHeight;
|
||||
|
||||
public Building(int numberOfStories, double storyHeight)
|
||||
{
|
||||
this.numberOfStories = numberOfStories;
|
||||
this.totalHeight = numberOfStories * storyHeight;
|
||||
}
|
||||
}
|
||||
|
||||
// Call a constructor with two arguments
|
||||
var largeBuilding = new Building(55, 6.2)
|
||||
```
|
||||
|
||||
## Instructions
|
||||
|
||||
In this exercise you'll be organizing races between various types of remote controlled cars. Each car has its own speed and battery drain characteristics.
|
||||
|
||||
Cars start with full (100%) batteries. Each time you drive the car using the remote control, it covers the car's speed in meters and decreases the remaining battery percentage by its battery drain.
|
||||
|
||||
If a car's battery is below its battery drain percentage, you can't drive the car anymore.
|
||||
|
||||
Each race track has its own distance. Cars are tested by checking if they can finish the track without running out of battery.
|
||||
|
||||
You have six tasks, each of which will work with remote controller car instances.
|
||||
|
||||
## 1. Creating a remote controlled car
|
||||
|
||||
Allow creating a remote controller car by defining a constructor for the `RemoteControlCar` class that takes the speed of the car in meters and the battery drain percentage as its two parameters (both of type `int`):
|
||||
|
||||
```csharp
|
||||
int speed = 5;
|
||||
int batteryDrain = 2;
|
||||
var car = new RemoteControlCar(speed, batteryDrain);
|
||||
```
|
||||
|
||||
## 2. Creating a race track
|
||||
|
||||
Allow creating a race track by defining a constructor for the `RaceTrack` class that takes the track's distance in meters as its sole parameter (which is of type `int`):
|
||||
|
||||
```csharp
|
||||
int distance = 800;
|
||||
var raceTrack = new RaceTrack(distance);
|
||||
```
|
||||
|
||||
## 3. Drive the car
|
||||
|
||||
Implement the `RemoteControlCar.Drive()` method that updates the number of meters driven based on the car's speed. Also implement the `RemoteControlCar.DistanceDriven()` method to return the number of meters driven by the car:
|
||||
|
||||
```csharp
|
||||
int speed = 5;
|
||||
int batteryDrain = 2;
|
||||
var car = new RemoteControlCar(speed, batteryDrain);
|
||||
car.Drive();
|
||||
|
||||
car.DistanceDriven();
|
||||
// => 5
|
||||
```
|
||||
|
||||
## 4. Check for a drained battery
|
||||
|
||||
Update the `RemoteControlCar.Drive()` method to drain the battery based on the car's battery drain. Also implement the `RemoteControlCar.BatteryDrained()` method that indicates if the battery is drained:
|
||||
|
||||
```csharp
|
||||
int speed = 5;
|
||||
int batteryDrain = 2;
|
||||
var car = new RemoteControlCar(speed, batteryDrain);
|
||||
car.Drive();
|
||||
|
||||
car.BatteryDrained();
|
||||
// => false
|
||||
```
|
||||
|
||||
## 5. Create the Nitro remote control car
|
||||
|
||||
The best-selling remote control car is the Nitro, which has a stunning top speed of 50 meters with a battery drain of 4%. Implement the (static) `RemoteControlCar.Nitro()` method to return this type of car:
|
||||
|
||||
```csharp
|
||||
var car = RemoteControlCar.Nitro();
|
||||
car.Drive();
|
||||
car.DistanceDriven();
|
||||
// => 50
|
||||
```
|
||||
## 6. Check if a remote control car can finish a race
|
||||
|
||||
To finish a race, a car has to be able to drive the race's distance. This means not draining its battery before having crossed the finish line. Implement the `Race.CarCanFinish()` method that takes a `RemoteControlCar` instance as its parameter and returns `true` if the car can finish the race; otherwise, return `false`:
|
||||
|
||||
```csharp
|
||||
int speed = 5;
|
||||
int batteryDrain = 2;
|
||||
var car = new RemoteControlCar(speed, batteryDrain);
|
||||
|
||||
int distance = 100;
|
||||
var race = new Race(distance);
|
||||
|
||||
race.CarCanFinish(car);
|
||||
// => true
|
||||
```
|
||||
|
||||
## Source
|
||||
|
||||
### Created by
|
||||
|
||||
- @ErikSchierboom
|
||||
|
||||
### Contributed to by
|
||||
|
||||
- @yzAlvin
|
|
@ -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
|
||||
|
||||
[ResistorColor.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
|
|
@ -0,0 +1,23 @@
|
|||
{
|
||||
"blurb": "Convert a resistor band's color to its numeric representation",
|
||||
"authors": [
|
||||
"ErikSchierboom"
|
||||
],
|
||||
"contributors": [
|
||||
"robkeim",
|
||||
"wolf99"
|
||||
],
|
||||
"files": {
|
||||
"solution": [
|
||||
"ResistorColor.cs"
|
||||
],
|
||||
"test": [
|
||||
"ResistorColorTests.cs"
|
||||
],
|
||||
"example": [
|
||||
".meta/Example.cs"
|
||||
]
|
||||
},
|
||||
"source": "Maud de Vries, Erik Schierboom",
|
||||
"source_url": "https://github.com/exercism/problem-specifications/issues/1458"
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
{"track":"csharp","exercise":"resistor-color","id":"68befdac931f40fcad02b4ff40848c1e","url":"https://exercism.org/tracks/csharp/exercises/resistor-color","handle":"benharri","is_requester":true,"auto_approve":false}
|
|
@ -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 ResistorColor.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.
|
|
@ -0,0 +1,55 @@
|
|||
# Resistor Color
|
||||
|
||||
Welcome to Resistor Color on Exercism's C# Track.
|
||||
If you need help running the tests or submitting your code, check out `HELP.md`.
|
||||
|
||||
## Instructions
|
||||
|
||||
If you want to build something using a Raspberry Pi, you'll probably use _resistors_.
|
||||
For this exercise, you need to know two things about them:
|
||||
|
||||
* Each resistor has a resistance value.
|
||||
* Resistors are small - so small in fact that if you printed the resistance value on them, it would be hard to read.
|
||||
|
||||
To get around this problem, manufacturers print color-coded bands onto the resistors to denote their resistance values.
|
||||
Each band has a position and a numeric value.
|
||||
|
||||
The first 2 bands of a resistor have a simple encoding scheme: each color maps to a single number.
|
||||
|
||||
In this exercise you are going to create a helpful program so that you don't have to remember the values of the bands.
|
||||
|
||||
These colors are encoded as follows:
|
||||
|
||||
- Black: 0
|
||||
- Brown: 1
|
||||
- Red: 2
|
||||
- Orange: 3
|
||||
- Yellow: 4
|
||||
- Green: 5
|
||||
- Blue: 6
|
||||
- Violet: 7
|
||||
- Grey: 8
|
||||
- White: 9
|
||||
|
||||
The goal of this exercise is to create a way:
|
||||
- to look up the numerical value associated with a particular color band
|
||||
- to list the different band colors
|
||||
|
||||
Mnemonics map the colors to the numbers, that, when stored as an array, happen to map to their index in the array: Better Be Right Or Your Great Big Values Go Wrong.
|
||||
|
||||
More information on the color encoding of resistors can be found in the [Electronic color code Wikipedia article](https://en.wikipedia.org/wiki/Electronic_color_code)
|
||||
|
||||
## Source
|
||||
|
||||
### Created by
|
||||
|
||||
- @ErikSchierboom
|
||||
|
||||
### Contributed to by
|
||||
|
||||
- @robkeim
|
||||
- @wolf99
|
||||
|
||||
### Based on
|
||||
|
||||
Maud de Vries, Erik Schierboom - https://github.com/exercism/problem-specifications/issues/1458
|
|
@ -0,0 +1,29 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
public static class ResistorColor
|
||||
{
|
||||
private static readonly Dictionary<string, int> colors = new()
|
||||
{
|
||||
{"black", 0},
|
||||
{"brown", 1},
|
||||
{"red", 2},
|
||||
{"orange", 3},
|
||||
{"yellow", 4},
|
||||
{"green", 5},
|
||||
{"blue", 6},
|
||||
{"violet", 7},
|
||||
{"grey", 8},
|
||||
{"white", 9}
|
||||
};
|
||||
|
||||
public static int ColorCode(string color)
|
||||
{
|
||||
if (colors.ContainsKey(color))
|
||||
return colors[color];
|
||||
throw new ArgumentException($"{color} does not have a matching color code", nameof(color));
|
||||
}
|
||||
|
||||
public static string[] Colors() => colors.Keys.ToArray();
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net5.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>
|
|
@ -0,0 +1,28 @@
|
|||
using Xunit;
|
||||
|
||||
public class ResistorColorTests
|
||||
{
|
||||
[Fact]
|
||||
public void Black()
|
||||
{
|
||||
Assert.Equal(0, ResistorColor.ColorCode("black"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void White()
|
||||
{
|
||||
Assert.Equal(9, ResistorColor.ColorCode("white"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Orange()
|
||||
{
|
||||
Assert.Equal(3, ResistorColor.ColorCode("orange"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Colors()
|
||||
{
|
||||
Assert.Equal(new[] { "black", "brown", "red", "orange", "yellow", "green", "blue", "violet", "grey", "white" }, ResistorColor.Colors());
|
||||
}
|
||||
}
|
|
@ -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
|
||||
|
||||
[RobotName.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
|
|
@ -0,0 +1,28 @@
|
|||
{
|
||||
"blurb": "Manage robot factory settings.",
|
||||
"authors": [
|
||||
"bressain"
|
||||
],
|
||||
"contributors": [
|
||||
"ErikSchierboom",
|
||||
"haus",
|
||||
"j2jensen",
|
||||
"jwood803",
|
||||
"kytrinyx",
|
||||
"robkeim",
|
||||
"vgrigoriu",
|
||||
"wolf99"
|
||||
],
|
||||
"files": {
|
||||
"solution": [
|
||||
"RobotName.cs"
|
||||
],
|
||||
"test": [
|
||||
"RobotNameTests.cs"
|
||||
],
|
||||
"example": [
|
||||
".meta/Example.cs"
|
||||
]
|
||||
},
|
||||
"source": "A debugging session with Paul Blackwell at gSchool."
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
{"track":"csharp","exercise":"robot-name","id":"3a305537987b413fa7b0ab77996f7f82","url":"https://exercism.org/tracks/csharp/exercises/robot-name","handle":"benharri","is_requester":true,"auto_approve":false}
|
|
@ -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 RobotName.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.
|
|
@ -0,0 +1,42 @@
|
|||
# Robot Name
|
||||
|
||||
Welcome to Robot Name on Exercism's C# Track.
|
||||
If you need help running the tests or submitting your code, check out `HELP.md`.
|
||||
|
||||
## Instructions
|
||||
|
||||
Manage robot factory settings.
|
||||
|
||||
When a robot comes off the factory floor, it has no name.
|
||||
|
||||
The first time you turn on a robot, a random name is generated in the format
|
||||
of two uppercase letters followed by three digits, such as RX837 or BC811.
|
||||
|
||||
Every once in a while we need to reset a robot to its factory settings,
|
||||
which means that its name gets wiped. The next time you ask, that robot will
|
||||
respond with a new random name.
|
||||
|
||||
The names must be random: they should not follow a predictable sequence.
|
||||
Using random names means a risk of collisions. Your solution must ensure that
|
||||
every existing robot has a unique name.
|
||||
|
||||
## Source
|
||||
|
||||
### Created by
|
||||
|
||||
- @bressain
|
||||
|
||||
### Contributed to by
|
||||
|
||||
- @ErikSchierboom
|
||||
- @haus
|
||||
- @j2jensen
|
||||
- @jwood803
|
||||
- @kytrinyx
|
||||
- @robkeim
|
||||
- @vgrigoriu
|
||||
- @wolf99
|
||||
|
||||
### Based on
|
||||
|
||||
A debugging session with Paul Blackwell at gSchool.
|
|
@ -0,0 +1,27 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class Robot
|
||||
{
|
||||
private static readonly HashSet<string> _usedNames = new();
|
||||
private readonly Random _rand;
|
||||
public string Name { get; set; }
|
||||
|
||||
public Robot()
|
||||
{
|
||||
_rand = new();
|
||||
Reset();
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
var name = GenerateRandomName();
|
||||
while (!_usedNames.Add(name))
|
||||
name = GenerateRandomName();
|
||||
|
||||
Name = name;
|
||||
}
|
||||
|
||||
private string GenerateRandomName() =>
|
||||
$"{Convert.ToChar(_rand.Next(65, 91))}{Convert.ToChar(_rand.Next(65, 91))}{_rand.Next(0, 9)}{_rand.Next(0, 9)}{_rand.Next(0, 9)}";
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net5.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>
|
|
@ -0,0 +1,72 @@
|
|||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
using Xunit;
|
||||
|
||||
public class RobotNameTests
|
||||
{
|
||||
private readonly Robot robot = new Robot();
|
||||
|
||||
[Fact]
|
||||
public void Robot_has_a_name()
|
||||
{
|
||||
Assert.Matches(@"^[A-Z]{2}\d{3}$", robot.Name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Name_is_the_same_each_time()
|
||||
{
|
||||
Assert.Equal(robot.Name, robot.Name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Different_robots_have_different_names()
|
||||
{
|
||||
var robot2 = new Robot();
|
||||
Assert.NotEqual(robot2.Name, robot.Name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Can_reset_the_name()
|
||||
{
|
||||
var originalName = robot.Name;
|
||||
robot.Reset();
|
||||
Assert.NotEqual(originalName, robot.Name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void After_reset_the_name_is_valid()
|
||||
{
|
||||
robot.Reset();
|
||||
Assert.Matches(@"^[A-Z]{2}\d{3}$", robot.Name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Robot_names_are_unique()
|
||||
{
|
||||
const int robotsCount = 10_000;
|
||||
var robots = new List<Robot>(robotsCount); // Needed to keep a reference to the robots as IDs of recycled robots may be re-issued
|
||||
var names = new HashSet<string>(robotsCount);
|
||||
for (int i = 0; i < robotsCount; i++) {
|
||||
var robot = new Robot();
|
||||
robots.Add(robot);
|
||||
Assert.True(names.Add(robot.Name));
|
||||
Assert.Matches(@"^[A-Z]{2}\d{3}$", robot.Name);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Robot_names_should_generate_edge_case_a()
|
||||
{
|
||||
const int robotsCount = 10_000;
|
||||
var robots = Enumerable.Range(0,robotsCount).Select( x => new Robot());
|
||||
Assert.Contains(robots, robot => robot.Name.Contains('A'));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Robot_names_should_generate_edge_case_z()
|
||||
{
|
||||
const int robotsCount = 10_000;
|
||||
var robots = Enumerable.Range(0,robotsCount).Select( x => new Robot());
|
||||
Assert.Contains(robots, robot => robot.Name.Contains('Z'));
|
||||
}
|
||||
}
|
|
@ -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
|
||||
|
||||
[RotationalCipher.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
|
|
@ -0,0 +1,24 @@
|
|||
{
|
||||
"blurb": "Create an implementation of the rotational cipher, also sometimes called the Caesar cipher.",
|
||||
"authors": [
|
||||
"ErikSchierboom"
|
||||
],
|
||||
"contributors": [
|
||||
"j2jensen",
|
||||
"robkeim",
|
||||
"wolf99"
|
||||
],
|
||||
"files": {
|
||||
"solution": [
|
||||
"RotationalCipher.cs"
|
||||
],
|
||||
"test": [
|
||||
"RotationalCipherTests.cs"
|
||||
],
|
||||
"example": [
|
||||
".meta/Example.cs"
|
||||
]
|
||||
},
|
||||
"source": "Wikipedia",
|
||||
"source_url": "https://en.wikipedia.org/wiki/Caesar_cipher"
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
{"track":"csharp","exercise":"rotational-cipher","id":"de6c3ce02df643ed811233a7bb73ed78","url":"https://exercism.org/tracks/csharp/exercises/rotational-cipher","handle":"benharri","is_requester":true,"auto_approve":false}
|
|
@ -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 RotationalCipher.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.
|
|
@ -0,0 +1,52 @@
|
|||
# Rotational Cipher
|
||||
|
||||
Welcome to Rotational Cipher on Exercism's C# Track.
|
||||
If you need help running the tests or submitting your code, check out `HELP.md`.
|
||||
|
||||
## Instructions
|
||||
|
||||
Create an implementation of the rotational cipher, also sometimes called the Caesar cipher.
|
||||
|
||||
The Caesar cipher is a simple shift cipher that relies on
|
||||
transposing all the letters in the alphabet using an integer key
|
||||
between `0` and `26`. Using a key of `0` or `26` will always yield
|
||||
the same output due to modular arithmetic. The letter is shifted
|
||||
for as many values as the value of the key.
|
||||
|
||||
The general notation for rotational ciphers is `ROT + <key>`.
|
||||
The most commonly used rotational cipher is `ROT13`.
|
||||
|
||||
A `ROT13` on the Latin alphabet would be as follows:
|
||||
|
||||
```text
|
||||
Plain: abcdefghijklmnopqrstuvwxyz
|
||||
Cipher: nopqrstuvwxyzabcdefghijklm
|
||||
```
|
||||
|
||||
It is stronger than the Atbash cipher because it has 27 possible keys, and 25 usable keys.
|
||||
|
||||
Ciphertext is written out in the same formatting as the input including spaces and punctuation.
|
||||
|
||||
## Examples
|
||||
|
||||
- ROT5 `omg` gives `trl`
|
||||
- ROT0 `c` gives `c`
|
||||
- ROT26 `Cool` gives `Cool`
|
||||
- ROT13 `The quick brown fox jumps over the lazy dog.` gives `Gur dhvpx oebja sbk whzcf bire gur ynml qbt.`
|
||||
- ROT13 `Gur dhvpx oebja sbk whzcf bire gur ynml qbt.` gives `The quick brown fox jumps over the lazy dog.`
|
||||
|
||||
## Source
|
||||
|
||||
### Created by
|
||||
|
||||
- @ErikSchierboom
|
||||
|
||||
### Contributed to by
|
||||
|
||||
- @j2jensen
|
||||
- @robkeim
|
||||
- @wolf99
|
||||
|
||||
### Based on
|
||||
|
||||
Wikipedia - https://en.wikipedia.org/wiki/Caesar_cipher
|
|
@ -0,0 +1,16 @@
|
|||
using System.Text;
|
||||
|
||||
public static class RotationalCipher
|
||||
{
|
||||
public static string Rotate(string text, int shiftKey)
|
||||
{
|
||||
StringBuilder result = new(text.Length);
|
||||
foreach (var c in text)
|
||||
{
|
||||
if (!char.IsLetter(c)) {result.Append(c);continue;}
|
||||
var d = char.IsUpper(c) ? 'A' : 'a';
|
||||
result.Append((char)((((c + shiftKey) - d) % 26) + d));
|
||||
}
|
||||
return result.ToString();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net5.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>
|
|
@ -0,0 +1,64 @@
|
|||
using Xunit;
|
||||
|
||||
public class RotationalCipherTests
|
||||
{
|
||||
[Fact]
|
||||
public void Rotate_a_by_0_same_output_as_input()
|
||||
{
|
||||
Assert.Equal("a", RotationalCipher.Rotate("a", 0));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Rotate_a_by_1()
|
||||
{
|
||||
Assert.Equal("b", RotationalCipher.Rotate("a", 1));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Rotate_a_by_26_same_output_as_input()
|
||||
{
|
||||
Assert.Equal("a", RotationalCipher.Rotate("a", 26));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Rotate_m_by_13()
|
||||
{
|
||||
Assert.Equal("z", RotationalCipher.Rotate("m", 13));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Rotate_n_by_13_with_wrap_around_alphabet()
|
||||
{
|
||||
Assert.Equal("a", RotationalCipher.Rotate("n", 13));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Rotate_capital_letters()
|
||||
{
|
||||
Assert.Equal("TRL", RotationalCipher.Rotate("OMG", 5));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Rotate_spaces()
|
||||
{
|
||||
Assert.Equal("T R L", RotationalCipher.Rotate("O M G", 5));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Rotate_numbers()
|
||||
{
|
||||
Assert.Equal("Xiwxmrk 1 2 3 xiwxmrk", RotationalCipher.Rotate("Testing 1 2 3 testing", 4));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Rotate_punctuation()
|
||||
{
|
||||
Assert.Equal("Gzo'n zvo, Bmviyhv!", RotationalCipher.Rotate("Let's eat, Grandma!", 21));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Rotate_all_letters()
|
||||
{
|
||||
Assert.Equal("Gur dhvpx oebja sbk whzcf bire gur ynml qbt.", RotationalCipher.Rotate("The quick brown fox jumps over the lazy dog.", 13));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
{
|
||||
"blurb": "Learn about casting by creating security passes.",
|
||||
"contributors": [
|
||||
"ErikSchierboom",
|
||||
"yzAlvin"
|
||||
],
|
||||
"authors": [
|
||||
"mikedamay"
|
||||
],
|
||||
"files": {
|
||||
"solution": [
|
||||
"SecureMunchesterUnited.cs"
|
||||
],
|
||||
"test": [
|
||||
"SecureMunchesterUnitedTests.cs"
|
||||
],
|
||||
"exemplar": [
|
||||
".meta/Exemplar.cs"
|
||||
]
|
||||
}
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
{"track":"csharp","exercise":"secure-munchester-united","id":"5f577767420743d88d545e16ab8f8592","url":"https://exercism.org/tracks/csharp/exercises/secure-munchester-united","handle":"benharri","is_requester":true,"auto_approve":false}
|
|
@ -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 SecureMunchesterUnited.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.
|
|
@ -0,0 +1,20 @@
|
|||
# Hints
|
||||
|
||||
## General
|
||||
|
||||
- [Type testing and cast operators][type-testing-and-cast-operators]: introduction to type testing and casting.
|
||||
- [cast operator][cast-operator]: the cast operator.
|
||||
|
||||
## 1. Get display name for a member of the support team as long as they are staff members
|
||||
|
||||
- Type conversions are discussed [here][is-operator].
|
||||
|
||||
## 3. Only designate principal security team members as priority personnel
|
||||
|
||||
- Testing of type equality is covered in this [article][get-type] and in [this article][typeof-operator].
|
||||
|
||||
[type-testing-and-cast-operators]: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/type-testing-and-cast
|
||||
[is-operator]: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/type-testing-and-cast#is-operator
|
||||
[cast-operator]: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/type-testing-and-cast#cast-expression
|
||||
[typeof-operator]: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/type-testing-and-cast#typeof-operator
|
||||
[get-type]: https://docs.microsoft.com/en-us/dotnet/api/system.object.gettype?view=netcore-3.1#System_Object_GetType
|
|
@ -0,0 +1,117 @@
|
|||
# Secure Munchester United
|
||||
|
||||
Welcome to Secure Munchester United on Exercism's C# Track.
|
||||
If you need help running the tests or submitting your code, check out `HELP.md`.
|
||||
If you get stuck on the exercise, check out `HINTS.md`, but try and solve it without using those first :)
|
||||
|
||||
## Introduction
|
||||
|
||||
Casting and type conversion are different ways of changing an expression from one data type to another.
|
||||
|
||||
An expression can be cast to another type with the cast operator `(<type>)`.
|
||||
|
||||
```csharp
|
||||
long l = 1000L;
|
||||
int i = (int)l;
|
||||
|
||||
object o = new Random();
|
||||
Random r = (Random)o;
|
||||
```
|
||||
|
||||
If the types are not compatible an instance of `InvalidCastException` is thrown. In the case of numbers this indicates that the receiving type cannot represent the cast value. In the case of classes, one of the types must be derived from the other (this also applies trivially to structs).
|
||||
|
||||
An alternative to _casting_ is _type conversion_ using the `is` operator. This is typically applied to reference and nullable types.
|
||||
|
||||
```csharp
|
||||
object o = new Random();
|
||||
if (o is Random rand)
|
||||
{
|
||||
int ii = rand.Next();
|
||||
// now, do something random
|
||||
}
|
||||
```
|
||||
|
||||
If you need to detect the precise type of an object then `is` may be a little too permissive as it will return true for a class and any of the classes and interfaces from which it is derived directly or indirectly. `typeof` and `Object.GetType()` are the solution in this case.
|
||||
|
||||
```csharp
|
||||
object o = new List<int>();
|
||||
|
||||
o is ICollection<int> // true
|
||||
o.GetType() == typeof(ICollection<int>) // false
|
||||
o is List<int> // true
|
||||
o.GetType() == typeof(List<int>) // true
|
||||
```
|
||||
|
||||
## Instructions
|
||||
|
||||
Our football club [exercise:csharp/football-match-reports]() is soaring in the leagues, and you have been invited to do some more work, this time on the security pass printing system.
|
||||
|
||||
The class hierarchy of the backroom staff is as follows
|
||||
|
||||
```
|
||||
TeamSupport (interface)
|
||||
├ Chairman
|
||||
├ Manager
|
||||
└ Staff (abstract)
|
||||
├ Physio
|
||||
├ OffensiveCoach
|
||||
├ GoalKeepingCoach
|
||||
└ Security
|
||||
├ SecurityJunior
|
||||
├ SecurityIntern
|
||||
└ PoliceLiaison
|
||||
```
|
||||
|
||||
A complete implementation of the hierarchy is provided as part of the source code for the exercise.
|
||||
|
||||
All data passed to the security pass maker has been validated and is guaranteed to be non-null.
|
||||
|
||||
## 1. Get display name for a member of the support team as long as they are staff members
|
||||
|
||||
Please implement the `SecurityPassMaker.GetDisplayName()` method. It should return the value of the `Title` field instances of all classes derived from `Staff` and, otherwise, "Too Important for a Security Pass".
|
||||
|
||||
```csharp
|
||||
var spm = new SecurityPassMaker();
|
||||
spm.GetDisplayName(new Manager());
|
||||
// => "Too Important for a Security Pass"
|
||||
spm.GetDisplayName(new Physio());
|
||||
// => "The Physio"
|
||||
```
|
||||
|
||||
## 2. Customize the display name for the security team
|
||||
|
||||
Please modify the `SecurityPassMaker.GetDisplayName()` method. It should behave as in Task 1 except that if the staff member is a member of the security team (either of type `Security` or one of its derivatives) then the text " Priority Personnel" should be displayed after the title.
|
||||
|
||||
```csharp
|
||||
var spm = new SecurityPassMaker();
|
||||
spm.GetDisplayName(new Physio());
|
||||
// => "The Physio"
|
||||
var spm2 = new SecurityPassMaker();
|
||||
spm2.GetDisplayName(new Security());
|
||||
// => "Security Team Member Priority Personnel"
|
||||
spm2.GetDisplayName(new SecurityJunior());
|
||||
// => "Security Junior Priority Personnel"
|
||||
```
|
||||
|
||||
## 3. Only designate principal security team members as priority personnel
|
||||
|
||||
Please modify the `SecurityPassMaker.GetDisplayName()` method. It should behave as in Task 2 except that the text " Priority Personnel" should not be displayed for instances of type `SecurityJunior`, `SecurityIntern` and `PoliceLiaison`.
|
||||
|
||||
```csharp
|
||||
var spm2 = new SecurityPassMaker();
|
||||
spm2.GetDisplayName(new Security());
|
||||
// => "Security Team Member Priority Personnel"
|
||||
spm2.GetDisplayName(new SecurityJunior());
|
||||
// => "Security Junior"
|
||||
```
|
||||
|
||||
## Source
|
||||
|
||||
### Created by
|
||||
|
||||
- @mikedamay
|
||||
|
||||
### Contributed to by
|
||||
|
||||
- @ErikSchierboom
|
||||
- @yzAlvin
|
|
@ -0,0 +1,33 @@
|
|||
using System;
|
||||
|
||||
public class SecurityPassMaker
|
||||
{
|
||||
public string GetDisplayName(TeamSupport support)
|
||||
{
|
||||
throw new NotImplementedException($"Please implement the SecurityPassMaker.GetDisplayName() method");
|
||||
}
|
||||
}
|
||||
|
||||
/**** Please do not alter the code below ****/
|
||||
|
||||
public interface TeamSupport { string Title { get; } }
|
||||
|
||||
public abstract class Staff : TeamSupport { public abstract string Title { get; } }
|
||||
|
||||
public class Manager : TeamSupport { public string Title { get; } = "The Manager"; }
|
||||
|
||||
public class Chairman : TeamSupport { public string Title { get; } = "The Chairman"; }
|
||||
|
||||
public class Physio : Staff { public override string Title { get; } = "The Physio"; }
|
||||
|
||||
public class OffensiveCoach : Staff { public override string Title { get; } = "Offensive Coach"; }
|
||||
|
||||
public class GoalKeepingCoach : Staff { public override string Title { get; } = "Goal Keeping Coach"; }
|
||||
|
||||
public class Security : Staff { public override string Title { get; } = "Security Team Member"; }
|
||||
|
||||
public class SecurityJunior : Security { public override string Title { get; } = "Security Junior"; }
|
||||
|
||||
public class SecurityIntern : Security { public override string Title { get; } = "Security Intern"; }
|
||||
|
||||
public class PoliceLiaison : Security { public override string Title { get; } = "Police Liaison Officer"; }
|
|
@ -0,0 +1,14 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net5.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>
|
|
@ -0,0 +1,54 @@
|
|||
using System;
|
||||
using Xunit;
|
||||
using Exercism.Tests;
|
||||
|
||||
public class SecureMunchesterUnitedTests
|
||||
{
|
||||
[Fact]
|
||||
[Task(1)]
|
||||
public void DisplaySecurityPass_manager()
|
||||
{
|
||||
var spm = new SecurityPassMaker();
|
||||
Assert.Equal("Too Important for a Security Pass", spm.GetDisplayName(new Manager()));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Task(1)]
|
||||
public void DisplaySecurityPass_physio()
|
||||
{
|
||||
var spm = new SecurityPassMaker();
|
||||
Assert.Equal("The Physio", spm.GetDisplayName(new Physio()));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Task(2)]
|
||||
public void DisplaySecurityPass_security()
|
||||
{
|
||||
var spm = new SecurityPassMaker();
|
||||
Assert.Equal("Security Team Member Priority Personnel", spm.GetDisplayName(new Security()));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Task(3)]
|
||||
public void DisplaySecurityPass_security_junior()
|
||||
{
|
||||
var spm = new SecurityPassMaker();
|
||||
Assert.Equal("Security Junior", spm.GetDisplayName(new SecurityJunior()));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Task(3)]
|
||||
public void DisplaySecurityPass_security_police_liaison()
|
||||
{
|
||||
var spm = new SecurityPassMaker();
|
||||
Assert.Equal("Police Liaison Officer", spm.GetDisplayName(new PoliceLiaison()));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Task(3)]
|
||||
public void DisplaySecurityPass_security_intern()
|
||||
{
|
||||
var spm = new SecurityPassMaker();
|
||||
Assert.Equal("Security Intern", spm.GetDisplayName(new SecurityIntern()));
|
||||
}
|
||||
}
|
|
@ -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
|
||||
|
||||
[SpaceAge.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
|
|
@ -0,0 +1,28 @@
|
|||
{
|
||||
"blurb": "Given an age in seconds, calculate how old someone is in terms of a given planet's solar years.",
|
||||
"authors": [
|
||||
"bressain"
|
||||
],
|
||||
"contributors": [
|
||||
"ErikSchierboom",
|
||||
"j2jensen",
|
||||
"jwood803",
|
||||
"kytrinyx",
|
||||
"mikecoop",
|
||||
"robkeim",
|
||||
"wolf99"
|
||||
],
|
||||
"files": {
|
||||
"solution": [
|
||||
"SpaceAge.cs"
|
||||
],
|
||||
"test": [
|
||||
"SpaceAgeTests.cs"
|
||||
],
|
||||
"example": [
|
||||
".meta/Example.cs"
|
||||
]
|
||||
},
|
||||
"source": "Partially inspired by Chapter 1 in Chris Pine's online Learn to Program tutorial.",
|
||||
"source_url": "http://pine.fm/LearnToProgram/?Chapter=01"
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
{"track":"csharp","exercise":"space-age","id":"00465cd77f444e09b9a6e5c562602dd6","url":"https://exercism.org/tracks/csharp/exercises/space-age","handle":"benharri","is_requester":true,"auto_approve":false}
|
|
@ -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 SpaceAge.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.
|
|
@ -0,0 +1,43 @@
|
|||
# Space Age
|
||||
|
||||
Welcome to Space Age on Exercism's C# Track.
|
||||
If you need help running the tests or submitting your code, check out `HELP.md`.
|
||||
|
||||
## Instructions
|
||||
|
||||
Given an age in seconds, calculate how old someone would be on:
|
||||
|
||||
- Mercury: orbital period 0.2408467 Earth years
|
||||
- Venus: orbital period 0.61519726 Earth years
|
||||
- Earth: orbital period 1.0 Earth years, 365.25 Earth days, or 31557600 seconds
|
||||
- Mars: orbital period 1.8808158 Earth years
|
||||
- Jupiter: orbital period 11.862615 Earth years
|
||||
- Saturn: orbital period 29.447498 Earth years
|
||||
- Uranus: orbital period 84.016846 Earth years
|
||||
- Neptune: orbital period 164.79132 Earth years
|
||||
|
||||
So if you were told someone were 1,000,000,000 seconds old, you should
|
||||
be able to say that they're 31.69 Earth-years old.
|
||||
|
||||
If you're wondering why Pluto didn't make the cut, go watch [this
|
||||
youtube video](http://www.youtube.com/watch?v=Z_2gbGXzFbs).
|
||||
|
||||
## Source
|
||||
|
||||
### Created by
|
||||
|
||||
- @bressain
|
||||
|
||||
### Contributed to by
|
||||
|
||||
- @ErikSchierboom
|
||||
- @j2jensen
|
||||
- @jwood803
|
||||
- @kytrinyx
|
||||
- @mikecoop
|
||||
- @robkeim
|
||||
- @wolf99
|
||||
|
||||
### Based on
|
||||
|
||||
Partially inspired by Chapter 1 in Chris Pine's online Learn to Program tutorial. - http://pine.fm/LearnToProgram/?Chapter=01
|
|
@ -0,0 +1,24 @@
|
|||
using System;
|
||||
|
||||
public class SpaceAge
|
||||
{
|
||||
private readonly int Seconds;
|
||||
|
||||
public SpaceAge(int seconds) => Seconds = seconds;
|
||||
|
||||
public double OnEarth() => Seconds / 31557600.0;
|
||||
|
||||
public double OnMercury() => OnEarth() / 0.2408467;
|
||||
|
||||
public double OnVenus() => OnEarth() / 0.61519726;
|
||||
|
||||
public double OnMars() => OnEarth() / 1.8808158;
|
||||
|
||||
public double OnJupiter() => OnEarth() / 11.862615;
|
||||
|
||||
public double OnSaturn() => OnEarth() / 29.447498;
|
||||
|
||||
public double OnUranus() => OnEarth() / 84.016846;
|
||||
|
||||
public double OnNeptune() => OnEarth() / 164.79132;
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net5.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>
|
|
@ -0,0 +1,60 @@
|
|||
using Xunit;
|
||||
|
||||
public class SpaceAgeTests
|
||||
{
|
||||
[Fact]
|
||||
public void Age_on_earth()
|
||||
{
|
||||
var sut = new SpaceAge(1000000000);
|
||||
Assert.Equal(31.69, sut.OnEarth(), precision: 2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Age_on_mercury()
|
||||
{
|
||||
var sut = new SpaceAge(2134835688);
|
||||
Assert.Equal(280.88, sut.OnMercury(), precision: 2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Age_on_venus()
|
||||
{
|
||||
var sut = new SpaceAge(189839836);
|
||||
Assert.Equal(9.78, sut.OnVenus(), precision: 2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Age_on_mars()
|
||||
{
|
||||
var sut = new SpaceAge(2129871239);
|
||||
Assert.Equal(35.88, sut.OnMars(), precision: 2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Age_on_jupiter()
|
||||
{
|
||||
var sut = new SpaceAge(901876382);
|
||||
Assert.Equal(2.41, sut.OnJupiter(), precision: 2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Age_on_saturn()
|
||||
{
|
||||
var sut = new SpaceAge(2000000000);
|
||||
Assert.Equal(2.15, sut.OnSaturn(), precision: 2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Age_on_uranus()
|
||||
{
|
||||
var sut = new SpaceAge(1210123456);
|
||||
Assert.Equal(0.46, sut.OnUranus(), precision: 2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Age_on_neptune()
|
||||
{
|
||||
var sut = new SpaceAge(1821023456);
|
||||
Assert.Equal(0.35, sut.OnNeptune(), precision: 2);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
{
|
||||
"blurb": "Learn about inheritance by creating an RPG.",
|
||||
"contributors": [
|
||||
"yzAlvin"
|
||||
],
|
||||
"authors": [
|
||||
"ErikSchierboom"
|
||||
],
|
||||
"files": {
|
||||
"solution": [
|
||||
"WizardsAndWarriors.cs"
|
||||
],
|
||||
"test": [
|
||||
"WizardsAndWarriorsTests.cs"
|
||||
],
|
||||
"exemplar": [
|
||||
".meta/Exemplar.cs"
|
||||
]
|
||||
}
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
{"track":"csharp","exercise":"wizards-and-warriors","id":"be0aed7abc6646939c0465821b629509","url":"https://exercism.org/tracks/csharp/exercises/wizards-and-warriors","handle":"benharri","is_requester":true,"auto_approve":false}
|
|
@ -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 WizardsAndWarriors.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.
|
|
@ -0,0 +1,35 @@
|
|||
# Hints
|
||||
|
||||
## 1. Describe a character
|
||||
|
||||
- Modify the [constructors][constructor-syntax] of the `Wizard` and `Warrior` classes to pass their character type to the [base class' constructor][instance-constructors].
|
||||
- Store the character type as a [field][fields] on the `Character` class.
|
||||
- The `Character` class implicitly inherits from the `object` class, which [`ToString()` method you can override][override-tostring].
|
||||
|
||||
## 2. Make characters not vulnerable by default
|
||||
|
||||
- Implement the `Vulnerable()` method in the `Character` class to always return `false`.
|
||||
|
||||
## 3. Allow Wizards to prepare a spell
|
||||
|
||||
- Add a field to the `Wizard` class to store if a spell was prepared and update this in the `PrepareSpell()` method.
|
||||
- The spell should start out as not being prepared.
|
||||
|
||||
## 4. Make Wizards vulnerable when not having prepared a spell
|
||||
|
||||
- Override the `Vulnerable()` method in the `Wizard` class to make Wizards vulnerable if they haven't prepared a spell.
|
||||
|
||||
## 5. Calculate the damage points for a Wizard
|
||||
|
||||
- Use a [conditional statement][if-else] to return the the damage points, taking into account the value of the prepare spell field.
|
||||
|
||||
## 6. Calculate the damage points for a Warrior
|
||||
|
||||
- You can call a method on the passed `Character` instance to determine its vulnerability.
|
||||
- Use a [conditional statement][if-else] to return the the damage points, taking into account the vulnerability of the target.
|
||||
|
||||
[constructor-syntax]: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/constructors#constructor-syntax
|
||||
[instance-constructors]: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/instance-constructors
|
||||
[fields]: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/fields
|
||||
[override-tostring]: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/how-to-override-the-tostring-method
|
||||
[if-else]: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/if-else
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue