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