I've been sitting on this problem for a while and i am kind of stuck. I am pretty new to Mathematica and have been trying to implement a recusive Algorithm, which splits/bisects a list of items in parts recursively. This means, that when the algorithm loops back it should bisect both already bisected parts. The algorithm stops, when there are no elements left to bisect. Regullary I would do after every bisection some calculation which is not important for this question. More importantly is the question how to handle multiple for loops in Mathematica like it is being done in python.
I have an Python-Code which illustrates the algorithm by printing the bisectionsteps. The first picture shows the algorithm without recursion, to illustrate the first iteration. In the second picture I did it with the while loop and print comand so you can see the functionality. As you can see, j iterates the left side, while k iterates through the right side. As I special problem I find to loop through ((0,len(i)//2) which is an array with only two elements and than plugging them in to i[j:k].
Hopefully there is someone who is experienced with both programming languages and can halp me out.
Thank you in advance!
// edit //
Thnak you very much for the help! Since @Rohit Namjoshi asked in the coments for my try and also for some clarification I will post it. I tried the Do-loop to mimic the procedure in Python since I am more used to that logic. Even though the answers are promising they are hard to understand as a beginer, especially building up on the functions is still hard. Maye it could be Done like in Python?! I will show my try for the inner loop whithout recursion.
The python code loops at first through the list. That's why there are two more brackets so that the first loop for i in cItems
gives back the list of numbers itself. In the second step the loops j,k
loop through an range without such that j gives us start values for the interval for the bisection and k the end Values. For the Python case it would look like this
sortIx=["a","b","c","d","e","f","g","h","i","j"]
cItems=[sortIx]
cItems = [i[j:k] for i in cItems for j, k in ((0, len(i) // 2), (len(i) // 2, len(i))) if len(i) > 1]
print(cItems)
with: j=(0,5), k=(5,10)
As you can see in the python code (sorry for the screenshot @High Performance Mark), i[j:k]
such that the first part of the bisection is i[0,5]
and the second i[5,10]
where i
is in the first iteration ["a","b","c","d","e","f","g","h","i","j"
.
Output: [['a', 'b', 'c', 'd', 'e'], ['f', 'g', 'h', 'i', 'j']]
Now my Do-Loop-Trial. I defined two functions to give me the start values of the interval j,k
so i can loop through that range of two values like in python:
startInterval[cItems_] := {1, Ceiling@(Length[cItems]/2) + 1}
endInterval[cItems_] := {Ceiling@(Length[cItems]/2), Length[cItems]}
In[317]:= startInterval[x[[1]]]
endInterval[x[[1]]]
Out[317]= {1, 6}
Out[318]= {5, 10}
My approach with the Do-Loop looks like this:
Do[i[[j ;; k]],
Do[ Items = x[[i]] , {i, 1, Length[x]},
Do[ (start = Items)[[j]], {j, Length[startInterval[Items]]}],
Do[ (end = Items)[[k]], {k, Length[endInterval[Items]]}]]]
They give the right output:
Do[ Items = x[[i]] , {i, 1, Length[x]}]
Items
startInterval[Items]
endInterval[Items]
{3, 1, 6, 2, 5, 10, 7, 4, 9, 8}
{1, 6}
{5, 10}
Does anyone know where the mistake is or can anyone tell me if that try is even possible? I know for most of you it is absurd to use that logic but especially as an beginner it seems easier to me to not loose track. Hopefully I improved my writing and formating. Thank's in advance!