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 9094389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:20:20+00:00 2026-06-16T23:20:20+00:00

I have implemented this algorithm: def get_hot_pages(self, radius = 2): if self.page == None

  • 0

I have implemented this algorithm:

def get_hot_pages(self, radius = 2):
    if self.page == None or self.max_pages == None: return []
    hot_pages = []
    for page in xrange(self.page - radius, self.page + radius + 1):
        if page < 0 or page >= self.max_pages : continue
        hot_pages.append(page)
    return hot_pages

But something tells me this can be implemented better. Is there a more pythonic way to do this?

  • 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-06-16T23:20:22+00:00Added an answer on June 16, 2026 at 11:20 pm

    The usual “trick” is to use max() and min() in order to set the minimum and maximum page numbers:

    def get_hot_pages(self, radius = 2):
        if self.page is None or self.max_pages is None: return []
        return range(max(self.page-radius, 0), min(self.page+radius+1, self.max_pages))
    

    The main advantages of this approach are:

    • People who know this standard procedure understand immediately what the code does (only one line to read). There is no need to read a whole loop block (which contains a filter test) in order to understand that some pages can be out of bound.
    • This is efficient: there is no test inside a loop that runs at every iteration and filters out incorrect page numbers. It is generally good to keep an eye on efficiency (even if it does not matter here, it may matter in other situations): it often happens that a solution be at the same time simple, legible and quite efficient (i.e., looking for efficiency can actually make the code more legible, because it forces one to isolate the essence of the problem at hand).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a singleton implemented like this: class Test123(object): _instance = None def __new__(cls,
This is regarding AES algorithm. Suppose i have implemented a AES algorithm and encrypt
I have implemented a simple parallel merge sort algorithm in Java. This cuts the
So far, I have implemented the algorithm found on this blog post with limited
I have this algorithm that I want to implement on VB6. Sub Main() dim
So, I have implemented this method to add a footer to my table view:
I have a collection class MyCollection<T> . I have implemented T this[string name] ,
I have implemented correctly bump's api, and added this code: - (void) configureBump {
Looking at this article: http://www.galloway.me.uk/tutorials/singleton-classes/ I have implemented a singleton, my question is the
I haven't yet implemented this, I'm still in the thinking stage, but I have

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.