Created
October 31, 2017 20:59
-
-
Save StefanoFiumara/2d24289fed4414b5ecf90973bbc3442e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace MartinAssignment | |
{ | |
/// <summary> | |
/// Create a collection that implements the following interface | |
/// The constructor should have the following signature: | |
/// NumberCollection(int[] values); | |
/// NumberCollection(int size); | |
/// </summary> | |
public interface INumberCollection | |
{ | |
/// <summary> | |
/// Returns the value at the specified index. | |
/// </summary> | |
int ValueAt(int index); | |
/// <summary> | |
/// Changes the value at the specified index. | |
/// </summary> | |
void SetValueAt(int index, int newValue); | |
/// <summary> | |
/// Returns true if the value exists in the collection. | |
/// </summary> | |
bool Find(int value); | |
/// <summary> | |
/// Returns the index of the first occurrence of the value in the collection. | |
/// </summary> | |
int IndexOf(int value); | |
/// <summary> | |
/// Returns the number of occurrences of the value in the collection. | |
/// </summary> | |
int Count(int value); | |
/// <summary> | |
/// Returns the sum of all the elements in the collection. | |
/// </summary> | |
int Sum(); | |
/// <summary> | |
/// Returns the maximum value in the collection | |
/// </summary> | |
int Max(); | |
/// <summary> | |
/// Returns the minimum value in the collection | |
/// </summary> | |
int Min(); | |
/// <summary> | |
/// Returns true if the elements in the collection are in numerical order, false otherwise. | |
/// </summary> | |
/// <returns></returns> | |
bool IsOrdered(); | |
/// <summary> | |
/// Returns a string representing all the elements in the collection | |
/// </summary> | |
string Display(); | |
/// <summary> | |
/// Returns true if the collection contains any duplicate elements, false otherwise. | |
/// </summary> | |
bool HasDuplicates(); | |
/// <summary> | |
/// returns a new collection with the combined elements of this collection and the given parameter. | |
/// </summary> | |
INumberCollection Combine(INumberCollection other); | |
/// <summary> | |
/// Returns a copy of this collection. | |
/// </summary> | |
INumberCollection Copy(); | |
/// <summary> | |
/// Returns a new collection with the elements reversed. | |
/// </summary> | |
INumberCollection Reverse(); | |
/// <summary> | |
/// Returns a new collection with the elements ordered. | |
/// </summary> | |
INumberCollection Sort(); | |
/// <summary> | |
/// Returns the size of the collection | |
/// </summary> | |
int Length { get; } | |
/// <summary> | |
/// Swaps the positions of elements at the given indexes. | |
/// </summary> | |
void Swap(int i, int j); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace MartinAssignment | |
{ | |
public class NumberCollection : INumberCollection | |
{ | |
private readonly int[] _arrayData; | |
public int Length => _arrayData.Length; | |
public NumberCollection(int[] values) | |
{ | |
_arrayData = new int[values.Length]; | |
for (int i = 0; i < _arrayData.Length; i++) | |
{ | |
_arrayData[i] = values[i]; | |
} | |
} | |
public NumberCollection(int size) | |
{ | |
_arrayData = new int[size]; | |
} | |
public int ValueAt(int index) | |
{ | |
return _arrayData[index]; | |
} | |
public void SetValueAt(int index, int newValue) | |
{ | |
_arrayData[index] = newValue; | |
} | |
public bool Find(int value) | |
{ | |
for (int i = 0; i < _arrayData.Length; i++) | |
{ | |
if (_arrayData[i] == value) return true; | |
} | |
return false; | |
} | |
public int IndexOf(int value) | |
{ | |
for (int i = 0; i < _arrayData.Length; i++) | |
{ | |
if (_arrayData[i] == value) return i; | |
} | |
return -1; | |
} | |
public int Count(int value) | |
{ | |
int amount = 0; | |
for (int i = 0; i < _arrayData.Length; i++) | |
{ | |
if (_arrayData[i] == value) amount++; | |
} | |
return amount; | |
} | |
public int Sum() | |
{ | |
int result = 0; | |
for (int i = 0; i < _arrayData.Length; i++) | |
{ | |
result += i; | |
} | |
return result; | |
} | |
public int Max() | |
{ | |
int result = int.MinValue; | |
for (int i = 0; i < _arrayData.Length; i++) | |
{ | |
if (_arrayData[i] > result) result = _arrayData[i]; | |
} | |
return result; | |
} | |
public int Min() | |
{ | |
int result = int.MaxValue; | |
for (int i = 0; i < _arrayData.Length; i++) | |
{ | |
if (_arrayData[i] < result) result = _arrayData[i]; | |
} | |
return result; | |
} | |
public bool IsOrdered() | |
{ | |
for (int i = 1; i < _arrayData.Length; i++) | |
{ | |
if (_arrayData[i] < _arrayData[i - 1]) return false; | |
} | |
return true; | |
} | |
public string Display() | |
{ | |
string str = ""; | |
for (int i = 0; i < _arrayData.Length; i++) | |
{ | |
str += _arrayData[i] + " "; | |
} | |
return str; | |
} | |
public bool HasDuplicates() | |
{ | |
for (int i = 0; i < _arrayData.Length; i++) | |
{ | |
for (int j = i + 1; j < _arrayData.Length; j++) | |
{ | |
if (_arrayData[i] == _arrayData[j]) | |
{ | |
return true; | |
} | |
} | |
} | |
return false; | |
} | |
public INumberCollection Combine(INumberCollection other) | |
{ | |
var result = new NumberCollection(_arrayData.Length + other.Length); | |
for (int i = 0; i < _arrayData.Length; i++) | |
{ | |
result.SetValueAt(i, _arrayData[i]); | |
} | |
for (int j = 0; j < other.Length; j++) | |
{ | |
result.SetValueAt(j + _arrayData.Length, other.ValueAt(j)); | |
} | |
return result; | |
} | |
public INumberCollection Copy() | |
{ | |
return new NumberCollection(_arrayData); | |
} | |
public INumberCollection Reverse() | |
{ | |
var result = Copy(); | |
int lastIndex = result.Length - 1; | |
for (int i = 0; i < result.Length / 2; i++) | |
{ | |
result.Swap(i, lastIndex); | |
lastIndex--; | |
} | |
return result; | |
} | |
public void Swap(int i, int j) | |
{ | |
int temp = _arrayData[i]; | |
_arrayData[i] = _arrayData[j]; | |
_arrayData[j] = temp; | |
} | |
public INumberCollection Sort() | |
{ | |
var result = new NumberCollection(_arrayData); | |
for (int i = 0; i < result.Length; i++) | |
{ | |
for (int j = i + 1; j < result.Length; j++) | |
{ | |
if (result.ValueAt(i) > result.ValueAt(j)) | |
{ | |
result.Swap(i, j); | |
} | |
} | |
} | |
return result; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment