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;
}

Recommended Answers

All 7 Replies

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.

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 ... " 

Please help me to convert this C++ code into python

#include <bits/stdc++.h>
using namespace std;
const int SZ=1000;
int N, M;
long long tmp[100002];
long long ans[100002];
map<int, int> updates;
void add_family(int x, int n)
{
    tmp[1]+=n;
    for(int i=x; i<=100000; i+=x)
    {
        tmp[i]+=-1LL*n*x;
        tmp[i+1]+=1LL*n*x;
    }
}
void interpolate()
{
    for(int i=1; i<=100000; i++)
        tmp[i]+=tmp[i-1];
    for(int i=1; i<=100000; i++)
    {
        ans[i]+=(tmp[i]+=tmp[i-1]);
        tmp[i-1]=0;
    }
    tmp[100000]=0;
}
int main()
{
    scanf("%d", &N);
    int a, b;
    for(int i=0; i<N; i++)
    {
        scanf("%d%d", &a, &b);
        add_family(a, b);
    }
    interpolate();
    scanf("%d", &M);
    char op;
    while(M--)
    {
        scanf(" %c%d", &op, &a);
        if(op=='?')
        {
            long long rans=ans[a];
            for(auto& it: updates)
                rans+=1LL*a%it.first*it.second;
            printf("%lld\n", rans);
        }
        else if(op=='+')
            updates[a]++;
        else
            updates[a]--;
        if(updates.size()>SZ)
        {
            for(auto& it: updates)
                add_family(it.first, it.second);
            interpolate();
            updates.clear();
        }
    }
    return 0;
}
#include<iostream>
#include<string>
using namespace std;
main()

string s="GCV";
int i,n;
char c;
cin>>s;
if(s.size()!=5||s[2]!='-'||
s[0]<'A'||s[0]>'H'||
s[3]<'A'||s[3]>'H'||
s[1]<'1'||s[1]>'8'||
s[4]<'1'||s[4]>'8')cout<<"ERROR";

else if(abs((s[0]-s[3])*(s[1]-s[4]))==2)cout<<"YES";
else cout<<"NO";
Be a part of the DaniWeb community

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