mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2026-07-11 22:51:35 -07:00
Add method for checking if a given array is in ascending order.
This commit is contained in:
@@ -48,5 +48,23 @@ namespace Reverse.Tests
|
||||
|
||||
Assert.True(input.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 result = Reverser.IsArrayInAscendingOrder(input);
|
||||
|
||||
Assert.True(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsArrayInOrder_WhenArrayElementsAreNotInNumericAscendingOrder_ReturnsFalse()
|
||||
{
|
||||
var result = Reverser.IsArrayInAscendingOrder(new int[] { 2, 1 });
|
||||
|
||||
Assert.False(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,5 +17,18 @@
|
||||
arrayToReverse[upperIndex] = temp;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool IsArrayInAscendingOrder(int[] array)
|
||||
{
|
||||
for (int i = 1; i < array.Length; i++)
|
||||
{
|
||||
if (array[i] < array[i - 1])
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user