Post Reply 
Programming puzzles: processing lists!
04-21-2017, 12:43 AM
Post: #12
RE: Programming puzzles: processing lists!
Many of the list processing shortcuts tend to misbehave if you provide them with lists containing no elements, 1 element, or invalid elements. For these challenges, I'm taking you at your word that the input won't include any of those situations.

I've played around with the first 12 of these so far, but am only including the first few here. My focus has been more on using the list commands than on performance considerations, and I suspect that most of what I've done could be sped up with a more brute-force approach.

Here's what I came up with for the first few:
Code:
@@@ SPOILER ALERT










Ch01
\<<
  DUP \<< == \>> DOSUBS
  \GSLIST "invalid" IFT
\>>

Ch02
\<<
  1 \<< 3 == 4 7 IFTE \>> DOLIST
\>>

Ch03
\<<
  \-> src
  \<<
    src SIZE DUP 3 / FLOOR - \-> bp
    \<<
      src 1 \<< NSUB bp \<= \<< DROP \>> IFT \>> DOSUBS
      src 1 \<< NSUB bp > \<< DROP \>> IFT \>> DOSUBS
      +
    \>>
  \>>
\>>

Ch04
\<<
  1 \-> carry
  \<<
    REVLIST 1 \<< carry + DUP 1 > 'carry' STO 1 == \>> DOLIST
    IF carry THEN 1 + END
  \>>
  REVLIST
\>>

Ch05
\<<
  0 \-> carry
  \<<
    REVLIST 1 \<< carry + 1 + DUP 1 > 'carry' STO 2 MOD \>> DOLIST
  \>>
  REVLIST
\>>

As I mentioned above, I've intentionally coded these with the assumption that the input is exactly as described. In particular, I haven't done any kind of validation that the input contains the proper numbers. As such, the code is only functional for valid input, and will fail miserably with unexpected input. For that matter, it may fail with valid input, too. Smile But it at least worked for the test cases I gave it.
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-21-2017 12:43 AM



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