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
171 changes: 109 additions & 62 deletions problem-solving/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,141 +15,188 @@ static void Main(string[] args)

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

public static long SumArrayOddValues(IEnumerable<int> arr)
{
// return the sum of all the values in the array that are odd
// TODO
return 0;
return arr.Where(number => number % 2 != 0).Select(n => (long)n).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;
return arr.Where((number, ix) => ix % 2 != 0).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;
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;
return arrA.Distinct().Where(number => arrB.Contains(number));
}

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;
return arrA.Except(arrB).Concat(arrB.Except(arrA));
}

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
var set = new HashSet<int>();
foreach (int number in arr)
{
if(set.Contains((int)target-number))
{
return true;
}
set.Add(number);
}
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;
if (arr == null) throw new ArgumentNullException();

var addedNums = new HashSet<int>();
var duplicates = new HashSet<int>();
int sum = 0;
foreach (int number in arr)
{
if(!addedNums.Contains(number))
{
sum += number;
addedNums.Add(number);
}
else if (!duplicates.Contains(number))
{
duplicates.Add(number);
sum -= number;
}
}
return 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;
if (s == null) throw new ArgumentNullException();

char[] charArray = new char[s.Length*2];
int ix = 0;
foreach (char c in s)
{
charArray[ix++] = c;
charArray[ix++] = c;
}
return new String(charArray);
}

public static int CountChars(String s, char c)
{
// return the count of how many times char c occurs in string s
return 0;
return s.Count(charInString => charInString == c);
}

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;
return s.Where(charInString => charInString > '0' && charInString <= '9')
.Select(charVal => charVal - '0')
.Sum();
}

public static long SumNumbers(String s)
{
// return the sum of the numbers that appear in the string, ignoring all other characters
// a number is a series of 1 or more digits in a row
// e.g. "11 22" returns 33
return 0;
String[] numbers = Regex.Split(s, "[^.0-9]");
return numbers.Where(stringVal => stringVal != "")
.Select(stringVal => Int32.Parse(stringVal))
.Sum();
}

public static Boolean IsAnagram(String s1, String s2)
{
// return true if String s1 is an anagram of s2, otherwise return false
// An anagram is produced by rearranging the letters of one string into another
// e.g. care is an anagram of race
// cat is not an anagram of rat
return false;
if (s1.Length != s2.Length) return false;
foreach(char s1Char in s1)
{
int matchingIx = s2.IndexOf(s1Char);
if (matchingIx < 0)
{
return false;
}
else
{
s2 = s2.Remove(matchingIx, 1);
}
}
return true;
}

public static int BlackJack(int count1, int count2)
{
// Given 2 integer values greater than 0,
// return whichever value is nearest to 21 without going over.
// Return 0 if they both go over.
return 0;
var counts = new List<int>();
counts.Add(count1);
counts.Add(count2);
return NPlayerBlackJack(counts);
}

public static int FivePlayerBlackJack(int count1, int count2, int count3, int count4, int count5)
{
// Given 5 integer values greater than 0,
// return whichever value is nearest to 21 without going over.
// Return 0 if they all go over.
return 0;
var counts = new List<int>();
counts.Add(count1);
counts.Add(count2);
counts.Add(count3);
counts.Add(count4);
counts.Add(count5);
return NPlayerBlackJack(counts);
}

public static int NPlayerBlackJack(IEnumerable<int> 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.
return 0;
IEnumerable<int> validHands = counts.Where(val => val <= 21 && val >= 0);

return validHands.Count() > 0 ? validHands.Max() : 0;
}

public static Dictionary<String, int> WordCount(IEnumerable<String> 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;
var dictionary = new Dictionary<String, int>();
foreach(String word in arr)
{
int currentCount;
dictionary.TryGetValue(word, out currentCount);
dictionary[word] = currentCount + 1;
}
return dictionary;
}

public static int Factorial(int n)
{
// Given n, return the factorial of n, which is n * (n-1) * (n-2) ... 1
return 0;
if (n <= 0) return 1;
return n * Factorial(n - 1);
}

public static List<String> FB(int n)
{
// Given n, print the numbers from 1 to n as a string to a List of strings, with the following exceptions:
// If the number is divisable by 3, replace it with the word "Fizz"
// If the number is divisable by 5, replace it with the word "Buzz"
// If the number is divisable by both 3 and 5, replace it with the word "FizzBuzz"
return null;
var output = new List<String>();
for (int i = 1; i <= n; i++)
{
String thisString = "";
if (i % 3 == 0)
{
thisString += "Fizz";
}
if (i % 5 == 0)
{
thisString += "Buzz";
}
if (thisString.Length == 0)
{
thisString = i.ToString();
}
output.Add(thisString);
}
return output;
}
}
}