Skip to content
Open
Show file tree
Hide file tree
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
36 changes: 36 additions & 0 deletions problem-solving/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace problem_solving
{
public static class Extensions
{
public static bool isOdd(this int i)
{
return i % 2 != 0;
}

public static Dictionary<T, int> makeCountDictionary<T>(this IEnumerable<T> source)
{
if (source == null)
throw new NullReferenceException(); // Boo

return source
.GroupBy(item => item)
.ToDictionary(entry => entry.Key, entry => entry.Count());
}

public static String toFizzBuzz(this int i)
{
String replacement = "";
if (i % 3 == 0)
replacement = "Fizz";
if (i % 5 == 0)
replacement += "Buzz";
return String.IsNullOrEmpty(replacement) ? i.ToString() : replacement;
}
}
}
98 changes: 36 additions & 62 deletions problem-solving/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,141 +15,115 @@ 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.Sum(i => (long)i);
}

public static long SumArrayOddValues(IEnumerable<int> arr)
{
// return the sum of all the values in the array that are odd
// TODO
return 0;
return SumArray(arr.Where(i => i.isOdd()));
}

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 SumArray(arr.Where((i, index) => index.isOdd()));
}

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.Intersect(arrB);
}

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
.Union(arrB)
.Except(GetArrayIntersect(arrA, arrB));
}

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
HashSet<long> seen = new HashSet<long>();

foreach (var currValue in arr)
{
if (seen.Contains(target - currValue))
return true;

seen.Add(currValue);
}

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(); // Boo
return SumArray(arr.makeCountDictionary().Where(entry => entry.Value == 1).Select(entry => entry.Key));
// return SumArray(arr.Where(i => arr.Count(v => v == i) == 1));
}

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;
return String.Concat(s.Select(c => new String(c, 2)));
}

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(k => k == 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 SumArray(s.Where(c => Char.IsDigit(c)).Select(c => (int)Char.GetNumericValue(c)));
}

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;
return SumArray(Regex.Matches(s, "[0-9]+").Cast<Match>().Select(m => int.Parse(m.Value)));
}

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;
return s1.makeCountDictionary().OrderBy(c => c.Key).SequenceEqual(s2.makeCountDictionary().OrderBy(c => c.Key));
}

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;
return NPlayerBlackJack(new int[] { count1, count2 });
//return count1 > 21
// ? count2 > 21 ? 0 : count2
// : count2 > 21 ? count1 : Math.Max(count1, count2);
}

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;
return NPlayerBlackJack(new int[] { count1, count2, count3, count4, count5 });
}

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;
return counts.Concat(new int[] {0}).Where(c => c <= 21).Max();
}

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;
return arr.makeCountDictionary();
}

public static int Factorial(int n)
{
// Given n, return the factorial of n, which is n * (n-1) * (n-2) ... 1
return 0;
return n > 1 ? (n * Factorial(n - 1)) : 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;
return Enumerable.Range(1, n).Select(i => i.toFizzBuzz()).ToList();
}
}
}
4 changes: 4 additions & 0 deletions problem-solving/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="NUnitTestAdapter.WithFramework" version="2.0.0" targetFramework="net45" />
</packages>
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading