# Alternating terms of two lists, as function.

Posted 8 months ago
1426 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 8 months 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 8 months 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 8 months 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 8 months 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 8 months 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 8 months ago
 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.
Posted 7 months 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 7 months ago
 This function has been improved and subsequently optimized and adapted to be able to appear in the repository, thank you Wolfram Repository Team. It now can be found in the function repository with the name AlternateElements or use it as ResourceFunction["AlternateElements"]:https://resources.wolframcloud.com/FunctionRepository/resources/AlternateElementsI preferred to keep it running with two lists at a time to be able to work with binary lists, for more details of the function and its properties ( for example: its use with random numbers, hybridization of numerical constants with alternate digits, and others.. ), as well as an example notebook, enter this link above or visit the page of the Wolfram Function Repository.Thanks also to Sander and Hans for the replies.
Posted 8 months 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. >>