using System; using System.Collections.Generic; using System.IO; using System.Text; namespace YeggePhoneScreen { public class Question1 { public static string Answer(string input) { char[] tempArray = input.ToCharArray(); for (int i = 0; i < tempArray.Length / 2; i++) { //swap the characters in place with dumb XOR trick tempArray[i] = (char)(tempArray[i] ^ tempArray[tempArray.Length - 1 - i]); tempArray[tempArray.Length - 1 - i] = (char)(tempArray[i] ^ tempArray[tempArray.Length - 1 - i]); tempArray[i] = (char)(tempArray[i] ^ tempArray[tempArray.Length - 1 - i]); } return new string(tempArray); } } public class Question2 { public static uint RecursiveAnswer(uint n) { //Lame, but a classic. Really slow for non-tiny values of n; if (n == 0) return 0; if (n == 1) return 1; return RecursiveAnswer(n - 1) + RecursiveAnswer(n - 2); } private static readonly Dictionary MemoCache = new Dictionary(); public static uint RecursiveAnswerWithMemoization(uint n) { //A lot more time efficient. Avoids recalculating the same value more than once. if (n == 0) return 0; if (n == 1) return 1; uint retval; bool lookupSuccess = MemoCache.TryGetValue(n, out retval); if (!lookupSuccess) { retval = RecursiveAnswerWithMemoization(n - 1) + RecursiveAnswerWithMemoization(n - 2); MemoCache.Add(n, retval); } return retval; } public static uint IterativeAnser(uint n) { //more efficient than the recursive answers, except in the case of RecursiveAnswerWithMemoization when //called repeatedly. To support that case, memoization could be added here too. if (n == 0) return 0; if (n == 1) return 1; uint nMinus2 = 0; uint nMinus1 = 1; for (int i = 2; i < n; i++) { uint nextNumber = nMinus1 + nMinus2; nMinus2 = nMinus1; nMinus1 = nextNumber; } return nMinus1 + nMinus2; } } public class Question3 { public static string LameAnswer() { //returning a constant string seems to violate the spirit of the question, but //but not the letter of the requirements return " 1 2 3 4 5 6 7 8 9 10 11 12" + Environment.NewLine + " 2 4 6 8 10 12 14 16 18 20 22 24" + Environment.NewLine + " 3 6 9 12 15 18 21 24 27 30 33 36" + Environment.NewLine + " 4 8 12 16 20 24 28 32 36 40 44 48" + Environment.NewLine + " 5 10 15 20 25 30 35 40 45 50 55 60" + Environment.NewLine + " 6 12 18 24 30 36 42 48 54 60 66 72" + Environment.NewLine + " 7 14 21 28 35 42 49 56 63 70 77 84" + Environment.NewLine + " 8 16 24 32 40 48 56 64 72 80 88 96" + Environment.NewLine + " 9 18 27 36 45 54 63 72 81 90 99 108" + Environment.NewLine + " 10 20 30 40 50 60 70 80 90 100 110 120" + Environment.NewLine + " 11 22 33 44 55 66 77 88 99 110 121 132" + Environment.NewLine + " 12 24 36 48 60 72 84 96 108 120 132 144" + Environment.NewLine; } public static string GeneralAnswer(uint n) { //this seems more in keeping with the spirit of the question. Works for any sized table. //will generate the same text as the lame answer for GeneralAnser(12); StringBuilder retval = new StringBuilder(); int spacing = (n * n).ToString().Length + 1; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { retval.Append((i * j).ToString().PadLeft(spacing)); } retval.Append(Environment.NewLine); } return retval.ToString(); } } public class Question4 { public static int Answer(string filePath) { //consider adding a check for overflow using (StreamReader sr = File.OpenText(filePath)) { int retval = 0; while (true) { string line = sr.ReadLine(); if (line != null) retval += int.Parse(line); else break; } return retval; } } } public class Question5 { public static string LameAnswer() { //Like question 3, seems to meet the letter of the requirements, but violates the spirit return "1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, " + "31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, " + "61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, " + "91, 93, 95, 97, 99"; } public static string GeneralAnswer(int start, int end) { //More general answer, will generate text equal to the lame answer for GeneralAnswer(1, 99); List retval = new List(); for (int i = start % 2 != 0 ? start : start + 1; i <= end; i += 2) { retval.Add(i.ToString()); } return string.Join(", ", retval.ToArray()); } } public class Question6 { public static int Answer(int[] input) { //simple linear search of the input array int retval = int.MinValue; foreach (int item in input) { if (item > retval) retval = item; } return retval; } } public class Question7 { public static string Answer(byte r, byte g, byte b) { //use format to turn the three aguments into two character hex strings return string.Format("{0:X2}{1:X2}{2:X2}", r, g, b); } } }