Post Reply 
Programming puzzles: processing lists!
05-01-2017, 05:45 PM
Post: #61
RE: Programming puzzles: processing lists!
(04-28-2017 09:51 PM)pier4r Wrote:  #6 (that is very similar to #10, my bad)
Pier: 3.04 seconds (impressive the slow down even using DOSUBS)
DavidM: waiting for fix, or using the result for #10

Rather than totally rewrite #6, I simply changed a couple of things to address the issue of a "homogenous" list. While I was at it, I went ahead and also handled a list of only one number, which I assumed to be valid in this case. These changes make my approach even slower than before, though not significantly. Still no speed demon here. :-)

Code:
\<<
  \-> src
  \<<
    src
    IF
      @ does the list contain more than 1 number?
      DUP SIZE 1 >
    THEN
      @ the source list has to have all equal numbers grouped together
      SORT

      @ compare two at a time
      2

      @ for each pair of elements:
      \<<
        @ if this is the first iteration, position a "1" as the running total
        IF
          NSUB 1 ==
        THEN
          1 UNROT
        END

        @ if these two numbers are the same, increment the existing running total
        IF
          ==
        THEN
          1 +
        ELSE
          @ the numbers are different, so leave the current running total for the
          @ result on the stack and start a new running total for the next number
          1
        END
      \>>
      DOSUBS

      IF
        @ more than 1 unique number was encountered
        DUP SIZE 1 >
      THEN
        @ convert the current result list to one that has:
        @ 0's for each pair that matches
        @ 1's for each pair that doesn't match
        \<< \=/ \>> DOSUBS

        @ sum up all the integers in the result.  If the total isn't 0, then
        @ the list didn't match the stated criteria.
        \GSLIST
      ELSE
        @ only 1 number represented, so the list was valid
        DROP 0
      END

      "invalid" 'src' IFTE

    END

    @ a source list with only 1 number is assumed to be valid
  \>>
\>>

Looks like you have some impressive times in your results! I've been working on some non-related things lately that haven't allowed for much time to devote to this. I'll give this another look now as I'm interested in seeing the approaches others have used to the problems I've already attempted. Still won't look at the ones I haven't yet tried, though.
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Programming puzzles: processing lists! - DavidM - 05-01-2017 05:45 PM



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