From 2aa741cb8613b0d0d2ef61e8c692f084a592d747 Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Wed, 29 Sep 2021 19:19:54 -0300 Subject: [PATCH] Exercism: Car Assemble --- csharp/cars-assemble/.exercism/config.json | 20 +++++ csharp/cars-assemble/.exercism/metadata.json | 1 + csharp/cars-assemble/CarsAssemble.cs | 22 +++++ csharp/cars-assemble/CarsAssemble.csproj | 14 +++ csharp/cars-assemble/CarsAssembleTests.cs | 77 ++++++++++++++++ csharp/cars-assemble/HELP.md | 39 ++++++++ csharp/cars-assemble/HINTS.md | 22 +++++ csharp/cars-assemble/README.md | 93 ++++++++++++++++++++ 8 files changed, 288 insertions(+) create mode 100644 csharp/cars-assemble/.exercism/config.json create mode 100644 csharp/cars-assemble/.exercism/metadata.json create mode 100644 csharp/cars-assemble/CarsAssemble.cs create mode 100644 csharp/cars-assemble/CarsAssemble.csproj create mode 100644 csharp/cars-assemble/CarsAssembleTests.cs create mode 100644 csharp/cars-assemble/HELP.md create mode 100644 csharp/cars-assemble/HINTS.md create mode 100644 csharp/cars-assemble/README.md diff --git a/csharp/cars-assemble/.exercism/config.json b/csharp/cars-assemble/.exercism/config.json new file mode 100644 index 0000000..3b5a38f --- /dev/null +++ b/csharp/cars-assemble/.exercism/config.json @@ -0,0 +1,20 @@ +{ + "blurb": "Learn about numbers by analyzing the production of an assembly line.", + "contributors": [ + "yzAlvin" + ], + "authors": [ + "ErikSchierboom" + ], + "files": { + "solution": [ + "CarsAssemble.cs" + ], + "test": [ + "CarsAssembleTests.cs" + ], + "exemplar": [ + ".meta/Exemplar.cs" + ] + } +} diff --git a/csharp/cars-assemble/.exercism/metadata.json b/csharp/cars-assemble/.exercism/metadata.json new file mode 100644 index 0000000..fae1e01 --- /dev/null +++ b/csharp/cars-assemble/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"csharp","exercise":"cars-assemble","id":"5edd50bd947e4680ac7fbb8764ba1531","url":"https://exercism.org/tracks/csharp/exercises/cars-assemble","handle":"JBiason","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/csharp/cars-assemble/CarsAssemble.cs b/csharp/cars-assemble/CarsAssemble.cs new file mode 100644 index 0000000..e40638f --- /dev/null +++ b/csharp/cars-assemble/CarsAssemble.cs @@ -0,0 +1,22 @@ +using System; + +static class AssemblyLine +{ + public static double ProductionRatePerHour(int speed) + { + double production = speed * 221; + if (speed == 10) { + production *= 0.77; + } else if (speed == 9) { + production *= 0.8; + } else if (speed > 4) { + production *= 0.9; + } + return production; + } + + public static int WorkingItemsPerMinute(int speed) + { + return (int)Math.Floor(ProductionRatePerHour(speed) / 60.0); + } +} diff --git a/csharp/cars-assemble/CarsAssemble.csproj b/csharp/cars-assemble/CarsAssemble.csproj new file mode 100644 index 0000000..34d13a4 --- /dev/null +++ b/csharp/cars-assemble/CarsAssemble.csproj @@ -0,0 +1,14 @@ + + + + net5.0 + + + + + + + + + + \ No newline at end of file diff --git a/csharp/cars-assemble/CarsAssembleTests.cs b/csharp/cars-assemble/CarsAssembleTests.cs new file mode 100644 index 0000000..acbb0e1 --- /dev/null +++ b/csharp/cars-assemble/CarsAssembleTests.cs @@ -0,0 +1,77 @@ +using Xunit; +using Exercism.Tests; + +public class AssemblyLineTests +{ + [Fact] + public void Production_rate_per_hour_for_speed_zero() + { + Assert.Equal(0.0, AssemblyLine.ProductionRatePerHour(0), precision: 1); + } + + [Fact] + public void Production_rate_per_hour_for_speed_one() + { + Assert.Equal(221.0, AssemblyLine.ProductionRatePerHour(1), precision: 1); + } + + [Fact] + public void Production_rate_per_hour_for_speed_four() + { + Assert.Equal(884.0, AssemblyLine.ProductionRatePerHour(4), precision: 1); + } + + [Fact] + public void Production_rate_per_hour_for_speed_seven() + { + Assert.Equal(1392.3, AssemblyLine.ProductionRatePerHour(7), precision: 1); + } + + [Fact] + public void Production_rate_per_hour_for_speed_nine() + { + Assert.Equal(1591.2, AssemblyLine.ProductionRatePerHour(9), precision: 1); + } + + [Fact] + public void Production_rate_per_hour_for_speed_ten() + { + Assert.Equal(1701.7, AssemblyLine.ProductionRatePerHour(10), precision: 1); + } + + [Fact] + public void Working_items_per_minute_for_speed_zero() + { + Assert.Equal(0, AssemblyLine.WorkingItemsPerMinute(0)); + } + + [Fact] + public void Working_items_per_minute_for_speed_one() + { + Assert.Equal(3, AssemblyLine.WorkingItemsPerMinute(1)); + } + + [Fact] + public void Working_items_per_minute_for_speed_five() + { + Assert.Equal(16, AssemblyLine.WorkingItemsPerMinute(5)); + } + + [Fact] + public void Working_items_per_minute_for_speed_eight() + { + Assert.Equal(26, AssemblyLine.WorkingItemsPerMinute(8)); + } + + [Fact] + public void Working_items_per_minute_for_speed_nine() + { + Assert.Equal(26, AssemblyLine.WorkingItemsPerMinute(9)); + } + + [Fact] + public void Working_items_per_minute_for_speed_ten() + { + Assert.Equal(28, AssemblyLine.WorkingItemsPerMinute(10)); + } +} diff --git a/csharp/cars-assemble/HELP.md b/csharp/cars-assemble/HELP.md new file mode 100644 index 0000000..555559a --- /dev/null +++ b/csharp/cars-assemble/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.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 CarsAssemble.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. \ No newline at end of file diff --git a/csharp/cars-assemble/HINTS.md b/csharp/cars-assemble/HINTS.md new file mode 100644 index 0000000..2393bf7 --- /dev/null +++ b/csharp/cars-assemble/HINTS.md @@ -0,0 +1,22 @@ +# Hints + +## General + +- [Numbers tutorial][numbers]. + +## 1. Calculate the production rate per second + +- Determining the success rate can be done through a [conditional statement][if-statement]. +- C# allows for multiplication to be applied to two different number types (such as an `int` and a `double`). It will automatically return the "largest" data type. +- Numbers can be compared using the built-in [comparison-][comparison-operators] and [equality operators][equality-operators]. + +## 2. Calculate the number of working items produced per second + +- Whereas an `int` can be automatically converted to a `double`, the reverse does not hold. The reason for this is that an `int` has less precision than a `double` so rounding has to be applied, also the range of numbers an `int` can represent is smaller than a `double`. To force this conversion, one can either use one of the [`Convert` class' methods][convert-class] or [cast to an int][cast-int]. + +[convert-class]: https://docs.microsoft.com/en-us/dotnet/api/system.convert?view=netcore-3.1#examples +[cast-int]: https://www.dotnetperls.com/cast-int +[numbers]: https://docs.microsoft.com/en-us/dotnet/csharp/tutorials/intro-to-csharp/numbers-in-csharp-local +[if-statement]: https://csharp.net-tutorials.com/control-structures/if-statement/ +[comparison-operators]: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/comparison-operators +[equality-operators]: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/equality-operators \ No newline at end of file diff --git a/csharp/cars-assemble/README.md b/csharp/cars-assemble/README.md new file mode 100644 index 0000000..ee7ff46 --- /dev/null +++ b/csharp/cars-assemble/README.md @@ -0,0 +1,93 @@ +# Cars, Assemble! + +Welcome to Cars, Assemble! on Exercism's C# Track. +If you need help running the tests or submitting your code, check out `HELP.md`. +If you get stuck on the exercise, check out `HINTS.md`, but try and solve it without using those first :) + +## Introduction + +## Numbers + +There are two different types of numbers in C#: + +- Integers: numbers with no digits behind the decimal separator (whole numbers). Examples are `-6`, `0`, `1`, `25`, `976` and `500000`. +- Floating-point numbers: numbers with zero or more digits behind the decimal separator. Examples are `-2.4`, `0.1`, `3.14`, `16.984025` and `1024.0`. + +The two most common numeric types in C# are `int` and `double`. An `int` is a 32-bit integer and a `double` is a 64-bit floating-point number. + +Arithmetic is done using the standard arithmetic operators. Numbers can be compared using the standard numeric comparison operators and the equality (`==`) and inequality (`!=`) operators. + +C# has two types of numeric conversions: + +1. Implicit conversions: no data will be lost and no additional syntax is required. +2. Explicit conversions: data could be lost and additional syntax in the form of a _cast_ is required. + +As an `int` has less precision than a `double`, converting from an `int` to a `double` is safe and is thus an implicit conversion. However, converting from a `double` to an `int` could mean losing data, so that requires an explicit conversion. + +## If Statements + +In this exercise you must conditionally execute logic. The most common way to do this in C# is by using an `if/else` statement: + +```csharp +int x = 6; + +if (x == 5) +{ + // Execute logic if x equals 5 +} +else if (x > 7) +{ + // Execute logic if x greater than 7 +} +else +{ + // Execute logic in all other cases +} +``` + +The condition of an `if` statement must be of type `bool`. C# has no concept of _truthy_ values. + +## Instructions + +In this exercise you'll be writing code to analyze the production of an assembly line in a car factory. The assembly line's speed can range from `0` (off) to `10` (maximum). + +At its lowest speed (`1`), `221` cars are produced each hour. The production increases linearly with the speed. So with the speed set to `4`, it should produce `4 * 221 = 884` cars per hour. However, higher speeds increase the likelihood that faulty cars are produced, which then have to be discarded. The following table shows how speed influences the success rate: + +- `1` to `4`: 100% success rate. +- `5` to `8`: 90% success rate. +- `9`: 80% success rate. +- `10`: 77% success rate. + +You have two tasks. + +## 1. Calculate the production rate per hour + +Implement the (_static_) `AssemblyLine.ProductionRatePerHour()` method to calculate the assembly line's production rate per hour, taking into account its success rate: + +```csharp +AssemblyLine.ProductionRatePerHour(6) +// => 1193.4 +``` + +Note that the value returned is a `double`. + +## 2. Calculate the number of working items produced per minute + +Implement the (_static_) `AssemblyLine.WorkingItemsPerMinute()` method to calculate how many working cars are produced per minute: + +```csharp +AssemblyLine.WorkingItemsPerMinute(6) +// => 19 +``` + +Note that the value returned is an `int`. + +## Source + +### Created by + +- @ErikSchierboom + +### Contributed to by + +- @yzAlvin \ No newline at end of file