|
1 | 1 | using System; |
2 | 2 | using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Reflection.Metadata.Ecma335; |
3 | 5 |
|
4 | 6 | namespace ChallengesWithTestsMark8 |
5 | 7 | { |
6 | 8 | public class ChallengesSet02 |
7 | 9 | { |
8 | 10 | public bool CharacterIsALetter(char c) |
9 | 11 | { |
10 | | - throw new NotImplementedException(); |
| 12 | + return (char.IsLetter(c)); |
| 13 | + |
| 14 | + //throw new NotImplementedException(); |
11 | 15 | } |
12 | 16 |
|
13 | 17 | public bool CountOfElementsIsEven(string[] vals) |
14 | 18 | { |
15 | | - throw new NotImplementedException(); |
| 19 | + return (vals.Length % 2 == 0); |
| 20 | + |
| 21 | + //throw new NotImplementedException(); |
16 | 22 | } |
17 | 23 |
|
18 | 24 | public bool IsNumberEven(int number) |
19 | 25 | { |
20 | | - throw new NotImplementedException(); |
| 26 | + return (number % 2 == 0); |
| 27 | + |
| 28 | + //throw new NotImplementedException(); |
21 | 29 | } |
22 | 30 |
|
23 | 31 | public bool IsNumberOdd(int num) |
24 | 32 | { |
25 | | - throw new NotImplementedException(); |
| 33 | + return (num % 2 != 0); |
| 34 | + //throw new NotImplementedException(); |
26 | 35 | } |
27 | 36 |
|
28 | 37 | public double SumOfMinAndMax(IEnumerable<double> numbers) |
29 | 38 | { |
30 | | - throw new NotImplementedException(); |
| 39 | + // I had to look up a function to use with IEnumerable. I'd like to learn what this type is. |
| 40 | + double min = double.MaxValue; |
| 41 | + double max = double.MinValue; |
| 42 | + |
| 43 | + if (numbers == null || numbers.Any() == false) |
| 44 | + { |
| 45 | + return 0; |
| 46 | + } |
| 47 | + else |
| 48 | + { |
| 49 | + |
| 50 | + foreach (var number in numbers) |
| 51 | + { |
| 52 | + if (number < min) |
| 53 | + { |
| 54 | + min = number; |
| 55 | + } |
| 56 | + if (number > max) |
| 57 | + { |
| 58 | + max = number; |
| 59 | + } |
| 60 | + |
| 61 | + } |
| 62 | + |
| 63 | + } |
| 64 | + return (min + max); |
| 65 | + //throw new NotImplementedException(); |
31 | 66 | } |
32 | 67 |
|
33 | 68 | public int GetLengthOfShortestString(string str1, string str2) |
34 | 69 | { |
35 | | - throw new NotImplementedException(); |
| 70 | + var result = str1.Length <= str2.Length ? str1.Length : str2.Length; |
| 71 | + |
| 72 | + return result; |
| 73 | + |
| 74 | + //throw new NotImplementedException(); |
36 | 75 | } |
37 | 76 |
|
38 | 77 | public int Sum(int[] numbers) |
39 | 78 | { |
40 | | - throw new NotImplementedException(); |
| 79 | + if (numbers == null) |
| 80 | + { |
| 81 | + return 0; |
| 82 | + } |
| 83 | + |
| 84 | + int sum = 0; |
| 85 | + foreach (int number in numbers) |
| 86 | + { |
| 87 | + sum += number; |
| 88 | + } |
| 89 | + return sum; |
| 90 | + |
| 91 | + //throw new NotImplementedException(); |
41 | 92 | } |
42 | 93 |
|
43 | 94 | public int SumEvens(int[] numbers) |
44 | 95 | { |
45 | | - throw new NotImplementedException(); |
| 96 | + if (numbers == null) |
| 97 | + { |
| 98 | + return 0; |
| 99 | + } |
| 100 | + |
| 101 | + int sum = 0; |
| 102 | + foreach (int number in numbers) |
| 103 | + { |
| 104 | + if(number % 2 == 0) |
| 105 | + { |
| 106 | + sum += number; |
| 107 | + } |
| 108 | + |
| 109 | + } |
| 110 | + return sum; |
| 111 | + //throw new NotImplementedException(); |
46 | 112 | } |
47 | 113 |
|
48 | 114 | public bool IsSumOdd(List<int> numbers) |
49 | 115 | { |
50 | | - throw new NotImplementedException(); |
| 116 | + if (numbers == null) |
| 117 | + { |
| 118 | + return false; |
| 119 | + } |
| 120 | + |
| 121 | + int sum = 0; |
| 122 | + foreach (int number in numbers) |
| 123 | + { |
| 124 | + sum += number; |
| 125 | + } |
| 126 | + |
| 127 | + bool odd = (sum == 0 || sum % 2 == 0) ? false : true; |
| 128 | + return odd; |
| 129 | + |
| 130 | + //throw new NotImplementedException(); |
51 | 131 | } |
52 | 132 |
|
53 | 133 | public long CountOfPositiveOddsBelowNumber(long number) |
54 | 134 | { |
55 | | - throw new NotImplementedException(); |
| 135 | + long count = 0; |
| 136 | + for (int i = 1; i < number; i+=2) |
| 137 | + { |
| 138 | + count++; |
| 139 | + } |
| 140 | + return count; |
| 141 | + //throw new NotImplementedException(); |
56 | 142 | } |
57 | 143 | } |
58 | 144 | } |
0 commit comments