Post Reply 
Programming puzzles: processing lists!
04-24-2017, 05:03 PM
Post: #24
RE: Programming puzzles: processing lists!
(04-22-2017 11:50 AM)pier4r Wrote:  VS davidM code of the previous post (nice code once again, but this time the operation was complicated)

Mine: @ avg 0.15 secs for 50 lists of 100 elements.
DavidM: @ avg 1.94 secs for 50 lists of 100 elements.

This time I got one back (although with an helper routine).

I thought the intent was to avoid exploding the list and processing the elements on the stack, and I used a very inefficient application of DOSUBS. In retrospect, I've decided that the SUB command should be considered as one of the "list operators" since it has an overloaded function that directly operates on a list. Using that command, I wrote a second version that should perform much better than my first:
Code:
\<<
  @ get list size
  DUP SIZE

  @ determine last position of first subset
  DUP 3 / FLOOR OVER SWAP -

  @ determine indices of last subset
  DUP 1 + ROT

  @ get last subset
  4 PICK UNROT SUB

  @ get first subset
  UNROT 1 SWAP SUB

  @ concatenate the lists
  +
\>>

(04-22-2017 11:50 AM)pier4r Wrote:  Side question: I am not able to save a list in a list, without using another variable.

I mean {1 2 3} {1 2 3} + returns {1 2 3 1 2 3}. What is the operation to return {1 2 3 {1 2 3} } ?

You essentially need to explode the first list, then combine all the elements with →LIST. So something like this would work:
Code:
\<<
  @ I'm using two different lists in this example for clarity
  { 1 2 3 }
  { 4 5 6 }

  @ explode the first list
  SWAP LIST\->

  @ reposition the second list by determining its current position
  DUP 2 + ROLL

  @ determine new list size and implode
  SWAP 1 + \->LIST
\>>
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Programming puzzles: processing lists! - DavidM - 04-24-2017 05:03 PM



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