Post Reply 
Programming puzzles: processing lists!
04-22-2017, 03:49 PM (This post was last modified: 04-22-2017 09:15 PM by pier4r.)
Post: #22
RE: Programming puzzles: processing lists!
Challenge 4 finished and got a strange result

Code:

    c4vaV1
    @ given alist of length 100 1s or 0s
    @ threat it as a binary number and add 1 to it.
    
    @ very nice to test, a result is valid for the next test.
    @ it works.
    @ avg: 0.86 secs 100 lists of 100 elements 
    \<<
      { } @resList
      0 @value
      10 @ufCarry
      \->
      @input
      inputList
      @local
      resList
      value
      ufCarry
      \<<
        ufCarry SF
          @ we add 1
        
        inputList REVLIST 
          @revert to read the binary number from low to high.
        1
          @taking one element at time 
        \<<
          'value' STO
          IF
            ufCarry FS?
          THEN
            @add
            
            @ If element read is 0, just leave a 1, stop carry.
            @ If element read is 1, just leave a zero, carry on.
            value 0 ==
            \<< 1 ufCarry CF \>>
            0
            IFTE
          ELSE
            value
          END
        \>>
        DOLIST
        
        ufCarry FS?
          @still a carry
          @add one to the list
        \<< 1 + \>>
        IFT
        
        REVLIST
          @revert like original list.
      \>>
    \>>


This code runs for an average of 0.86 secs for 100 lists of 100 elements.
The code of DavidM takes 0.98 secs for the same workload.

I wonder why, his code seems way more compact (although more cryptic ), doing mostly the same of what my code does, or even less.

same with challenge 5.
Code:

\<<
      0 @value
      10 @ufSub
      \->
      @input
      inputList
      @local
      value
      ufSub
      \<<
        ufSub SF
          @ we sub 1
        
        inputList REVLIST 
          @revert to read the binary number from low to high.
        1
          @taking one element at time 
        \<<
          'value' STO
          IF
            ufSub FS?
          THEN
            @sub
            
            @ If element read is 0, just leave a 1, continue subtract..
            @ If element read is 1, just leave a zero, stop subtract.
            value 0 ==
            1
            \<< 0 ufSub CF \>>
            IFTE
          ELSE
            value
          END
        \>>
        DOLIST
        
        REVLIST
          @revert like original list.
      \>>
    \>>

mine: 0.87 secs, 100 lists 100 elements.
davidM: 0.96 seconds, 100 lists of 100 elements.

Wikis are great, Contribute :)
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Programming puzzles: processing lists! - pier4r - 04-22-2017 03:49 PM



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