Post Reply 
Programming puzzles: processing lists!
04-26-2017, 04:49 PM
Post: #47
RE: Programming puzzles: processing lists!
OK, I've formatted and commented the others that I've attempted so far.

#8. Very similar to #7 in approach (and appearance):
Code:
\<<
  @ 'src' is the original source list
  \-> src
  \<<
    @ get list size
    src 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

    @ if equal, return source, else "invalid"
    == 'src' "invalid" IFTE
  \>>
\>>

#9:
Code:
\<<
  @ 'src' is the original source list
  \-> src
  \<<

    @ first, make a collapsed list of the number groups

    src HEAD 1 +  @ choose an arbitrary number that can't be the same as the
                  @ first element

    \-> last      @ store it as the "last encountered element"
    \<<
      src @ original list
      1   @ look at each element
      \<<
        IF
          @ current element the same as the last one?
          DUP last ==
        THEN
          @ yes, ignore it
          DROP
        ELSE
          @ no, leave it on stack and update 'last'
          DUP 'last' STO
        END
      \>>
      DOSUBS
    \>>

    @ now, make a list of the counts of the individual groups
    @ 'tot' is the running count of an element
    1 \-> tot
    \<<
      src @ original list
      2   @ compare two at a time

      @ for each pair of elements:
      \<<
        IF
          ==
        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 list
      tot +
    \>>

    @ convert to a list containing 0s for matching pairs, 1s for mismatches
    \<< \=/ \>> DOSUBS

    @ sum the list; anything other than 0 indicates something didn't match
    \GSLIST

    @ add the sum to the collapsed groups list
    +

    @ final result must be { 3 5 3 0 }, otherwise the src list is invalid
    { 3 5 3 0 } == 'src' "invalid" IFTE
  \>>
\>>

#10:
Code:
\<<
  @ 'src' is the original source list
  \-> src
  \<<
    @ create a sorted list of the individual elements encountered

    src SORT DUP HEAD 1 + @ choose an arbitrary number that can't be the same as the first
    \-> src2 last
    \<<
      src2 @ original list
      1   @ look at each element
      \<<
        IF
          @ current element the same as the last one?
          DUP last ==
        THEN
          @ yes, ignore it
          DROP
        ELSE
          @ no, leave it on stack and update 'last'
          DUP 'last' STO
        END
      \>>
      DOSUBS

      @ create a list of the counts of the individual elements in the same order

      1 \-> tot   @ first count is 1
      \<<
        src2 @ original list
        2    @ compare two at a time

        @ for each pair of elements:
        \<<
          IF
            @ does the pair match?
            ==
          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 count to list
        tot +
      \>>
    \>>

    @ if the lists aren't the same, the source was invalid
    == 'src' "invalid" IFTE
  \>>
\>>

#11. Similar approach to others using SUB. Care needed to get the proper indices for even/odd element counts:
Code:
\<<
  @ 'src' is the original source list
  \-> src
  \<<
    @ get list size
    src DUP SIZE

    @ determine last position of first subset
    DUP 2 / CEIL

    @ determine indices of last subset
    OVER 2 / IP 1 + ROT

    @ get last subset, reverse it
    4 PICK UNROT SUB REVLIST

    @ get first subset
    UNROT 1 SWAP SUB

    @ if equal, return source, else "invalid"
    == 'src' "invalid" IFTE
  \>>
\>>

#12. Decided to do this one with a more robust approach than #2. Slower, but behaves better and isn't assumptive:
Code:
\<<
  1   @ check each element by itself
  \<<
    @ check each element for special handling
    CASE
      @ 3 -> 5
      DUP 3 == THEN DROP 5 END
      @ 5 -> 3
      DUP 5 == THEN DROP 3 END
      @ any other element will be left as-is
    END
  \>>
  DOSUBS
\>>
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 04:49 PM



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