Post Reply 
Programming puzzles: processing lists!
05-24-2017, 12:44 AM
Post: #82
RE: Programming puzzles: processing lists!
After having attempted a handful of these challenges, I realized that I tended to use a couple of recurring methods for several of them. I thought it might be useful for future explorations in list-land if I simply created a few list functions of my own as a challenge to myself. Here's some of the functions I came up with, and samples of how they could be used in the context of these challenges:

LSHF (List Shuffle)

Input
1: { list of 0 or more objects }

Output
1: { same objects as input, in random order }

Shuffles a list of 1000 reals in about 7.3 seconds. This one is more useful for generating test data than any of the actual challenges themselves.



LDDUP (List DeDup)

Input
1: { list of 0 or more objects }

Output
1: { same list with duplicates removed }

This is simply a UserRPL wrapper around the internal COMPRIMext command. Makes short work of #21:
Code:
\<< LDDUP \>>


LREPL (List Replace)

Input
3: { list of 0 or more objects }
2: { list of target objects }
1: { list of replacement objects }

Output
1: { list from SL3 with objects matched in SL2 list replaced by objects in SL1 list }

The following examples would be solutions to challenges 2 & 12:
Code:
Ch2
\<< { 3 5 } { 4 7 } LREPL \>>

Ch12
\<< { 3 5 } { 5 3 } LREPL \>>


LROT (List Rotate)

Input
2: { list of 0 or more objects }
1: rotation value [integer] (neg. for left rotation, pos. for right)

Rotation value of 0 leaves the list unchanged. Rotation wraps accordingly if the rotation value is greater than the size of the list. Sample use for challenge #3:
Code:
\<<
    DUP SIZE
    3 / FLOOR LROT
\>>


LMRPT (List Multiple Repeat)

Input
2: { list of 0 or more objects }
1: repeat factor [integer]

Output
1: { list with same objects from SL2 input repeated as directed by repeat factor }

A repeat factor of 1 returns the same list. A repeat factor of 0 returns an empty list. Otherwise, the list contents are repeated in the same order as the original list. This one wouldn't be hard to do in UserRPL, but this version's advantage is that it is quite speedy -- creating a list containing 20 repeats of 50 real numbers (for 1000 final list elements) completes in about 0.09 seconds. Examples:
Code:
{ 1 2 3 } 0 LMRPT => { }
{ 1 2 3 } 1 LMRPT => { 1 2 3 }
{ 1 2 3 } 3 LMRPT => { 1 2 3 1 2 3 1 2 3 }


LSUM (List Sum)

Input
1: { list of 0 or more objects }

Output
1: <the result of the list elements added together>

This command is essentially the same as \GSLIST, but it also accepts an empty list or a list with only 1 element as input. I chose to give a result of 0 for any empty list, simply because it fit my needs. Smile The sum of a single element is simply that same element. In the same fashion as \GSLIST, object types must be compatible or an error will result.


LEQ (List Equal)

Input
1: { list of 0 or more objects }

Output
1: 0 (FALSE) or 1 (TRUE)

This checks to see if there are any non-matching elements in the list. For purposes of this function, an empty list assumes a result of TRUE as there are no non-matching elements. Likewise, a list of 1 element is also assumed to be TRUE for the same reason.


LRPCT (List Repeat Count)

Input
1: { list of 0 or more objects }

Output
1: { { elements in the order encountered } { number of repetitions of each element in the prev. list } }

Example: { 1 1 1 2 3 1 } => { { 1 2 3 1 } { 3 1 1 1 } }

An empty list will result in a list of two empty lists { } => { { } { } } for consistency. This one proved to be useful for several of the challenges, especially when combined with LEQ:
Code:
Ch6
\<<
    SORT LRPCT
    LIST\-> DROP NIP
    LEQ
\>>

Ch9
\<<
    LRPCT
    LIST\-> DROP
    LEQ SWAP
    { 3 5 3 } ==
    AND
\>>

Ch10
\<<
    SORT LRPCT
    LIST\-> DROP
    ==
\>>

I did this mostly as a personal exercise for myself, but I'm happy to share the library containing these commands if anyone's interested.
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Programming puzzles: processing lists! - DavidM - 05-24-2017 12:44 AM



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