Browse Source

Exercism: Car Assemble

master
Julio Biason 3 years ago
parent
commit
2aa741cb86
  1. 20
      csharp/cars-assemble/.exercism/config.json
  2. 1
      csharp/cars-assemble/.exercism/metadata.json
  3. 22
      csharp/cars-assemble/CarsAssemble.cs
  4. 14
      csharp/cars-assemble/CarsAssemble.csproj
  5. 77
      csharp/cars-assemble/CarsAssembleTests.cs
  6. 39
      csharp/cars-assemble/HELP.md
  7. 22
      csharp/cars-assemble/HINTS.md
  8. 93
      csharp/cars-assemble/README.md

20
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"
]
}
}

1
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}

22
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);
}
}

14
csharp/cars-assemble/CarsAssemble.csproj

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

77
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));
}
}

39
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.

22
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

93
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
Loading…
Cancel
Save