
Untitled
By: a guest on
May 7th, 2012 | syntax:
None | size: 1.58 KB | hits: 18 | expires: Never
How to find minimum and maximum of cartesian co-ordinates in Scala
override def toString:String =
{
val output:StringBuilder = new StringBuilder();
val minOfRowColumn = for
{
cell <- aliveCells
row = cell.row
column = cell.column
} yield if( row < column ) row else column
val min = minOfRowColumn.min
val maxOfRowColumn = for
{
cell <- aliveCells
row = cell.row
column = cell.column
} yield if( row > column ) row else column
val max = maxOfRowColumn.max
var row = min;
var column = min;
while(row <= max)
{
while(column <= max)
{
if(aliveCells.contains(Cell(row,column)))
{
output.append('X')
}
else
output.append('-')
column = column + 1
}
output.append("n");
column = min
row = row + 1
}
//remove the last new line addded.
val indexOfNewLine = output.lastIndexOf("n");
if( -1 != indexOfNewLine )
output.delete(indexOfNewLine,output.length());
return output.toString();
}
override def toString = {
val min = aliveCells.iterator.flatMap(c => Seq(c.row, c.column)).min
val max = aliveCells.iterator.flatMap(c => Seq(c.row, c.column)).max
(min to max) map { row =>
(min to max) map (col => if (aliveCells(Cell(row, col))) "X" else "-") mkString
} mkString ("n")
}
val minC = aliveCells.iterator.map(_.column).min