Re: programming comptition II

*To*: mathgroup at smc.vnet.net*Subject*: [mg5049] Re: [mg5043] programming comptition II*From*: fransm at win.tue.nl (Frans Martens)*Date*: Wed, 23 Oct 1996 01:38:11 -0400*Sender*: owner-wri-mathgroup at wolfram.com

Hello, Xah Lee mentioned two programming problems with solutions. Begin forwarded message: >From: Xah Lee <xah at best.com> >Subject: [mg5043] programming comptition II Two pure programing problems some of you will enjoy. Problem One: Constructing Dimensions. Given a one dimensional list, how to partition it so it becomes a nested list with a specified Dimensions. Example and solution: Array[f, {5,2,3}] First@ Fold[Partition, Flatten@ %, Reverse at {5,2,3} ] %% === % -------------------------------------- Problem Two: Pairing of Nodes. Given two list of the same Dimensions, how to combine the list so that the end nodes of the new list pair the end nodes of given lists. Example and solution: Clear[li1, li2] li1 = Array[f, {2,3,4}] li2 = Array[g, {2,3,4}] Transpose[ {li1, li2}, RotateRight@ Range@ Length@ Dimensions@ {li1, li2}] -------------------------------------- A solution for problem Two: Times[li1,li2] This solution works also for general nested lists with the same structure, thus not only for tensors. For problem One I have a small improvement. Array[f, {5,2,3}] Fold[Partition, Flatten@ %, Reverse at {2,3} ] % === %% In other words: ff[b_,c:{__}]:= Fold[Partition,b,Reverse[Drop[c,1]]] Frans Martens Eindhoven University of Technology The Netherlands