2

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

PhillipJacobs
  • 2,337
  • 1
  • 16
  • 32

2 Answers2

1

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}

  • Hi @ShashaHaven. I found the same-like solution in the wolfram v12 documentation. But i like your little way of Clearing the variable. Cause now you can run that same code in the same instance and listB will always have the expected output. – PhillipJacobs Jun 20 '19 at 12:24
  • Tnx @PhillipJacobs! I saw you had an answer. I tried upvoting it but had to low of a reputation. We're were few that answers these wolfram questions, do you know what ? and thanks for the upvote – Shasha Haven Jun 20 '19 at 12:26
  • Shasha, i think most people with wolfram knowledge, uses Mathematica as well and I believe they ask their questions on https://mathematica.stackexchange.com/ ... I'm just trying to learn the Wolfram language so I thought I could ask on here. I saw another guy that seems pretty clued up. @DavidBullock ... I tagged him but not sure if he got the notification. I'll try to tag you next time. – PhillipJacobs Jun 20 '19 at 12:30
  • @ShashaHaven In general, it is more efficient in terms of memory and time to avoid constructs such as `listA = Join[listA, x]` in WL. It is usually much more efficient to use a side effect free functional solution. – Rohit Namjoshi Jun 23 '19 at 01:51
1
Range[#] & /* Reverse /@ {3, 4, 5} // Flatten

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

Update

Someone voted to delete my answer without providing a reason. Perhaps because it did not use Join. To address that

Range[#] & /* Reverse /@ {3, 4, 5} // Apply[Join]
Rohit Namjoshi
  • 669
  • 5
  • 17
  • thanks for your answer. It’s short and powerful but I understand very little of it. Do you have any kind of reference to documentation that’ll explain this characters’ purpose: “#, &, /* ... ...” please – PhillipJacobs Jun 23 '19 at 06:03
  • @PhillipJacobs Take a look at [this](https://mathematica.stackexchange.com/a/25616/58370). And [this](http://www.wolfram.com/language/elementary-introduction/2nd-ed/preface.html), a nice introduction to WL. – Rohit Namjoshi Jun 24 '19 at 01:05
  • This is some great links. Thanks so much. I appreciate it so much! – PhillipJacobs Jun 24 '19 at 07:26