Post Reply 
Programming puzzles: processing lists!
04-26-2017, 01:30 AM
Post: #45
RE: Programming puzzles: processing lists!
(04-24-2017 08:27 PM)pier4r Wrote:  Please provide also the rest of the challenges otherwise I challenge myself (not bad, but still).

Here's my attempt at #6:
Code:
\<<
  @ 'src' is the source list
  @ 'tot' is the count of the current list element being checked
  1 \-> src tot
  \<<
    @ the source list has to have all equal numbers grouped together
    src SORT

    2 @ compare two at a time

    @ for each pair of elements:
    \<<
      IF
        @ are these both the same?
        ==
      THEN
        @ the numbers are equal, so increment the total for this number
        1 'tot' STO+
      ELSE
        @ the numbers are different, so leave the current running total for the
        @ result and start a new running total for the next number
        tot
        1 'tot' STO
      END
    \>>
    DOSUBS

    @ add the final total to the result list
    tot +

    @ at this point, we have a list result that shows the counts of
    @ each unique list element encountered

    @ convert the current result list to one that has:
    @ 0's for each pair of counts that matches
    @ 1's for each pair that doesn't match
    \<< \=/ \>> DOSUBS

    @ sum up all the integers in the above result.  If the total isn't 0, then
    @ the list didn't match the stated criteria.
    \GSLIST "invalid" 'src' IFTE
  \>>
\>>

As before, I focused more on using list processing features than performance. This time I used more appropriate line breaks and added comments, so hopefully it will be more readable.

I'll go ahead an include #7 as well. It should look somewhat familiar:
Code:
\<<
  @ get list size
  DUP SIZE

  @ determine last position of first subset
  DUP 2 / IP

  @ determine indices of last subset
  DUP 1 + ROT

  @ get last subset
  4 PICK UNROT SUB

  @ get first subset
  UNROT 1 SWAP SUB
  
  @ add -1 (sentinel) to first subset
  -1 +

  @ concatenate the lists in the proper order
  SWAP +
\>>
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-26-2017 01:30 AM



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