using System;
using System.Linq;
namespace MyCompiler
{
class Program
{
public static void Main(string[] args)
{
PrintSeparator();
Evaluate(input: new int[] {3, 2, 1}, expectedOutput: new int[] {2, 3, 6});
PrintSeparator();
Evaluate(input: new int[] {1, 2, 3, 4, 5}, expectedOutput: new int[] {120, 60, 40, 30, 24});
PrintSeparator();
}
public static void Evaluate(int[] input, int[] expectedOutput)
{
var result = GetOptimizedProductOfAllOtherElements(input);
Console.WriteLine("Input: [{0}]", string.Join(", ", input));
Console.WriteLine("ExpectedOutput: [{0}]", string.Join(", ", expectedOutput));
Console.WriteLine("Result: [{0}]", string.Join(", ", result));
Console.WriteLine("IsCorrect: [{0}]", Enumerable.SequenceEqual(result, expectedOutput));
}
public static void PrintSeparator(int size = 64)
{
Console.WriteLine(string.Empty.PadLeft(size, '-'));
}
public static int[] GetOptimizedProductOfAllOtherElements(int[] numbers)
{
var products = new int[numbers.Length];
for (var i = 0; i < numbers.Length; i++)
{
products[i] = 1;
for (var j = 0; j < numbers.Length; j++)
{
if (i != j)
{
products[i] = products[i] * numbers[j];
}
}
}
return products;
}
}
}
To embed this project on your website, copy the following code and paste it into your website's HTML: