Skip to content

Commit 2ecc55f

Browse files
Weekly Challenges Week mvdoyle#5
1 parent 39a4c7d commit 2ecc55f

File tree

8 files changed

+87
-7
lines changed

8 files changed

+87
-7
lines changed
Binary file not shown.
Binary file not shown.

ChallengesWithTestsMark8/ChallengesSet05.cs

Lines changed: 87 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,117 @@ public class ChallengesSet05
77
{
88
public int GetNextNumberDivisibleByN(int startNumber, int n)
99
{
10-
throw new NotImplementedException();
10+
return ((startNumber / n) + 1) * n;
1111
}
1212

1313
public void ChangeNamesOfBusinessesWithNoRevenueTo_CLOSED(Business[] businesses)
1414
{
15-
throw new NotImplementedException();
15+
foreach (Business business in businesses)
16+
{
17+
if (business.TotalRevenue <= 0)
18+
{
19+
business.Name = "CLOSED";
20+
}
21+
}
1622
}
1723

1824
public bool IsAscendingOrder(int[] numbers)
1925
{
20-
throw new NotImplementedException();
26+
if (numbers == null || numbers.Length == 0) return false;
27+
int prevNumber = numbers[0];
28+
for (int i = 1; i < numbers.Length; i++)
29+
{
30+
if (numbers[i] < prevNumber)
31+
{
32+
return false;
33+
}
34+
prevNumber = numbers[i];
35+
}
36+
return true;
2137
}
2238

2339
public int SumElementsThatFollowAnEven(int[] numbers)
2440
{
25-
throw new NotImplementedException();
41+
42+
if (numbers == null) return 0;
43+
int sum = 0;
44+
for (int i = 0; i < numbers.Length; i++)
45+
{
46+
if (numbers[i] % 2 == 0)
47+
{
48+
try { sum += numbers[i + 1]; }
49+
catch (System.IndexOutOfRangeException)
50+
{
51+
// no number to add to sum value
52+
}
53+
}
54+
}
55+
return sum;
56+
2657
}
2758

2859
public string TurnWordsIntoSentence(string[] words)
2960
{
30-
throw new NotImplementedException();
61+
62+
if (words == null || words.Length == 0 || words.Length == 1)
63+
{
64+
return "";
65+
}
66+
string sentence = "";
67+
int counter = 0;
68+
foreach (string str in words)
69+
{
70+
if (str == " " || str == "")
71+
{
72+
counter++;
73+
continue;
74+
}
75+
76+
string toAdd = str.Replace(" ", "");
77+
sentence += toAdd;
78+
79+
if (counter < words.Length - 1 && toAdd != "") sentence += " ";
80+
else if (counter == words.Length - 1) sentence += ".";
81+
82+
counter++;
83+
}
84+
return sentence;
85+
3186
}
3287

3388
public double[] GetEveryFourthElement(List<double> elements)
3489
{
35-
throw new NotImplementedException();
90+
91+
if (elements == null) return new double[0];
92+
List<double> everyFourth = new List<double>();
93+
for (int i = 1; i <= elements.Count; i++)
94+
{
95+
if (i % 4 == 0)
96+
{
97+
everyFourth.Add(elements[i - 1]);
98+
}
99+
}
100+
return everyFourth.ToArray();
101+
36102
}
37103

38104
public bool TwoDifferentElementsInArrayCanSumToTargetNumber(int[] nums, int targetNumber)
39105
{
40-
throw new NotImplementedException();
106+
107+
if (nums.Length == 0 || nums.Length == 1) return false;
108+
for (int i = 0; i < nums.Length; i++)
109+
{
110+
for (int j = 0; j < nums.Length; j++)
111+
{
112+
if (i == j) continue;
113+
if (nums[i] + nums[j] == targetNumber)
114+
{
115+
return true;
116+
}
117+
}
118+
}
119+
return false;
120+
41121
}
42122
}
43123
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)