Generation

generate functionSun, 13 Nov 2022

Return adjacents from a given element of a matrix

fun adjacents(i: Int, j: Int) : List<Pair<Int, Int>> { val adjs = mutableListOf<Pair<Int, Int>>() if (i > 0) adjs.add(Pair(i-1, j)) if (j > 0) adjs.add(Pair(i, j-1)) if (i < 2) adjs.add(Pair(i+1, j)) if (j < 2) adjs.add(Pair(i, j+1)) return adjs } val matrix = arrayOf(intArrayOf(1, 2, 3), intArrayOf(4, 5, 6), intArrayOf(7, 8, 9)) adjacents(0, 0)

Questions about programming?Chat with your personal AI assistant