Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6768159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:02:33+00:00 2026-05-26T15:02:33+00:00

I am struggling to understand the ‘reduce’ call written below in python. I have

  • 0

I am struggling to understand the ‘reduce’ call written below in python.

I have found several sources, both here and elsewhere, that state what the function does, and that there’s an equivalent ‘aggregate’ for lists in C#, but I am unable to understand what do the calls below actually -expect-… possibly because I can’t really figure what does ‘_keep_left’ returns?

So:

1- can anybody help telling me what does ‘_keep_left’ return?

2- what does , []) mean in the reduce call?

Many thanks.

TURN_LEFT, TURN_RIGHT, TURN_NONE = (1, -1, 0)

def turn(p, q, r):
    """Returns -1, 0, 1 if p,q,r forms a right, straight, or left turn."""
    return cmp((q[0] - p[0])*(r[1] - p[1]) - (r[0] - p[0])*(q[1] - p[1]), 0)

def _keep_left(hull, r):
    while len(hull) > 1 and turn(hull[-2], hull[-1], r) != TURN_LEFT:
            hull.pop()
    return (not len(hull) or hull[-1] != r) and hull.append(r) or hull

def _graham_scan(points):
    """Returns points on convex hull of an array of points in CCW order."""
    points.sort()
    lh = reduce(_keep_left, points, [])
    uh = reduce(_keep_left, reversed(points), [])
    return lh.extend(uh[i] for i in xrange(1, len(uh) - 1)) or lh
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:02:33+00:00Added an answer on May 26, 2026 at 3:02 pm
    1. _keep_left returns a list hull, which is initially empty. Turns not going left are removed from it. The current point is added into it, unless it is already the last element on the list.

    2. , []) is the third parameter to reduce. It is the initial accumulator value, which will be passed to _keep_left, thus making hull (and, in the end, lh and uh) initially empty.

    It performs Graham scan by first sorting the points, then going through all the points twice (lh and uh stand for lower half and upper half), and with each sweep the points are accumulated to the list. The points are accumulated with reduce, that is, the result is originally empty, and the points are passed to _keep_left one by one (in the sorted order), and for each point the points causing a right turn are removed from the accumulated list. Then the current point is added to the accumulated list.

    The return value from _keep_left is a bit tricky: not len(hull) returns True if the list is empty. hull[-1] != r checks if r (the current point) is the last element in the list. hull.append(r) is in the boolean expression only for the side effect of appending r to the list (looks a bit dirty to me), so that if the last element of hull is r, hull will be returned without appending r to it.

    Put in other words, due to short circuiting hull will always be returned, but r will be appended to it before returning it if it’s not the last element. The same logic should be easy to implement in a nicer, yet more verbose, way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have for the last several years been struggling to understand why the Internet
I am struggling to understand why the initialization of pprocessor, below, is written like
I'm struggling to understand Dependency Properties in Silverlight 2. Does anybody have a good
I am really struggling to understand tail recursion in Erlang. I have the following
I'm struggling to understand why the following problem appears. I have an input box
I'm currently struggling to understand how I should organize/structure a class which I have
I am maintaining a Python program, and am struggling to understand the relationships between
So, for the past few days I have been struggling to understand how to
I'm struggling to understand why my if statement below always results in false. I
I am struggling to understand what I am doing wrong. I have a simple

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.