I have never learn python and i need to write one small program in it. I have C++ code so can someone convert that code to python code ?

code:

#include <stdio.h>

int N;
struct { int y, x1, x2; } platforme[100];

int spusti(double x, int y) {
   int rez = 0;
   for ( int i=0; i<N; ++i ) {
      if ( platforme[i].y  < y &&
           platforme[i].x1 < x &&
           platforme[i].x2 > x &&
           platforme[i].y  > rez ) {
         rez = platforme[i].y;
      }
   }
   return rez;
}

int main() {
   scanf( "%d", &N );
   for ( int i=0; i<N; ++i )
      scanf( "%d%d%d", &platforme[i].y, &platforme[i].x1, &platforme[i].x2 );

   int rez = 0;
   for ( int i=0; i<N; ++i ) {
      rez += platforme[i].y - spusti(platforme[i].x1 + 0.5, platforme[i].y);
      rez += platforme[i].y - spusti(platforme[i].x2 - 0.5, platforme[i].y);
   }
   printf( "%d\n", rez );

   return 0;
}

This is a simple program, and converting it to another programming language should not be a major effort. I think it is time you RTFM regarding Python programming documentation... Python programming is a skill in demand these days, so it will do you well to learn it.

Yes. Someone can. But there is no real automated converter.

This task sounds like (boring) work. And work is paid.

To get someone to do this you have to motivate them with your own effort. If you write down the specification and the goal of the program, someone will help.

It is for school. I know i ask that someone do that for me, but i just cant learn python in short time.

Anyway ty.

If you can write C++ code, you can learn and code in python well enough in a day or two to convert this program. The logic will not change - only the program syntax. IE, if you started to learn it when you first posted, you would be done about now, or this time tomorrow at the latest.

Edited 2 Years Ago by rubberman

Once you get started coding in Python ... and get some feel for its (often very) simple coding style and power ... you may then want to DO many of your programming jobs in Python first ... and many of those, then ... may never get recoded in C++ :)

Here is a little demo C++

struct 'Contact' ...

with a way one might recode it in Python 3

that may help get you past 'go' ...

Firstly the C++ code ...

// Cpp_vs_Python.cpp //

#include <iostream>
#include <string>

using namespace std;

struct Contact
{
    string name;
    string phone;

    string toString() const
    {
        return name + ", " + phone;
    }
} ;


typedef Contact* iter;

int main()
{
    Contact book[] = { {"Sam", "1234567890"},
                       {"Bill", "2345678901"} };

    iter it,
         begin = book,
         end = book + (sizeof book / sizeof *book) ;

    cout << "Showing contacts in book ...\n";
    for( it = begin; it != end; ++it )
         cout << it->toString() << endl;

    cout << "Press 'Enter' to continue/exit ... " << flush;
    string dummy;
    getline( cin, dummy );
}

Ok ... now here is how one might recode this in Python 3
(Note how much shorter and simpler is the Python code.)

# Cpp_vs_Python.py #

class Contact:
    def __init__(self, name, phone):
        self.name = name
        self.phone = phone
    def __str__(self):
        return self.name + ', ' + self.phone

# get some data into a book (a list of Contact)

book = [ Contact("Sam", "1234567890"), Contact("Bill", "2345678901") ]


#show the book
print( 'Showing contacts in book ...' )
for item in book:
        print( item )

input( "Press 'Enter' to continue/exit ... " 

Edited 2 Years Ago by David W: fixed comments / code

This article has been dead for over six months. Start a new discussion instead.