Skip to content

Commit

Permalink
code from 06.12.23 seminar
Browse files Browse the repository at this point in the history
  • Loading branch information
ScarletSurge committed Dec 6, 2023
1 parent 984358b commit f5a9590
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 8 deletions.
40 changes: 37 additions & 3 deletions DigitalCathedral.Task2/CombinatoricsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,49 @@ public static IEnumerable<IEnumerable<T>> GetAllPermutations<T>(
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 permutation in GetAllPermutations(new List<T>(valuesArray.Length), valuesArray.ToList()))
{
yield return permutation;
}
}

private static IEnumerable<IEnumerable<T>> GetAllPermutations<T>(
List<T> wereAdded,
List<T> notAdded)
{
if (notAdded.Count == 0)
{
yield return wereAdded.ToList();
}

for (var i = 0; i < notAdded.Count; i++)
{
wereAdded.Add(notAdded[i]);
notAdded.RemoveAt(i);

foreach (var permutation in GetAllPermutations(wereAdded, notAdded))
{
yield return permutation;
}

notAdded.Insert(i, wereAdded.Last());
wereAdded.RemoveAt(wereAdded.Count - 1);
}
}

private static void ThrowIfNotDistinctElements<T>(
IEnumerable<T> values,
IEqualityComparer<T> equalityComparer)
{
var distinctValues = values.Distinct(equalityComparer).ToArray();
if (distinctValues.Length != values.Count())
if (values.Distinct(equalityComparer).Count() != values.Count())
{
throw new ArgumentException("Found equal elements in collection.", nameof(values));
}
Expand Down
24 changes: 24 additions & 0 deletions DigitalCathedral.Task2/IEnumerableImpl.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Collections;

namespace DigitalCathedral.Task2;

public class IEnumerableImpl:
IEnumerable<int>
{

private readonly int[] _values = new[] { 1, 2, 3, 7, 5 };

IEnumerator IEnumerable.GetEnumerator()
{
return _values.GetEnumerator();
}

public IEnumerator<int> GetEnumerator()
{
for (var i = 0; i < _values.Length; i++)
{
yield return _values[i];
}
}

}
14 changes: 12 additions & 2 deletions DigitalCathedral.Task2/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,18 @@
try
{
IEqualityComparer<int> equalityComparer = new IntEqualityComparer();
var values = new int [] { 1, 2, 3, 4, 5 };
values.GetCombinations(3, equalityComparer);
var i = 0;
foreach (var permutation in new IEnumerableImpl().Prepend(8).Append(15).GetAllPermutations(EqualityComparer<int>.Default).Take(1000))
{
// Console.Write("[ ");
// foreach (var permutationComponent in permutation)
// {
// Console.Write($"{permutationComponent} ");
// }
// Console.WriteLine("]");

Console.WriteLine($"{++i}: [ {string.Join(" ", permutation)} ]");
}
}
catch (ArgumentException ex)
{
Expand Down
17 changes: 14 additions & 3 deletions DigitalCathedral/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,20 @@
Console.WriteLine((int)iterator.Current);
}

(impl as IEnumerable<int>).
var iterator2 = ((IEnumerable<int>)impl).GetEnumerator();
while (iterator2.MoveNext())
{
var value = iterator2.Current;
if (value == 2)
{
break;
}
Console.WriteLine(iterator2.Current);
}

//(impl as IEnumerable<int>).

Enumerable.Cast<int>(impl);
//Enumerable.Cast<int>(impl);

void Foo2(int value1, string value2)
{
Expand Down Expand Up @@ -348,7 +359,7 @@ double IntegrandFunc(double value)
Integral integral = null;
integral += (f, bounds, eps) =>
{

throw new NotImplementedException();
};
integral?.Invoke(IntegrandFunc, (5.0, 10.0), 0.0001);

Expand Down

0 comments on commit f5a9590

Please sign in to comment.