Post Reply 
Programming puzzles: processing lists!
05-05-2017, 07:32 PM
Post: #73
RE: Programming puzzles: processing lists!
(05-05-2017 11:51 AM)pier4r Wrote:  So first I had to handle the case that the second dosubs returns a list of only one element, that \GSLIST does not like, If I understood your comments correctly. Therefore I add a 0 to the list returned by dosubs and GSLIST likes it.

Yes, I can't believe I missed that in the version that I copied into the last post. I cleaned it up a bit and went ahead and simply returned 0 (invalid) or 1 (valid) in this version:
Code:
\<<
  SORT                    @ input list must have elements grouped together
  IF                      @ if more than 1 element
    DUP SIZE 1 >
  THEN                    @ yes, more than 1 element in source list
    2
    \<<
      IF                  @ is this the first pair in the list?
        NSUB 1 ==
      THEN
        1 UNROT           @ if so, place the first running total
      END

      IF                  @ if the pair of list elements is the same
        ==
      THEN
        1 +               @ yes, add 1 to current running total
      ELSE
        1                 @ no, make a new running total
      END
    \>>
    DOSUBS

    IF                    @ was there more than 1 unique element?
      DUP SIZE 1 >
    THEN                  @ yes, make sure all counts were the same
      \<< \=/ \>> DOSUBS
      0 +                 @ \GSLIST protection for list with 1 element
      \GSLIST             @ sum the result to check for any inequalities
      NOT                 @ convert sum to result
    ELSE                  @ no, just one unique element repeated multiple times
      DROP                @ the list containing 1 element
      1                   @ list is valid by definition
    END
  ELSE                    @ there was only 1 element
    DROP                  @ drop the source list
    1                     @ list is valid by definition
  END
\>>
I know this particular algorithm isn't the fastest, but I wanted to go ahead and stick with the original plan to see it through.

(05-05-2017 11:51 AM)pier4r Wrote:  If I am not mistaken. The list { 5 5 5 5 5 6 6 6 6 6 } is returned as valid, but it is not.
Also: { 1 2 3 1 2 3 } is valid (but it is not).

I don't understand why those two lists should be considered invalid. Your original challenge for #6 says "verify that every integer in the list appears the same amount of times". Doesn't every integer appear the same number of times in both of those lists? What am I missing?
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-05-2017 07:32 PM



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