[Date Index]
[Thread Index]
[Author Index]
Re: Programming: List Structure Manipulation
*To*: mathgroup at christensen.cybernetics.net
*Subject*: [mg930] Re: Programming: List Structure Manipulation
*From*: Count Dracula <lk3a at kelvin.seas.virginia.edu>
*Date*: Wed, 3 May 1995 00:09:15 -0400
*Organization*: University of Virginia
In article 250 of comp.soft-sys.math.mathematica
rubin at msu.edu (Paul A. Rubin) writes:
> In article <3nck7m$a07 at news0.cybernetics.net>,
> Xah Y Lee <xyl10060 at fhda.edu> wrote:
> ->Can anyone show me better ways to write the following function?
> ->
> ->fun[listA_, styleList_]:=
> -> Table[
> -> Append[
> -> Flatten@{ First@RotateLeft[styleList,t-1] }, listA[[t]]
> -> ],
> -> {t, Length@listA}
> -> ]
> ->
> [snip]
> The following, somewhat cumbersome, definitions produce a function "fun"
> (defined, for convenience, in terms of another function "fun1") which
> matches all your examples and seems tolerably quick. There is probably a
> more efficient way of coding it, but at the moment this is the best that
> comes to mind.
>
> Clear[ fun, fun1 ];
> fun1[ x_List, y_List ] := MapThread[ fun1, {x, y} ] /;
> Length[ x ] == Length[ y ]
> fun1[ x_List, y_List ] :=
> Module[
> {z = Partition[ x, Length[ y ] ], w},
> w = Drop[ x, Length[ Flatten[ z, 1 ] ] ];
> If[ w != {}, z = Append[ z, w ] ];
> fun1[ #, y ]& /@ z
> ]/; Length[ x ] > Length[ y ]
> fun1[ x_List, y_List ] :=
> fun1[ x, Take[ y, Length[ x ] ] ] /; Length[ x ] < Length[ y ]
> fun1[ x_, y_ ] := Flatten[ {y, x} ] /; Head[ x ] =!= List
> fun[ x_, y_ ] := Flatten[ fun1[ x, y ], 1 ]
>
An alternative definition is:
func[index_, letters_] :=
Flatten /@ Inner[List, Take[Join @@ Array[letters &,
Ceiling[Length[index]/Length[letters]]], Length[index]], index, List]
The following are the test cases offered by Xah Y Lee:
test1 := func[Range[0,5], {a}] == {{a, 0}, {a, 1}, {a, 2}, {a, 3}, {a, 4}, {a, 5}}
test2 := func[Range[0,5], {a,b,c}] == {{a, 0}, {b, 1}, {c, 2}, {a, 3}, {b, 4}, {c, 5}}
test3 := func[Range[0,10], {{a1, a2}, b}] ==
{{a1, a2, 0}, {b, 1}, {a1, a2, 2}, {b, 3}, {a1, a2, 4}, {b, 5}, {a1, a2,
6}, {b, 7}, {a1, a2, 8}, {b, 9}, {a1, a2, 10}}
test4 := func[Range[0,10], {{a1, a2}, {{b}}, {c1,c2} }] ==
{{a1, a2, 0}, {b, 1}, {c1, c2, 2}, {a1, a2, 3}, {b, 4}, {c1, c2, 5},
{a1, a2, 6}, {b, 7}, {c1, c2, 8}, {a1, a2, 9}, {b, 10}}
The function works as needed (at least for these tests):
In[3]:= {test1, test2, test3, test4}
Out[3]= {True, True, True, True}
--
___________________________________________________________________________________
Levent Kitis lk3a at cars.mech.virginia.edu lk3a at kelvin.seas.virginia.edu
University of Virginia Department of Mechanical, Aerospace and Nuclear Engineering
___________________________________________________________________________________
Prev by Date:
**Re: Strange answer from Eigensystem[]!**
Next by Date:
**Options and FilterOptions**
Previous by thread:
**Re: Strange answer from Eigensystem[]!**
Next by thread:
**Re: Programming: List Structure Manipulation**
| |