CONVERT 1 & 2 from C++ to Python   1.NON RECURSIVE #include using namespace std; int main() {     int days,buy_on_this_day ,sell_on_this_day;     cout<<"Enter number of days: ";     cin>>days;     int stock_price[days];     for(int i=0;i>stock_price[i];     }     int i=0;     for(int i=0;i= stock_price[i-1])         i++;         sell_on_this_day =i-1;         cout< using namespace std; int maxPro(int stock_price[], int starting_day, int last_day) {     if (last_day <= starting_day)         return 0;     int maximum_profit = 0;     for (int i = starting_day; i < last_day; i++)      {         for (int j = i + 1; j <= last_day; j++)          {             if (stock_price[j] > stock_price[i])              {                 //update the profit                 int pro = (stock_price[j] - stock_price[i]) + maxPro(stock_price, starting_day, i - 1) + maxPro(stock_price, j + 1, last_day);                //update the maximum profit                 maximum_profit = max(maximum_profit, pro);             }         }     }     return maximum_profit; } int main() {     int days,buy_on_this_day ,sell_on_this_day ;     cout<<"Enter number of days: ";     cin>>days;     int stock_stock_price[days];     for(int i=0;i>stock_stock_price[i];     }     int ans =maxPro(stock_stock_price,0,days-1);     cout<

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

CONVERT 1 & 2 from C++ to Python

 

1.NON RECURSIVE

#include <iostream>
using namespace std;

int main()
{
    int days,buy_on_this_day ,sell_on_this_day;
    cout<<"Enter number of days: ";
    cin>>days;
    int stock_price[days];
    for(int i=0;i<days;i++)
    {
        cin>>stock_price[i];
    }
    int i=0;
    for(int i=0;i<days-1;i++)
    {
        //using this line we compare current price with the next day 
        //price and find the local minima
        while(i<days-1 && stock_price[i+1]<=stock_price[i])
        i++;

        if(i==days-1)
        break;
        buy_on_this_day  =i++;

        while(i<days && stock_price[i]>= stock_price[i-1])
        i++;
        sell_on_this_day =i-1;
        cout<<buy_on_this_day <<" : index of the change before which we buy"<<endl;
        cout<<sell_on_this_day<<"  :index of the change before which we sell"<<endl;
    }
    int profit;
    int maxim =0;
    int minSofar = stock_price[0];
    for(int i=0;i<days;i++)
    {
        minSofar = min(minSofar, stock_price[i]);// update the min so far
        profit= stock_price[i]-minSofar; // update the profit
        maxim = max(maxim,profit);// update the maximum profit
        
    }
    cout<<"Maximum Profit: "<<maxim;
}

 

2. Recursive Approach

#include <iostream>
using namespace std;

int maxPro(int stock_price[], int starting_day, int last_day)
{
    if (last_day <= starting_day)
        return 0;
    int maximum_profit = 0;
    for (int i = starting_day; i < last_day; i++)
     {
        for (int j = i + 1; j <= last_day; j++)
         {
            if (stock_price[j] > stock_price[i]) 
            {
                //update the profit
                int pro = (stock_price[j] - stock_price[i]) + maxPro(stock_price, starting_day, i - 1) + maxPro(stock_price, j + 1, last_day);
               //update the maximum profit
                maximum_profit = max(maximum_profit, pro);
            }
        }
    }
    return maximum_profit;
}
int main()
{
    int days,buy_on_this_day ,sell_on_this_day ;
    cout<<"Enter number of days: ";
    cin>>days;
    int stock_stock_price[days];
    for(int i=0;i<days;i++)
    {
        cin>>stock_stock_price[i];
    }
    int ans =maxPro(stock_stock_price,0,days-1);
    cout<<ans;
}

Expert Solution
steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Declaring and Defining the Function
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education