Post Reply 
Programming puzzles: processing lists!
05-06-2017, 02:48 AM
Post: #75
RE: Programming puzzles: processing lists!
I finally got around to working on #13. This was more fun than some of the others.

Code:
\<<
  DUP         @ src - source list
  SIZE        @ srcsz - source list size
  1           @ kg - "keep going" boolean
  DUP         @ subsz - sublist size (initially 1)
  0           @ subcnt - quantity of sublists for valid solution
  \-> src srcsz kg subsz subcnt
  \<<
    WHILE
      kg      @ "keep going" flag
    REPEAT
      IF      @ only attempt the current sublist if it fits
        srcsz subsz MOD NOT

      THEN    @ this is a valid sublist size to check

        @ build a test list based on the current sublist size
        src 1 subsz SUB       @ create test sublist based on source
        srcsz subsz /         @ the number of sublists is based on source size
        NDUPN                 @ replicate the sublists
        1 - 1 SWAP START + NEXT @ add the test sublists together

        IF    @ does the test list match the source?
          src ==

        THEN  @ yes, a match
          srcsz subsz / 'subcnt' STO    @ store the matching sublist count
          0 'kg' STO                    @ signal that loop exit condition
        END
      END

      @ increment subsz and signal exit condition if max size reached
      'subsz' INCR
      IF    @ is the next sublist size >= the source list size?
        srcsz \>=

      THEN  @ yes, signal the exit condition
        0 'kg' STO
      END
    END

    @ report final result; either the sublist count or "invalid"
    subcnt DUP "invalid" IFTE

  \>>
\>>
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-06-2017 02:48 AM



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