Skip to content

Instantly share code, notes, and snippets.

View ignaciomosca's full-sized avatar
🎯
Focusing

Ignacio Mosca ignaciomosca

🎯
Focusing
View GitHub Profile
@ignaciomosca
ignaciomosca / Diagonal Difference
Created February 16, 2018 18:43
HackerRank Challenge
def diagonalDifference(a: Array[Array[Int]]): Int = {
val dimension = a.length
val primaryDiagonal = for{
x <- 0 until dimension
y <- 0 until dimension
if x==y
}yield a(x)(y)
val secondaryDiagonal = for{
x <- 0 until dimension
@ignaciomosca
ignaciomosca / postmortem.md
Created April 9, 2018 16:53 — forked from mlafeldt/postmortem.md
Example Postmortem from SRE book, pp. 487-491

Shakespeare Sonnet++ Postmortem (incident #465)

Date

2015-10-21

Authors

  • jennifer
  • martym
public abstract class ChessPiece{
private int row;
private int col;
public abstract char piece();
}
public class Bishop extends ChessPiece {
public boolean attacks(ChessPiece dest) {
return Math.abs(dest.getRow() - this.getRow()) == Math.abs(dest.getCol() - this.getCol());
}
@Override
public char piece() {
return 'B';
}
public class Board {
private int M;
private int N;
private char[][] board;
private ArrayList<ChessPiece> usedPieces;
}
/***
* @param board ChessBoard
* @param pieces Chess Pieces selected by the user
* @param solutions valid solutions to the problem
* @param testedConfigurations Board configurations to which solutions were not found
* @return return a list of possible solutions to the problem in the form of a list of filled chess boards
*/
public static Set<Board> solution(Board board, List<Character> pieces, Set<Board> solutions, Set<Board> testedConfigurations) {
if (!pieces.isEmpty()) { // if pieces is empty I return the solutions
for (int i = 1; i < board.getM(); i++) {
data PieceType = Rook | Bishop | Knight | Queen | King deriving(Show, Eq, Ord)
data ChessPiece = Piece {row:: !Int, col:: !Int, piece:: PieceType} deriving(Eq, Ord)
data Board = Board{m:: !Int, n:: !Int, usedPieces:: Set ChessPiece , numberOfPieces:: !Int} deriving(Eq, Ord)
findCandidate :: ChessPiece -> Board -> Set Board
findCandidate p b
= Data.Set.fromList $
[ newBoard
| rr <- [1 .. (m b)]
, cc <- [1 .. (n b)]
, let pp = createPiece (piece p) rr cc
, let newBoard = place b pp
, isSafe b pp]
done::Board->Bool
done Board{usedPieces = up, numberOfPieces=n} = length up == n
package main
import "fmt"
type ChessPiece struct {
row int
col int
piece rune
}