Skip to content

Commit

Permalink
code from 29.11 seminar
Browse files Browse the repository at this point in the history
  • Loading branch information
ScarletSurge committed Nov 29, 2023
1 parent d6bba4f commit c9c8f78
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 37 deletions.
9 changes: 7 additions & 2 deletions DigitalCathedral/Animal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@ public class Animal : IEquatable<Animal>
{
private const double Epsilon = 1e-9;

private readonly int _value1;
private int _value1;
private readonly double _value2;
private readonly string _value3 = "My awesome string";

public void ChangeValue1()
{
var randomSource = new Random();
_value1 = randomSource.Next(-1000, 1001);
}

public Animal(
int value1,
double value2,
Expand Down Expand Up @@ -47,7 +53,6 @@ public string GetValue3()
public override int GetHashCode()
{
HashCode result = new HashCode();
result.Add(_value1);
result.Add(_value2);
result.Add(_value3);
return result.ToHashCode();
Expand Down
51 changes: 51 additions & 0 deletions DigitalCathedral/CombinatoricsExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
namespace DigitalCathedral;

public static class CombinatoricsExtensions
{

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

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

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

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

private static void ThrowIfNotDistinctElements<T>(
IEnumerable<T> values,
IEqualityComparer<T> equalityComparer)
{
var distinctValues = values.Distinct(equalityComparer).ToArray();
if (distinctValues.Length != values.Count())
{
throw new ArgumentException("Found equal elements in collection.", nameof(values));
}
}

}
28 changes: 28 additions & 0 deletions DigitalCathedral/IntEqualityComparer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace DigitalCathedral;

public sealed class IntEqualityComparer : IEqualityComparer<int>
{
private const int EventIntHashCode = 0;

public bool Equals(
int x,
int y)
{
if (x % 2 == 0 && y % 2 == 0)
{
return true;
}

return x.Equals(y);
}

public int GetHashCode(
int obj)
{
if (obj % 2 == 0)
{
return EventIntHashCode;
}
return obj.GetHashCode();
}
}
76 changes: 57 additions & 19 deletions DigitalCathedral/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,28 +243,66 @@
// Console.WriteLine(animal == animal2);
// Console.WriteLine(animal.Equals(animal2));

// var animal = new Animal(1, 1.1, "2.2");
// Dictionary<Animal, string> collection = new Dictionary<Animal, string>();
// collection.Add(animal, string.Empty);
// var found = collection.TryGetValue(animal, out _);
// animal.ChangeValue1();
// found = collection.TryGetValue(animal, out _);
//
// var collectionEnumerable = collection as IEnumerable<KeyValuePair<Animal, string>>;
// var array = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
//
// Console.WriteLine(Enumerable.Aggregate(Enumerable.Where(Enumerable.Select(array, x => x * 2), x => x % 3 == 0), 1, (accum, value) => accum * value));
//
// Console.WriteLine(array.Select(x => x * 2)
// .Where(x => x % 3 == 0)
// .Aggregate(1, (accum, value) => accum * value));
//
//
//
// foreach (var item in collection)
// {
// // TODO: work with value
// }
//
// Student s1 = new Student("", "", "", "", "", 3);
// Student s2 = new Student("", "", "", "", "", 3);
// Console.WriteLine(s1.Equals(s2));
//
// try
// {
// Student st = new Student("Ivanov", "Ivan", "Ivanovich", "M1O-301B-21", "1234-123-12", 3);
//
// Console.WriteLine("No exceptions were thrown");
// }
// catch (ArgumentNullException ex)
// {
// //
// Console.WriteLine(ex.Message);
// }
// catch (ArgumentException ex)
// {
// //
// Console.WriteLine(ex.Message);
// }
// Console.WriteLine("after try/catch...");
//
// var fraction = new Fraction(2, 5);
// var fraction2 = new Fraction(1, 7);
// Console.WriteLine(fraction.Equals(fraction2));
//
// var multiplicationResult = Fraction.Multiplication(fraction, fraction2);
//
// Console.WriteLine(fraction.CompareTo(fraction2));

try
{
Student st = new Student("Ivanov", "Ivan", "Ivanovich", "M1O-301B-21", "1234-123-12", 3);

Console.WriteLine("No exceptions were thrown");
}
catch (ArgumentNullException ex)
{
//
Console.WriteLine(ex.Message);
IEqualityComparer<int> equalityComparer = new IntEqualityComparer();
var values = Enumerable.Range(1, 10).ToArray();
values.GetCombinations(3, equalityComparer);
}
catch (ArgumentException ex)
{
//
Console.WriteLine(ex.Message);
}
Console.WriteLine("after try/catch...");

var fraction = new Fraction(2, 5);
var fraction2 = new Fraction(1, 7);
Console.WriteLine(fraction.Equals(fraction2));

var multiplicationResult = Fraction.Multiplication(fraction, fraction2);

Console.WriteLine(fraction.CompareTo(fraction2));
}
67 changes: 51 additions & 16 deletions DigitalCathedral/Student.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
namespace DigitalCathedral;

public sealed class Student
public sealed class Student:
IEquatable<Student>
{

private string _surname;
private string _name;
private string _patronymic;
private string _group;
private string _zachetka;
private int _course;
private readonly string _surname;
private readonly string _name;
private readonly string _patronymic;
private readonly string _group;
private readonly string _zachetka;
private readonly int _course;

public Student(
string surname,
string name,
string patronymic,
string group,
string zachetka,
string? surname,
string? name,
string? patronymic,
string? group,
string? zachetka,
int course)
{
if (surname == null)
if (surname is null)
{
throw new ArgumentNullException(nameof(surname));
}
Expand Down Expand Up @@ -61,12 +62,46 @@ public override string ToString()

public override int GetHashCode()
{
return base.GetHashCode();
var result = new HashCode();
result.Add(_surname);
result.Add(_name);
result.Add(_patronymic);
result.Add(_group);
result.Add(_zachetka);
result.Add(_course);
return result.ToHashCode();
}

public override bool Equals(object? obj)
public override bool Equals(
object? obj)
{
return base.Equals(obj);
if (obj is null)
{
return false;
}

if (obj is Student student)
{
return Equals(student);
}

return false;
}

public bool Equals(
Student? student)
{
if (student is null)
{
return false;
}

return _surname.Equals(student._surname) &&
_name.Equals(student._name) &&
_patronymic.Equals(student._patronymic) &&
_group.Equals(student._group) &&
_zachetka.Equals(student._zachetka) &&
_course.Equals(student._course);
}

}

0 comments on commit c9c8f78

Please sign in to comment.