Problem with arrays

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.

8 Likes

The simplest (and, arguably, fastest) way to shuffle an array is to walk through the array and, for each element, swap it with another.

2 Likes

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!

5 Likes

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… :wink:

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

1 Like

you could create a new array, pick a random value of the original array to add and then remove, and repeat until empty!

3 Likes

Here is a solution

Click on the Show Data Simulator to see the array randomized when you press A.

4 Likes

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! :wink:

2 Likes

Thank you all for replying!

3 Likes

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

5 Likes

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?

1 Like

I’m not sure honestly. Could you elaborate a bit further on what you’re trying to do?

2 Likes

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.

2 Likes