wolfram-language

How to reverse multiple lists using the Wolfram-Language


I'm trying to find the best way to solve the question: "Use Range, Reverse and Join to create {3, 2, 1, 4, 3, 2, 1, 5, 4, 3, 2, 1}"

So basically the given lists are {1, 2, 3, 4, 5}, {1, 2, 3, 4}, {1, 2, 3}.

I could easily solve this question but wanted to know if there is a better way (more efficient) than what i've come up with.:

My Solutions:

In[136]:= Join[ Reverse[Range[3]], Reverse[Range[4]], Reverse[Range[5]] ]

In[141]:= Reverse[Join[ Range[5], Range[4], Range[3] ]]

given lists: {1, 2, 3, 4, 5}, {1, 2, 3, 4}, {1, 2, 3}, where you have to use the functions Range, Reverse and Join to create the expected output:

{3, 2, 1, 4, 3, 2, 1, 5, 4, 3, 2, 1}

My solution will not be efficient if there were to be 100 lists instead of three.

Thanks in advance for the help


Solution

  • RUNNING EACH ELEMENT OF A LIST THROUGH A FUNCTION:

    listA = {}
    Function[x, listA = Join[listA,  x]] /@ {Range[5], Range[4], Range[3]}
    listB = Reverse[listA]
    Clear[listA]
    

    output:

    result -> listB: {3, 2, 1, 4, 3, 2, 1, 5, 4, 3, 2, 1}