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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:28:13+00:00 2026-05-18T07:28:13+00:00

i am suppose to solve this question but i am stuck. Write a program

  • 0

i am suppose to solve this question but i am stuck.

Write a program to find the time period(s) of the largest price drop(s) when a list of price(s) is given. For instance, if the list is [300,301,303,299,300,298,301,305], then there is one period of the largest price drop: from time 2 with price 303 to time 5 with price 298.

Below is my solution but there is a flaw

def maxdrop(p):
  high = low = drop = newhigh = 0
  for i in range(len(p)):
    if p[i] >= p[high]:
      newhigh = i # invariant: p[high] <= p[newhigh]
    else: # so: p[i] < p[high] <= p[newhigh]
      newdrop = p[newhigh] - p[i]
      if newdrop >= drop:
        high, low, drop = newhigh, i, newdrop
  return ((high, p[high]), (low, p[low]), drop)
def test():
  p = [20,22,19,20,24,18,21,24,27]
  print p, maxdrop(p)
  p = list(reversed(p))
  print p, maxdrop(p)
  if __name__ == "__main__":
  test()

If you try with the below list
[2,1,2,3,4,3,2]

the sharpest drop should occurs over 4,3,2 – the last 3 elements.
But with my code, the output is 2,1 – the first 2 elements.

Please assist, thanks!

  • 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-18T07:28:14+00:00Added an answer on May 18, 2026 at 7:28 am

    Here’s my try at it. It works correctly on all the examples that you gave.

    I basically just go through the array and when there is a drop between two points, referring to the first point as A, look ahead until there is a value that is higher than A. I keep track of the minimum in this region. If the difference between A and the minimum is a bigger drop than what I’ve already found, I hold onto it. I then start looking again for a drop between two points, starting at the next point that was higher than A.

    Here’s the code. It isn’t very Python-esque, but it works pretty well (I’d just go to Cython if I needed it to be faster). Also, it returns the magnitude of the drop.

    def maxdrop(p):
        bestdrop = 0
        wheredrop = -1,-1
        i = 0
        while i < len(p) - 1:
            if p[i+1] < p[i]:
                bestlocal = p[i+1]
                wherelocal = i+1
                j = i + 1
                while j < len(p) - 1 and p[j + 1] < p[i]:
                    j += 1
                    if p[j] < bestlocal:
                        bestlocal = p[j]
                        wherelocal = j
                if p[i] - bestlocal > bestdrop:
                    bestdrop = p[i] - bestlocal
                    wheredrop = i, wherelocal
                i = j+1
            else:
                i += 1
        return bestdrop,wheredrop
    

    A big problem with your code is that you only look at the next value for the biggest drop after a new high value is found.

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

Sidebar

Related Questions

This sounds like a look-up-in-the-manual question to me, but I can't find it. Suppose
I tried many ways to solve this problem but none works. suppose I have
I know this is very common question but I am really stuck in this
This is a question about how to solve a possible, real, problem occurring when
I know that this question might have been asked like 100 times, but, believe
I'm attempting to solve the below exam question but I'm having difficulties with it.
This is the question I'm trying to solve: The following divide-and-conquer algorithm is proposed
This may be a bit silly question, but I just started doing pretty things
Suppose I have a large list of words. For an example: >>> with open('/usr/share/dict/words')
EDIT 22 March 2011 : This question is no longer that relevant since Youtube

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.