diff --git a/Exercism/csharp/clock/.editorconfig b/Exercism/csharp/clock/.editorconfig new file mode 100644 index 0000000..fad75d8 --- /dev/null +++ b/Exercism/csharp/clock/.editorconfig @@ -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 diff --git a/Exercism/csharp/clock/Clock.cs b/Exercism/csharp/clock/Clock.cs new file mode 100644 index 0000000..0700fe4 --- /dev/null +++ b/Exercism/csharp/clock/Clock.cs @@ -0,0 +1,84 @@ +using System; + +public class Clock +{ + private int _hours; + private int _minutes; + + public Clock(int hours, int minutes) + { + var hoursAndminutes = CalculeMinutesAndHours(hours, minutes); + + _hours = hoursAndminutes.Item1; + _minutes = hoursAndminutes.Item2; + } + + public Clock Add(int minutesToAdd) + { + var hoursAndminutes = CalculeMinutesAndHours(_hours, _minutes + minutesToAdd); + + return new Clock(hoursAndminutes.Item1, hoursAndminutes.Item2); + } + + public Clock Subtract(int minutesToSubtract) + { + var hoursAndminutes = CalculeMinutesAndHours(_hours, _minutes - minutesToSubtract); + + return new Clock(hoursAndminutes.Item1, hoursAndminutes.Item2); + } + + public override string ToString() + { + return $"{_hours.ToString("d2")}:{_minutes.ToString("d2")}"; + } + + public override bool Equals(Object obj) + { + if ((obj == null) || ! this.GetType().Equals(obj.GetType())) + { + return false; + } else { + return ToString() == obj.ToString(); + } + } + + private static (int, int) CalculeMinutesAndHours(int hours, int minutes) + { + if (minutes >= 60) + { + hours = hours + (minutes / 60); + minutes = minutes % 60; + } + else if (minutes < 0) + { + var minutesToHours = Math.Abs(minutes / 60); + hours -= minutesToHours; + if (minutes < 0) + { + minutes += minutesToHours * 60; + } + else + { + minutes -= minutesToHours * 60; + } + } + + hours = hours % 24; + if (hours < 0) + { + hours = 24 - Math.Abs(hours); + } + + if (minutes < 0) + { + hours -= 1; + if (hours < 0) + { + hours = 24 - Math.Abs(hours); + } + minutes = 60 - Math.Abs(minutes); + } + + return (hours, minutes); + } +} diff --git a/Exercism/csharp/clock/Clock.csproj b/Exercism/csharp/clock/Clock.csproj new file mode 100644 index 0000000..0e01175 --- /dev/null +++ b/Exercism/csharp/clock/Clock.csproj @@ -0,0 +1,14 @@ + + + + net6.0 + + + + + + + + + + diff --git a/Exercism/csharp/clock/ClockTests.cs b/Exercism/csharp/clock/ClockTests.cs new file mode 100644 index 0000000..a71d388 --- /dev/null +++ b/Exercism/csharp/clock/ClockTests.cs @@ -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(Skip = "Remove this Skip property to run this test")] + public void Past_the_hour() + { + var sut = new Clock(11, 9); + Assert.Equal("11:09", sut.ToString()); + } + + [Fact(Skip = "Remove this Skip property to run this test")] + public void Midnight_is_zero_hours() + { + var sut = new Clock(24, 0); + Assert.Equal("00:00", sut.ToString()); + } + + [Fact(Skip = "Remove this Skip property to run this test")] + public void Hour_rolls_over() + { + var sut = new Clock(25, 0); + Assert.Equal("01:00", sut.ToString()); + } + + [Fact(Skip = "Remove this Skip property to run this test")] + public void Hour_rolls_over_continuously() + { + var sut = new Clock(100, 0); + Assert.Equal("04:00", sut.ToString()); + } + + [Fact(Skip = "Remove this Skip property to run this test")] + public void Sixty_minutes_is_next_hour() + { + var sut = new Clock(1, 60); + Assert.Equal("02:00", sut.ToString()); + } + + [Fact(Skip = "Remove this Skip property to run this test")] + public void Minutes_roll_over() + { + var sut = new Clock(0, 160); + Assert.Equal("02:40", sut.ToString()); + } + + [Fact(Skip = "Remove this Skip property to run this test")] + public void Minutes_roll_over_continuously() + { + var sut = new Clock(0, 1723); + Assert.Equal("04:43", sut.ToString()); + } + + [Fact(Skip = "Remove this Skip property to run this test")] + public void Hour_and_minutes_roll_over() + { + var sut = new Clock(25, 160); + Assert.Equal("03:40", sut.ToString()); + } + + [Fact(Skip = "Remove this Skip property to run this test")] + public void Hour_and_minutes_roll_over_continuously() + { + var sut = new Clock(201, 3001); + Assert.Equal("11:01", sut.ToString()); + } + + [Fact(Skip = "Remove this Skip property to run this test")] + public void Hour_and_minutes_roll_over_to_exactly_midnight() + { + var sut = new Clock(72, 8640); + Assert.Equal("00:00", sut.ToString()); + } + + [Fact(Skip = "Remove this Skip property to run this test")] + public void Negative_hour() + { + var sut = new Clock(-1, 15); + Assert.Equal("23:15", sut.ToString()); + } + + [Fact(Skip = "Remove this Skip property to run this test")] + public void Negative_hour_rolls_over() + { + var sut = new Clock(-25, 0); + Assert.Equal("23:00", sut.ToString()); + } + + [Fact(Skip = "Remove this Skip property to run this test")] + public void Negative_hour_rolls_over_continuously() + { + var sut = new Clock(-91, 0); + Assert.Equal("05:00", sut.ToString()); + } + + [Fact(Skip = "Remove this Skip property to run this test")] + public void Negative_minutes() + { + var sut = new Clock(1, -40); + Assert.Equal("00:20", sut.ToString()); + } + + [Fact(Skip = "Remove this Skip property to run this test")] + public void Negative_minutes_roll_over() + { + var sut = new Clock(1, -160); + Assert.Equal("22:20", sut.ToString()); + } + + [Fact(Skip = "Remove this Skip property to run this test")] + public void Negative_minutes_roll_over_continuously() + { + var sut = new Clock(1, -4820); + Assert.Equal("16:40", sut.ToString()); + } + + [Fact(Skip = "Remove this Skip property to run this test")] + public void Negative_sixty_minutes_is_previous_hour() + { + var sut = new Clock(2, -60); + Assert.Equal("01:00", sut.ToString()); + } + + [Fact(Skip = "Remove this Skip property to run this test")] + public void Negative_hour_and_minutes_both_roll_over() + { + var sut = new Clock(-25, -160); + Assert.Equal("20:20", sut.ToString()); + } + + [Fact(Skip = "Remove this Skip property to run this test")] + public void Negative_hour_and_minutes_both_roll_over_continuously() + { + var sut = new Clock(-121, -5810); + Assert.Equal("22:10", sut.ToString()); + } + + [Fact(Skip = "Remove this Skip property to run this test")] + public void Add_minutes() + { + var sut = new Clock(10, 0); + Assert.Equal("10:03", sut.Add(3).ToString()); + } + + [Fact(Skip = "Remove this Skip property to run this test")] + public void Add_no_minutes() + { + var sut = new Clock(6, 41); + Assert.Equal("06:41", sut.Add(0).ToString()); + } + + [Fact(Skip = "Remove this Skip property to run this test")] + public void Add_to_next_hour() + { + var sut = new Clock(0, 45); + Assert.Equal("01:25", sut.Add(40).ToString()); + } + + [Fact(Skip = "Remove this Skip property to run this test")] + public void Add_more_than_one_hour() + { + var sut = new Clock(10, 0); + Assert.Equal("11:01", sut.Add(61).ToString()); + } + + [Fact(Skip = "Remove this Skip property to run this test")] + public void Add_more_than_two_hours_with_carry() + { + var sut = new Clock(0, 45); + Assert.Equal("03:25", sut.Add(160).ToString()); + } + + [Fact(Skip = "Remove this Skip property to run this test")] + public void Add_across_midnight() + { + var sut = new Clock(23, 59); + Assert.Equal("00:01", sut.Add(2).ToString()); + } + + [Fact(Skip = "Remove this Skip property to run this test")] + 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(Skip = "Remove this Skip property to run this test")] + public void Add_more_than_two_days() + { + var sut = new Clock(1, 1); + Assert.Equal("11:21", sut.Add(3500).ToString()); + } + + [Fact(Skip = "Remove this Skip property to run this test")] + public void Subtract_minutes() + { + var sut = new Clock(10, 3); + Assert.Equal("10:00", sut.Subtract(3).ToString()); + } + + [Fact(Skip = "Remove this Skip property to run this test")] + public void Subtract_to_previous_hour() + { + var sut = new Clock(10, 3); + Assert.Equal("09:33", sut.Subtract(30).ToString()); + } + + [Fact(Skip = "Remove this Skip property to run this test")] + public void Subtract_more_than_an_hour() + { + var sut = new Clock(10, 3); + Assert.Equal("08:53", sut.Subtract(70).ToString()); + } + + [Fact(Skip = "Remove this Skip property to run this test")] + public void Subtract_across_midnight() + { + var sut = new Clock(0, 3); + Assert.Equal("23:59", sut.Subtract(4).ToString()); + } + + [Fact(Skip = "Remove this Skip property to run this test")] + public void Subtract_more_than_two_hours() + { + var sut = new Clock(0, 0); + Assert.Equal("21:20", sut.Subtract(160).ToString()); + } + + [Fact(Skip = "Remove this Skip property to run this test")] + public void Subtract_more_than_two_hours_with_borrow() + { + var sut = new Clock(6, 15); + Assert.Equal("03:35", sut.Subtract(160).ToString()); + } + + [Fact(Skip = "Remove this Skip property to run this test")] + 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(Skip = "Remove this Skip property to run this test")] + public void Subtract_more_than_two_days() + { + var sut = new Clock(2, 20); + Assert.Equal("00:20", sut.Subtract(3000).ToString()); + } + + [Fact(Skip = "Remove this Skip property to run this test")] + public void Clocks_with_same_time() + { + var sut = new Clock(15, 37); + Assert.Equal(new Clock(15, 37), sut); + } + + [Fact(Skip = "Remove this Skip property to run this test")] + public void Clocks_a_minute_apart() + { + var sut = new Clock(15, 37); + Assert.NotEqual(new Clock(15, 36), sut); + } + + [Fact(Skip = "Remove this Skip property to run this test")] + public void Clocks_an_hour_apart() + { + var sut = new Clock(15, 37); + Assert.NotEqual(new Clock(14, 37), sut); + } + + [Fact(Skip = "Remove this Skip property to run this test")] + public void Clocks_with_hour_overflow() + { + var sut = new Clock(34, 37); + Assert.Equal(new Clock(10, 37), sut); + } + + [Fact(Skip = "Remove this Skip property to run this test")] + public void Clocks_with_hour_overflow_by_several_days() + { + var sut = new Clock(99, 11); + Assert.Equal(new Clock(3, 11), sut); + } + + [Fact(Skip = "Remove this Skip property to run this test")] + public void Clocks_with_negative_hour() + { + var sut = new Clock(-2, 40); + Assert.Equal(new Clock(22, 40), sut); + } + + [Fact(Skip = "Remove this Skip property to run this test")] + public void Clocks_with_negative_hour_that_wraps() + { + var sut = new Clock(-31, 3); + Assert.Equal(new Clock(17, 3), sut); + } + + [Fact(Skip = "Remove this Skip property to run this test")] + public void Clocks_with_negative_hour_that_wraps_multiple_times() + { + var sut = new Clock(-83, 49); + Assert.Equal(new Clock(13, 49), sut); + } + + [Fact(Skip = "Remove this Skip property to run this test")] + public void Clocks_with_minute_overflow() + { + var sut = new Clock(0, 1441); + Assert.Equal(new Clock(0, 1), sut); + } + + [Fact(Skip = "Remove this Skip property to run this test")] + public void Clocks_with_minute_overflow_by_several_days() + { + var sut = new Clock(2, 4322); + Assert.Equal(new Clock(2, 2), sut); + } + + [Fact(Skip = "Remove this Skip property to run this test")] + public void Clocks_with_negative_minute() + { + var sut = new Clock(3, -20); + Assert.Equal(new Clock(2, 40), sut); + } + + [Fact(Skip = "Remove this Skip property to run this test")] + public void Clocks_with_negative_minute_that_wraps() + { + var sut = new Clock(5, -1490); + Assert.Equal(new Clock(4, 10), sut); + } + + [Fact(Skip = "Remove this Skip property to run this test")] + public void Clocks_with_negative_minute_that_wraps_multiple_times() + { + var sut = new Clock(6, -4305); + Assert.Equal(new Clock(6, 15), sut); + } + + [Fact(Skip = "Remove this Skip property to run this test")] + public void Clocks_with_negative_hours_and_minutes() + { + var sut = new Clock(-12, -268); + Assert.Equal(new Clock(7, 32), sut); + } + + [Fact(Skip = "Remove this Skip property to run this test")] + public void Clocks_with_negative_hours_and_minutes_that_wrap() + { + var sut = new Clock(-54, -11513); + Assert.Equal(new Clock(18, 7), sut); + } + + [Fact(Skip = "Remove this Skip property to run this test")] + public void Full_clock_and_zeroed_clock() + { + var sut = new Clock(0, 0); + Assert.Equal(new Clock(24, 0), sut); + } + + [Fact(Skip = "Remove this Skip property to run this test")] + public void Clocks_are_immutable() + { + var sut = new Clock(0, 0); + var sutPlus1 = sut.Add(1); + Assert.NotEqual(sutPlus1, sut); + } +} \ No newline at end of file diff --git a/Exercism/csharp/clock/HELP.md b/Exercism/csharp/clock/HELP.md new file mode 100644 index 0000000..332f9bb --- /dev/null +++ b/Exercism/csharp/clock/HELP.md @@ -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.org/docs/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 programming category on the forum](https://forum.exercism.org/c/programming/5) +- 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. \ No newline at end of file diff --git a/Exercism/csharp/clock/README.md b/Exercism/csharp/clock/README.md new file mode 100644 index 0000000..5563f5d --- /dev/null +++ b/Exercism/csharp/clock/README.md @@ -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 \ No newline at end of file