Random array generator? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Random array generator?

Would anyone please write a function that generates a random N by N array of 1 and 0 in such a way that it resembles a path represented by 1? Here's an example of such array: [ [0,1,0,0,0,1], [1,1,0,0,0,1], [1,0,0,1,1,1], [1,1,0,1,0,0], [0,1,0,1,0,0], [0,1,1,1,0,0] ] I would really appreciate it if anybody would write such function in JS, Python, Java or C++. Thank you.

5th Dec 2019, 12:39 AM
Fernando Moceces
Fernando Moceces - avatar
6 Answers
+ 3
What have you tried so far?
5th Dec 2019, 1:18 AM
Denise Roßberg
Denise Roßberg - avatar
+ 2
I think this is what you need C++: https://code.sololearn.com/cffOzL8291Uc/?ref=app Hope this will help you 😊
7th Dec 2019, 12:21 AM
$p@rK
$p@rK - avatar
+ 1
Denise Roßberg this https://code.sololearn.com/WAeB2Bqzd7dP/?ref=app but it only makes S-shaped pattern sadly.
6th Dec 2019, 12:42 AM
Fernando Moceces
Fernando Moceces - avatar
+ 1
Fernando Moceces You can have a look on my code. https://code.sololearn.com/cv99P67rf5ve/?ref=app It is not perfect because I don't check for neighbours. I guess to get a "clear" path each 1 should have max 2 neighbours. I am not sure if it would work when you just create conditions for the directions. * start: random * up/down/left/right: when no 1 occurs, when it is not out of range, when the new 1 has only the old 1 as neighbour * break loop when no move is possible
6th Dec 2019, 11:05 AM
Denise Roßberg
Denise Roßberg - avatar
+ 1
$p@rK very nice but the path needs to be a single continuous path
8th Dec 2019, 12:34 AM
Fernando Moceces
Fernando Moceces - avatar
0
This looks good. I would say now add a second loop to get from bottom to top.
6th Dec 2019, 1:06 AM
Denise Roßberg
Denise Roßberg - avatar