Skip to content

Commit 52d76cf

Browse files
committed
Updated Mathproduct class and added test method for the Other Products code challenge.
1 parent 317f9ad commit 52d76cf

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-1
lines changed

Coderbyte_CSharp/1 Easy Challenges/EasyChallenges.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Letter Capitalize 02/22/2021 ChangeLetter
1212
Letter Changes 02/22/2021 ChangeLetter
1313
Longest Word
1414
Moving Median
15-
Other Products
15+
Other Products 02/22/2021 MathProduct
1616
Palindrome 02/10/2021 PalindromeChecker
1717
Power of 2 02/22/2021 ExponentTwo
1818
Product Digits 02/22/2021 MathProduct

Coderbyte_CSharp/1 Easy Challenges/MathProduct.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,55 @@ public int ProductDigits(int num)
4747
return result;
4848
}
4949

50+
// Have the function OtherProducts(arr) take the array of numbers stored in arr
51+
// and return a new list of the products of all the other numbers in the array
52+
// for each element.
53+
54+
// For example: if arr is [1, 2, 3, 4, 5] then the new array,
55+
// where each location in the new array is the product of all other elements,
56+
// is [120, 60, 40, 30, 24]. The following calculations were performed to get
57+
// this answer: [(2*3*4*5), (1*3*4*5), (1*2*4*5), (1*2*3*5), (1*2*3*4)]. You
58+
// should generate this new array and then return the numbers as a string
59+
// joined by a hyphen: 120-60-40-30-24. The array will contain at most 10
60+
// elements and at least 1 element of only positive integers.
61+
public string OtherProducts(int[] arr, int length)
62+
{
63+
string result = String.Empty;
64+
List<int> productValues = new List<int>(length);
65+
int product = 1;
66+
67+
for (int index = 0; index < length; index++)
68+
{
69+
product = ComputeProduct(arr, index);
70+
productValues.Add(product);
71+
}
72+
73+
StringBuilder sb = new StringBuilder();
74+
75+
foreach (int item in productValues)
76+
{
77+
sb.Append(item);
78+
sb.Append("-");
79+
}
80+
81+
result = sb.ToString();
82+
result = result.Remove(result.Length - 1);
83+
84+
return result;
85+
}
86+
87+
protected int ComputeProduct(int[] arr, int index)
88+
{
89+
int result = 1;
90+
int length = arr.Length;
91+
92+
for (int i = 0; i < length; i++)
93+
{
94+
if (i == index) { continue; }
95+
result *= arr[i];
96+
}
97+
98+
return result;
99+
}
50100
}
51101
}

Coderbyte_CSharp/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ static void Main(string[] args)
2929
tc.Test_LetterCapitalize();
3030
tc.Test_PowerOfTwo();
3131
tc.Test_ProductDigits();
32+
tc.Test_OtherProducts();
3233

3334
// Medium
3435
Console.WriteLine("Medium Code Challenges:");

Coderbyte_CSharp/TestChallenge.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,18 @@ public void Test_ProductDigits()
256256
Console.WriteLine("Input: {0}", num);
257257
Console.WriteLine("Output: {0}", prod.ProductDigits(num));
258258
Console.WriteLine();
259+
}
260+
261+
public void Test_OtherProducts()
262+
{
263+
MathProduct prod = new MathProduct();
264+
int[] arr = new int[] {1, 2, 3, 4, 5 };
265+
266+
267+
Console.WriteLine("Other Products:");
268+
PrintArray(arr, arr.Length);
269+
Console.WriteLine("Output: {0}", prod.OtherProducts(arr, arr.Length));
270+
Console.WriteLine();
259271

260272
}
261273

0 commit comments

Comments
 (0)