How can realized this? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
0

How can realized this?

There is two-dimensional matrix 10x10, with random values from 0 to 9. How to find the indices of the elements, the value of which: 1) is greater than the sum of all neighbors (vertically and horizontally) 2) occurs no more than once in a column and in a line at the same time. Code for example (local compiler not start this code): fun main(){ val matrix = Array(10) { IntArray(10) } val rand = Random() val result = mutableListOf<Pair<Int, IntĀ»() for (i in matrix.indices) { for (j in matrix[i].indices) { matrix[i][j] = rand.nextInt(10) if (matrix[i][j] > 7){ result.add(Pair(i,j)) } } } for (i in 0..9) { for (j in 0..9) print(matrix[i][j].toString() + "\t") println() } println(result) }

26th May 2021, 8:42 AM
Andrey Rudnev
Andrey Rudnev - avatar
10 Respostas
+ 2
first advice: better indent your code, and share a code playground project link rather than copy-paste it ^^ so, local compiler as well as any compiler would not start your code because of errors ^^ you try to use Random, wich need to be imported: I don't know why, but trying to import package kotlin.random fail in app but success in web :o however, you could use Math.random for your use case ;P also, you cannot call an abstract class, so your rand initialization must not have parenthesis after Random... on next line, you use a special char instead of double closing angular brackets (">>"). once both syntax error corrected, your code run, despite logical mistakes: instead of following requirements, you fill your result with matrix element indices where value is greater than 7 ;P here's a solution I wrote for you: https://code.sololearn.com/cUbolZdo4rxN/?ref=app ... assuming that "neighbors" are not cyclic (ie: cell [9,9] has only two neighbors instead of four if we consider [9,0] and [0,9]
27th May 2021, 1:30 AM
visph
visph - avatar
+ 1
visph It's the best solution, thank a lot! I took note of all your comments. But, if you have time, could you explain how your code works, in detail if possible, you can in a personal. Thanks again!
27th May 2021, 2:05 PM
Andrey Rudnev
Andrey Rudnev - avatar
+ 1
in deep details that may be too long for a post (and I don't have pm as I use a quite old version of sololearn app ;P)... anyway in short: populate and output the matrix with random values (use of same nested loops to do both at once) once matrix populated use another nested loops to check if element is greater than sum of neighbors, if so, loop over its row and column to check if another same value is present... set a flag to false (initialized to true) if so always if first check is right, when second check loop is over and flag remain true, then append indices pair to result. feel free to ask for details on specific parts ;)
27th May 2021, 2:31 PM
visph
visph - avatar
+ 1
visph Good answer, thank you! But what is "var f = true" and in the other line "var f = false"?!
27th May 2021, 2:36 PM
Andrey Rudnev
Andrey Rudnev - avatar
+ 1
that's the flag (f for flag ^^) to keep track of duplicate in row/column check result: if true there's no duplicate (initial value), else set to false ^^
27th May 2021, 2:38 PM
visph
visph - avatar
+ 1
visph oh, i understand, thanks) it's very informative šŸ¤“
27th May 2021, 2:47 PM
Andrey Rudnev
Andrey Rudnev - avatar
+ 1
27th May 2021, 3:13 PM
Andrey Rudnev
Andrey Rudnev - avatar
+ 1
you've got empty result because you never add values to it ;) if you want to log only the first check as I guess you're trying to do, leave only the 'res.add(Pair(i,j))' from the part you've commented (either just after or just before it)
27th May 2021, 3:17 PM
visph
visph - avatar
+ 1
visph yeap, thank you)
27th May 2021, 3:32 PM
Andrey Rudnev
Andrey Rudnev - avatar
0
visph Thank a lot! I try to use it and understand how it works! Thank you for your informative answer!
27th May 2021, 8:09 AM
Andrey Rudnev
Andrey Rudnev - avatar