Last active
December 4, 2016 13:26
-
-
Save vdovinanton/a23605882399b134e01b8a3d3137ee9f to your computer and use it in GitHub Desktop.
Clean up dirty job from the DenKey
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace ConsoleApplication2 | |
{ | |
public class Program | |
{ | |
static void Main(string[] args) | |
{ | |
const int dimension = 10; | |
var myMatrix = new MyMatrix(dimension); | |
var max = myMatrix.MaxInDiagonal; | |
var min = myMatrix.MinInDiagonal; | |
Console.WriteLine($"\nMAX: [{max.Coordiante.Row}][{max.Coordiante.Column}] {max.Value}"); | |
Console.WriteLine($"MIN: [{min.Coordiante.Row}][{min.Coordiante.Column}] {min.Value}"); | |
Console.ReadKey(); | |
} | |
} | |
public class MyMatrix | |
{ | |
private readonly int _matrixSize; | |
private readonly Random _random = new Random(); | |
private readonly IList<MatrixLine> _matrix = new List<MatrixLine>(); | |
public MatrixLine MinInDiagonal => FindSecondMin(); | |
public MatrixLine MaxInDiagonal => FindMainMax(); | |
public MyMatrix(int matrixSise) | |
{ | |
_matrixSize = matrixSise; | |
FillMatrix(); | |
PrintMatrix(); | |
} | |
private void FillMatrix() | |
{ | |
for (var i = 0; i < _matrixSize; i++) | |
{ | |
for (var j = 0; j < _matrixSize; j++) | |
{ | |
_matrix.Add(new MatrixLine | |
{ | |
Value = _random.Next(10, 99), | |
Coordiante = new Coordinate | |
{ | |
Row = i, | |
Column = j | |
} | |
}); | |
} | |
} | |
} | |
private void PrintMatrix() | |
{ | |
var temp = 0; | |
foreach (var item in _matrix) | |
{ | |
var output = $"[{item.Coordiante.Row}][{item.Coordiante.Column}]{item.Value} "; | |
output = temp != item.Coordiante.Row ? $"\n{output}" : output; | |
temp = item.Coordiante.Row; | |
Console.Write(output); | |
} | |
} | |
private MatrixLine FindSecondMin() | |
{ | |
var diagonal = new List<MatrixLine>(); | |
for (var i = 0; i < _matrixSize; i++) | |
{ | |
var value = _matrix.FirstOrDefault(q => q.Coordiante.Row == i && q.Coordiante.Column == (_matrixSize - 1) - i); | |
if (value != null) diagonal.Add(value); | |
} | |
return diagonal.OrderByDescending(i => i.Value).Reverse().FirstOrDefault(); | |
} | |
private MatrixLine FindMainMax() | |
{ | |
var diagonal = new List<MatrixLine>(); | |
for (var i = 0; i < _matrixSize; i++) | |
{ | |
var value = _matrix.FirstOrDefault(q => q.Coordiante.Row == i && q.Coordiante.Column == i); | |
diagonal.Add(value); | |
} | |
return diagonal.OrderByDescending(i => i.Value).FirstOrDefault(); ; | |
} | |
} | |
#region Types | |
public class Coordinate | |
{ | |
public int Column { get; set; } | |
public int Row { get; set; } | |
} | |
public class MatrixLine | |
{ | |
public int Value { get; set; } | |
public Coordinate Coordiante { get; set; } | |
} | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment