Post Reply 
Programming puzzles: processing lists!
04-28-2017, 03:40 PM (This post was last modified: 04-28-2017 04:01 PM by pier4r.)
Post: #56
RE: Programming puzzles: processing lists!
(04-26-2017 01:30 AM)DavidM Wrote:  Here's my attempt at #6:

I have problems with this one (clever function though! I still have to improve a lot to match the ideas). At the end one has \GSLIST that seems not to work when the list to sum is {1. } (I have approximate mode on, and at first I run tests with 10 elements, not 100, to see if I did everything correctly)

Could you confirm?

In the meanwhile I add a 0 to the result list (but it will add to the execution time) because { 1. 0. } works with \GSLIST .

Ok after the fix, your function fails when the entire list is composed by one element (like a 10 element list composed by 10 times 10).

The version I'm using is the following

      @ 'src' is the source list
      @ 'tot' is the count of the current list element being checked
      1 \-> src tot
        @ the source list has to have all equal numbers grouped together
        src SORT

        2 @ compare two at a time

        @ for each pair of elements:
            @ are these both the same?
            @ the numbers are equal, so increment the total for this number
            1 'tot' STO+
            @ the numbers are different, so leave the current running total for the
            @ result and start a new running total for the next number
            1 'tot' STO

        @ add the final total to the result list
        tot +

        @ at this point, we have a list result that shows the counts of
        @ each unique list element encountered

        @ convert the current result list to one that has:
        @ 0's for each pair of counts that matches
        @ 1's for each pair that doesn't match
        \<< \=/ \>> DOSUBS

        @ sum up all the integers in the above result.  If the total isn't 0, then
        @ the list didn't match the stated criteria.
        @\GSLIST "invalid" 'src' IFTE
        0 +
          @Pier: adding a 0 to the list because \GSLIST fail with lists of 1 element.
        \GSLIST 0 1 IFTE
          @Pier: normalize the result like my function to compare.
          @ 0 invalid, 1 valid.

The the input list generator can be found at the link below, it may provide quick debug possibilities.
So now I try to check your second attempt at #3 using the SUB command.

You #3 is very fast!


Pier#1: avg: 50 lists of 100 elements 0.15 secs
DavidM#1 avg: 50 lists of 100 elements 1.94 secs
DavidM#2 avg: 50 lists of 100 elements 0.043 secs

Side question. The tester function is the following (the complete code is here)


      10 @iterations
      10 @listSize
      { } @inputList
      \<< c1vaV1 \>>
      \<< c1DavidM \>>
      \<< c2vaV1 \>>
      \<< c2DavidM \>>
      \<< c3vaV1 \>>
      \<< c3DavidM \>>
      \<< c4vaV1 \>>
      \<< c4DavidM \>>
      \<< c5vaV1 \>>
      \<< c5DavidM \>>
      \<< c6vaV1 \>>
      \<< c6DavidM \>>
      0 @cFuncP
      0 @cFuncDavidM
      { } @timeListP
      { } @timeListDavidM
      { } @posCheckedListP
      { } @posCheckedListDavidM
      0 @listGenFunc
      \<< \<-listSize {3 5} getRandomListFromValues \>>
      \<< \<-listSize {3 5} getRandomListFromValues \>>
      \<< \<-listSize -10 10 getRandomList \>>
      \<< \<-listSize { 0 1 } getRandomListFromValues \>>
      \<< \<-listSize { 0 1 } getRandomListFromValues \>>
      \<< \<-listSize c6listGen \>>
        inputListGenC6 'listGenFunc' STO
        c6FuncP 'cFuncP' STO
        c6FuncDavidM 'cFuncDavidM' STO
        1 iterations
          listGenFunc EVAL 'inputList' STO
          inputList cFuncP TEVAL
          @collect the timing
          OBJ\-> DROP
          'timeListP' SWAP STO+
          @ collect the pos checked.
          'posCheckedListP' SWAP STO+
          inputList cFuncDavidM TEVAL
          @collect the timing
          OBJ\-> DROP
          'timeListDavidM' SWAP STO+
          @ collect the pos checked.
          'posCheckedListDavidM' SWAP STO+

Now in the input functions, like inputListGenC6, I have to use a compiled variable otherwise the value of "listSize" is not recalled as I think.

For example in \<< \<-listSize c6listGen \>> the behavior when I use a plain "listSize" (instead of the compiled variable) is the following: listSize is saved as 'listSize' in the variable listSizeV of the c6listGen routine. Then in the routine:

\<< n \>> 'n' 1 listSizeV 1 SEQ

listSizeV 2 *
returns the algebraic object 'listSize*2'

I thought that variables were properly seen by subprograms in a program, in this case I mean \<< listSize c6listGen \>> should see the value of the variable.
So I thought a compiled variable should not be necessary but so far I don't see a workaround (and I was lucky that so far all the other input functions worked without compiled variables, using only "listSize", that then got changed in compiled variable for challenge 6)

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-28-2017 03:40 PM

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