Post Reply 
Walkthrough of an RPL program for an RPN programmer
08-19-2018, 01:13 PM (This post was last modified: 08-19-2018 02:35 PM by Albert Chan.)
Post: #5
RE: Walkthrough of an RPL program for an RPN programmer
(08-19-2018 10:15 AM)Thomas Klemm Wrote:  I'm not aware of a way to partition a list in Python.
However we can use map with an additional copy of the list that is rotated by one place:

You mean like this ?
Code:
def partition(lst, n, step=0):    # no step = no overlap
    return [ lst[i-n:i] for i in range(n, len(lst) + 1, step or n) ]

Perhaps we do not need partitioned list, map, and sum. Just do it all at once.
Code:
def cross_and_sum(((x, y), sum), v):
    return v, sum + (x * v[1] - y * v[0])

def area(v):
    'Area of polygon using shoelace formula'
    _, s = reduce(cross_and_sum, v, (v[-1], 0))
    return 0.5 * abs(s)
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Walkthrough of an RPL program for an RPN programmer - Albert Chan - 08-19-2018 01:13 PM



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