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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:06:33+00:00 2026-06-04T01:06:33+00:00

Im a newbie to python , and I came across this particular code snippet

  • 0

Im a newbie to python , and I came across this particular code snippet to calculate the greatest slice in an sequence, however I simply cant seem to understand the following code

best = A[0] 
for size in range(1,n+1): 
   cur = sum(A[:size]) 
   for i in range(n-size): 
      cur += A[i+size] -= A[i] # <- what happens here?
      best = max(best, cur) 

Any idea on how the code functions and with that line in particular would be helpful!

  • 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-04T01:06:35+00:00Added an answer on June 4, 2026 at 1:06 am

    First off, the error in your code is an extra =. Working code:

    n = len(A)
    best = A[0] 
    for size in range(1,n+1): 
       cur = sum(A[:size]) 
       for i in range(n-size): 
          cur += A[i+size] - A[i] # <- what happens here?
          best = max(best, cur)
    

    The outer for loop creates larger and larger slices, starting with size set to 1. Then cur is set to the sum of the first size items of A.

    In the inner loop, this slice is “moved” to the right by adding the value just to the right of the slice (A[i+size]) and subtracting the first value of the slice (A[i]).

    Finally best is set to whichever is larger of the newly computed sum and the largest value found so far.

    In the end, best contains the largest sum. Unless A contains negative values, the answer is trivial: sum(A).

    EDIT: I just noticed that there’s a bug: The leftmost slice is not counted, except for slice size 1 (best = A[0]). So if A = [4, 3, 2] it outputs 5 instead of 9. Fix by adding a best = max(best, cur) line above the inner for loop.

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

Sidebar

Related Questions

I'm newbie in Python. I can't understand why this code does not work: reOptions
I'm newbie in Python. I have this simple code a = 0 b =
I'm a Python newbie, so I'm sure this is easy. Here's the code in
I'm newbie in Python, but this question is not a homework (actually this code
I'm a newbie to python and the app engine. I have this code that
Python newbie at time of writing. This came up because I want a user
I'm a newbie to Python, with a Java background. I came across the following
python newbie here. I'm writing the code to control an experiment that has multiple
I'm a python newbie so I apologize in advance if this question has been
I am Python newbie, so maybe don't knew if this is obvious or not.

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.