From a32e48e66a249835165f7c065aa4c09d9f6d8905 Mon Sep 17 00:00:00 2001 From: SajidYarKhan Date: Thu, 30 Jun 2016 15:41:19 -0500 Subject: [PATCH] Comitting changes for Sajid Yar Khan --- problem-solving/Program.cs | 115 ++++++++++++++++++++++++++++++------- 1 file changed, 93 insertions(+), 22 deletions(-) diff --git a/problem-solving/Program.cs b/problem-solving/Program.cs index 15e54d4..7079e8a 100644 --- a/problem-solving/Program.cs +++ b/problem-solving/Program.cs @@ -1,6 +1,9 @@ using System; using System.Collections.Generic; +using System.Diagnostics; +using System.Diagnostics.Eventing.Reader; using System.Linq; +using System.Runtime.Remoting.Messaging; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; @@ -11,54 +14,86 @@ public class Program { static void Main(string[] args) { + var listA = new List(new[] { 1, 1, 2, 3, 3, 4, 5, 5, 6, 7, 7, 8, 9, 10 }); + var listB = new List(new[] { 1,3, 4, 6, 8, 9, 10 }); + var nPlayerBlackJack = new List(new[] {20,23,25,15,14,7,11}); + const string doubleString = "PEACE"; + const string countChar = "PEEAACE"; + const string c = "A"; + const string sumDigits = "P1E22A33C4E55"; + const int factorial = 9; + + Console.WriteLine("Sum of All Values: "+SumArray(listA)); + Console.WriteLine("Sum of Odd Values: " + SumArrayOddValues(listA)); + Console.WriteLine("Sum of every second value: " + SumArrayEverySecondValue(listA)); + Console.WriteLine("List of unique values"); + foreach (var uniqueList in GetUniqueValues(listA)) + Console.WriteLine(" " + Convert.ToString(uniqueList)); + Console.WriteLine("List of intersect values"); + foreach (var uniqueList in GetArrayIntersect(listA, listB)) + Console.WriteLine(" " + Convert.ToString(uniqueList)); + Console.WriteLine("List of non intersect values"); + foreach (var uniqueList in GetArrayNotIntersect(listA, listB)) + Console.WriteLine(" " + Convert.ToString(uniqueList)); + Console.WriteLine("Has Sum : " + (HasSum(listA,5) ? "Yes" : "No")); + Console.WriteLine("Lone Sum: " + LoneSum(listA)); + Console.WriteLine("Double String: "+ DoubleString(doubleString)); + Console.WriteLine("Count Chars: " + CountChars(countChar,Convert.ToChar(c))); + Console.WriteLine("Sum Digits: " + SumDigits(sumDigits)); + Console.WriteLine("Sum Numbers: " + SumNumbers(sumDigits)); + Console.WriteLine("Is Anagram : " + (IsAnagram("care","erac") ? "Yes" : "No")); + Console.WriteLine("BlackJack value: " + BlackJack(23,20)); + Console.WriteLine("Five Player BlackJack: " + FivePlayerBlackJack(10,11,15,30,1)); + Console.WriteLine("N Player BlackJack: " + NPlayerBlackJack(nPlayerBlackJack)); + Console.WriteLine("Factorial of {0} is : " + Factorial(factorial), factorial); + Console.ReadLine(); } public static long SumArray(IEnumerable arr) { // return the sum of all the values in the array - // TODO - return 0; + return arr.Sum(); } public static long SumArrayOddValues(IEnumerable arr) { // return the sum of all the values in the array that are odd - // TODO - return 0; + return arr.Where(i=> i%2 != 0).Sum(); } public static long SumArrayEverySecondValue(IEnumerable 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.Skip(1).Where((c, i) => i%2 == 0).Sum(); + } public static IEnumerable GetUniqueValues(IEnumerable arr) { // return an array that contains only unique values from the passed in array - // TODO - return null; + return arr.Distinct(); + } public static IEnumerable GetArrayIntersect(IEnumerable arrA, IEnumerable 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 GetArrayNotIntersect(IEnumerable arrA, IEnumerable 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); } public static Boolean HasSum(IEnumerable arr, long target) { // return true if any 2 values in the array have a sum equal to the target value - // TODO + foreach (var i in arr) + { + + } return false; } @@ -66,27 +101,36 @@ public static long LoneSum(IEnumerable 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; + var sum = 0; + var previousValue = 0; + foreach (var i in arr) + { + if (i != previousValue) + sum += i; + + previousValue = i; + } + 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; + return s.Aggregate(string.Empty, (current, c) => current + (Convert.ToString(c) + Convert.ToString(c))); } 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(x => x == 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(c => c > '0' && c <= '9').Sum(c => (int) Char.GetNumericValue(c)); } public static long SumNumbers(String s) @@ -94,6 +138,8 @@ 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 + var a = s.Where(c => c > '0' && c <= '9'); + return 0; } @@ -103,7 +149,7 @@ public static Boolean IsAnagram(String s1, String s2) // 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 string.Concat(s1.Reverse()).Equals(s2); } public static int BlackJack(int count1, int count2) @@ -111,7 +157,7 @@ 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 (count1 > 21) && (count2 > 21) ? 0 : ((21 - count1) > (21 - count2) ? count1 : count2); } public static int FivePlayerBlackJack(int count1, int count2, int count3, int count4, int count5) @@ -119,7 +165,18 @@ public static int FivePlayerBlackJack(int count1, int count2, int count3, int co // 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; + if ((count1 > 21) && (count2 > 21) && (count3 > 21) && (count4 > 21) && (count5 > 21)) + return 0; + + var dictionary = new Dictionary(); + + if (count1 < 21) dictionary.Add(Convert.ToString(count1), (21 - count1)); + if (count2 < 21) dictionary.Add(Convert.ToString(count2), (21 - count2)); + if (count3 < 21) dictionary.Add(Convert.ToString(count3), (21 - count3)); + if (count4 < 21) dictionary.Add(Convert.ToString(count4), (21 - count4)); + if (count5 < 21) dictionary.Add(Convert.ToString(count5), (21 - count5)); + + return Convert.ToInt32(dictionary.Max(k => k.Key)); } public static int NPlayerBlackJack(IEnumerable counts) @@ -127,7 +184,14 @@ public static int NPlayerBlackJack(IEnumerable 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; + var c = counts.Count(n => n > 21); + + if (c == counts.Count()) + return 0; + + var dictionary = counts.Where(count => count < 21).ToDictionary(count => Convert.ToString(count)); + + return Convert.ToInt32(dictionary.Max(a => a.Value)); } public static Dictionary WordCount(IEnumerable arr) @@ -140,7 +204,14 @@ public static Dictionary WordCount(IEnumerable arr) public static int Factorial(int n) { // Given n, return the factorial of n, which is n * (n-1) * (n-2) ... 1 - return 0; + var factorial = 1; + + for (var counter = 1; counter <= n; counter++) + { + factorial = factorial*counter; + } + + return factorial; } public static List FB(int n)