Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added runs for the leetcode two-sum problem. #29

Merged
merged 33 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
782e90f
Defined two sum leetcode module.
tacosontitan Apr 9, 2024
b43ea07
Defined leetcode test cases for the two sum module.
tacosontitan Apr 9, 2024
7a3e843
Defined the setup for the two-sum module.
tacosontitan Apr 9, 2024
13f80db
Completed dry run of the two-sum problem.
tacosontitan Apr 9, 2024
2472bc0
Completed dry run for the two-sum problem.
tacosontitan Apr 9, 2024
9dc6728
Added a run for the two-sum problem with reduced time complexity.
tacosontitan Apr 9, 2024
705e0be
Added golf run shell to the two-sum problem test suite.
tacosontitan Apr 9, 2024
c03f4bb
Improved starting point of brute force attempt for two-sum problem.
tacosontitan Apr 9, 2024
7c19fb3
Created golfed dry run.
tacosontitan Apr 10, 2024
d74bbbd
Added a golfed variant of the complexity run for the two-sum problem.
tacosontitan Apr 10, 2024
96f6b2e
Moved basic for each for expansion.
tacosontitan Apr 10, 2024
4bc995f
Created an indexed for each overload.
tacosontitan Apr 10, 2024
927e3f5
Created cartesian for-each extensions.
tacosontitan Apr 10, 2024
781c7a2
Moved map method to a folder.
tacosontitan Apr 11, 2024
d0ed4d0
Created basic index of method.
tacosontitan Apr 11, 2024
0957fbf
Updated package information.
tacosontitan Apr 11, 2024
7bbb9ce
Created a basic indexed primitive.
tacosontitan Apr 11, 2024
42c2933
Created an indexed mapping function.
tacosontitan Apr 11, 2024
8cf0c1d
Created a two-term cartesian mapping function.
tacosontitan Apr 11, 2024
5178243
Created a three-term cartesian mapping function.
tacosontitan Apr 11, 2024
f79b0e4
Renamed indexed mapping functions.
tacosontitan Apr 11, 2024
8bc6100
Improved mapping function support.
tacosontitan Apr 11, 2024
8261fb5
Condensed indexed type names.
tacosontitan Apr 11, 2024
a2f2c41
Removed unused using directive.
tacosontitan Apr 11, 2024
6d2ba2d
Renamed first.
tacosontitan Apr 11, 2024
894a816
Package setup.
tacosontitan Apr 11, 2024
4a9cebc
Created null function.
tacosontitan Apr 11, 2024
55814e7
Created not null function.
tacosontitan Apr 11, 2024
7f686e4
Started creating collection type.
tacosontitan Apr 11, 2024
d138f8a
Updated R# namespace rules.
tacosontitan Apr 11, 2024
24d051a
✨WIP: Indexed first methods.
tacosontitan Apr 11, 2024
99583fd
✨WIP: Collection allocation methods.
tacosontitan Apr 11, 2024
e72d661
Completed initial hussy dry run for two-sum.
tacosontitan Apr 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Hussy.Net.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/UserDictionary/Words/=leetcode/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=tacosontitan/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=c270154/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=c270154_005Ctests/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=golfing_005Cc270154_005Ctests/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=golfing_005Cc270154/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=golfing_005Cc270154/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=leetcode_005Ceasy_005Ctwosum/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=leetcode_005Ceasy_005Ctwosum_005Ctests/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
39 changes: 39 additions & 0 deletions samples/Hussy.Net.Playground/Leetcode/Easy/TwoSum/ComplexityRun.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Glitter.Collections;

namespace Hussy.Net.Playground.Leetcode.Easy;

/// <summary>
/// Represents a solution and its tests for the leetcode two-sum problem.
/// </summary>
/// <see href="https://leetcode.com/problems/two-sum/description/"/>
public sealed partial class TwoSum
{
/// <summary>
/// A direct approach to solving the problem with less time complexity
/// to understand where Hussy may be lacking.
/// </summary>
/// <param name="numbers">The array of numbers to look through.</param>
/// <param name="target">
/// The target number which the two numbers in <paramref name="numbers"/> must add up to.
/// </param>
/// <returns>
/// The two indices of the numbers which add up to the <paramref name="target"/>.
/// </returns>
private static IEnumerable<int> ComplexityRun(
int[] numbers,
int target)
{
var seen = new Dictionary<int, int>();
for (var i = 0; i < numbers.Length; i++)
{
var currentNumber = numbers[i];
var complement = target - currentNumber;
if (seen.TryGetValue(complement, out var value))
return [value, i];

seen[currentNumber] = i;
}

return Enumerable.Empty<int>();
}
}
39 changes: 39 additions & 0 deletions samples/Hussy.Net.Playground/Leetcode/Easy/TwoSum/DryRun.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
namespace Hussy.Net.Playground.Leetcode.Easy;

/// <summary>
/// Represents a solution and its tests for the leetcode two-sum problem.
/// </summary>
/// <see href="https://leetcode.com/problems/two-sum/description/"/>
public sealed partial class TwoSum
{
/// <summary>
/// A very mundane and direct approach to solving the problem to understand where Hussy may be lacking.
/// </summary>
/// <param name="numbers">The array of numbers to look through.</param>
/// <param name="target">
/// The target number which the two numbers in <paramref name="numbers"/> must add up to.
/// </param>
/// <returns>
/// The two indices of the numbers which add up to the <paramref name="target"/>.
/// </returns>
private static IEnumerable<int> DryRun(
int[] numbers,
int target)
{
for (var x = 0; x < numbers.Length; x++)
{
for (var y = 0; y < numbers.Length; y++)
tacosontitan marked this conversation as resolved.
Show resolved Hide resolved
{
if (x == y)
continue;

var a = numbers[x];
var b = numbers[y];
if (a + b == target)
return [x, y];
}
}

return Enumerable.Empty<int>();
}
}
25 changes: 25 additions & 0 deletions samples/Hussy.Net.Playground/Leetcode/Easy/TwoSum/GolfRun.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace Hussy.Net.Playground.Leetcode.Easy;

/// <summary>
/// Represents a solution and its tests for the leetcode two-sum problem.
/// </summary>
/// <see href="https://leetcode.com/problems/two-sum/description/"/>
public sealed partial class TwoSum
{
/// <summary>
/// A simple golf run to prepare for the Hussy run.
/// </summary>
/// <param name="numbers">The array of numbers to look through.</param>
/// <param name="target">
/// The target number which the two numbers in <paramref name="numbers"/> must add up to.
/// </param>
/// <returns>
/// The two indices of the numbers which add up to the <paramref name="target"/>.
/// </returns>
private static IEnumerable<int> GolfRun(
int[] numbers,
int target)
{
return Enumerable.Empty<int>();
}
}
58 changes: 58 additions & 0 deletions samples/Hussy.Net.Playground/Leetcode/Easy/TwoSum/Overview.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
namespace Hussy.Net.Playground.Leetcode.Easy;

/// <summary>
/// Represents a solution and its tests for the leetcode two-sum problem.
/// </summary>
/// <see href="https://leetcode.com/problems/two-sum/description/"/>
public sealed partial class TwoSum
{
/// <summary>
/// Given an array of integers <paramref name="numbers"/> and an integer <paramref name="target"/>,
/// return the indices of the two numbers within <paramref name="numbers"/> that add up to the
/// specified <paramref name="target"/>.
/// </summary>
/// <param name="numbers">The array of numbers to look through.</param>
/// <param name="target">
/// The target number which the two numbers in <paramref name="numbers"/> must add up to.
/// </param>
/// <returns>
/// The indices of the two numbers within <paramref name="numbers"/> that add up to the
/// specified <paramref name="target"/>.
/// </returns>
/// <remarks>
/// <para>You can return the answer in <i>any</i> order.</para>
/// <para>
/// <b>Requirements:</b>
/// <list type="bullet">
/// <item>You may <b>NOT</b> use the same element twice.</item>
/// </list>
/// </para>
/// <para>
/// <b>Assumptions:</b>
/// <list type="bullet">
/// <item>
/// Each sequence of <paramref name="numbers"/> will have <i>exactly
/// <b>one</b></i> solution.
/// </item>
/// </list>
/// </para>
/// <para>
/// <b>Constraints:</b>
/// <list type="bullet">
/// <item>
/// <c>2</c> is less than or equal to <paramref name="numbers"/><c>.Length</c> is less than or equal to <c>104</c>
/// </item>
/// <item>
/// <c>-109</c> is less than or equal to <c>nums[i]</c> is less than or equal to <c>109</c>
/// </item>
/// <item>
/// <c>-109</c> is less than or equal to <paramref name="target"/> is less than or equal to <c>109</c>
/// </item>
/// <item>Only one valid answer exists.</item>
/// </list>
/// </para>
/// </remarks>
private delegate IEnumerable<int> TestFunction(
int[] numbers,
int target);
}
38 changes: 38 additions & 0 deletions samples/Hussy.Net.Playground/Leetcode/Easy/TwoSum/Tests/Setup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
namespace Hussy.Net.Playground.Leetcode.Easy;

/// <summary>
/// Represents a solution and its tests for the leetcode two-sum problem.
/// </summary>
/// <see href="https://leetcode.com/problems/two-sum/description/"/>
public sealed partial class TwoSum
{
/// <summary>
/// Gets the functions the tests should be applied to.
/// </summary>
private static TestFunction[] TestFunctions =>
[
DryRun,
ComplexityRun,
GolfRun
];

/// <summary>
/// Tests all runs to ensure each has the same expected result.
/// </summary>
private static void RunTest(
IEnumerable<int> expectedResults,
int[] numbers,
int target)
{
var expectedSequence = expectedResults.ToList();
foreach (var testFunction in TestFunctions)
{
var results = testFunction(numbers, target);
var resultingSequence = results.ToList();

var excessResults = resultingSequence.Except(expectedSequence);
Assert.True(expectedSequence.TrueForAll(resultingSequence.Contains));
Assert.Empty(excessResults);
}
}
}
20 changes: 20 additions & 0 deletions samples/Hussy.Net.Playground/Leetcode/Easy/TwoSum/Tests/TestOne.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace Hussy.Net.Playground.Leetcode.Easy;

/// <summary>
/// Represents a solution and its tests for the leetcode two-sum problem.
/// </summary>
/// <see href="https://leetcode.com/problems/two-sum/description/"/>
public sealed partial class TwoSum
{
/// <summary>
/// The first example given on Leetcode comes with the explanation that
/// the numbers <c>2</c> and <c>7</c> which are located at indices <c>0</c>
/// and <c>1</c>, respectively, add up to the specified target value of
/// <c>9</c> so the expected output is <c>[0, 1]</c>.
/// </summary>
[Fact]
public void TestCaseOne_Passes() => RunTest(
expectedResults: [0, 1],
numbers: [2, 7, 11, 15],
target: 9);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Hussy.Net.Playground.Leetcode.Easy;

/// <summary>
/// Represents a solution and its tests for the leetcode two-sum problem.
/// </summary>
/// <see href="https://leetcode.com/problems/two-sum/description/"/>
public sealed partial class TwoSum
{
[Fact]
public void TestCaseThree_Passes() => RunTest(
expectedResults: [0, 1],
numbers: [3, 3],
target: 6);
}
14 changes: 14 additions & 0 deletions samples/Hussy.Net.Playground/Leetcode/Easy/TwoSum/Tests/TestTwo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Hussy.Net.Playground.Leetcode.Easy;

/// <summary>
/// Represents a solution and its tests for the leetcode two-sum problem.
/// </summary>
/// <see href="https://leetcode.com/problems/two-sum/description/"/>
public sealed partial class TwoSum
{
[Fact]
public void TestCaseTwo_Passes() => RunTest(
expectedResults: [1, 2],
numbers: [3, 2, 4],
target: 6);
}
Loading