sequence and separate out individual numbers from two multiple digit numbers.

I want to take two numbers, for example 37665432 and 59374696 (later extrapolate to 3 or 4 etc... sets of numbers) and take each individual number alternately and put them into a list. 

Here's what I have

a:=37665432;

b:=59374696;

c:=convert(a,base,10);

d:=convert(b,base,10);

e:=ListTools:-Reverse(c);

f:=ListTools:-Reverse(d);

So now I have the numbers all in a list and I want to select each one alternately into another list.

g:=seq(e[i],f[i],i=1..8); 

Error, invalid input: seq expects its 3rd argument, step, to be of type numeric, but received i = 1 .. 3

I can put square brackets around e[i],f[i] like this

g:=seq([e[i],f[i]],i=1..8); and I get

[3,5],[7,9],[6,3], ... etc.  but that's not exactly what I want. 

How can I get it into a list like this [3,5,7,9,6,3,...etc]

 

Axel Vogt's picture

prevent evaluation

Have not looked into ListTools, but the following seems to be what you are looking for:
  g:=[seq('(e[i],f[i])',i=1..8)]; 
        g := [3, 5, 7, 9, 6, 3, 6, 7, 5, 4, 4, 6, 3, 9, 2, 6]

Robert Israel's picture

zip, or Interleave

Another way, perhaps slightly more elegant:

g := zip(()->args, e, f);

Or you can use

g := ListTools:-Interleave(e,f);
alec's picture

strings

I would do such things with strings - using sprintf instead of converting to base 10, and then using StringTools.

Alec

mathematical manipulation

If I use stringtools I couldn't mathematically manipulate the numbers. 

mathematical manipulation

If I use stringtools I couldn't mathematically manipulate the numbers. 

mathematical manipulation

If I use stringtools I couldn't mathematically manipulate the numbers. 

alec's picture

parse

Why not? You can use parse for that.

Alec

Parse

Sorry for the multiple posts, my connection was interrupted. 

And thank you for that command!  I've been searching for ages to figure something out, you've finally solved it for me.

A while back I was working on something using string tools and plotted numbers from it but later forgot how I did it.  The only thing I found was the convert(x,base,10) command which eventually did what I wanted but not what I had done earlier but it was so annoying that I couldn't figure it out and eventually I gave up trying to find out what commands I used.   Now I realize to convert a string number to it's integer value to use parse.  B-E-A-utiful!  Thanks again. 

This ties up nicely the last question I asked at the end of this thread. 

Is there another way to separate?

Awesome!  Thanks.  I had to use ListTools :- Reverse because convert reverses the order of the number. 

Can the digits of a number be separated in another way other than using the convert command?

other ways

N := 1234567:
seq(irem(N,10,'N'),i=1..length(N));
                              7, 6, 5, 4, 3, 2, 1

If you convert to a string then parse, you don't need to reverse the order:

N := 1234567:                       
seq(parse(d), d = sprintf("%d",N)); 
                              1, 2, 3, 4, 5, 6, 7

alec's picture

Matrix way to Interleave

Also, interleaving of e and f can be done as

g:=[seq(i,i in Matrix([e,f]))];
        g := [3, 5, 7, 9, 6, 3, 6, 7, 5, 4, 4, 6, 3, 9, 2, 6]

or

g:=convert(Matrix([e,f]),compose,Vector,list);
        g := [3, 5, 7, 9, 6, 3, 6, 7, 5, 4, 4, 6, 3, 9, 2, 6]

That may be convenient for more than 2 numbers. ListTools:-Interleave can be used for more than 2 lists though, too.

Alec

list and expseq

Thanks.  I noticed that in Joe's method, the output doesn't contain square brackets.  Running the whattype shows it as an exprsseq, and in Alec's method a list. 

I can force Joe's output to a list by putting square brackets around the seq.  If I don't is there really any difference when I try to manipulate the output? 

alec's picture

Interleave

The best way, I think, is Interleave in Robert Israel's post. It is, generally, the same seq as in your original post, but it is easier to work with. My Matrix examples were mostly just for curiosity.

Alec

exprseq

Manipulating an exprseq can be tricky; you have to be careful when passing a sequence to a procedure because Maple then interprets it as separate arguments, which may not be what you want. Also, be careful when doing something like

exprseq := [a,b],[c,d]:
for elim in exprseq do ... end do;

This may not do what you want if the exprseq contains only one sublist, it will then "unpack" the list.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
}