Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 90 additions & 18 deletions problem-solving/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,80 +13,150 @@ static void Main(string[] args)
{
}

public static void NullCheck(Object arr)
{
if (arr == null)
throw new ArgumentNullException("arr");
}

public static long SumArray(IEnumerable<int> arr)
{
// return the sum of all the values in the array
// TODO
return 0;

NullCheck(arr);

long sum = 0;
foreach (var item in arr)
{
sum += item;
}
return sum;


//return arr.Sum;
}

public static long SumArrayOddValues(IEnumerable<int> arr)
{
// return the sum of all the values in the array that are odd
// TODO
return 0;
NullCheck(arr);
long sum = 0;
foreach (var item in arr)
{
if (item % 2 == 1)
{
sum += item;
}
}
return sum;
}

public static long SumArrayEverySecondValue(IEnumerable<int> arr)
{
// return the sum of every second value in the array. i.e. the 2nd value + the 4th value + the 6th value ...
// TODO
return 0;
NullCheck(arr);
long sum = 0;
for (int i = 1; i < arr.Count(); i += 2)
{
sum += arr.ToArray()[i];
}
return sum;
}

public static IEnumerable<int> GetUniqueValues(IEnumerable<int> arr)
{
// return an array that contains only unique values from the passed in array
// TODO
return null;
NullCheck(arr);


return arr.Distinct();
}

public static IEnumerable<int> GetArrayIntersect(IEnumerable<int> arrA, IEnumerable<int> arrB)
{
// return an array that contains all the values that are in array A and array B
// TODO
return null;
NullCheck(arrA);
NullCheck(arrB);

return arrA.Intersect(arrB);
}

class NotInteresectCompare : IEqualityComparer<int>
{
public bool Equals(int x, int y)
{
return x != y;
}
public int GetHashCode(int codeh)
{
return codeh.GetHashCode();
}
}

public static IEnumerable<int> GetArrayNotIntersect(IEnumerable<int> arrA, IEnumerable<int> arrB)
{
// return an array that contains all the values that are in array A or array B but not in both array A and array B
// TODO
return null;
NullCheck(arrA);
NullCheck(arrB);

return arrA.Intersect(arrB, new NotInteresectCompare());
}

public static Boolean HasSum(IEnumerable<int> arr, long target)
{
// return true if any 2 values in the array have a sum equal to the target value
// TODO
return false;


return false;
}

public static long LoneSum(IEnumerable<int> arr)
{
// Given an array of int values, return their sum.
// However, if any of the values is the same as another of the values, it does not count towards the sum.
return 0;
NullCheck(arr);
return arr.Distinct().Sum();
}

public static String DoubleString(String s)
{
// return a string that is the original string with each character in the string repeated twice
// e.g. for input "ABCDE", return "AABBCCDDEE"
return null;
NullCheck(s);
string output = "";
foreach (var item in s)
{
output += item.ToString() + item.ToString();
}
return output;
}

public static int CountChars(String s, char c)
{

// return the count of how many times char c occurs in string s
return 0;
int output = 0;
foreach (char item in s)
{
if (item == c)
output++;
}
return output;
}

public static long SumDigits(String s)
{
// return the sum of the digits 0-9 that appear in the string, ignoring all other characters
// e.g. "123" return 6
return 0;
long output = 0;
foreach (char item in s)
{
if (char.IsNumber(item))
{
output += int.Parse(item.ToString());
}
}
return output;
}

public static long SumNumbers(String s)
Expand Down Expand Up @@ -124,6 +194,7 @@ public static int FivePlayerBlackJack(int count1, int count2, int count3, int co

public static int NPlayerBlackJack(IEnumerable<int> counts)
{
NullCheck(counts);
// Given a list of integer values greater than 0,
// return whichever value is nearest to 21 without going over.
// Return 0 if they all go over.
Expand All @@ -132,6 +203,7 @@ public static int NPlayerBlackJack(IEnumerable<int> counts)

public static Dictionary<String, int> WordCount(IEnumerable<String> arr)
{
NullCheck(arr);
// Given an array of Strings,
// return a dictionary keyed on the string with the count of how many times each string appears in the array
return null;
Expand Down