Compare commits

...

2 commits

8 changed files with 367 additions and 4 deletions

View file

@ -4,21 +4,30 @@ static class QuestLogic
{
public static bool CanFastAttack(bool knightIsAwake)
{
throw new NotImplementedException("Please implement the (static) QuestLogic.CanFastAttack() method");
return !knightIsAwake;
}
public static bool CanSpy(bool knightIsAwake, bool archerIsAwake, bool prisonerIsAwake)
{
throw new NotImplementedException("Please implement the (static) QuestLogic.CanSpy() method");
return knightIsAwake || archerIsAwake || prisonerIsAwake;
}
public static bool CanSignalPrisoner(bool archerIsAwake, bool prisonerIsAwake)
{
throw new NotImplementedException("Please implement the (static) QuestLogic.CanSignalPrisoner() method");
return !archerIsAwake && prisonerIsAwake;
}
public static bool CanFreePrisoner(bool knightIsAwake, bool archerIsAwake, bool prisonerIsAwake, bool petDogIsPresent)
{
throw new NotImplementedException("Please implement the (static) QuestLogic.CanFreePrisoner() method");
if (petDogIsPresent && !archerIsAwake)
{
return true;
}
if (!petDogIsPresent && !archerIsAwake && !knightIsAwake && prisonerIsAwake)
{
return true;
}
return false;
}
}

View file

@ -0,0 +1,23 @@
{
"authors": [
"ErikSchierboom"
],
"contributors": [
"yzAlvin"
],
"files": {
"solution": [
"CarsAssemble.cs"
],
"test": [
"CarsAssembleTests.cs"
],
"exemplar": [
".meta/Exemplar.cs"
],
"invalidator": [
"CarsAssemble.csproj"
]
},
"blurb": "Learn about numbers by analyzing the production of an assembly line."
}

View file

@ -0,0 +1,19 @@
using System;
static class AssemblyLine
{
public static double SuccessRate(int speed)
{
throw new NotImplementedException("Please implement the (static) AssemblyLine.SuccessRate() method");
}
public static double ProductionRatePerHour(int speed)
{
throw new NotImplementedException("Please implement the (static) AssemblyLine.ProductionRatePerHour() method");
}
public static int WorkingItemsPerMinute(int speed)
{
throw new NotImplementedException("Please implement the (static) AssemblyLine.WorkingItemsPerMinute() method");
}
}

View file

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.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,130 @@
using Xunit;
using Exercism.Tests;
public class CarsAssembleTests
{
[Fact]
[Task(1)]
public void Success_rate_for_speed_zero()
{
Assert.Equal(0.0, AssemblyLine.SuccessRate(0), precision:1);
}
[Fact]
[Task(1)]
public void Success_rate_for_speed_one()
{
Assert.Equal(1.0, AssemblyLine.SuccessRate(1), precision:1);
}
[Fact]
[Task(1)]
public void Success_rate_for_speed_four()
{
Assert.Equal(1.0, AssemblyLine.SuccessRate(4), precision:1);
}
[Fact]
[Task(1)]
public void Success_rate_for_speed_five()
{
Assert.Equal(0.9, AssemblyLine.SuccessRate(5), precision:1);
}
[Fact]
[Task(1)]
public void Success_rate_for_speed_nine()
{
Assert.Equal(0.8, AssemblyLine.SuccessRate(9), precision:1);
}
[Fact]
[Task(1)]
public void Success_rate_for_speed_ten()
{
Assert.Equal(0.77, AssemblyLine.SuccessRate(10), precision:2);
}
[Fact]
[Task(2)]
public void Production_rate_per_hour_for_speed_zero()
{
Assert.Equal(0.0, AssemblyLine.ProductionRatePerHour(0), precision: 1);
}
[Fact]
[Task(2)]
public void Production_rate_per_hour_for_speed_one()
{
Assert.Equal(221.0, AssemblyLine.ProductionRatePerHour(1), precision: 1);
}
[Fact]
[Task(2)]
public void Production_rate_per_hour_for_speed_four()
{
Assert.Equal(884.0, AssemblyLine.ProductionRatePerHour(4), precision: 1);
}
[Fact]
[Task(2)]
public void Production_rate_per_hour_for_speed_seven()
{
Assert.Equal(1392.3, AssemblyLine.ProductionRatePerHour(7), precision: 1);
}
[Fact]
[Task(2)]
public void Production_rate_per_hour_for_speed_nine()
{
Assert.Equal(1591.2, AssemblyLine.ProductionRatePerHour(9), precision: 1);
}
[Fact]
[Task(2)]
public void Production_rate_per_hour_for_speed_ten()
{
Assert.Equal(1701.7, AssemblyLine.ProductionRatePerHour(10), precision: 1);
}
[Fact]
[Task(3)]
public void Working_items_per_minute_for_speed_zero()
{
Assert.Equal(0, AssemblyLine.WorkingItemsPerMinute(0));
}
[Fact]
[Task(3)]
public void Working_items_per_minute_for_speed_one()
{
Assert.Equal(3, AssemblyLine.WorkingItemsPerMinute(1));
}
[Fact]
[Task(3)]
public void Working_items_per_minute_for_speed_five()
{
Assert.Equal(16, AssemblyLine.WorkingItemsPerMinute(5));
}
[Fact]
[Task(3)]
public void Working_items_per_minute_for_speed_eight()
{
Assert.Equal(26, AssemblyLine.WorkingItemsPerMinute(8));
}
[Fact]
[Task(3)]
public void Working_items_per_minute_for_speed_nine()
{
Assert.Equal(26, AssemblyLine.WorkingItemsPerMinute(9));
}
[Fact]
[Task(3)]
public void Working_items_per_minute_for_speed_ten()
{
Assert.Equal(28, AssemblyLine.WorkingItemsPerMinute(10));
}
}

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.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 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)
- The [C# track's programming category on the forum](https://forum.exercism.org/c/programming/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:
- [/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,26 @@
# Hints
## General
- [Numbers tutorial][numbers].
## 1. Calculate the success rate
- Determining the success rate can be done through a [conditional statement][if-statement].
## 2. Calculate the production rate per second
- Use the `AssemblyLine.SuccessRate()` method you wrote earlier to determine the success rate.
- 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].
## 3. 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
[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

View file

@ -0,0 +1,103 @@
# 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.
You have three tasks.
## 1. Calculate the success rate
Implement the (_static_) `AssemblyLine.SuccessRate()` method to calculate the ratio of an item being created without error for a given speed. The following table shows how speed influences the success rate:
- `0`: 0% success rate.
- `1` to `4`: 100% success rate.
- `5` to `8`: 90% success rate.
- `9`: 80% success rate.
- `10`: 77% success rate.
```csharp
AssemblyLine.SuccessRate(10)
// => 0.77
```
## 2. 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`.
## 3. 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