Post Reply 
Programming puzzles: processing lists!
06-14-2017, 08:57 AM (This post was last modified: 06-14-2017 12:08 PM by pier4r.)
Post: #153
RE: Programming puzzles: processing lists!
@DavidM: another request if I may ask.

POS , as far as I know, returns the first matching position in a list.

What if I want to know all the positions that matches the element? I did, for one of the challenges, an helper routine. Do you find reasonable to develop one for your library?

Small argument by me: whether a routine it is good already in userRPL, it does not mean that it ready available for a user (which has to find it or develop it), that is the reason to have a "useful" library.

request:
Code:

returnAllPosHelp
"input:
The built in POS, as far as I know,
returns only the first match.
L2: input list
L1: element to search

output
L1: list of position where elment appears in list
"
  
  returnAllPos
  \<<
    { } @resultList
    \->
    @input
    inputList
    elementV
    @local
    resultList
    
    \<<
      inputList
      \<<
        \->
        value
        \<<
          IF
            value elementV ==
          THEN
            'resultList' NSUB STO+
          END
        \>>
      \>>
      DOSUBS
      resultList
    \>>
  \>>

edit. another request, list contains
Code:

listContainsHelp
"input:
2: list L1 
1: list L2

output:
1: { list of elements in L2 present in L1}

remarks:
- duplicates not checked.

todo:
- handle duplicates. If the same element occurs in L2 N times
and it appears in L1 K times, with K <= N . Then the element
will appear in the result K times, not N.

    
    listContains
    @it seems to work
    \<<
      {} "resList" DROP
      
      \->
      @input
      inL1
      inL2
      
      @var
      resList
      
      \<<
        {} @handle empty result
      
        inL2
        1
        \<<
          \->
          @input
          element
          
          \<<
            inL1 element POS 0 \=/
            element
            IFT
          \>>  
        \>>
        DOSUBS
        
        IF
          DUP SIZE 0 >
        THEN
          @non empty result
          @clear the list before
          +
        END
      \>>
    \>>

request: does your library complement goferlist or not? (I would say: until you find faster and/more flexibile code, yes) Because otherwise diff/intersect may be useful.

edit. Also #36

Code:

c36vaV1
    @given a list made of sublists
    @returns the sublists (positions) that contain a given element
    
    @it seems working
    \<<
      {} "resList" DROP
        @in the case it will be the result when no match is found
      
      \->
      @input
      inL1
      element
      
      @local var
      @L1size
      resList
      \<<
        
        1 inL1 SIZE
        FOR index
          inL1 index GET  @get sublist
          element POS @get the position
          IF
            @if the element is present (at least once)
            DUP 0 \=/
          THEN
            @at the position to the result
            'resList' index STO+
          END
          DROP @clean the stack
        NEXT
        
        resList
      \>>
    \>>

edit: check goferlist from time to time, I have to say that the documentation of the single commands is, well, too "twitter like". Sometimes really cryptic. Whatever the product, a proper documentation is part of its value.

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 - 06-14-2017 08:57 AM



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