How do I add an array to another array but also randomise it? Another way to put it would be how to shuffle an array.
The simplest (and, arguably, fastest) way to shuffle an array is to walk through the array and, for each element, swap it with another.
Hereâs a quick implementation I did, it might not be the most optimal one but in that case Iâm sure @Blobbey or @Vegz78 will submit their better versions.
First I just concatenated the arrays with the add
method, and after that I put the elements of the array into a new array in a random order until all of the elements were depleted. You can take a look at the code yourself (and/or reuse it) here:
Hope this helps!
Since I am too old for crosswords and sudoku, but find challenges like this interesting, hereâs my bid:
Probably not very efficient, waiting for the elders to optimize furtherâŚ
PS: Added sort() just because this is the only such array operation I have found already there in MCA. Sort() modifies the original array, my functions copy. To make ShuffleList() modify the original array instead, remove the tempList entirely and replace it were used with the inputList parameter.
PS2: How to get the native sort() to sort numbers above 9 correctly(9, 10, 11,âŚ)?
PS3: Some more related information:
https://forum.makecode.com/t/anyone-know-how-to-join-two-arrays-together/17660/14
https://forum.makecode.com/t/sorting-algorithm-extension-for-blocks/16260
you could create a new array, pick a random value of the original array to add and then remove, and repeat until empty!
Click on the Show Data Simulator to see the array randomized when you press A.
Rewarding to experience the shared interest on this forum for the great fun of algorithms!!!
Somebody should create a Wordle for algorithms, âThe Daily Algoâ, or something!
Thank you all for replying!
Quick reimplementation of that add
function, I realized that it could be more efficient. Optimized it from O(2n) to O(n) time complexity by removing a loop because itâs unnecessary
Great, @Sarge! I attempted the same, actually on the basis of a previous similar post from you, but I did not manage to do this without modifying the original list1 itself by reference all the way back to before the function callâŚ
Would this be possible with a copy/call by value as well?
Iâm not sure honestly. Could you elaborate a bit further on what youâre trying to do?
Hi @Sarge,
I was trying to add two arrays together without using 2 loops and still keep the original array intact(copy).
I found out from one of your earlier posts that this was possible with concat():
Still, I have not found out how to do this with only blocks/without grey blocksâŚ
Here, illustrated with the functions AddList_loop(), AddList_no_lopp() and AddList_reference(), which are 2 loops, concat and same as yours above, respectively:
As you can see, in the last one - AddList_reference() and the same as yours above -, result seems only to be a reference to array1 via the parameter list1, and the original array1 is overwritten.