Skip to content

Commit

Permalink
code from 18.12.23 seminar
Browse files Browse the repository at this point in the history
  • Loading branch information
ScarletSurge committed Dec 18, 2023
1 parent f5a9590 commit 0c64357
Show file tree
Hide file tree
Showing 5 changed files with 211 additions and 4 deletions.
133 changes: 130 additions & 3 deletions DigitalCathedral.Task2/CombinatoricsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,64 @@
public static class CombinatoricsExtensions
{

#region Combinations

private static IEnumerable<IEnumerable<T>> GetCombinations<T>(
List<T> wereAdded,
List<T> notAdded,
int k,
int lastAddedValueIndex,
bool isUniqueValues)
{
if (wereAdded.Count == k)
{
yield return wereAdded;
yield break;
}

for (var i = isUniqueValues ? lastAddedValueIndex + 1 : 0; i < notAdded.Count; i++)
{
wereAdded.Add(notAdded[i]);

foreach (var combination in GetCombinations(wereAdded, notAdded, k, i, isUniqueValues))
{
yield return combination;
}

wereAdded.RemoveAt(wereAdded.Count - 1);
}
}

public static IEnumerable<IEnumerable<T>> GetCombinations<T>(
this IEnumerable<T> values,
int k,
IEqualityComparer<T> equalityComparer)
{
ThrowIfNotDistinctElements(values, equalityComparer);
throw new NotImplementedException();

var valuesArray = values.ToList();

if (k < 0 || k > valuesArray.Count)
{
throw new ArgumentException("k must be GT or EQ 0 and LT or EQ values count", nameof(k));
}

if (k == 0)
{
yield return Enumerable.Empty<T>();
yield break;
}

if (valuesArray.Count == k)
{
yield return valuesArray;
yield break;
}

foreach (var combination in GetCombinations(new List<T>(k), valuesArray, k, -1, false))
{
yield return combination;
}
}

public static IEnumerable<IEnumerable<T>> GetUniqueCombinations<T>(
Expand All @@ -18,16 +69,84 @@ public static IEnumerable<IEnumerable<T>> GetUniqueCombinations<T>(
IEqualityComparer<T> equalityComparer)
{
ThrowIfNotDistinctElements(values, equalityComparer);
throw new NotImplementedException();

var valuesArray = values.ToList();

if (k < 0 || k > valuesArray.Count)
{
throw new ArgumentException("k must be GT or EQ 0 and LT or EQ values count", nameof(k));
}

if (k == 0)
{
yield return Enumerable.Empty<T>();
yield break;
}

if (valuesArray.Count == k)
{
yield return valuesArray;
yield break;
}

foreach (var combination in GetCombinations(new List<T>(k), valuesArray, k, -1, true))
{
yield return combination;
}
}

#endregion

#region Subsets

private static IEnumerable<IEnumerable<T>> GetSetOfAllSubsets<T>(
T[] values,
List<T> constructedSubset,
int lastUsedValueIndex)
{
if (lastUsedValueIndex == values.Length - 1)
{
yield return constructedSubset.ToList();
yield break;
}

foreach (var subset in GetSetOfAllSubsets(values, constructedSubset, lastUsedValueIndex + 1))
{
yield return subset.ToList();
}

constructedSubset.Add(values[lastUsedValueIndex + 1]);

foreach (var subset in GetSetOfAllSubsets(values, constructedSubset, lastUsedValueIndex + 1))
{
yield return subset.ToList();
}

constructedSubset.RemoveAt(constructedSubset.Count - 1);
}

public static IEnumerable<IEnumerable<T>> GetSetOfAllSubsets<T>(
this IEnumerable<T> values,
IEqualityComparer<T> equalityComparer)
{
ThrowIfNotDistinctElements(values, equalityComparer);
throw new NotImplementedException();

var valuesArray = values.ToArray();
if (valuesArray.Length == 0)
{
yield return Enumerable.Empty<T>();
yield break;
}

foreach (var subset in GetSetOfAllSubsets(valuesArray, new List<T>(valuesArray.Length), -1))
{
yield return subset;
}
}

#endregion

#region Permutations

public static IEnumerable<IEnumerable<T>> GetAllPermutations<T>(
this IEnumerable<T> values,
Expand All @@ -42,6 +161,8 @@ public static IEnumerable<IEnumerable<T>> GetAllPermutations<T>(
yield break;
}



foreach (var permutation in GetAllPermutations(new List<T>(valuesArray.Length), valuesArray.ToList()))
{
yield return permutation;
Expand Down Expand Up @@ -71,6 +192,10 @@ private static IEnumerable<IEnumerable<T>> GetAllPermutations<T>(
wereAdded.RemoveAt(wereAdded.Count - 1);
}
}

#endregion

#region Validation

private static void ThrowIfNotDistinctElements<T>(
IEnumerable<T> values,
Expand All @@ -82,4 +207,6 @@ private static void ThrowIfNotDistinctElements<T>(
}
}

#endregion

}
2 changes: 1 addition & 1 deletion DigitalCathedral.Task2/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{
IEqualityComparer<int> equalityComparer = new IntEqualityComparer();
var i = 0;
foreach (var permutation in new IEnumerableImpl().Prepend(8).Append(15).GetAllPermutations(EqualityComparer<int>.Default).Take(1000))
foreach (var permutation in new [] { 1, 2, 3 }.GetSetOfAllSubsets(EqualityComparer<int>.Default))
{
// Console.Write("[ ");
// foreach (var permutationComponent in permutation)
Expand Down
10 changes: 10 additions & 0 deletions DigitalCathedral.Task4/DigitalCathedral.Task4.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
63 changes: 63 additions & 0 deletions DigitalCathedral.Task4/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// See https://aka.ms/new-console-template for more information

Console.WriteLine("Hello, World!");
Console.WriteLine(Math.Abs(2.0 - Math.Pow(Math.Sqrt(2.0), 2)) < 1e-8);

double Foo(double x)
{
return x * x - 3 * x + Math.Sin(x / 2);
}

interface IIntegralSolver
{

double Solve(Func<double, double> integrand, double leftBound, double rightBound, double epsilon);

string Name { get; }

}

abstract class IntegralSolverBase : IIntegralSolver
{

public double Solve(Func<double, double> integrand, double leftBound, double rightBound, double epsilon)
{
if (integrand == null)
{
throw new ArgumentNullException(nameof(integrand));
}

if (epsilon <= 0)
{
throw new ArgumentOutOfRangeException(nameof(epsilon));
}

if (rightBound - leftBound < epsilon)
{
throw new ArgumentOutOfRangeException(nameof(leftBound));
}

return SolveInner(integrand, leftBound, rightBound, epsilon);
}

protected abstract double SolveInner(Func<double, double> integrand, double leftBound, double rightBound, double epsilon);

public abstract string Name { get; }

}

sealed class LeftRectanglesIntegralSolver : IntegralSolverBase
{

protected override double SolveInner(Func<double, double> integrand, double leftBound, double rightBound,
double epsilon)
{
// TODO: implement logic

throw new NotImplementedException();
}

public override string Name =>
"Left rectangles method";

}
7 changes: 7 additions & 0 deletions DigitalCathedral.sln
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DigitalCathedral.Task1", "D
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DigitalCathedral.Task2", "DigitalCathedral.Task2\DigitalCathedral.Task2.csproj", "{26461CE6-A23D-4B47-B9B4-C1354CBBC946}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DigitalCathedral.Task4", "DigitalCathedral.Task4\DigitalCathedral.Task4.csproj", "{C490A2A7-3710-4BDB-9B95-A6AB7B3A9309}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -26,9 +28,14 @@ Global
{26461CE6-A23D-4B47-B9B4-C1354CBBC946}.Debug|Any CPU.Build.0 = Debug|Any CPU
{26461CE6-A23D-4B47-B9B4-C1354CBBC946}.Release|Any CPU.ActiveCfg = Release|Any CPU
{26461CE6-A23D-4B47-B9B4-C1354CBBC946}.Release|Any CPU.Build.0 = Release|Any CPU
{C490A2A7-3710-4BDB-9B95-A6AB7B3A9309}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C490A2A7-3710-4BDB-9B95-A6AB7B3A9309}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C490A2A7-3710-4BDB-9B95-A6AB7B3A9309}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C490A2A7-3710-4BDB-9B95-A6AB7B3A9309}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{8022E880-A4E4-441B-8D90-195C4382F1C8} = {8193D3C5-610D-4FEF-B2C2-8E41D6594161}
{26461CE6-A23D-4B47-B9B4-C1354CBBC946} = {8193D3C5-610D-4FEF-B2C2-8E41D6594161}
{C490A2A7-3710-4BDB-9B95-A6AB7B3A9309} = {8193D3C5-610D-4FEF-B2C2-8E41D6594161}
EndGlobalSection
EndGlobal

0 comments on commit 0c64357

Please sign in to comment.