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 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: \<< 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 => { } 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. 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 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. |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 1 Guest(s)