Get Take Counts From Two Sources To Meet One Total Quota

CSharp · Console

/Program.cs

using System;

public class Program
{
	private static bool allTestsPassed = true;
	
	public static void Main()
	{
		TestCartisian();
		
		Console.WriteLine($"allTestsPassed: {allTestsPassed}");
	}

	/// <summary>
	/// This is used whenever you have two sources, and you definitely want some results from both sources, but need to still cap the results to one ultimate / final 'take' count
	/// </summary>
	/// <param name="take">The ultimate/final amount that should be taken from the total results count</param>
	/// <param name="leftCount">The number of results that are currently in the first or 'left' data set</param>
	/// <param name="rightCount">The number of results that are currently in the second or 'right' data set</param>
	/// <returns><code>tuple (int leftTakeCount, int rightTakeCount)</code></returns>
	public static (int leftTakeCount, int rightTakeCount) GetTakeCountsFromTwoSourcesToMeetOneTotalQuota(int take, int leftCount, int rightCount)
	{
		int halfTake = take / 2; // since it's an int here, the decimal will just truncate
		int totalCount = leftCount + rightCount;

		bool totalCountAboveTake = totalCount > take;

		bool leftAboveHalfTake = leftCount > halfTake;
		bool shouldReduceLeft = totalCountAboveTake && leftAboveHalfTake;

		bool rightAboveHalfTake = rightCount > halfTake;
		bool shouldReduceRight = totalCountAboveTake && rightAboveHalfTake;

		// When both counts are above the requested take count and also above the halfTake, then we can just chop both down to the half count
		bool canReduceBothToHalftake = totalCountAboveTake & leftAboveHalfTake & rightAboveHalfTake;
		int leftReductionToHalfTake = canReduceBothToHalftake ? leftCount - halfTake : 0;
		int rightReductionToHalfTake = canReduceBothToHalftake ? rightCount - halfTake : 0;

		// When one of the counts is below halfTake, we need to calculate the difference so that we'll end up with some of both
		int leftReductionToDifference = (shouldReduceLeft & !canReduceBothToHalftake) ? leftCount - (take - rightCount) : 0;
		int rightReductionToDifference = (shouldReduceRight & !canReduceBothToHalftake) ? rightCount - (take - leftCount) : 0;

		// Since we used booleans above, one of these will end up as 0. So get the other one
		int maxLeftReduction = Math.Max(leftReductionToHalfTake, leftReductionToDifference);
		int maxRightReduction = Math.Max(rightReductionToHalfTake, rightReductionToDifference);

		int finalLeftReduction = shouldReduceLeft ? maxLeftReduction : 0;
		int finalRightReduction = shouldReduceRight ? maxRightReduction : 0;

		int leftTakeCount = leftCount - finalLeftReduction;
		int rightTakeCount = rightCount - finalRightReduction;

		return (leftTakeCount, rightTakeCount);
	}
	
	private static void TestCartisian()
	{
		var runUntil = 20;
		var cartisianRunUntil = 20;
		var ultimateTakeLimit = 10;
		var expectedCount = 0;
		
		// Only Left rows
		for(int left = 0; left < runUntil; left++)
		{
			expectedCount = Math.Min(ultimateTakeLimit, left);
			TestLine(ultimateTakeLimit, left, 0, expectedCount);
		}
		
		// Only Right rows
		for(int right = 0; right < runUntil; right++)
		{
			expectedCount = Math.Min(ultimateTakeLimit, right);
			TestLine(ultimateTakeLimit, 0, right, expectedCount);
		}
		
		// Cartisian Left side
		for(int left = 0; left < cartisianRunUntil; left++)
		{
			for(int right = 0; right < cartisianRunUntil; right++)
			{
				expectedCount = Math.Min(ultimateTakeLimit, left+right);
				TestLine(ultimateTakeLimit, left, right, expectedCount);
			}
		}
	}
	
	private static void TestLine(int finalTakeCount, int leftDataSetCount, int rightDataSetCount, int expectedCount)
	{
		var takeCounts = GetTakeCountsFromTwoSourcesToMeetOneTotalQuota(finalTakeCount, leftDataSetCount, rightDataSetCount);
		int resultCount = takeCounts.leftTakeCount + takeCounts.rightTakeCount;
		bool passes = resultCount == expectedCount;
		
		allTestsPassed = allTestsPassed && passes;
		
		Console.WriteLine($"leftDataSet: {leftDataSetCount}.Take({takeCounts.leftTakeCount})    rightDataSet: {rightDataSetCount}.Take({takeCounts.rightTakeCount})    resultCount: {resultCount}    expectedCount: {expectedCount}    passes: {passes}\r\n");
	}
}
;