Skip to content

Commit fc77cc4

Browse files
committed
Added test for ugly numbers
1 parent bd7725d commit fc77cc4

File tree

1 file changed

+64
-1
lines changed

1 file changed

+64
-1
lines changed

CodingInterview/CodingInterview/Tests.cs

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,53 @@ private static bool CheckIfArrayContainsDuplicatedElements(int[] inputArray)
203203
return false;
204204
}
205205

206-
[Test]
206+
private static bool CheckIfUgly(int number)
207+
{
208+
if(number < 0)
209+
throw new ArgumentException(nameof(number), "number should be positive");
210+
211+
switch (number)
212+
{
213+
case 0:
214+
return false;
215+
case 1:
216+
return true;
217+
}
218+
219+
while (number >= 0)
220+
{
221+
switch (number)
222+
{
223+
case 0:
224+
return false;
225+
case 1:
226+
return true;
227+
}
228+
229+
if (number % 2 == 0)
230+
{
231+
number /= 2;
232+
continue;
233+
}
234+
235+
if (number % 3 == 0)
236+
{
237+
number /= 3;
238+
continue;
239+
}
240+
241+
if (number % 5 == 0)
242+
{
243+
number /= 5;
244+
continue;
245+
}
246+
return false;
247+
248+
}
249+
return false;
250+
}
251+
252+
[Test]
207253
public void RemoveDuplicatesFromSortedArray_1()
208254
{
209255
//given
@@ -341,5 +387,22 @@ public void ContainsDuplicates_82(int []inputArray, bool isDuplicateElement)
341387
//then
342388
Assert.AreEqual(isDuplicateElement, isPalindrome);
343389
}
390+
391+
[Test]
392+
[TestCase(8, true)]
393+
[TestCase(7, false)]
394+
[TestCase(0, false)]
395+
[TestCase(1, true)]
396+
[TestCase(10, true)]
397+
[TestCase(13, false)]
398+
public void CheckUglyNumbers_99(int number, bool isUgly)
399+
{
400+
//given
401+
//when
402+
var isPalindrome = CheckIfUgly(number);
403+
//then
404+
Assert.AreEqual(isUgly, isPalindrome);
405+
}
406+
344407
}
345408
}

0 commit comments

Comments
 (0)