did a couple more

This commit is contained in:
Ben Harris 2021-11-09 22:37:26 -05:00
parent 9c00dded72
commit 81173dd380
48 changed files with 2346 additions and 0 deletions

View File

@ -0,0 +1,141 @@
###############################
# Core EditorConfig Options #
###############################
; This file is for unifying the coding style for different editors and IDEs.
; More information at:
; https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference?view=vs-2017
; https://docs.microsoft.com/en-us/visualstudio/ide/create-portable-custom-editor-options?view=vs-2017
root = true
[*]
indent_style = space
[BinarySearch.cs]
indent_size = 4
###############################
# .NET Coding Conventions #
###############################
# Organize usings
dotnet_sort_system_directives_first = true
dotnet_separate_import_directive_groups = true
# this. preferences
dotnet_style_qualification_for_field = false:suggestion
dotnet_style_qualification_for_property = false:suggestion
dotnet_style_qualification_for_method = false:suggestion
dotnet_style_qualification_for_event = false:suggestion
# Language keywords vs BCL types preferences
dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion
dotnet_style_predefined_type_for_member_access = true:suggestion
# Parentheses preferences
dotnet_style_parentheses_in_arithmetic_binary_operators = never_if_unnecessary:none
dotnet_style_parentheses_in_relational_binary_operators = never_if_unnecessary:none
dotnet_style_parentheses_in_other_binary_operators = never_if_unnecessary:none
dotnet_style_parentheses_in_other_operators = never_if_unnecessary:suggestion
# Modifier preferences
dotnet_style_require_accessibility_modifiers = always:suggestion
dotnet_style_readonly_field = true:suggestion
# Expression-level preferences
dotnet_style_object_initializer = true:suggestion
dotnet_style_collection_initializer = true:suggestion
dotnet_style_explicit_tuple_names = true:suggestion
dotnet_style_prefer_inferred_tuple_names = true:suggestion
dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion
dotnet_style_prefer_auto_properties = true:suggestion
dotnet_style_prefer_is_null_check_over_reference_equality_method = true:suggestion
dotnet_style_prefer_conditional_expression_over_assignment = true:suggestion
dotnet_style_prefer_conditional_expression_over_return = true:suggestion
dotnet_style_coalesce_expression = true:suggestion
dotnet_style_null_propagation = true:suggestion
###############################
# Naming Conventions #
###############################
# Style Definitions
dotnet_naming_style.pascal_case_style.capitalization = pascal_case
# Use PascalCase for constant fields
dotnet_naming_rule.constant_fields_should_be_pascal_case.severity = suggestion
dotnet_naming_rule.constant_fields_should_be_pascal_case.symbols = constant_fields
dotnet_naming_rule.constant_fields_should_be_pascal_case.style = pascal_case_style
dotnet_naming_symbols.constant_fields.applicable_kinds = field
dotnet_naming_symbols.constant_fields.applicable_accessibilities = *
dotnet_naming_symbols.constant_fields.required_modifiers = const
###############################
# C# Code Style Rules #
###############################
# var preferences
csharp_style_var_for_built_in_types = true:none
csharp_style_var_when_type_is_apparent = true:none
csharp_style_var_elsewhere = true:none
# Expression-bodied members
csharp_style_expression_bodied_methods = true:suggestion
csharp_style_expression_bodied_constructors = true:suggestion
csharp_style_expression_bodied_operators = true:suggestion
csharp_style_expression_bodied_properties = true:suggestion
csharp_style_expression_bodied_indexers = true:suggestion
csharp_style_expression_bodied_accessors = true:suggestion
# Pattern-matching preferences
csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion
csharp_style_pattern_matching_over_as_with_null_check = true:suggestion
# Null-checking preferences
csharp_style_throw_expression = true:suggestion
csharp_style_conditional_delegate_call = true:suggestion
# Modifier preferences
csharp_preferred_modifier_order = public,private,protected,internal,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,volatile,async:suggestion
# Expression-level preferences
csharp_prefer_braces = true:none
csharp_prefer_simple_default_expression = true:suggestion
csharp_style_deconstructed_variable_declaration = true:suggestion
csharp_style_pattern_local_over_anonymous_function = true:suggestion
csharp_style_inlined_variable_declaration = true:suggestion
###############################
# C# Formatting Rules #
###############################
# New line preferences
csharp_new_line_before_open_brace = all
csharp_new_line_before_else = true
csharp_new_line_before_catch = true
csharp_new_line_before_finally = true
csharp_new_line_before_members_in_object_initializers = false
csharp_new_line_before_members_in_anonymous_types = false
csharp_new_line_between_query_expression_clauses = true
# Indentation preferences
csharp_indent_case_contents = true
csharp_indent_switch_labels = true
csharp_indent_labels = flush_left
# Space preferences
csharp_space_after_cast = false
csharp_space_after_keywords_in_control_flow_statements = true
csharp_space_between_method_declaration_parameter_list_parentheses = false
csharp_space_between_method_call_parameter_list_parentheses = false
csharp_space_before_colon_in_inheritance_clause = true
csharp_space_after_colon_in_inheritance_clause = true
csharp_space_around_binary_operators = before_and_after
csharp_space_between_method_declaration_empty_parameter_list_parentheses = false
csharp_space_between_method_call_name_and_opening_parenthesis = false
csharp_space_between_method_call_empty_parameter_list_parentheses = false
# Wrapping preferences
csharp_preserve_single_line_blocks = true
csharp_preserve_single_line_statements = true

View File

@ -0,0 +1,26 @@
{
"blurb": "Implement a binary search algorithm.",
"authors": [
"ErikSchierboom"
],
"contributors": [
"abecerramatias",
"j2jensen",
"robkeim",
"vgrigoriu",
"wolf99"
],
"files": {
"solution": [
"BinarySearch.cs"
],
"test": [
"BinarySearchTests.cs"
],
"example": [
".meta/Example.cs"
]
},
"source": "Wikipedia",
"source_url": "http://en.wikipedia.org/wiki/Binary_search_algorithm"
}

View File

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

View File

@ -0,0 +1,10 @@
using System;
public static class BinarySearch
{
public static int Find(int[] input, int value)
{
var i = Array.BinarySearch(input, value);
return i < 0 ? -1 : i;
}
}

View File

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

View File

@ -0,0 +1,93 @@
using System;
using Xunit;
public class BinarySearchTests
{
[Fact]
public void Finds_a_value_in_an_array_with_one_element()
{
var array = new[] { 6 };
var value = 6;
Assert.Equal(0, BinarySearch.Find(array, value));
}
[Fact]
public void Finds_a_value_in_the_middle_of_an_array()
{
var array = new[] { 1, 3, 4, 6, 8, 9, 11 };
var value = 6;
Assert.Equal(3, BinarySearch.Find(array, value));
}
[Fact]
public void Finds_a_value_at_the_beginning_of_an_array()
{
var array = new[] { 1, 3, 4, 6, 8, 9, 11 };
var value = 1;
Assert.Equal(0, BinarySearch.Find(array, value));
}
[Fact]
public void Finds_a_value_at_the_end_of_an_array()
{
var array = new[] { 1, 3, 4, 6, 8, 9, 11 };
var value = 11;
Assert.Equal(6, BinarySearch.Find(array, value));
}
[Fact]
public void Finds_a_value_in_an_array_of_odd_length()
{
var array = new[] { 1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 634 };
var value = 144;
Assert.Equal(9, BinarySearch.Find(array, value));
}
[Fact]
public void Finds_a_value_in_an_array_of_even_length()
{
var array = new[] { 1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377 };
var value = 21;
Assert.Equal(5, BinarySearch.Find(array, value));
}
[Fact]
public void Identifies_that_a_value_is_not_included_in_the_array()
{
var array = new[] { 1, 3, 4, 6, 8, 9, 11 };
var value = 7;
Assert.Equal(-1, BinarySearch.Find(array, value));
}
[Fact]
public void A_value_smaller_than_the_arrays_smallest_value_is_not_found()
{
var array = new[] { 1, 3, 4, 6, 8, 9, 11 };
var value = 0;
Assert.Equal(-1, BinarySearch.Find(array, value));
}
[Fact]
public void A_value_larger_than_the_arrays_largest_value_is_not_found()
{
var array = new[] { 1, 3, 4, 6, 8, 9, 11 };
var value = 13;
Assert.Equal(-1, BinarySearch.Find(array, value));
}
[Fact]
public void Nothing_is_found_in_an_empty_array()
{
var array = Array.Empty<int>();
var value = 1;
Assert.Equal(-1, BinarySearch.Find(array, value));
}
[Fact]
public void Nothing_is_found_when_the_left_and_right_bounds_cross()
{
var array = new[] { 1, 2 };
var value = 0;
Assert.Equal(-1, BinarySearch.Find(array, value));
}
}

View File

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

View File

@ -0,0 +1,58 @@
# Binary Search
Welcome to Binary Search on Exercism's C# Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
## Instructions
Implement a binary search algorithm.
Searching a sorted collection is a common task. A dictionary is a sorted
list of word definitions. Given a word, one can find its definition. A
telephone book is a sorted list of people's names, addresses, and
telephone numbers. Knowing someone's name allows one to quickly find
their telephone number and address.
If the list to be searched contains more than a few items (a dozen, say)
a binary search will require far fewer comparisons than a linear search,
but it imposes the requirement that the list be sorted.
In computer science, a binary search or half-interval search algorithm
finds the position of a specified input value (the search "key") within
an array sorted by key value.
In each step, the algorithm compares the search key value with the key
value of the middle element of the array.
If the keys match, then a matching element has been found and its index,
or position, is returned.
Otherwise, if the search key is less than the middle element's key, then
the algorithm repeats its action on the sub-array to the left of the
middle element or, if the search key is greater, on the sub-array to the
right.
If the remaining array to be searched is empty, then the key cannot be
found in the array and a special "not found" indication is returned.
A binary search halves the number of items to check with each iteration,
so locating an item (or determining its absence) takes logarithmic time.
A binary search is a dichotomic divide and conquer search algorithm.
## Source
### Created by
- @ErikSchierboom
### Contributed to by
- @abecerramatias
- @j2jensen
- @robkeim
- @vgrigoriu
- @wolf99
### Based on
Wikipedia - http://en.wikipedia.org/wiki/Binary_search_algorithm

141
csharp/etl/.editorconfig Normal file
View File

@ -0,0 +1,141 @@
###############################
# Core EditorConfig Options #
###############################
; This file is for unifying the coding style for different editors and IDEs.
; More information at:
; https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference?view=vs-2017
; https://docs.microsoft.com/en-us/visualstudio/ide/create-portable-custom-editor-options?view=vs-2017
root = true
[*]
indent_style = space
[Etl.cs]
indent_size = 4
###############################
# .NET Coding Conventions #
###############################
# Organize usings
dotnet_sort_system_directives_first = true
dotnet_separate_import_directive_groups = true
# this. preferences
dotnet_style_qualification_for_field = false:suggestion
dotnet_style_qualification_for_property = false:suggestion
dotnet_style_qualification_for_method = false:suggestion
dotnet_style_qualification_for_event = false:suggestion
# Language keywords vs BCL types preferences
dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion
dotnet_style_predefined_type_for_member_access = true:suggestion
# Parentheses preferences
dotnet_style_parentheses_in_arithmetic_binary_operators = never_if_unnecessary:none
dotnet_style_parentheses_in_relational_binary_operators = never_if_unnecessary:none
dotnet_style_parentheses_in_other_binary_operators = never_if_unnecessary:none
dotnet_style_parentheses_in_other_operators = never_if_unnecessary:suggestion
# Modifier preferences
dotnet_style_require_accessibility_modifiers = always:suggestion
dotnet_style_readonly_field = true:suggestion
# Expression-level preferences
dotnet_style_object_initializer = true:suggestion
dotnet_style_collection_initializer = true:suggestion
dotnet_style_explicit_tuple_names = true:suggestion
dotnet_style_prefer_inferred_tuple_names = true:suggestion
dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion
dotnet_style_prefer_auto_properties = true:suggestion
dotnet_style_prefer_is_null_check_over_reference_equality_method = true:suggestion
dotnet_style_prefer_conditional_expression_over_assignment = true:suggestion
dotnet_style_prefer_conditional_expression_over_return = true:suggestion
dotnet_style_coalesce_expression = true:suggestion
dotnet_style_null_propagation = true:suggestion
###############################
# Naming Conventions #
###############################
# Style Definitions
dotnet_naming_style.pascal_case_style.capitalization = pascal_case
# Use PascalCase for constant fields
dotnet_naming_rule.constant_fields_should_be_pascal_case.severity = suggestion
dotnet_naming_rule.constant_fields_should_be_pascal_case.symbols = constant_fields
dotnet_naming_rule.constant_fields_should_be_pascal_case.style = pascal_case_style
dotnet_naming_symbols.constant_fields.applicable_kinds = field
dotnet_naming_symbols.constant_fields.applicable_accessibilities = *
dotnet_naming_symbols.constant_fields.required_modifiers = const
###############################
# C# Code Style Rules #
###############################
# var preferences
csharp_style_var_for_built_in_types = true:none
csharp_style_var_when_type_is_apparent = true:none
csharp_style_var_elsewhere = true:none
# Expression-bodied members
csharp_style_expression_bodied_methods = true:suggestion
csharp_style_expression_bodied_constructors = true:suggestion
csharp_style_expression_bodied_operators = true:suggestion
csharp_style_expression_bodied_properties = true:suggestion
csharp_style_expression_bodied_indexers = true:suggestion
csharp_style_expression_bodied_accessors = true:suggestion
# Pattern-matching preferences
csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion
csharp_style_pattern_matching_over_as_with_null_check = true:suggestion
# Null-checking preferences
csharp_style_throw_expression = true:suggestion
csharp_style_conditional_delegate_call = true:suggestion
# Modifier preferences
csharp_preferred_modifier_order = public,private,protected,internal,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,volatile,async:suggestion
# Expression-level preferences
csharp_prefer_braces = true:none
csharp_prefer_simple_default_expression = true:suggestion
csharp_style_deconstructed_variable_declaration = true:suggestion
csharp_style_pattern_local_over_anonymous_function = true:suggestion
csharp_style_inlined_variable_declaration = true:suggestion
###############################
# C# Formatting Rules #
###############################
# New line preferences
csharp_new_line_before_open_brace = all
csharp_new_line_before_else = true
csharp_new_line_before_catch = true
csharp_new_line_before_finally = true
csharp_new_line_before_members_in_object_initializers = false
csharp_new_line_before_members_in_anonymous_types = false
csharp_new_line_between_query_expression_clauses = true
# Indentation preferences
csharp_indent_case_contents = true
csharp_indent_switch_labels = true
csharp_indent_labels = flush_left
# Space preferences
csharp_space_after_cast = false
csharp_space_after_keywords_in_control_flow_statements = true
csharp_space_between_method_declaration_parameter_list_parentheses = false
csharp_space_between_method_call_parameter_list_parentheses = false
csharp_space_before_colon_in_inheritance_clause = true
csharp_space_after_colon_in_inheritance_clause = true
csharp_space_around_binary_operators = before_and_after
csharp_space_between_method_declaration_empty_parameter_list_parentheses = false
csharp_space_between_method_call_name_and_opening_parenthesis = false
csharp_space_between_method_call_empty_parameter_list_parentheses = false
# Wrapping preferences
csharp_preserve_single_line_blocks = true
csharp_preserve_single_line_statements = true

View File

@ -0,0 +1,28 @@
{
"blurb": "We are going to do the `Transform` step of an Extract-Transform-Load.",
"authors": [
"bressain"
],
"contributors": [
"ErikSchierboom",
"InKahootz",
"j2jensen",
"jwood803",
"kytrinyx",
"robkeim",
"wolf99"
],
"files": {
"solution": [
"Etl.cs"
],
"test": [
"EtlTests.cs"
],
"example": [
".meta/Example.cs"
]
},
"source": "The Jumpstart Lab team",
"source_url": "http://jumpstartlab.com"
}

View File

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

15
csharp/etl/Etl.cs Normal file
View File

@ -0,0 +1,15 @@
using System.Collections.Generic;
using System.Linq;
public static class Etl
{
public static Dictionary<string, int> Transform(Dictionary<int, string[]> old)
{
Dictionary<string, int> result = new();
foreach (var d in old)
foreach (var l in d.Value.Select(v => v.ToLower()))
result[l] = d.Key;
return result;
}
}

14
csharp/etl/Etl.csproj Normal file
View File

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

100
csharp/etl/EtlTests.cs Normal file
View File

@ -0,0 +1,100 @@
using System.Collections.Generic;
using Xunit;
public class EtlTests
{
[Fact]
public void Single_letter()
{
var input = new Dictionary<int, string[]>
{
[1] = new[] { "A" }
};
var expected = new Dictionary<string, int>
{
["a"] = 1
};
Assert.Equal(expected, Etl.Transform(input));
}
[Fact]
public void Single_score_with_multiple_letters()
{
var input = new Dictionary<int, string[]>
{
[1] = new[] { "A", "E", "I", "O", "U" }
};
var expected = new Dictionary<string, int>
{
["a"] = 1,
["e"] = 1,
["i"] = 1,
["o"] = 1,
["u"] = 1
};
Assert.Equal(expected, Etl.Transform(input));
}
[Fact]
public void Multiple_scores_with_multiple_letters()
{
var input = new Dictionary<int, string[]>
{
[1] = new[] { "A", "E" },
[2] = new[] { "D", "G" }
};
var expected = new Dictionary<string, int>
{
["a"] = 1,
["d"] = 2,
["e"] = 1,
["g"] = 2
};
Assert.Equal(expected, Etl.Transform(input));
}
[Fact]
public void Multiple_scores_with_differing_numbers_of_letters()
{
var input = new Dictionary<int, string[]>
{
[1] = new[] { "A", "E", "I", "O", "U", "L", "N", "R", "S", "T" },
[2] = new[] { "D", "G" },
[3] = new[] { "B", "C", "M", "P" },
[4] = new[] { "F", "H", "V", "W", "Y" },
[5] = new[] { "K" },
[8] = new[] { "J", "X" },
[10] = new[] { "Q", "Z" }
};
var expected = new Dictionary<string, int>
{
["a"] = 1,
["b"] = 3,
["c"] = 3,
["d"] = 2,
["e"] = 1,
["f"] = 4,
["g"] = 2,
["h"] = 4,
["i"] = 1,
["j"] = 8,
["k"] = 5,
["l"] = 1,
["m"] = 3,
["n"] = 1,
["o"] = 1,
["p"] = 3,
["q"] = 10,
["r"] = 1,
["s"] = 1,
["t"] = 1,
["u"] = 1,
["v"] = 4,
["w"] = 4,
["x"] = 8,
["y"] = 4,
["z"] = 10
};
Assert.Equal(expected, Etl.Transform(input));
}
}

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

@ -0,0 +1,39 @@
# Help
## Running the tests
You can run the tests by opening a command prompt in the exercise's directory, and then running the [`dotnet test` command](https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-test)
Alternatively, most IDE's have built-in support for running tests, including [Visual Studio](https://docs.microsoft.com/en-us/visualstudio/test/run-unit-tests-with-test-explorer), [Rider](https://www.jetbrains.com/help/rider/Unit_Testing_in_Solution.html) and [Visual Studio code](https://github.com/OmniSharp/omnisharp-vscode/wiki/How-to-run-and-debug-unit-tests).
See the [tests page](https://exercism.io/tracks/csharp/tests) for more information.
## Skipped tests
Initially, only the first test will be enabled.
This is to encourage you to solve the exercise one step at a time.
Once you get the first test passing, remove the `Skip` property from the next test and work on getting that test passing.
## Submitting your solution
You can submit your solution using the `exercism submit Etl.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.

72
csharp/etl/README.md Normal file
View File

@ -0,0 +1,72 @@
# Etl
Welcome to Etl on Exercism's C# Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
## Instructions
We are going to do the `Transform` step of an Extract-Transform-Load.
## ETL
Extract-Transform-Load (ETL) is a fancy way of saying, "We have some crufty, legacy data over in this system, and now we need it in this shiny new system over here, so
we're going to migrate this."
(Typically, this is followed by, "We're only going to need to run this
once." That's then typically followed by much forehead slapping and
moaning about how stupid we could possibly be.)
## The goal
We're going to extract some Scrabble scores from a legacy system.
The old system stored a list of letters per score:
- 1 point: "A", "E", "I", "O", "U", "L", "N", "R", "S", "T",
- 2 points: "D", "G",
- 3 points: "B", "C", "M", "P",
- 4 points: "F", "H", "V", "W", "Y",
- 5 points: "K",
- 8 points: "J", "X",
- 10 points: "Q", "Z",
The shiny new Scrabble system instead stores the score per letter, which
makes it much faster and easier to calculate the score for a word. It
also stores the letters in lower-case regardless of the case of the
input letters:
- "a" is worth 1 point.
- "b" is worth 3 points.
- "c" is worth 3 points.
- "d" is worth 2 points.
- Etc.
Your mission, should you choose to accept it, is to transform the legacy data
format to the shiny new format.
## Notes
A final note about scoring, Scrabble is played around the world in a
variety of languages, each with its own unique scoring table. For
example, an "E" is scored at 2 in the Māori-language version of the
game while being scored at 4 in the Hawaiian-language version.
## Source
### Created by
- @bressain
### Contributed to by
- @ErikSchierboom
- @InKahootz
- @j2jensen
- @jwood803
- @kytrinyx
- @robkeim
- @wolf99
### Based on
The Jumpstart Lab team - http://jumpstartlab.com

141
csharp/grains/.editorconfig Normal file
View File

@ -0,0 +1,141 @@
###############################
# Core EditorConfig Options #
###############################
; This file is for unifying the coding style for different editors and IDEs.
; More information at:
; https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference?view=vs-2017
; https://docs.microsoft.com/en-us/visualstudio/ide/create-portable-custom-editor-options?view=vs-2017
root = true
[*]
indent_style = space
[Grains.cs]
indent_size = 4
###############################
# .NET Coding Conventions #
###############################
# Organize usings
dotnet_sort_system_directives_first = true
dotnet_separate_import_directive_groups = true
# this. preferences
dotnet_style_qualification_for_field = false:suggestion
dotnet_style_qualification_for_property = false:suggestion
dotnet_style_qualification_for_method = false:suggestion
dotnet_style_qualification_for_event = false:suggestion
# Language keywords vs BCL types preferences
dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion
dotnet_style_predefined_type_for_member_access = true:suggestion
# Parentheses preferences
dotnet_style_parentheses_in_arithmetic_binary_operators = never_if_unnecessary:none
dotnet_style_parentheses_in_relational_binary_operators = never_if_unnecessary:none
dotnet_style_parentheses_in_other_binary_operators = never_if_unnecessary:none
dotnet_style_parentheses_in_other_operators = never_if_unnecessary:suggestion
# Modifier preferences
dotnet_style_require_accessibility_modifiers = always:suggestion
dotnet_style_readonly_field = true:suggestion
# Expression-level preferences
dotnet_style_object_initializer = true:suggestion
dotnet_style_collection_initializer = true:suggestion
dotnet_style_explicit_tuple_names = true:suggestion
dotnet_style_prefer_inferred_tuple_names = true:suggestion
dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion
dotnet_style_prefer_auto_properties = true:suggestion
dotnet_style_prefer_is_null_check_over_reference_equality_method = true:suggestion
dotnet_style_prefer_conditional_expression_over_assignment = true:suggestion
dotnet_style_prefer_conditional_expression_over_return = true:suggestion
dotnet_style_coalesce_expression = true:suggestion
dotnet_style_null_propagation = true:suggestion
###############################
# Naming Conventions #
###############################
# Style Definitions
dotnet_naming_style.pascal_case_style.capitalization = pascal_case
# Use PascalCase for constant fields
dotnet_naming_rule.constant_fields_should_be_pascal_case.severity = suggestion
dotnet_naming_rule.constant_fields_should_be_pascal_case.symbols = constant_fields
dotnet_naming_rule.constant_fields_should_be_pascal_case.style = pascal_case_style
dotnet_naming_symbols.constant_fields.applicable_kinds = field
dotnet_naming_symbols.constant_fields.applicable_accessibilities = *
dotnet_naming_symbols.constant_fields.required_modifiers = const
###############################
# C# Code Style Rules #
###############################
# var preferences
csharp_style_var_for_built_in_types = true:none
csharp_style_var_when_type_is_apparent = true:none
csharp_style_var_elsewhere = true:none
# Expression-bodied members
csharp_style_expression_bodied_methods = true:suggestion
csharp_style_expression_bodied_constructors = true:suggestion
csharp_style_expression_bodied_operators = true:suggestion
csharp_style_expression_bodied_properties = true:suggestion
csharp_style_expression_bodied_indexers = true:suggestion
csharp_style_expression_bodied_accessors = true:suggestion
# Pattern-matching preferences
csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion
csharp_style_pattern_matching_over_as_with_null_check = true:suggestion
# Null-checking preferences
csharp_style_throw_expression = true:suggestion
csharp_style_conditional_delegate_call = true:suggestion
# Modifier preferences
csharp_preferred_modifier_order = public,private,protected,internal,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,volatile,async:suggestion
# Expression-level preferences
csharp_prefer_braces = true:none
csharp_prefer_simple_default_expression = true:suggestion
csharp_style_deconstructed_variable_declaration = true:suggestion
csharp_style_pattern_local_over_anonymous_function = true:suggestion
csharp_style_inlined_variable_declaration = true:suggestion
###############################
# C# Formatting Rules #
###############################
# New line preferences
csharp_new_line_before_open_brace = all
csharp_new_line_before_else = true
csharp_new_line_before_catch = true
csharp_new_line_before_finally = true
csharp_new_line_before_members_in_object_initializers = false
csharp_new_line_before_members_in_anonymous_types = false
csharp_new_line_between_query_expression_clauses = true
# Indentation preferences
csharp_indent_case_contents = true
csharp_indent_switch_labels = true
csharp_indent_labels = flush_left
# Space preferences
csharp_space_after_cast = false
csharp_space_after_keywords_in_control_flow_statements = true
csharp_space_between_method_declaration_parameter_list_parentheses = false
csharp_space_between_method_call_parameter_list_parentheses = false
csharp_space_before_colon_in_inheritance_clause = true
csharp_space_after_colon_in_inheritance_clause = true
csharp_space_around_binary_operators = before_and_after
csharp_space_between_method_declaration_empty_parameter_list_parentheses = false
csharp_space_between_method_call_name_and_opening_parenthesis = false
csharp_space_between_method_call_empty_parameter_list_parentheses = false
# Wrapping preferences
csharp_preserve_single_line_blocks = true
csharp_preserve_single_line_statements = true

View File

@ -0,0 +1,26 @@
{
"blurb": "Calculate the number of grains of wheat on a chessboard given that the number on each square doubles.",
"authors": [
"robkeim"
],
"contributors": [
"ErikSchierboom",
"FizzBuzz791",
"j2jensen",
"tushartyagi",
"wolf99"
],
"files": {
"solution": [
"Grains.cs"
],
"test": [
"GrainsTests.cs"
],
"example": [
".meta/Example.cs"
]
},
"source": "JavaRanch Cattle Drive, exercise 6",
"source_url": "http://www.javaranch.com/grains.jsp"
}

View File

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

19
csharp/grains/Grains.cs Normal file
View File

@ -0,0 +1,19 @@
using System;
public static class Grains
{
public static ulong Square(int n) =>
n < 1 || n > 64
? throw new ArgumentOutOfRangeException()
: (ulong)Math.Pow(2, n - 1);
public static ulong Total()
{
var total = 0ul;
for (int i = 1; i <= 64; i++)
{
total += Square(i);
}
return total;
}
}

View File

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

View File

@ -0,0 +1,71 @@
using System;
using Xunit;
public class GrainsTests
{
[Fact]
public void Grains_on_square_1()
{
Assert.Equal(1UL, Grains.Square(1));
}
[Fact]
public void Grains_on_square_2()
{
Assert.Equal(2UL, Grains.Square(2));
}
[Fact]
public void Grains_on_square_3()
{
Assert.Equal(4UL, Grains.Square(3));
}
[Fact]
public void Grains_on_square_4()
{
Assert.Equal(8UL, Grains.Square(4));
}
[Fact]
public void Grains_on_square_16()
{
Assert.Equal(32768UL, Grains.Square(16));
}
[Fact]
public void Grains_on_square_32()
{
Assert.Equal(2147483648UL, Grains.Square(32));
}
[Fact]
public void Grains_on_square_64()
{
Assert.Equal(9223372036854775808UL, Grains.Square(64));
}
[Fact]
public void Square_0_raises_an_exception()
{
Assert.Throws<ArgumentOutOfRangeException>(() => Grains.Square(0));
}
[Fact]
public void Negative_square_raises_an_exception()
{
Assert.Throws<ArgumentOutOfRangeException>(() => Grains.Square(-1));
}
[Fact]
public void Square_greater_than_64_raises_an_exception()
{
Assert.Throws<ArgumentOutOfRangeException>(() => Grains.Square(65));
}
[Fact]
public void Returns_the_total_number_of_grains_on_the_board()
{
Assert.Equal(18446744073709551615UL, Grains.Total());
}
}

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

@ -0,0 +1,39 @@
# Help
## Running the tests
You can run the tests by opening a command prompt in the exercise's directory, and then running the [`dotnet test` command](https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-test)
Alternatively, most IDE's have built-in support for running tests, including [Visual Studio](https://docs.microsoft.com/en-us/visualstudio/test/run-unit-tests-with-test-explorer), [Rider](https://www.jetbrains.com/help/rider/Unit_Testing_in_Solution.html) and [Visual Studio code](https://github.com/OmniSharp/omnisharp-vscode/wiki/How-to-run-and-debug-unit-tests).
See the [tests page](https://exercism.io/tracks/csharp/tests) for more information.
## Skipped tests
Initially, only the first test will be enabled.
This is to encourage you to solve the exercise one step at a time.
Once you get the first test passing, remove the `Skip` property from the next test and work on getting that test passing.
## Submitting your solution
You can submit your solution using the `exercism submit Grains.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.

50
csharp/grains/README.md Normal file
View File

@ -0,0 +1,50 @@
# Grains
Welcome to Grains on Exercism's C# Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
## Instructions
Calculate the number of grains of wheat on a chessboard given that the number
on each square doubles.
There once was a wise servant who saved the life of a prince. The king
promised to pay whatever the servant could dream up. Knowing that the
king loved chess, the servant told the king he would like to have grains
of wheat. One grain on the first square of a chess board, with the number
of grains doubling on each successive square.
There are 64 squares on a chessboard (where square 1 has one grain, square 2 has two grains, and so on).
Write code that shows:
- how many grains were on a given square, and
- the total number of grains on the chessboard
## For bonus points
Did you get the tests passing and the code clean? If you want to, these
are some additional things you could try:
- Optimize for speed.
- Optimize for readability.
Then please share your thoughts in a comment on the submission. Did this
experiment make the code better? Worse? Did you learn anything from it?
## Source
### Created by
- @robkeim
### Contributed to by
- @ErikSchierboom
- @FizzBuzz791
- @j2jensen
- @tushartyagi
- @wolf99
### Based on
JavaRanch Cattle Drive, exercise 6 - http://www.javaranch.com/grains.jsp

View File

@ -0,0 +1,141 @@
###############################
# Core EditorConfig Options #
###############################
; This file is for unifying the coding style for different editors and IDEs.
; More information at:
; https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference?view=vs-2017
; https://docs.microsoft.com/en-us/visualstudio/ide/create-portable-custom-editor-options?view=vs-2017
root = true
[*]
indent_style = space
[PhoneNumber.cs]
indent_size = 4
###############################
# .NET Coding Conventions #
###############################
# Organize usings
dotnet_sort_system_directives_first = true
dotnet_separate_import_directive_groups = true
# this. preferences
dotnet_style_qualification_for_field = false:suggestion
dotnet_style_qualification_for_property = false:suggestion
dotnet_style_qualification_for_method = false:suggestion
dotnet_style_qualification_for_event = false:suggestion
# Language keywords vs BCL types preferences
dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion
dotnet_style_predefined_type_for_member_access = true:suggestion
# Parentheses preferences
dotnet_style_parentheses_in_arithmetic_binary_operators = never_if_unnecessary:none
dotnet_style_parentheses_in_relational_binary_operators = never_if_unnecessary:none
dotnet_style_parentheses_in_other_binary_operators = never_if_unnecessary:none
dotnet_style_parentheses_in_other_operators = never_if_unnecessary:suggestion
# Modifier preferences
dotnet_style_require_accessibility_modifiers = always:suggestion
dotnet_style_readonly_field = true:suggestion
# Expression-level preferences
dotnet_style_object_initializer = true:suggestion
dotnet_style_collection_initializer = true:suggestion
dotnet_style_explicit_tuple_names = true:suggestion
dotnet_style_prefer_inferred_tuple_names = true:suggestion
dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion
dotnet_style_prefer_auto_properties = true:suggestion
dotnet_style_prefer_is_null_check_over_reference_equality_method = true:suggestion
dotnet_style_prefer_conditional_expression_over_assignment = true:suggestion
dotnet_style_prefer_conditional_expression_over_return = true:suggestion
dotnet_style_coalesce_expression = true:suggestion
dotnet_style_null_propagation = true:suggestion
###############################
# Naming Conventions #
###############################
# Style Definitions
dotnet_naming_style.pascal_case_style.capitalization = pascal_case
# Use PascalCase for constant fields
dotnet_naming_rule.constant_fields_should_be_pascal_case.severity = suggestion
dotnet_naming_rule.constant_fields_should_be_pascal_case.symbols = constant_fields
dotnet_naming_rule.constant_fields_should_be_pascal_case.style = pascal_case_style
dotnet_naming_symbols.constant_fields.applicable_kinds = field
dotnet_naming_symbols.constant_fields.applicable_accessibilities = *
dotnet_naming_symbols.constant_fields.required_modifiers = const
###############################
# C# Code Style Rules #
###############################
# var preferences
csharp_style_var_for_built_in_types = true:none
csharp_style_var_when_type_is_apparent = true:none
csharp_style_var_elsewhere = true:none
# Expression-bodied members
csharp_style_expression_bodied_methods = true:suggestion
csharp_style_expression_bodied_constructors = true:suggestion
csharp_style_expression_bodied_operators = true:suggestion
csharp_style_expression_bodied_properties = true:suggestion
csharp_style_expression_bodied_indexers = true:suggestion
csharp_style_expression_bodied_accessors = true:suggestion
# Pattern-matching preferences
csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion
csharp_style_pattern_matching_over_as_with_null_check = true:suggestion
# Null-checking preferences
csharp_style_throw_expression = true:suggestion
csharp_style_conditional_delegate_call = true:suggestion
# Modifier preferences
csharp_preferred_modifier_order = public,private,protected,internal,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,volatile,async:suggestion
# Expression-level preferences
csharp_prefer_braces = true:none
csharp_prefer_simple_default_expression = true:suggestion
csharp_style_deconstructed_variable_declaration = true:suggestion
csharp_style_pattern_local_over_anonymous_function = true:suggestion
csharp_style_inlined_variable_declaration = true:suggestion
###############################
# C# Formatting Rules #
###############################
# New line preferences
csharp_new_line_before_open_brace = all
csharp_new_line_before_else = true
csharp_new_line_before_catch = true
csharp_new_line_before_finally = true
csharp_new_line_before_members_in_object_initializers = false
csharp_new_line_before_members_in_anonymous_types = false
csharp_new_line_between_query_expression_clauses = true
# Indentation preferences
csharp_indent_case_contents = true
csharp_indent_switch_labels = true
csharp_indent_labels = flush_left
# Space preferences
csharp_space_after_cast = false
csharp_space_after_keywords_in_control_flow_statements = true
csharp_space_between_method_declaration_parameter_list_parentheses = false
csharp_space_between_method_call_parameter_list_parentheses = false
csharp_space_before_colon_in_inheritance_clause = true
csharp_space_after_colon_in_inheritance_clause = true
csharp_space_around_binary_operators = before_and_after
csharp_space_between_method_declaration_empty_parameter_list_parentheses = false
csharp_space_between_method_call_name_and_opening_parenthesis = false
csharp_space_between_method_call_empty_parameter_list_parentheses = false
# Wrapping preferences
csharp_preserve_single_line_blocks = true
csharp_preserve_single_line_statements = true

View File

@ -0,0 +1,29 @@
{
"blurb": "Clean up user-entered phone numbers so that they can be sent SMS messages.",
"authors": [
"bressain"
],
"contributors": [
"ErikSchierboom",
"j2jensen",
"jwood803",
"kytrinyx",
"robkeim",
"tomschluter",
"vgrigoriu",
"wolf99"
],
"files": {
"solution": [
"PhoneNumber.cs"
],
"test": [
"PhoneNumberTests.cs"
],
"example": [
".meta/Example.cs"
]
},
"source": "Event Manager by JumpstartLab",
"source_url": "http://tutorials.jumpstartlab.com/projects/eventmanager.html"
}

View File

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

View File

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

View File

@ -0,0 +1,13 @@
using System;
using System.Linq;
public class PhoneNumber
{
public static string Clean(string phoneNumber)
{
var clean = phoneNumber.TrimStart('+').TrimStart('1').Replace("(", "").Replace(")", "").Replace("-", "").Replace(".", "").Replace(" ", "");
return clean.StartsWith('1') || clean.StartsWith('0') || clean.Any(char.IsLetter) || clean[3] == '0' || clean[3] == '1' || clean.Length < 10 || clean.Length > 11 || clean.Length == 11 && !clean.StartsWith('1')
? throw new ArgumentException(phoneNumber)
: clean;
}
}

View File

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

View File

@ -0,0 +1,131 @@
using System;
using Xunit;
public class PhoneNumberTests
{
[Fact]
public void Cleans_the_number()
{
var phrase = "(223) 456-7890";
Assert.Equal("2234567890", PhoneNumber.Clean(phrase));
}
[Fact]
public void Cleans_numbers_with_dots()
{
var phrase = "223.456.7890";
Assert.Equal("2234567890", PhoneNumber.Clean(phrase));
}
[Fact]
public void Cleans_numbers_with_multiple_spaces()
{
var phrase = "223 456 7890 ";
Assert.Equal("2234567890", PhoneNumber.Clean(phrase));
}
[Fact]
public void Invalid_when_9_digits()
{
var phrase = "123456789";
Assert.Throws<ArgumentException>(() => PhoneNumber.Clean(phrase));
}
[Fact]
public void Invalid_when_11_digits_does_not_start_with_a_1()
{
var phrase = "22234567890";
Assert.Throws<ArgumentException>(() => PhoneNumber.Clean(phrase));
}
[Fact]
public void Valid_when_11_digits_and_starting_with_1()
{
var phrase = "12234567890";
Assert.Equal("2234567890", PhoneNumber.Clean(phrase));
}
[Fact]
public void Valid_when_11_digits_and_starting_with_1_even_with_punctuation()
{
var phrase = "+1 (223) 456-7890";
Assert.Equal("2234567890", PhoneNumber.Clean(phrase));
}
[Fact]
public void Invalid_when_more_than_11_digits()
{
var phrase = "321234567890";
Assert.Throws<ArgumentException>(() => PhoneNumber.Clean(phrase));
}
[Fact]
public void Invalid_with_letters()
{
var phrase = "123-abc-7890";
Assert.Throws<ArgumentException>(() => PhoneNumber.Clean(phrase));
}
[Fact]
public void Invalid_with_punctuations()
{
var phrase = "123-@:!-7890";
Assert.Throws<ArgumentException>(() => PhoneNumber.Clean(phrase));
}
[Fact]
public void Invalid_if_area_code_starts_with_0()
{
var phrase = "(023) 456-7890";
Assert.Throws<ArgumentException>(() => PhoneNumber.Clean(phrase));
}
[Fact]
public void Invalid_if_area_code_starts_with_1()
{
var phrase = "(123) 456-7890";
Assert.Throws<ArgumentException>(() => PhoneNumber.Clean(phrase));
}
[Fact]
public void Invalid_if_exchange_code_starts_with_0()
{
var phrase = "(223) 056-7890";
Assert.Throws<ArgumentException>(() => PhoneNumber.Clean(phrase));
}
[Fact]
public void Invalid_if_exchange_code_starts_with_1()
{
var phrase = "(223) 156-7890";
Assert.Throws<ArgumentException>(() => PhoneNumber.Clean(phrase));
}
[Fact]
public void Invalid_if_area_code_starts_with_0_on_valid_11_digit_number()
{
var phrase = "1 (023) 456-7890";
Assert.Throws<ArgumentException>(() => PhoneNumber.Clean(phrase));
}
[Fact]
public void Invalid_if_area_code_starts_with_1_on_valid_11_digit_number()
{
var phrase = "1 (123) 456-7890";
Assert.Throws<ArgumentException>(() => PhoneNumber.Clean(phrase));
}
[Fact]
public void Invalid_if_exchange_code_starts_with_0_on_valid_11_digit_number()
{
var phrase = "1 (223) 056-7890";
Assert.Throws<ArgumentException>(() => PhoneNumber.Clean(phrase));
}
[Fact]
public void Invalid_if_exchange_code_starts_with_1_on_valid_11_digit_number()
{
var phrase = "1 (223) 156-7890";
Assert.Throws<ArgumentException>(() => PhoneNumber.Clean(phrase));
}
}

View File

@ -0,0 +1,55 @@
# Phone Number
Welcome to Phone Number on Exercism's C# Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
## Instructions
Clean up user-entered phone numbers so that they can be sent SMS messages.
The **North American Numbering Plan (NANP)** is a telephone numbering system used by many countries in North America like the United States, Canada or Bermuda. All NANP-countries share the same international country code: `1`.
NANP numbers are ten-digit numbers consisting of a three-digit Numbering Plan Area code, commonly known as *area code*, followed by a seven-digit local number. The first three digits of the local number represent the *exchange code*, followed by the unique four-digit number which is the *subscriber number*.
The format is usually represented as
```text
(NXX)-NXX-XXXX
```
where `N` is any digit from 2 through 9 and `X` is any digit from 0 through 9.
Your task is to clean up differently formatted telephone numbers by removing punctuation and the country code (1) if present.
For example, the inputs
- `+1 (613)-995-0253`
- `613-995-0253`
- `1 613 995 0253`
- `613.995.0253`
should all produce the output
`6139950253`
**Note:** As this exercise only deals with telephone numbers used in NANP-countries, only 1 is considered a valid country code.
## Source
### Created by
- @bressain
### Contributed to by
- @ErikSchierboom
- @j2jensen
- @jwood803
- @kytrinyx
- @robkeim
- @tomschluter
- @vgrigoriu
- @wolf99
### Based on
Event Manager by JumpstartLab - http://tutorials.jumpstartlab.com/projects/eventmanager.html

View File

@ -0,0 +1,141 @@
###############################
# Core EditorConfig Options #
###############################
; This file is for unifying the coding style for different editors and IDEs.
; More information at:
; https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference?view=vs-2017
; https://docs.microsoft.com/en-us/visualstudio/ide/create-portable-custom-editor-options?view=vs-2017
root = true
[*]
indent_style = space
[ScrabbleScore.cs]
indent_size = 4
###############################
# .NET Coding Conventions #
###############################
# Organize usings
dotnet_sort_system_directives_first = true
dotnet_separate_import_directive_groups = true
# this. preferences
dotnet_style_qualification_for_field = false:suggestion
dotnet_style_qualification_for_property = false:suggestion
dotnet_style_qualification_for_method = false:suggestion
dotnet_style_qualification_for_event = false:suggestion
# Language keywords vs BCL types preferences
dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion
dotnet_style_predefined_type_for_member_access = true:suggestion
# Parentheses preferences
dotnet_style_parentheses_in_arithmetic_binary_operators = never_if_unnecessary:none
dotnet_style_parentheses_in_relational_binary_operators = never_if_unnecessary:none
dotnet_style_parentheses_in_other_binary_operators = never_if_unnecessary:none
dotnet_style_parentheses_in_other_operators = never_if_unnecessary:suggestion
# Modifier preferences
dotnet_style_require_accessibility_modifiers = always:suggestion
dotnet_style_readonly_field = true:suggestion
# Expression-level preferences
dotnet_style_object_initializer = true:suggestion
dotnet_style_collection_initializer = true:suggestion
dotnet_style_explicit_tuple_names = true:suggestion
dotnet_style_prefer_inferred_tuple_names = true:suggestion
dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion
dotnet_style_prefer_auto_properties = true:suggestion
dotnet_style_prefer_is_null_check_over_reference_equality_method = true:suggestion
dotnet_style_prefer_conditional_expression_over_assignment = true:suggestion
dotnet_style_prefer_conditional_expression_over_return = true:suggestion
dotnet_style_coalesce_expression = true:suggestion
dotnet_style_null_propagation = true:suggestion
###############################
# Naming Conventions #
###############################
# Style Definitions
dotnet_naming_style.pascal_case_style.capitalization = pascal_case
# Use PascalCase for constant fields
dotnet_naming_rule.constant_fields_should_be_pascal_case.severity = suggestion
dotnet_naming_rule.constant_fields_should_be_pascal_case.symbols = constant_fields
dotnet_naming_rule.constant_fields_should_be_pascal_case.style = pascal_case_style
dotnet_naming_symbols.constant_fields.applicable_kinds = field
dotnet_naming_symbols.constant_fields.applicable_accessibilities = *
dotnet_naming_symbols.constant_fields.required_modifiers = const
###############################
# C# Code Style Rules #
###############################
# var preferences
csharp_style_var_for_built_in_types = true:none
csharp_style_var_when_type_is_apparent = true:none
csharp_style_var_elsewhere = true:none
# Expression-bodied members
csharp_style_expression_bodied_methods = true:suggestion
csharp_style_expression_bodied_constructors = true:suggestion
csharp_style_expression_bodied_operators = true:suggestion
csharp_style_expression_bodied_properties = true:suggestion
csharp_style_expression_bodied_indexers = true:suggestion
csharp_style_expression_bodied_accessors = true:suggestion
# Pattern-matching preferences
csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion
csharp_style_pattern_matching_over_as_with_null_check = true:suggestion
# Null-checking preferences
csharp_style_throw_expression = true:suggestion
csharp_style_conditional_delegate_call = true:suggestion
# Modifier preferences
csharp_preferred_modifier_order = public,private,protected,internal,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,volatile,async:suggestion
# Expression-level preferences
csharp_prefer_braces = true:none
csharp_prefer_simple_default_expression = true:suggestion
csharp_style_deconstructed_variable_declaration = true:suggestion
csharp_style_pattern_local_over_anonymous_function = true:suggestion
csharp_style_inlined_variable_declaration = true:suggestion
###############################
# C# Formatting Rules #
###############################
# New line preferences
csharp_new_line_before_open_brace = all
csharp_new_line_before_else = true
csharp_new_line_before_catch = true
csharp_new_line_before_finally = true
csharp_new_line_before_members_in_object_initializers = false
csharp_new_line_before_members_in_anonymous_types = false
csharp_new_line_between_query_expression_clauses = true
# Indentation preferences
csharp_indent_case_contents = true
csharp_indent_switch_labels = true
csharp_indent_labels = flush_left
# Space preferences
csharp_space_after_cast = false
csharp_space_after_keywords_in_control_flow_statements = true
csharp_space_between_method_declaration_parameter_list_parentheses = false
csharp_space_between_method_call_parameter_list_parentheses = false
csharp_space_before_colon_in_inheritance_clause = true
csharp_space_after_colon_in_inheritance_clause = true
csharp_space_around_binary_operators = before_and_after
csharp_space_between_method_declaration_empty_parameter_list_parentheses = false
csharp_space_between_method_call_name_and_opening_parenthesis = false
csharp_space_between_method_call_empty_parameter_list_parentheses = false
# Wrapping preferences
csharp_preserve_single_line_blocks = true
csharp_preserve_single_line_statements = true

View File

@ -0,0 +1,26 @@
{
"blurb": "Given a word, compute the Scrabble score for that word.",
"authors": [
"bressain"
],
"contributors": [
"ErikSchierboom",
"j2jensen",
"jwood803",
"robkeim",
"wolf99"
],
"files": {
"solution": [
"ScrabbleScore.cs"
],
"test": [
"ScrabbleScoreTests.cs"
],
"example": [
".meta/Example.cs"
]
},
"source": "Inspired by the Extreme Startup game",
"source_url": "https://github.com/rchatley/extreme_startup"
}

View File

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

View File

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

View File

@ -0,0 +1,63 @@
# Scrabble Score
Welcome to Scrabble Score on Exercism's C# Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
## Instructions
Given a word, compute the Scrabble score for that word.
## Letter Values
You'll need these:
```text
Letter Value
A, E, I, O, U, L, N, R, S, T 1
D, G 2
B, C, M, P 3
F, H, V, W, Y 4
K 5
J, X 8
Q, Z 10
```
## Examples
"cabbage" should be scored as worth 14 points:
- 3 points for C
- 1 point for A, twice
- 3 points for B, twice
- 2 points for G
- 1 point for E
And to total:
- `3 + 2*1 + 2*3 + 2 + 1`
- = `3 + 2 + 6 + 3`
- = `5 + 9`
- = 14
## Extensions
- You can play a double or a triple letter.
- You can play a double or a triple word.
## Source
### Created by
- @bressain
### Contributed to by
- @ErikSchierboom
- @j2jensen
- @jwood803
- @robkeim
- @wolf99
### Based on
Inspired by the Extreme Startup game - https://github.com/rchatley/extreme_startup

View File

@ -0,0 +1,21 @@
using System;
using System.Linq;
public static class ScrabbleScore
{
public static int Score(string input) =>
input.Select(Score).Sum();
private static int Score(char c) =>
char.ToLower(c) switch
{
'a' or 'e' or 'i' or 'o' or 'u' or 'l' or 'n' or 'r' or 's' or 't' => 1,
'd' or 'g' => 2,
'b' or 'c' or 'm' or 'p' => 3,
'f' or 'h' or 'v' or 'w' or 'y' => 4,
'k' => 5,
'j' or 'x' => 8,
'q' or 'z' => 10,
_ => throw new ArgumentOutOfRangeException(nameof(c)),
};
}

View File

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

View File

@ -0,0 +1,70 @@
using Xunit;
public class ScrabbleScoreTests
{
[Fact]
public void Lowercase_letter()
{
Assert.Equal(1, ScrabbleScore.Score("a"));
}
[Fact]
public void Uppercase_letter()
{
Assert.Equal(1, ScrabbleScore.Score("A"));
}
[Fact]
public void Valuable_letter()
{
Assert.Equal(4, ScrabbleScore.Score("f"));
}
[Fact]
public void Short_word()
{
Assert.Equal(2, ScrabbleScore.Score("at"));
}
[Fact]
public void Short_valuable_word()
{
Assert.Equal(12, ScrabbleScore.Score("zoo"));
}
[Fact]
public void Medium_word()
{
Assert.Equal(6, ScrabbleScore.Score("street"));
}
[Fact]
public void Medium_valuable_word()
{
Assert.Equal(22, ScrabbleScore.Score("quirky"));
}
[Fact]
public void Long_mixed_case_word()
{
Assert.Equal(41, ScrabbleScore.Score("OxyphenButazone"));
}
[Fact]
public void English_like_word()
{
Assert.Equal(8, ScrabbleScore.Score("pinata"));
}
[Fact]
public void Empty_input()
{
Assert.Equal(0, ScrabbleScore.Score(""));
}
[Fact]
public void Entire_alphabet_available()
{
Assert.Equal(87, ScrabbleScore.Score("abcdefghijklmnopqrstuvwxyz"));
}
}

141
csharp/strain/.editorconfig Normal file
View File

@ -0,0 +1,141 @@
###############################
# Core EditorConfig Options #
###############################
; This file is for unifying the coding style for different editors and IDEs.
; More information at:
; https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference?view=vs-2017
; https://docs.microsoft.com/en-us/visualstudio/ide/create-portable-custom-editor-options?view=vs-2017
root = true
[*]
indent_style = space
[Strain.cs]
indent_size = 4
###############################
# .NET Coding Conventions #
###############################
# Organize usings
dotnet_sort_system_directives_first = true
dotnet_separate_import_directive_groups = true
# this. preferences
dotnet_style_qualification_for_field = false:suggestion
dotnet_style_qualification_for_property = false:suggestion
dotnet_style_qualification_for_method = false:suggestion
dotnet_style_qualification_for_event = false:suggestion
# Language keywords vs BCL types preferences
dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion
dotnet_style_predefined_type_for_member_access = true:suggestion
# Parentheses preferences
dotnet_style_parentheses_in_arithmetic_binary_operators = never_if_unnecessary:none
dotnet_style_parentheses_in_relational_binary_operators = never_if_unnecessary:none
dotnet_style_parentheses_in_other_binary_operators = never_if_unnecessary:none
dotnet_style_parentheses_in_other_operators = never_if_unnecessary:suggestion
# Modifier preferences
dotnet_style_require_accessibility_modifiers = always:suggestion
dotnet_style_readonly_field = true:suggestion
# Expression-level preferences
dotnet_style_object_initializer = true:suggestion
dotnet_style_collection_initializer = true:suggestion
dotnet_style_explicit_tuple_names = true:suggestion
dotnet_style_prefer_inferred_tuple_names = true:suggestion
dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion
dotnet_style_prefer_auto_properties = true:suggestion
dotnet_style_prefer_is_null_check_over_reference_equality_method = true:suggestion
dotnet_style_prefer_conditional_expression_over_assignment = true:suggestion
dotnet_style_prefer_conditional_expression_over_return = true:suggestion
dotnet_style_coalesce_expression = true:suggestion
dotnet_style_null_propagation = true:suggestion
###############################
# Naming Conventions #
###############################
# Style Definitions
dotnet_naming_style.pascal_case_style.capitalization = pascal_case
# Use PascalCase for constant fields
dotnet_naming_rule.constant_fields_should_be_pascal_case.severity = suggestion
dotnet_naming_rule.constant_fields_should_be_pascal_case.symbols = constant_fields
dotnet_naming_rule.constant_fields_should_be_pascal_case.style = pascal_case_style
dotnet_naming_symbols.constant_fields.applicable_kinds = field
dotnet_naming_symbols.constant_fields.applicable_accessibilities = *
dotnet_naming_symbols.constant_fields.required_modifiers = const
###############################
# C# Code Style Rules #
###############################
# var preferences
csharp_style_var_for_built_in_types = true:none
csharp_style_var_when_type_is_apparent = true:none
csharp_style_var_elsewhere = true:none
# Expression-bodied members
csharp_style_expression_bodied_methods = true:suggestion
csharp_style_expression_bodied_constructors = true:suggestion
csharp_style_expression_bodied_operators = true:suggestion
csharp_style_expression_bodied_properties = true:suggestion
csharp_style_expression_bodied_indexers = true:suggestion
csharp_style_expression_bodied_accessors = true:suggestion
# Pattern-matching preferences
csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion
csharp_style_pattern_matching_over_as_with_null_check = true:suggestion
# Null-checking preferences
csharp_style_throw_expression = true:suggestion
csharp_style_conditional_delegate_call = true:suggestion
# Modifier preferences
csharp_preferred_modifier_order = public,private,protected,internal,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,volatile,async:suggestion
# Expression-level preferences
csharp_prefer_braces = true:none
csharp_prefer_simple_default_expression = true:suggestion
csharp_style_deconstructed_variable_declaration = true:suggestion
csharp_style_pattern_local_over_anonymous_function = true:suggestion
csharp_style_inlined_variable_declaration = true:suggestion
###############################
# C# Formatting Rules #
###############################
# New line preferences
csharp_new_line_before_open_brace = all
csharp_new_line_before_else = true
csharp_new_line_before_catch = true
csharp_new_line_before_finally = true
csharp_new_line_before_members_in_object_initializers = false
csharp_new_line_before_members_in_anonymous_types = false
csharp_new_line_between_query_expression_clauses = true
# Indentation preferences
csharp_indent_case_contents = true
csharp_indent_switch_labels = true
csharp_indent_labels = flush_left
# Space preferences
csharp_space_after_cast = false
csharp_space_after_keywords_in_control_flow_statements = true
csharp_space_between_method_declaration_parameter_list_parentheses = false
csharp_space_between_method_call_parameter_list_parentheses = false
csharp_space_before_colon_in_inheritance_clause = true
csharp_space_after_colon_in_inheritance_clause = true
csharp_space_around_binary_operators = before_and_after
csharp_space_between_method_declaration_empty_parameter_list_parentheses = false
csharp_space_between_method_call_name_and_opening_parenthesis = false
csharp_space_between_method_call_empty_parameter_list_parentheses = false
# Wrapping preferences
csharp_preserve_single_line_blocks = true
csharp_preserve_single_line_statements = true

View File

@ -0,0 +1,26 @@
{
"blurb": "Implement the `keep` and `discard` operation on collections. Given a collection and a predicate on the collection's elements, `keep` returns a new collection containing those elements where the predicate is true, while `discard` returns a new collection containing those elements where the predicate is false.",
"authors": [
"bressain"
],
"contributors": [
"ErikSchierboom",
"j2jensen",
"jwood803",
"robkeim",
"wolf99"
],
"files": {
"solution": [
"Strain.cs"
],
"test": [
"StrainTests.cs"
],
"example": [
".meta/Example.cs"
]
},
"source": "Conversation with James Edward Gray II",
"source_url": "https://twitter.com/jeg2"
}

View File

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

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

@ -0,0 +1,39 @@
# Help
## Running the tests
You can run the tests by opening a command prompt in the exercise's directory, and then running the [`dotnet test` command](https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-test)
Alternatively, most IDE's have built-in support for running tests, including [Visual Studio](https://docs.microsoft.com/en-us/visualstudio/test/run-unit-tests-with-test-explorer), [Rider](https://www.jetbrains.com/help/rider/Unit_Testing_in_Solution.html) and [Visual Studio code](https://github.com/OmniSharp/omnisharp-vscode/wiki/How-to-run-and-debug-unit-tests).
See the [tests page](https://exercism.io/tracks/csharp/tests) for more information.
## Skipped tests
Initially, only the first test will be enabled.
This is to encourage you to solve the exercise one step at a time.
Once you get the first test passing, remove the `Skip` property from the next test and work on getting that test passing.
## Submitting your solution
You can submit your solution using the `exercism submit Strain.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.

57
csharp/strain/README.md Normal file
View File

@ -0,0 +1,57 @@
# Strain
Welcome to Strain on Exercism's C# Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
## Instructions
Implement the `keep` and `discard` operation on collections. Given a collection
and a predicate on the collection's elements, `keep` returns a new collection
containing those elements where the predicate is true, while `discard` returns
a new collection containing those elements where the predicate is false.
For example, given the collection of numbers:
- 1, 2, 3, 4, 5
And the predicate:
- is the number even?
Then your keep operation should produce:
- 2, 4
While your discard operation should produce:
- 1, 3, 5
Note that the union of keep and discard is all the elements.
The functions may be called `keep` and `discard`, or they may need different
names in order to not clash with existing functions or concepts in your
language.
## Restrictions
Keep your hands off that filter/reject/whatchamacallit functionality
provided by your standard library! Solve this one yourself using other
basic tools instead.
## Source
### Created by
- @bressain
### Contributed to by
- @ErikSchierboom
- @j2jensen
- @jwood803
- @robkeim
- @wolf99
### Based on
Conversation with James Edward Gray II - https://twitter.com/jeg2

15
csharp/strain/Strain.cs Normal file
View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
public static class Strain
{
public static IEnumerable<T> Keep<T>(this IEnumerable<T> collection, Func<T, bool> predicate)
{
foreach (var item in collection)
if (predicate(item))
yield return item;
}
public static IEnumerable<T> Discard<T>(this IEnumerable<T> collection, Func<T, bool> predicate) =>
collection.Keep(i => !predicate(i));
}

View File

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

View File

@ -0,0 +1,102 @@
using System.Collections.Generic;
using System.Linq;
using Xunit;
public class StrainTests
{
[Fact]
public void Empty_keep()
{
Assert.Equal(new LinkedList<int>(), new LinkedList<int>().Keep(x => x < 10));
}
[Fact]
public void Keep_everything()
{
Assert.Equal(new HashSet<int> { 1, 2, 3 }, new HashSet<int> { 1, 2, 3 }.Keep(x => x < 10));
}
[Fact]
public void Keep_first_and_last()
{
Assert.Equal(new[] { 1, 3 }, new[] { 1, 2, 3 }.Keep(x => x % 2 != 0));
}
[Fact]
public void Keep_neither_first_nor_last()
{
Assert.Equal(new List<int> { 2, 4 }, new List<int> { 1, 2, 3, 4, 5 }.Keep(x => x % 2 == 0));
}
[Fact]
public void Keep_strings()
{
var words = "apple zebra banana zombies cherimoya zelot".Split(' ');
Assert.Equal("zebra zombies zelot".Split(' '), words.Keep(x => x.StartsWith("z")));
}
[Fact]
public void Keep_arrays()
{
var actual = new[]
{
new[] { 1, 2, 3 },
new[] { 5, 5, 5 },
new[] { 5, 1, 2 },
new[] { 2, 1, 2 },
new[] { 1, 5, 2 },
new[] { 2, 2, 1 },
new[] { 1, 2, 5 }
};
var expected = new[] { new[] { 5, 5, 5 }, new[] { 5, 1, 2 }, new[] { 1, 5, 2 }, new[] { 1, 2, 5 } };
Assert.Equal(expected, actual.Keep(x => x.Contains(5)));
}
[Fact]
public void Empty_discard()
{
Assert.Equal(new LinkedList<int>(), new LinkedList<int>().Discard(x => x < 10));
}
[Fact]
public void Discard_nothing()
{
Assert.Equal(new HashSet<int> { 1, 2, 3 }, new HashSet<int> { 1, 2, 3 }.Discard(x => x > 10));
}
[Fact]
public void Discard_first_and_last()
{
Assert.Equal(new[] { 2 }, new[] { 1, 2, 3 }.Discard(x => x % 2 != 0));
}
[Fact]
public void Discard_neither_first_nor_last()
{
Assert.Equal(new List<int> { 1, 3, 5 }, new List<int> { 1, 2, 3, 4, 5 }.Discard(x => x % 2 == 0));
}
[Fact]
public void Discard_strings()
{
var words = "apple zebra banana zombies cherimoya zelot".Split(' ');
Assert.Equal("apple banana cherimoya".Split(' '), words.Discard(x => x.StartsWith("z")));
}
[Fact]
public void Discard_arrays()
{
var actual = new[]
{
new[] { 1, 2, 3 },
new[] { 5, 5, 5 },
new[] { 5, 1, 2 },
new[] { 2, 1, 2 },
new[] { 1, 5, 2 },
new[] { 2, 2, 1 },
new[] { 1, 2, 5 }
};
var expected = new[] { new[] { 1, 2, 3 }, new[] { 2, 1, 2 }, new[] { 2, 2, 1 } };
Assert.Equal(expected, actual.Discard(x => x.Contains(5)));
}
}