im trying to create and algorithm in python but i created it in C++ instead as i don't know python well enough i know it's easy and i can learn it in one day or two it's just the syntax but i don't have the time to learn python right now (will do it later xd) so can you please convert it into python?
Code:
all i need to do is creating an algorithm that see integer numbers in a list lets call the list stocks or something and choose the min price without using min function and buy it on this day (just a variable called bought_on_day and print the number of the index) and same for sell but only difference is it will sell it at max price

#include <iostream>

using namespace std;

int main()

{

int max_profit = 0;

int n;

int buyday,sellday;

//input array size

cout<<"Enter the number of days : ";

cin>>n;

int arr[n];



//input all array values

for(int i=0;i<n;i++)

{cout<<"Enter the price on day "<<i+1<<": ";

cin>>arr[i];}

//logic is traverse the rightside of array from current day and update values accordingly

for(int i=0;i<n;i++)

{

for(int j=i+1;j<n;j++)

{

if(arr[j]-arr[i]>max_profit)

{max_profit=arr[j]-arr[i];

buyday=i;sellday=j;}

}

}

cout<<"Buying date :"<<"day "<<buyday+1<<", at the price of $"<<arr[buyday]<<"\n";

cout<<"Selling date :"<<"day "<<sellday+1<<", at the price of $"<<arr[sellday]<<"\n";

cout<<"Maximum profit : $"<<max_profit;

return 0;

}

Recommended Answers

It appears a lot of new members are not trying at all. Your code is badly formatted with extra blank lines and more bad formatting.

If I ignore this it looks like basic ask some questions, get input and so on. Why not start with a tutorial or two about …

Jump to Post

All 4 Replies

It appears a lot of new members are not trying at all. Your code is badly formatted with extra blank lines and more bad formatting.

If I ignore this it looks like basic ask some questions, get input and so on. Why not start with a tutorial or two about these areas?

https://www.w3schools.com/python/ref_func_input.asp to start!

commented: but thank you for the link tho. and im trying to do it myself :D +0

As you commented, you wish to do this yourself. To that end, did you write something small that would ask the user for the number of days?

Or where are you stuck?

PS. Your other comment was:

i understand , im new at programming and it's a project for an exam so the problem is idon't have enough time to learn python and recreate the program

Everyone starts somewhere but I'm going to try to help you past the hurdles if you can point them out.

When converting like this you can try line by line or understand what the old app did and write it from the beginning. Either method would be acceptable on small programs like this. Since this is for an exam, it must be your work and not ours.

To convert c++ to python you need to download code writer from Microsoft store. And save the format from C++ to python

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.