Post Reply 
Programming puzzles: processing lists!
01-27-2019, 07:18 PM
Post: #250
RE: Programming puzzles: processing lists!
(01-27-2019 11:49 AM)pier4r Wrote:  Another point is that one has a list of sublists of the same dimension and one would like to know the average value of a particular position across the sublists. Say
{ { 1 2 3 } { 2 3 4 } { 3 4 5} } what is the average value in the 3rd position of the sublists?
So far I thought that having the sum of the sublists and then dividing for the number of lists, then picking the wanted element is the fastest (also in terms of code readability) way to reach the solution. Picking only the k-th element from every sublist and keeping a local sum and then computing the average should be more costly at runtime and in terms of readability.

Though it seems counterintuitive, list processing commands on the HP 50g are usually faster than array commands. The following short program will return a list of the last elements of each sublist:

Code:

\<< DUP LXIL SWAP SIZE LDIST LPOPR NIP
\>>

Averaging (or any other operation) can then be performed on the resulting list. You can also use n GET instead of LPOPR NIP to get any nth element of each sublist.

BTW, your example list makes this process hard to visualize because it is its own transpose! Try {{ 1 2 3 }{ 4 5 6 }{ 7 8 9 }} instead.
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Programming puzzles: processing lists! - John Keith - 01-27-2019 07:18 PM



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