Post Reply 
Simple challenge in RPL, Extract the odd elements from a list
08-16-2017, 12:28 PM (This post was last modified: 08-16-2017 08:29 PM by compsystems.)
Post: #1
Simple challenge in RPL, Extract the odd elements from a list
Extract the odd elements from a list, with commands: SUB, MAP, AXL, TRAN others

example https://www.mathworks.com/help/symbolic/factor.html

with FOR cmd
PHP Code:
«
  
'F' STO
  1 
'K' STO
  0 
'S' STO
  
{ } 'L' STO
  F SIZE 
'S' STO
  1 S
  
FOR
   
K F K GET 'L' STO+
   
2
  STEP
  F
  L REVLIST
» 
example1
input:
'x^4-1' FACTORS
{ 'x+1' 1. 'x-1' 1. 'x+i' 1. 'x-i' 1. }
output
{ 'x+1' 'x-1' 'x+i' 'x-i' }

example2
input:
'x^4+x^3-6*x^2-4*x+8' FACTORS
{ x-2, 1,x-1, 1, x+2, 2 }
output
{ x-2, x-1, x+2 }

With SEQ cmd
PHP Code:
«
 
'F' STO
  
'K' PURGE
  F SIZE 
'S' STO
  « F K GET »
  K
  1
  S
  2
  SEQ 
» 
Find all posts by this user
Quote this message in a reply
08-16-2017, 12:54 PM (This post was last modified: 08-16-2017 07:09 PM by compsystems.)
Post: #2
RE: Simple challenge in RPL
A simpler code than the next one?

with DOLIST
PHP Code:
«
  
'F' STO
  1 
'K' STO
  
{ } 'L' STO
  F SIZE 
'S' STO
  F
  1
  « F K GET 
'L' STOK 2 'K' STO K S >= « CLEAR L REVLIST KILL » IFT »
  DOLIST
  L REVLIST
» 
Find all posts by this user
Quote this message in a reply
08-16-2017, 01:41 PM
Post: #3
RE: Simple challenge in RPL, Extract the odd elements from a list
is DOSUBS allowed?

Code:
\<<
  1
  \<< NSUB 2 MOD NOT DROPN \>>
  DOSUBS
\>>

Cheers, Werner

41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE
Find all posts by this user
Quote this message in a reply
08-16-2017, 01:50 PM (This post was last modified: 08-16-2017 01:50 PM by pier4r.)
Post: #4
RE: Simple challenge in RPL, Extract the odd elements from a list
With DOSUBS/DOLIST I guess it will be the fastest way.

I though DavidM made a command to specify multiple position to pick elements from a list (like { 3 5 7 9 11 13 } { 2 4 } LGET -> { 5 9 } . Note that in this case LGET would be short of a sort of generalized SUB ) but it is not the case, so at most one can use LHDTL to go through the list.

edit: and Werner found a very nice way.

Wikis are great, Contribute :)
Find all posts by this user
Quote this message in a reply
08-16-2017, 06:27 PM
Post: #5
RE: Simple challenge in RPL, Extract the odd elements from a list
(08-16-2017 01:41 PM)Werner Wrote:  
Code:
\<<
  1
  \<< NSUB 2 MOD NOT DROPN \>>
  DOSUBS
\>>

Werner's is probably the most flexible standard RPL approach you will find. It doesn't care if the total number of elements is even or odd, it leaves just the odd ones in the result.

(08-16-2017 01:50 PM)pier4r Wrote:  I though DavidM made a command to specify multiple position to pick elements from a list (like { 3 5 7 9 11 13 } { 2 4 } LGET -> { 5 9 } . Note that in this case LGET would be short of a sort of generalized SUB ) but it is not the case, so at most one can use LHDTL to go through the list.

If I was to use the ListExt library AND I knew that number of objects in the list was even, I would use this to extract the odd ones:
Code:
2 LDST HEAD
Changing HEAD to TAIL would result in the even-numbered elements. Or for a more "general purpose" approach, you could get every "nth" element (provided the list size is an even multiple of n) by placing n on the stack and executing the following:
Code:
LDST LRLL HEAD
Find all posts by this user
Quote this message in a reply
08-16-2017, 07:06 PM
Post: #6
RE: Simple challenge in RPL, Extract the odd elements from a list
Hi

Code:

\<<
  1
  \<< NSUB 2 MOD SWAP IFT\>>
  DOSUBS
\>>

Same size, same idea that the program of Werner but a _little_ faster :

0.8561s for a list of 100 items vs 0.879s
Find all posts by this user
Quote this message in a reply
08-16-2017, 07:13 PM (This post was last modified: 08-16-2017 07:22 PM by compsystems.)
Post: #7
RE: Simple challenge in RPL, Extract the odd elements from a list
Ok with DOSUBS, waiting other alternatives (converting to array, tran, etc.) or optimizations with DOLIST

PHP Code:
'x^4-1' FACTORS
«
  1
  « NSUB 2 MOD SWAP IFT »
  DOSUBS
» 
Find all posts by this user
Quote this message in a reply
08-16-2017, 07:38 PM (This post was last modified: 08-16-2017 08:00 PM by Gilles59.)
Post: #8
RE: Simple challenge in RPL, Extract the odd elements from a list
(08-16-2017 07:13 PM)compsystems Wrote:  Ok with DOSUBS, waiting other alternatives (converting to array, tran, etc.) or optimizations with DOLIST

PHP Code:
'x^4-1' FACTORS
«
  1
  « NSUB 2 MOD SWAP IFT »
  DOSUBS
» 

Hi Compystems,

With your example, you can also do

Code:
 « DUP « TYPE »  MAP SWAP IFT »

Of with GoferList, just :

Code:
 « « TYPE »  Filter »

These filters only the items that are not real numbers from the list.
Find all posts by this user
Quote this message in a reply
08-16-2017, 07:45 PM (This post was last modified: 08-16-2017 09:41 PM by pier4r.)
Post: #9
RE: Simple challenge in RPL, Extract the odd elements from a list
(08-16-2017 06:27 PM)DavidM Wrote:  If I was to use the ListExt library AND I knew that number of objects in the list was even, I would use this to extract the odd ones:
Code:
2 LDST HEAD
Changing HEAD to TAIL would result in the even-numbered elements. Or for a more "general purpose" approach, you could get every "nth" element (provided the list size is an even multiple of n) by placing n on the stack and executing the following:
Code:
LDST LRLL HEAD

Grammar question from non-native side: why did you use "if I was to use" instead of "if I had to use" ?

Anyway thanks for the info! I nevertheless drop a little objection reusing what someone said in another topic (maybe an old topic that I read while reading the general forum).

It is nice to know relatively short codes to define functionality over an existing library, but wouldn't be it more reasonable to have those - the quite generic ones - included in the library instead of having everyone build a library extension by himself? Especially if the author himself is so neat to offer the solution!

To me it seems like:
We have the set of functions A (from the userRPL builtin library), we can solve a lot with those but not that fast sometimes, so someone extends them with a library (or libraries) offering the set of functions B (see ListExt or Goferlist). Then there are still generic functions left out A and B that are discussed but not implemented in B, so one has to combine the functions in A and B to have his own library C of further generic functions.

Of course the above is possible and it is fun, but personally I'd like to see the additional functions in B.

Sure, if one does not find the functions of C in A or B, it is better than nothing to have them in one's own written library.

Said this, to emphasize that the objection was little: :hug:

Wikis are great, Contribute :)
Find all posts by this user
Quote this message in a reply
08-16-2017, 08:52 PM
Post: #10
RE: Simple challenge in RPL, Extract the odd elements from a list
(08-16-2017 07:45 PM)pier4r Wrote:  Grammar question from non-native side: why did you use "if I was to use" instead of "if I had to use" ?

Well, first: I am very appreciative that so many here (including yourself) go to the trouble to participate in this forum using a non-native language. It makes it somewhat ironic that I, as a native English speaker, screwed this one up as I did. If we're going to be more grammatically correct I should have said "if I were to use" instead. Smile But I wouldn't have said "had to use", as that is an idiom that would imply that I had no choice in the matter, which is distinctly different than saying "had used" which would simply mean that I had done it in the past. Language is fun, isn't it?

(08-16-2017 07:45 PM)pier4r Wrote:  Anyway thanks for the info! I nevertheless drop a little objection reusing what someone said in another topic (maybe an old one that I read while reading the general forum)...

...Said this, to emphasize that the objection was little: :hug:

I guess I'm not following what you're actually objecting to in my earlier post. Is there something wrong with my pointing out how LDST (and LRLL) could be used to achieve similar results? I was actually just trying to show the use of two commands that people might not have otherwise considered for this type of situation. Or is there something else?
Find all posts by this user
Quote this message in a reply
08-16-2017, 09:41 PM
Post: #11
RE: Simple challenge in RPL, Extract the odd elements from a list
(08-16-2017 08:52 PM)DavidM Wrote:  Or is there something else?

Sorry, it is likely I wrote a confused rambling.

I wanted to convey: why don't you put the functions that you suggested in your library of functions ? Even if they are "short", they would be centralized in one library and people don't have to implement them again..

Wikis are great, Contribute :)
Find all posts by this user
Quote this message in a reply
08-17-2017, 12:26 PM (This post was last modified: 08-18-2017 12:11 PM by John Keith.)
Post: #12
RE: Simple challenge in RPL, Extract the odd elements from a list
Never mind, misread original post. :-(
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)