Create reverser that reverses an array at a given position.

This commit is contained in:
Kristian Stolen
2022-01-12 17:56:26 +08:00
parent 2eab61fd1d
commit 84d473d8f2
6 changed files with 134 additions and 0 deletions

View File

@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Reverse\Reverse.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,41 @@
using System.Linq;
using Xunit;
namespace Reverse.Tests
{
public class ReverserTests
{
[Theory]
[InlineData(new int[] { 1 }, new int[] { 1 })]
[InlineData(new int[] { 1, 2 }, new int[] { 2, 1 })]
[InlineData(new int[] { 1, 2, 3 }, new int[] { 3, 2, 1 })]
public void ReverserReversesTheArray(int[] input, int[] output)
{
Reverser.Reverse(input, input.Length);
Assert.True(input.SequenceEqual(output));
}
[Fact]
public void ReverserReversesTheArrayAtTheSpecifiedIndex()
{
var input = new int[] { 1, 2, 3, 4 };
var output = new int[] { 2, 1, 3, 4 };
Reverser.Reverse(input, 2);
Assert.True(input.SequenceEqual(output));
}
[Fact]
public void ReversingAtIndexOneDoesNotChangeTheArray()
{
var input = new int[] { 1, 2 };
var output = new int[] { 1, 2 };
Reverser.Reverse(input, 1);
Assert.True(input.SequenceEqual(output));
}
}
}

View File

@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.32002.261
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Reverse", "Reverse\Reverse.csproj", "{39463B63-6A71-4DCF-A4F2-FAA74FDEEC01}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Reverse.Tests", "Reverse.Tests\Reverse.Tests.csproj", "{96E824F8-0353-4FF2-9FEA-F850E2BE7312}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{39463B63-6A71-4DCF-A4F2-FAA74FDEEC01}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{39463B63-6A71-4DCF-A4F2-FAA74FDEEC01}.Debug|Any CPU.Build.0 = Debug|Any CPU
{39463B63-6A71-4DCF-A4F2-FAA74FDEEC01}.Release|Any CPU.ActiveCfg = Release|Any CPU
{39463B63-6A71-4DCF-A4F2-FAA74FDEEC01}.Release|Any CPU.Build.0 = Release|Any CPU
{96E824F8-0353-4FF2-9FEA-F850E2BE7312}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{96E824F8-0353-4FF2-9FEA-F850E2BE7312}.Debug|Any CPU.Build.0 = Debug|Any CPU
{96E824F8-0353-4FF2-9FEA-F850E2BE7312}.Release|Any CPU.ActiveCfg = Release|Any CPU
{96E824F8-0353-4FF2-9FEA-F850E2BE7312}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {1DCA2723-D126-4B37-A698-D40DA03643A9}
EndGlobalSection
EndGlobal

View File

@@ -0,0 +1,12 @@
using System;
namespace Reverse
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}

View File

@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,16 @@
namespace Reverse
{
public class Reverser
{
public static void Reverse(int[] arrayToReverse, int indexToReverseTo)
{
for (int i = 0; i < indexToReverseTo / 2; i++)
{
int temp = arrayToReverse[i];
int upperIndex = indexToReverseTo - 1 - i;
arrayToReverse[i] = arrayToReverse[upperIndex];
arrayToReverse[upperIndex] = temp;
}
}
}
}