Skip to content

Instantly share code, notes, and snippets.

@StefanoFiumara
Last active October 31, 2017 02:31
Show Gist options
  • Save StefanoFiumara/05cec1362ea78f4b0b5124775d9103f3 to your computer and use it in GitHub Desktop.
Save StefanoFiumara/05cec1362ea78f4b0b5124775d9103f3 to your computer and use it in GitHub Desktop.
public class Arrays
{
public bool Find(int[] list, int value)
{
for (int i = 0; i < list.Length; i++)
{
if (list[i] == value) return true;
}
return false;
}
public int FindIndex(int[] list, int value)
{
for (int i = 0; i < list.Length; i++)
{
if (list[i] == value) return i;
}
return -1;
}
public int Count(int[] list, int value)
{
int amount = 0;
for (int i = 0; i < list.Length; i++)
{
if (list[i] == value) amount++;
}
return amount;
}
public int Sum(int[] list)
{
int result = 0;
for (int i = 0; i < list.Length; i++)
{
result += i;
}
return result;
}
public int Max(int[] list)
{
int result = int.MinValue;
for (int i = 0; i < list.Length; i++)
{
if (list[i] > result) result = list[i];
}
return result;
}
public int Min(int[] list)
{
int result = int.MaxValue;
for (int i = 0; i < list.Length; i++)
{
if (list[i] < result) result = list[i];
}
return result;
}
public bool IsOrdered(int[] list)
{
for (int i = 1; i < list.Length; i++)
{
if (list[i] < list[i - 1]) return false;
}
return true;
}
public string Display(int[] list)
{
string str = "";
for (int i = 0; i < list.Length; i++)
{
str += list[i] + " ";
}
return str;
}
public void Swap(int[] list, int i, int j)
{
int temp = list[i];
list[i] = list[j];
list[j] = temp;
}
public bool HasDuplicates(int[] list)
{
for (int i = 0; i < list.Length; i++)
{
for (int j = i+1; j < list.Length; j++)
{
if (list[i] == list[j])
{
return true;
}
}
}
return false;
}
public void Sort(int[] list)
{
for (int i = 0; i < list.Length; i++)
{
for (int j = i + 1; j < list.Length; j++)
{
if (list[i] > list[j])
{
Swap(list, i, j);
}
}
}
}
public int[] Combine(int[] list1, int[] list2)
{
int[] result = new int[list1.Length + list2.Length];
for (int i = 0; i < list1.Length; i++)
{
result[i] = list1[i];
}
for (int j = 0; j < list2.Length; j++)
{
result[j + list1.Length] = list2[j];
}
return result;
}
public void Reverse(int[] list)
{
int lastIndex = list.Length - 1;
for (int i = 0; i < list.Length/2; i++)
{
Swap(list, i, lastIndex);
lastIndex--;
}
}
}
[TestClass]
public class ArraysTest
{
private Arrays A { get; } = new Arrays();
[TestMethod]
public void SwapTest()
{
int[] list = new[] {1, 2, 3, 4, 5};
A.Swap(list, 0, 4);
CollectionAssert.AreEqual(new[]{5,2,3,4,1}, list);
}
[TestMethod]
public void DuplicateTest()
{
int[] list = new[] {1, 2, 3, 4, 5};
int[] listWithDuplicates = new[] {1, 0, 2, 3, 1};
Assert.IsTrue(A.HasDuplicates(listWithDuplicates));
Assert.IsFalse(A.HasDuplicates(list));
}
[TestMethod]
public void SortTest()
{
int[] list = new[] {1, 4, 5, 3, 2};
int[] sortedList = new[] {1, 2, 3, 4, 5};
A.Sort(list);
CollectionAssert.AreEqual(list, sortedList);
}
[TestMethod]
public void CombineTest()
{
int[] list1 = new[] {1, 2, 3, 4};
int[] list2 = new[] {5, 6, 7, 8};
CollectionAssert.AreEqual(A.Combine(list1, list2), new[] {1,2,3,4,5,6,7,8});
}
[TestMethod]
public void ReverseTest()
{
int[] list = new[] {1, 2, 3, 4, 5};
int[] reverseList = new[] {5, 4, 3, 2, 1};
A.Reverse(list);
CollectionAssert.AreEqual(list, reverseList);
}
[TestMethod]
public void CountTest()
{
int[] list = new[] {1, 1, 1, 2, 3, 4, 5, 5, 5, 5, 5, 5, 5};
Assert.IsTrue(A.Count(list, 1) == 3);
Assert.IsTrue(A.Count(list, 5) == 7);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment