Merge pull request #497 from kstolen0/main

Reverse in CSharp
This commit is contained in:
Jeff Atwood
2022-01-13 08:55:12 -08:00
committed by GitHub
8 changed files with 453 additions and 0 deletions

View File

@@ -0,0 +1,10 @@
using FsCheck;
namespace Reverse.Tests.Generators
{
public static class PositiveIntegerGenerator
{
public static Arbitrary<int> Generate() =>
Arb.Default.Int32().Filter(x => x > 0);
}
}

View File

@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FsCheck.Xunit" Version="2.16.4" />
<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,148 @@
using FsCheck.Xunit;
using Reverse.Tests.Generators;
using System;
using System.Linq;
using Xunit;
namespace Reverse.Tests
{
public class ReverserTests
{
[Fact]
public void Constructor_CannotAcceptNumberLessThanZero()
{
Action action = () => new Reverser(0);
Assert.Throws<ArgumentOutOfRangeException>(action);
}
[Property(Arbitrary = new[] { typeof(PositiveIntegerGenerator) })]
public void Constructor_CreatesRandomArrayOfSpecifiedLength(int size)
{
var sut = new TestReverser(size);
Assert.Equal(size, sut.GetArray().Length);
}
[Property(Arbitrary = new[] { typeof(PositiveIntegerGenerator) })]
public void ConstructorArray_MaxElementValueIsEqualToSize(int size)
{
var sut = new TestReverser(size);
Assert.Equal(size, sut.GetArray().Max());
}
[Property(Arbitrary = new[] { typeof(PositiveIntegerGenerator) })]
public void ConstructorArray_ReturnsRandomArrayWithDistinctElements(int size)
{
var sut = new TestReverser(size);
var array = sut.GetArray();
var arrayGroup = array.GroupBy(x => x);
var duplicateFound = arrayGroup.Any(x => x.Count() > 1);
Assert.False(duplicateFound);
}
[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 Reverse_WillReverseEntireArray(int[] input, int[] output)
{
var sut = new TestReverser(1);
sut.SetArray(input);
sut.Reverse(input.Length);
Assert.True(sut.GetArray().SequenceEqual(output));
}
[Fact]
public void Reverse_WithSpecifiedIndex_ReversesItemsUpToThatIndex()
{
var input = new int[] { 1, 2, 3, 4 };
var output = new int[] { 2, 1, 3, 4 };
var sut = new TestReverser(1);
sut.SetArray(input);
sut.Reverse(2);
Assert.True(sut.GetArray().SequenceEqual(output));
}
[Fact]
public void Reverse_WithIndexOne_DoesNothing()
{
var input = new int[] { 1, 2 };
var output = new int[] { 1, 2 };
var sut = new TestReverser(1);
sut.SetArray(input);
sut.Reverse(1);
Assert.True(sut.GetArray().SequenceEqual(output));
}
[Fact]
public void Reverse_WithIndexGreaterThanArrayLength_DoesNothing()
{
var input = new int[] { 1, 2 };
var output = new int[] { 1, 2 };
var sut = new TestReverser(1);
sut.SetArray(input);
sut.Reverse(sut.GetArray().Length + 1);
Assert.True(sut.GetArray().SequenceEqual(output));
}
[Fact]
public void Reverse_WithIndexLessThanZero_DoesNothing()
{
var input = new int[] { 1, 2 };
var output = new int[] { 1, 2 };
var sut = new TestReverser(1);
sut.SetArray(input);
sut.Reverse(-1);
Assert.True(sut.GetArray().SequenceEqual(output));
}
[Theory]
[InlineData(new int[] { 1 })]
[InlineData(new int[] { 1, 2 })]
[InlineData(new int[] { 1, 1 })]
public void IsArrayInAscendingOrder_WhenArrayElementsAreInNumericAscendingOrder_ReturnsTrue(int[] input)
{
var sut = new TestReverser(1);
sut.SetArray(input);
var result = sut.IsArrayInAscendingOrder();
Assert.True(result);
}
[Fact]
public void IsArrayInOrder_WhenArrayElementsAreNotInNumericAscendingOrder_ReturnsFalse()
{
var sut = new TestReverser(1);
sut.SetArray(new int[] { 2, 1 });
var result = sut.IsArrayInAscendingOrder();
Assert.False(result);
}
[Fact]
public void GetArrayString_ReturnsSpaceSeparatedElementsOfArrayInStringFormat()
{
var sut = new TestReverser(1);
sut.SetArray(new int[] { 1, 2 });
var result = sut.GetArrayString();
Assert.Equal(" 1 2 ", result);
}
}
}

View File

@@ -0,0 +1,17 @@
namespace Reverse.Tests
{
internal class TestReverser : Reverser
{
public TestReverser(int arraySize) : base(arraySize) { }
public int[] GetArray()
{
return _array;
}
public void SetArray(int[] array)
{
_array = array;
}
}
}

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,130 @@
using System;
namespace Reverse
{
class Program
{
private static int arrayLength = 9;
static void Main(string[] args)
{
PrintTitle();
Console.Write("DO YOU WANT THE RULES? ");
var needRulesInput = Console.ReadLine();
Console.WriteLine();
if (string.Equals(needRulesInput, "YES", StringComparison.OrdinalIgnoreCase))
{
DisplayRules();
}
var tryAgain = string.Empty;
while (!string.Equals(tryAgain, "NO", StringComparison.OrdinalIgnoreCase))
{
var reverser = new Reverser(arrayLength);
Console.WriteLine("HERE WE GO ... THE LIST IS:");
PrintList(reverser.GetArrayString());
var arrayIsInAscendingOrder = false;
var numberOfMoves = 0;
while (arrayIsInAscendingOrder == false)
{
int index = ReadNextInput();
if (index == 0)
{
break;
}
reverser.Reverse(index);
PrintList(reverser.GetArrayString());
arrayIsInAscendingOrder = reverser.IsArrayInAscendingOrder();
numberOfMoves++;
}
if (arrayIsInAscendingOrder)
{
Console.WriteLine($"YOU WON IT IN {numberOfMoves} MOVES!!!");
}
Console.WriteLine();
Console.WriteLine();
Console.Write("TRY AGAIN (YES OR NO) ");
tryAgain = Console.ReadLine();
}
Console.WriteLine();
Console.WriteLine("OK HOPE YOU HAD FUN!!");
}
private static int ReadNextInput()
{
Console.Write("HOW MANY SHALL I REVERSE? ");
var input = ReadIntegerInput();
while (input > 9 || input < 0)
{
if (input > 9)
{
Console.WriteLine($"OOPS! TOO MANY! I CAN REVERSE AT MOST {arrayLength}");
}
if (input < 0)
{
Console.WriteLine($"OOPS! TOO FEW! I CAN REVERSE BETWEEN 1 AND {arrayLength}");
}
Console.Write("HOW MANY SHALL I REVERSE? ");
input = ReadIntegerInput();
}
return input;
}
private static int ReadIntegerInput()
{
var input = Console.ReadLine();
int.TryParse(input, out var index);
return index;
}
private static void PrintList(string list)
{
Console.WriteLine();
Console.WriteLine(list);
Console.WriteLine();
}
private static void PrintTitle()
{
Console.WriteLine("\t\t REVERSE");
Console.WriteLine(" CREATIVE COMPUTING MORRISTON, NEW JERSEY");
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("REVERSE -- A GAME OF SKILL");
Console.WriteLine();
}
private static void DisplayRules()
{
Console.WriteLine();
Console.WriteLine("THIS IS THE GAME OF 'REVERSE'. TO WIN, ALL YOU HAVE");
Console.WriteLine("TO DO IS ARRANGE A LIST OF NUMBERS (1 THOUGH 9 )");
Console.WriteLine("IN NUMERICAL ORDER FROM LEFT TO RIGHT. TO MOVE, YOU");
Console.WriteLine("TELL ME HOW MANY NUMBERS (COUNTING FROM THE LEFT) TO");
Console.WriteLine("REVERSE. FOR EXAMPLE, IF THE CURRENT LIST IS:");
Console.WriteLine();
Console.WriteLine("2 3 4 5 1 6 7 8 9");
Console.WriteLine();
Console.WriteLine("AND YOU REVERSE 4, THE RESULT WILL BE:");
Console.WriteLine();
Console.WriteLine("5 4 3 2 1 6 7 8 9");
Console.WriteLine();
Console.WriteLine("NOW IF YOU REVERSE 5, YOU WIN!");
Console.WriteLine();
Console.WriteLine("1 2 3 4 5 6 7 8 9");
Console.WriteLine();
Console.WriteLine("NO DOUBT YOU WILL LIKE THIS GAME, BUT ");
Console.WriteLine("IF YOU WANT TO QUIT, REVERSE 0 (ZERO)");
Console.WriteLine();
Console.WriteLine();
}
}
}

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,82 @@
using System;
using System.Text;
namespace Reverse
{
public class Reverser
{
protected int[] _array;
public Reverser(int arraySize)
{
_array = CreateRandomArray(arraySize);
}
public void Reverse(int index)
{
if (index > _array.Length)
{
return;
}
for (int i = 0; i < index / 2; i++)
{
int temp = _array[i];
int upperIndex = index - 1 - i;
_array[i] = _array[upperIndex];
_array[upperIndex] = temp;
}
}
public bool IsArrayInAscendingOrder()
{
for (int i = 1; i < _array.Length; i++)
{
if (_array[i] < _array[i - 1])
{
return false;
}
}
return true;
}
private int[] CreateRandomArray(int size)
{
if (size < 1)
{
throw new ArgumentOutOfRangeException(nameof(size), "Array size must be a positive integer");
}
var array = new int[size];
for (int i = 1; i <= size; i++)
{
array[i - 1] = i;
}
var rnd = new Random();
for (int i = size; i > 1;)
{
int k = rnd.Next(i);
--i;
int temp = array[i];
array[i] = array[k];
array[k] = temp;
}
return array;
}
public string GetArrayString()
{
var sb = new StringBuilder();
foreach (int i in _array)
{
sb.Append(" " + i + " ");
}
return sb.ToString();
}
}
}