public static class Kata { public static int SameCase(char a, char b) { if (!char.IsLetter(a) || !char.IsLetter(b)) { return -1; } return char.IsUpper(a) == char.IsUpper(b) ? 1 : 0; } } // 3ms faster :)
- public static class Kata
- {
public static int SameCase(char a, char b) =>char.IsLetter(a) && char.IsLetter(b)? char.IsUpper(a) == char.IsUpper(b)? 1 : 0: -1;}- public static int SameCase(char a, char b)
- {
- if (!char.IsLetter(a) || !char.IsLetter(b))
- {
- return -1;
- }
- return char.IsUpper(a) == char.IsUpper(b) ? 1 : 0;
- }
- }
- // 3ms faster :)
You are given a function called FindSquaresInArray written in C#. This function takes an array of integers and returns the sum of the squares of each element. Your task is to refactor this function to improve its performance and readability.
Constraints:
Do not change the function signature.
Do not use any external libraries.
using System;
public class Program
{
public static int FindSquaresInArray(int[] arr)
{
int sum = 0;
for (int i = 0; i < arr.Length; i++)
{
for (int j = 0; j < arr[i]; j++)
{
sum += arr[i];
}
}
return sum;
}
}
using NUnit.Framework;
using System;
namespace Solution
{
[TestFixture]
public class SolutionTest
{
[Test]
public void TestEmptyArray()
{
int[] emptyArray = new int[0];
Assert.AreEqual(0, Program.FindSquaresInArray(emptyArray));
}
[Test]
public void TestArrayWithPositiveIntegers()
{
int[] positiveArray = { 1, 2, 3, 4, 5 };
Assert.AreEqual(55, Program.FindSquaresInArray(positiveArray));
}
[Test]
public void TestArrayWithSingleElement()
{
int[] singleElementArray = { 4 };
Assert.AreEqual(16, Program.FindSquaresInArray(singleElementArray));
}
[Test]
public void TestArrayWithAllZeros()
{
int[] zeroArray = { 0, 0, 0, 0 };
Assert.AreEqual(0, Program.FindSquaresInArray(zeroArray));
}
}
}