Skip to content

Commit

Permalink
Added LCD digits kata
Browse files Browse the repository at this point in the history
  • Loading branch information
zhendrikse committed Nov 7, 2024
1 parent ad461fc commit 80bbae1
Show file tree
Hide file tree
Showing 10 changed files with 197 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ on:
paths:
- '.github/workflows/dotnet.yml'
- 'kata-solutions/shunting-yard-algo/shunting-yard-algo-dotnet/**'
- 'kata-solutions/lcd-digits-kata/lcd-digits-dotnet/**'
pull_request:
branches: [ "master" ]

Expand All @@ -29,3 +30,15 @@ jobs:
dotnet restore
dotnet build --no-restore
dotnet test --no-build --verbosity normal
- uses: actions/checkout@v3
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 7.0.x
- name: LCD Digits kata
working-directory: kata-solutions/lcd-digits-kata/lcd-digits-dotnet
run: |
dotnet restore
dotnet build --no-restore
dotnet test --no-build --verbosity normal
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\LcdDigits\LcdDigits.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using Xunit;
using System;

namespace Kata.Tests;

public class LcdDigitsTest
{
[Fact]
public void Digit_the_number_0()
{
string expect = " _ " + "\r\n" +
"| |" + "\r\n" +
"|_|" + "\r\n";

Assert.Equivalent(expect, LcdDigits.GetDigits(0));
}

[Fact]
public void Digit_the_number_1()
{
string expect = " " + "\r\n" +
" |" + "\r\n" +
" |" + "\r\n";

Assert.Equivalent(expect, LcdDigits.GetDigits(1));
}

[Fact]
public void Digit_the_number_2()
{
string expect = " _ " + "\r\n" +
" _|" + "\r\n" +
"|_ " + "\r\n";

Assert.Equivalent(expect, LcdDigits.GetDigits(2));
}

[Fact]
public void Digit_the_number_34()
{
string expect = " _ " + "\r\n" +
" _||_|" + "\r\n" +
" _| |" + "\r\n";

Assert.Equivalent(expect, LcdDigits.GetDigits(34));
}


[Fact]
public void Digit_the_number_357()
{
string expect = " _ _ _ " + "\r\n" +
" _||_ |" + "\r\n" +
" _| _| |" + "\r\n";

Assert.Equivalent(expect, LcdDigits.GetDigits(357));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Xunit;
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

using System.Data;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;

namespace Kata;

public class LcdDigits
{
private static string _digits =
" _ _ _ _ _ _ _ _ " +
"| | | _| _||_||_ |_ ||_||_|" +
"|_| ||_ _| | _||_| ||_| _|";

private static string[] ConvertToSingleDigitStringArray(int number) {
return (from i in Enumerable.Range(0, 3) select _digits.Substring(i * 30 + number * 3, 3)).ToArray();
}

private static string[] AddDigits(string[] digit_1, string[] digit_2) {
return (from i in Enumerable.Range(0, 3) select digit_1[i] + digit_2[i]).ToArray();
}

public static string[] NumberAsString(int number)
{
int size = ("" + number).Length;
int power = 1;
for (int i = 1; i < size; i++)
power *= 10;

if (number < 10)
return ConvertToSingleDigitStringArray(number / power);
else
return AddDigits(ConvertToSingleDigitStringArray(number / power), NumberAsString(number % power));
}

public static string GetDigits(int number) {
string[] numberAsString = NumberAsString(number);
return numberAsString[0] + "\r\n" + numberAsString[1] + "\r\n" + numberAsString[2] + "\r\n";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
27 changes: 27 additions & 0 deletions kata-solutions/lcd-digits-kata/lcd-digits-dotnet/lcddigits.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LcdDigits", "LcdDigits\LcdDigits.csproj", "{966F6F47-23DB-459B-959F-3115EB010C0A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LcdDigits.Tests", "LcdDigits.Tests\LcdDigits.Tests.csproj", "{BBE7EAF8-7CBB-4C49-81A2-D7D43663EFFB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{966F6F47-23DB-459B-959F-3115EB010C0A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{966F6F47-23DB-459B-959F-3115EB010C0A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{966F6F47-23DB-459B-959F-3115EB010C0A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{966F6F47-23DB-459B-959F-3115EB010C0A}.Release|Any CPU.Build.0 = Release|Any CPU
{BBE7EAF8-7CBB-4C49-81A2-D7D43663EFFB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BBE7EAF8-7CBB-4C49-81A2-D7D43663EFFB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BBE7EAF8-7CBB-4C49-81A2-D7D43663EFFB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BBE7EAF8-7CBB-4C49-81A2-D7D43663EFFB}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dotnet watch test --project lcddigits.sln
1 change: 1 addition & 0 deletions tdd-katas/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Difficulties range from low (**L**) &rarr; medium (**M**) &rarr; high (**H**).
| [game-of-life](./game-of-life) | M | **Generic TDD** / **Small steps** ||||||||
| [gilded-rose-kata](./gilded-rose-kata) | M / H | **Legacy code** / **Refactoring** / Approval tests ||||||||
| [greed-kata](./greed-kata) | M | Implement complex rules in **small steps** ||||||||
| [locker-room-kata](./lcd-digits-kata) | L | Getting started with TDD / Recursion ||||||||
| [locker-room-kata](./locker-room-kata) | M | Stateless / Functional programming ||||||||
| [manhattan-distance](./manhattan-distance) | L | 1, 2, N / Encapsulation ||||||||
| [mars-rover](./mars-rover) | M | **Generic TDD** / Design decisions ||||||||
Expand Down
18 changes: 18 additions & 0 deletions tdd-katas/lcd-digits-kata/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# About the LCD Digits Kata

The goal of this kata is to convert any arbitrary (integer) number
into a string representation like so:

```
_ _ _ _ _ _ _ _
| | | _| _||_||_ |_ ||_||_|
|_| ||_ _| | _||_| ||_| _|
```

For example, the number 318 must be rendered as

```
_ _
_| ||_|
_| ||_|
```

0 comments on commit 80bbae1

Please sign in to comment.