# Alternating terms of two lists, as function.

Posted 12 days ago
244 Views
|
11 Replies
|
13 Total Likes
|
 Hello community. I have created a function and would like to know if it is worth submitting in Function Repository or if there is already something simpler that does this same job? If anyone can give any opinion on this I will be very grateful.I modestly have created a function that can interleave two lists by alternating their terms (unlike Riffle, which only fits the terms into gaps, this function does this keeping the same number of terms as it replaces them by both functions simultaneously). It works like this: If the third term inside the function ("c_") is {} the function does this automatically in a 1 to 1 pattern of each group: Alternate[a_, b_] := Alternate[a, b, {}] Alternate[a_, b_, c_] := PadRight[a* PadRight[ Take[Flatten@Table[If[c == {}, {1, 0}, c], Count[a, _]], Min[Count[a, _], Count[b, _]]], Count[a, _], 1], Max[Count[a, _], Count[b, _]]] + PadRight[b* PadRight[ Take[Flatten@Table[Abs[If[c == {}, {1, 0}, c] - 1], Count[b, _]], Min[Count[a, _], Count[b, _]]], Count[b, _], 1], Max[Count[a, _], Count[b, _]]] r = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}; s = {3, 5, 7, 9, 11, 13, 15, 17, 19, 21}; Alternate[r, s] Alternate[s, r] The function works even with lists of different sizes, keeping the terms in excess unchanged: p = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}; q = {3, 5, 7, 9, 11, 13, 15, 17}; Alternate[p, q] Alternate[q, p] Or you can change the third term ("c") in the function to any pattern (eg: {0,1,1,1}). Where "1" refers to the first term ("a") from within the function while "0" refers to the term ("b") from within the function: t = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}; u = {3, 5, 7, 9, 11, 13, 15, 17}; Alternate[t, u, {0, 1, 1, 1}] Alternate[u, t, {0, 1, 1, 1}] I would like to know if is this a good idea or there is a simpler way to do this? Is it worth sending a repository request?Thank you.
11 Replies
Sort By:
Posted 12 days ago
 Not sure about the name. Perhaps Interweave, because Alternate really means it switches back and forth, but with c some pattern it doesn't 'alternate'. Interweave is like weaving with a pattern. But not necessary of courseâ€¦ Not sure if like the sequence of 0's and 1's. Mathematica indexes the first item as 1, not 0. so 1 and 2 would make more sense I think. Would be nice to expand it to arbitrary amount of arguments. If you give 3 lists or 4 lists it will alternate between all 3 or 4â€¦ I'm not sure about the performance of the function, but the implementation looks quite elaborateâ€¦? the third argument should be optional. You could add a definition:Alternate[a, b] := Alternate[a, b,{}] Such that it 'defaults' to {}. Or using the function Optionalâ€¦
Posted 12 days ago
 Thank you very much for the answer Sander! I really liked the idea of calling Interweave! I will also modify the term for Interweave[a, b ]:=Interweave[a,b,{}]...very good your suggestions. Thanks! I'm going to work a little harder on this function to test it....
Posted 12 days ago
 I would do it something like this: ClearAll[Interweave] Options[Interweave] = {"WeavePattern" -> Automatic}; Interweave[a__List, OptionsPattern[]] := Module[{pat, lsts, len, cur, tooshort, s}, lsts = {a}; pat = OptionValue[Interweave, "WeavePattern"]; If[pat === Automatic, pat = Range[Length[lsts]]]; len = Max[Length /@ lsts]; Table[ tooshort = MapIndexed[If[Length[#1] < i, First[#2], -1] &, lsts]; pat = DeleteCases[pat, Alternatives @@ tooshort]; s = First[pat]; pat = RotateLeft[pat]; lsts[[s, i]] , {i, len} ] ] So testing out: Interweave[ {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}, {3, 5, 7, 9, 11, 13, 15, 17, 19, 21} ] {2, 5, 6, 9, 10, 13, 14, 17, 18, 21} and: Interweave[ {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}, {3, 5, 7, 9, 11, 13, 15, 17} ] {2, 5, 6, 9, 10, 13, 14, 17, 18, 20} and with three arguments: Interweave[ {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}, {3, 5, 7, 9, 11, 13, 15, 17, 19, 21}, {a, b, c, d, e, f, g} ] {2, 5, c, 8, 11, f, 14, 17, 18, 21} and with three arguments and a weave pattern: Interweave[ {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}, {3, 5, 7, 9, 11, 13, 15, 17, 19, 21}, {a, b, c, d, e, f, g}, "WeavePattern" -> {1, 2, 2, 3} ] {2, 5, 7, d, 10, 13, 15, 16, 19, 21} Feel free to use the codeâ€¦ it could definitely be optimized a bitâ€¦
Posted 12 days ago
 O btw, After thinking about it, Alternate might still be a better nameâ€¦ With alternate it is a bit more obvious that it only select a part of eachâ€¦ with interweave it is not so clearâ€¦ so maybe alternate is actually betterâ€¦
Posted 12 days ago
 Thanks again Sander, I will see what improvements I can do, because the idea of several lists is good.. I will study your code to get an idea... Thanks for the replies! Helped a lot!
Posted 12 days ago
 What happens here? I get Table::itform: Argument Count[{2,4,6,8,10,12,14,16,18,20},_] at position 2 does not have the correct form for an iterator. >> 
Posted 12 days ago
 Hello Hans, I could not reproduce this error message here ... the error persisted? ..How did it occur? Thanks for the reply, I am honored by the messages both yours and Sander! Thanks.
Posted 11 days ago
 Hello Claudio,really interesting. I had to replace your Count [ a, _ ] by { Count[ a, _ ] } in your Table-statements, and the code gives your results (this is probably a question of version: I am using Mma 7.0),And when I change Count [ a, _ ] to { Length[ a ] } things work as well. Why did you use Count instead of Length? In[1]:= Alternate[a_, b_] := Alternate[a, b, {}] Alternate[a_, b_, c_] := PadRight[a* PadRight[ Take[Flatten@Table[If[c == {}, {1, 0}, c], {Length[a]}], Min[Length[a], Length[b]]], Length[a], 1], Max[Length[a], Length[b]]] + PadRight[b* PadRight[ Take[Flatten@Table[Abs[If[c == {}, {1, 0}, c] - 1], {Length[b]}], Min[Length[a], Length[b]]], Length[b], 1], Max[Length[a], Length[b]]] r = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}; s = {3, 5, 7, 9, 11, 13, 15, 17, 19, 21}; Alternate[r, s] Alternate[s, r] Out[5]= {2, 5, 6, 9, 10, 13, 14, 17, 18, 21} Out[6]= {3, 4, 7, 8, 11, 12, 15, 16, 19, 20} 
Posted 11 days ago
 Hans, this is a good question ... for me it works both ways, it was just a matter of choice ... ..but comparing the two versions of the function yours turned out slightly faster! Thanks for the tip ... I will do more tests to know all the capabilities of this function and every tip is very welcome!
 A practical example of how the Alternate function can be used:We can take any two real numbers and create hybrid numbers between them by alternating the digits between the two using this Alternate function. Below is an example of how this can be done for a given number of digits "n": n = 100; x = Pi; y = E; N[x, 50] N[y, 50] g = IntegerDigits@IntegerPart[N[x*10^(n - 1), n + 10]]; h = IntegerDigits@IntegerPart[N[y*10^(n - 1), n + 10]]; N[FromDigits@Alternate[g, h]/10^(n - 1), n] N[FromDigits@Alternate[h, g]/10^(n - 1), n] Above is an example of two hybrid numbers created with the help of the Alternate function using these real constants.Note that these "hybrid" numbers are formed by alternating the digits between Pi and E. This given example was done with pattern 1 to 1, or default {1,0} by the function.