View difference between Paste ID: f68bdaade and
SHOW:
|
|
- or go back to the newest paste.
1 | - | |
1 | + | using System; |
2 | using System.Collections.Generic; | |
3 | using System.IO; | |
4 | using System.Text; | |
5 | ||
6 | namespace YeggePhoneScreen | |
7 | { | |
8 | public class Question1 | |
9 | { | |
10 | public static string Answer(string input) | |
11 | { | |
12 | char[] tempArray = input.ToCharArray(); | |
13 | for (int i = 0; i < tempArray.Length / 2; i++) | |
14 | { | |
15 | //swap the characters in place with dumb XOR trick | |
16 | tempArray[i] = (char)(tempArray[i] ^ tempArray[tempArray.Length - 1 - i]); | |
17 | tempArray[tempArray.Length - 1 - i] = (char)(tempArray[i] ^ tempArray[tempArray.Length - 1 - i]); | |
18 | tempArray[i] = (char)(tempArray[i] ^ tempArray[tempArray.Length - 1 - i]); | |
19 | } | |
20 | return new string(tempArray); | |
21 | } | |
22 | } | |
23 | ||
24 | public class Question2 | |
25 | { | |
26 | public static uint RecursiveAnswer(uint n) | |
27 | { | |
28 | //Lame, but a classic. Really slow for non-tiny values of n; | |
29 | if (n == 0) | |
30 | return 0; | |
31 | if (n == 1) | |
32 | return 1; | |
33 | return RecursiveAnswer(n - 1) + RecursiveAnswer(n - 2); | |
34 | } | |
35 | ||
36 | private static readonly Dictionary<uint, uint> MemoCache = new Dictionary<uint, uint>(); | |
37 | public static uint RecursiveAnswerWithMemoization(uint n) | |
38 | { | |
39 | //A lot more time efficient. Avoids recalculating the same value more than once. | |
40 | if (n == 0) | |
41 | return 0; | |
42 | if (n == 1) | |
43 | return 1; | |
44 | ||
45 | uint retval; | |
46 | bool lookupSuccess = MemoCache.TryGetValue(n, out retval); | |
47 | if (!lookupSuccess) | |
48 | { | |
49 | retval = RecursiveAnswerWithMemoization(n - 1) + RecursiveAnswerWithMemoization(n - 2); | |
50 | MemoCache.Add(n, retval); | |
51 | } | |
52 | return retval; | |
53 | } | |
54 | ||
55 | public static uint IterativeAnser(uint n) | |
56 | { | |
57 | //more efficient than the recursive answers, except in the case of RecursiveAnswerWithMemoization when | |
58 | //called repeatedly. To support that case, memoization could be added here too. | |
59 | if (n == 0) | |
60 | return 0; | |
61 | if (n == 1) | |
62 | return 1; | |
63 | ||
64 | uint nMinus2 = 0; | |
65 | uint nMinus1 = 1; | |
66 | for (int i = 2; i < n; i++) | |
67 | { | |
68 | uint nextNumber = nMinus1 + nMinus2; | |
69 | nMinus2 = nMinus1; | |
70 | nMinus1 = nextNumber; | |
71 | } | |
72 | ||
73 | return nMinus1 + nMinus2; | |
74 | } | |
75 | } | |
76 | ||
77 | public class Question3 | |
78 | { | |
79 | public static string LameAnswer() | |
80 | { | |
81 | //returning a constant string seems to violate the spirit of the question, but | |
82 | //but not the letter of the requirements | |
83 | return " 1 2 3 4 5 6 7 8 9 10 11 12" + Environment.NewLine + | |
84 | " 2 4 6 8 10 12 14 16 18 20 22 24" + Environment.NewLine + | |
85 | " 3 6 9 12 15 18 21 24 27 30 33 36" + Environment.NewLine + | |
86 | " 4 8 12 16 20 24 28 32 36 40 44 48" + Environment.NewLine + | |
87 | " 5 10 15 20 25 30 35 40 45 50 55 60" + Environment.NewLine + | |
88 | " 6 12 18 24 30 36 42 48 54 60 66 72" + Environment.NewLine + | |
89 | " 7 14 21 28 35 42 49 56 63 70 77 84" + Environment.NewLine + | |
90 | " 8 16 24 32 40 48 56 64 72 80 88 96" + Environment.NewLine + | |
91 | " 9 18 27 36 45 54 63 72 81 90 99 108" + Environment.NewLine + | |
92 | " 10 20 30 40 50 60 70 80 90 100 110 120" + Environment.NewLine + | |
93 | " 11 22 33 44 55 66 77 88 99 110 121 132" + Environment.NewLine + | |
94 | " 12 24 36 48 60 72 84 96 108 120 132 144" + Environment.NewLine; | |
95 | } | |
96 | ||
97 | public static string GeneralAnswer(uint n) | |
98 | { | |
99 | //this seems more in keeping with the spirit of the question. Works for any sized table. | |
100 | //will generate the same text as the lame answer for GeneralAnser(12); | |
101 | StringBuilder retval = new StringBuilder(); | |
102 | int spacing = (n * n).ToString().Length + 1; | |
103 | for (int i = 1; i <= n; i++) | |
104 | { | |
105 | for (int j = 1; j <= n; j++) | |
106 | { | |
107 | retval.Append((i * j).ToString().PadLeft(spacing)); | |
108 | } | |
109 | retval.Append(Environment.NewLine); | |
110 | } | |
111 | return retval.ToString(); | |
112 | } | |
113 | } | |
114 | ||
115 | public class Question4 | |
116 | { | |
117 | public static int Answer(string filePath) | |
118 | { | |
119 | //consider adding a check for overflow | |
120 | using (StreamReader sr = File.OpenText(filePath)) | |
121 | { | |
122 | int retval = 0; | |
123 | while (true) | |
124 | { | |
125 | string line = sr.ReadLine(); | |
126 | if (line != null) | |
127 | retval += int.Parse(line); | |
128 | else | |
129 | break; | |
130 | } | |
131 | return retval; | |
132 | } | |
133 | } | |
134 | } | |
135 | ||
136 | public class Question5 | |
137 | { | |
138 | public static string LameAnswer() | |
139 | { | |
140 | //Like question 3, seems to meet the letter of the requirements, but violates the spirit | |
141 | return "1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, " + | |
142 | "31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, " + | |
143 | "61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, " + | |
144 | "91, 93, 95, 97, 99"; | |
145 | } | |
146 | ||
147 | public static string GeneralAnswer(int start, int end) | |
148 | { | |
149 | //More general answer, will generate text equal to the lame answer for GeneralAnswer(1, 99); | |
150 | List<string> retval = new List<string>(); | |
151 | for (int i = start % 2 != 0 ? start : start + 1; i <= end; i += 2) | |
152 | { | |
153 | retval.Add(i.ToString()); | |
154 | } | |
155 | return string.Join(", ", retval.ToArray()); | |
156 | } | |
157 | } | |
158 | ||
159 | public class Question6 | |
160 | { | |
161 | public static int Answer(int[] input) | |
162 | { | |
163 | //simple linear search of the input array | |
164 | int retval = int.MinValue; | |
165 | foreach (int item in input) | |
166 | { | |
167 | if (item > retval) | |
168 | retval = item; | |
169 | } | |
170 | return retval; | |
171 | } | |
172 | } | |
173 | ||
174 | public class Question7 | |
175 | { | |
176 | public static string Answer(byte r, byte g, byte b) | |
177 | { | |
178 | //use format to turn the three aguments into two character hex strings | |
179 | return string.Format("{0:X2}{1:X2}{2:X2}", r, g, b); | |
180 | } | |
181 | } | |
182 | } |