Lists, ops, nops...

Q: Write a procedure OddEven that takes a list L of integers and returns a list of the odd-numbered entries followed by the even-numbered entries.  So, for example,
OddEven([1,3,5,7,9,11]);
should return [1,5,9,3,7,11].  (Note: unless you find a clever solution you will need a several-line procedure using proc..end proc.   You will perhaps need an if statement so that you can deal with the case where the list has an odd number of elements and where it has an even number of elements separately.)

 

 

My ideas...

1) Define 'i' to be even, 'j' to be odd, then use A:=L[i] and B:=L[j] (somehow...) to create 2 lists of the even and odd-numbered entries and put A and B together as a sequence...

Have tried using the assume function and type(i,odd)=true function but i really have no idea what im doing...

2) Using the 'by' function to count every 2nd term starting from 0 for even numbers and 1 for odd numbers. Dont think that would work tho...

3) Some sort of L -> [  L[1] , L[3] , .. , L[n] ] for the odd numbers... similar for even... not sure how to define n, maybe  something like... if n mod 2 = o do => n => else => n-1?

 

Help!

 

hint

Try the seq procedure with the optional third argument.

Gotta be honest, that hasn't

Gotta be honest, that hasn't really helped. Optional 3rd argument? You mean the third idea i stated? If the answers yes then i still have no clue. I know the sequence operator plays a part but im gonna need a bigger hint than that...

maybe this will

I meant the optional third argument to the procedure seq. For example,

L := [seq(1..10)];         
                                L := [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

seq(L[i], i=1..nops(L), 3);
                                              1, 4, 7, 10

seq(L[i], i=2..nops(L), 3); 
                                                2, 5, 8


OddEven

here is a solution with seq:

f1 := (i, l) -> if `mod`(i,2) = 1 then l[i] end if;

f2 := (i, l) -> if `mod`(i,2) = 0 then l[i] end if;

OddEven := L -> [seq(f1(i,L),i = 1 .. nops(L)), seq(f2(i,L),i = 1 .. nops(L))];

OddEven([1,3,5,7,9,11]);
[1, 5, 9, 3, 7, 11]
 

 

 

 

Robert Israel's picture

One-line solution

If you want a one-liner,

> OddEven:= L -> seq(L[i],i=map(op,[selectremove](type,[$1..nops(L)],odd)));

Warning: if you submit this, your prof is likely to guess that it's not your own work.

JacquesC's picture

Another one-liner

[sorry, I have fallen behind my postings]

OddEven := L -> [seq([L[],L[2..-1][]][2*i-1],i=1..nops(L))];

flawed

That fails when the number of elements is odd.

Robert Israel's picture

Patched one-liner

A bit less elegant?:


OddEven := L -> [seq([L[],op(select(type,{nops(L)},odd)),L[2..-1][]][2*i-1],i=1..nops(L))];
Robert Israel's picture

or perhaps...

OddEven := L -> [seq([L[],1$-(-1)^nops(L),L[2..-1][]][2*i-1],i=1..nops(L))];
JacquesC's picture

like that, but without an extra element

OddEven := L -> [seq([L[],L[(2+(-1)^nops(L))..-1][]][2*i-1],i=1..nops(L))];

Thanks very much guys very

Thanks very much guys very helpful. Its not an actual assignment or anything, just part of a maple tutorial im working through.

OddEven

In that case, here is what I was hinting at:

OddEven := proc(L::list)
local i;
     [seq(L[i],i=1..nops(L),2), seq(L[i],i=2..nops(L),2)];
end proc:

Comment viewing options

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