13 Replies - 502 Views - Last Post: 13 February 2014 - 03:21 AM Rate Topic: -----

#1 zerohero911  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 12-February 14

Converting c++ program to Python

Posted 12 February 2014 - 07:20 PM

I'm not sure how to convert this c++ code into python.

int main(){

int n;
cout << "Enter n " << endl;
cin >> n;

int C[n];
C[0] = 1;

for(unsigned int zz = 0; zz <=n; ++zz) {
  sum = 0;
  for(unsigned int aa = 0, bb = zz; aa <= zz; ++aa, --bb) {
      sum += arr[aa] * arr[bb]
  }
 cout << C[i] << endl;
 C[zz + 1] = sum;
} 



Any help would be appreciated!

Is This A Good Question/Topic? 0
  • +

Replies To: Converting c++ program to Python

#2 tarmizi_adam2005  Icon User is offline

  • جوروترا

Reputation: 224
  • View blog
  • Posts: 801
  • Joined: 18-April 09

Re: Converting c++ program to Python

Posted 12 February 2014 - 07:23 PM

Me either, Maybe this should go into the Python forum instead ?
Was This Post Helpful? 0
  • +
  • -

#3 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8963
  • View blog
  • Posts: 33,623
  • Joined: 12-June 08

Re: Converting c++ program to Python

Posted 12 February 2014 - 07:24 PM

Would you be more specific on what you need help with, or are you just looking for someone to do the conversion?
Was This Post Helpful? 0
  • +
  • -

#4 zerohero911  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 12-February 14

Re: Converting c++ program to Python

Posted 12 February 2014 - 07:30 PM

I'm not sure how the iteration works on the for loops. I have it incrementing and decrementing for different loops. Also I'm not sure how to do multivariable for loops like
for(unsigned int aa = 0, bb = zz; aa <= zz; ++aa, --bb)

Was This Post Helpful? 0
  • +
  • -

#5 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8963
  • View blog
  • Posts: 33,623
  • Joined: 12-June 08

Re: Converting c++ program to Python

Posted 12 February 2014 - 07:32 PM

Moving to python..
Was This Post Helpful? 0
  • +
  • -

#6 zerohero911  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 12-February 14

Re: Converting c++ program to Python

Posted 12 February 2014 - 07:39 PM

I'm having trouble converting c++ code into python. Specifically I'm not sure how cin works, how increment and decrement works for for loops such as
for(unsigned int zz = 0; zz <=n; ++zz) 
and also how to convert multivariable for loops like
 [code]for(unsigned int aa = 0, bb = zz; aa <= zz; ++aa, --bb)

Was This Post Helpful? 0
  • +
  • -

#7 Black_Epiphany  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 64
  • Joined: 02-February 14

Re: Converting c++ program to Python

Posted 12 February 2014 - 07:39 PM

for loops in python are super simple
for i in array:
    print(i)


https://wiki.python.org/moin/ForLoop

You should google stuff first. There are a lot of examples of python programs and a ton of documentation

This post has been edited by Black_Epiphany: 12 February 2014 - 07:40 PM

Was This Post Helpful? 0
  • +
  • -

#8 zerohero911  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 12-February 14

Re: Converting c++ program to Python

Posted 12 February 2014 - 07:57 PM

Is there a way to do for loops like
for(unsigned int aa = 0, bb = zz; aa <= zz; ++aa, --bb)

Was This Post Helpful? 0
  • +
  • -

#9 Black_Epiphany  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 64
  • Joined: 02-February 14

Re: Converting c++ program to Python

Posted 12 February 2014 - 08:28 PM

View Postzerohero911, on 13 February 2014 - 02:57 AM, said:

Is there a way to do for loops like
for(unsigned int aa = 0, bb = zz; aa <= zz; ++aa, --bb)


You could just do a while loop
aa = 0
bb = zz
while aa <= zz:
    #your code
    ++aa
    --bb


This post has been edited by Black_Epiphany: 12 February 2014 - 08:34 PM

Was This Post Helpful? 0
  • +
  • -

#10 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 116
  • View blog
  • Posts: 212
  • Joined: 11-January 13

Re: Converting c++ program to Python

Posted 12 February 2014 - 10:34 PM

View Postzerohero911, on 13 February 2014 - 02:20 AM, said:

I'm not sure how to convert this c++ code into python.
The code as you posted it won't run in any language.

Where is arr defined; what type is sum; how do you intend to make an array from a variable input?

-Mek
Was This Post Helpful? 0
  • +
  • -

#11 Ntwiles  Icon User is offline

  • D.I.C Addict

Reputation: 148
  • View blog
  • Posts: 825
  • Joined: 26-May 10

Re: Converting c++ program to Python

Posted 12 February 2014 - 11:14 PM

Too bad it's not in C#. You could have just called Convert.ToPython();
Was This Post Helpful? 0
  • +
  • -

#12 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7578
  • View blog
  • Posts: 12,746
  • Joined: 19-March 11

Re: Converting c++ program to Python

Posted 13 February 2014 - 12:28 AM

Yeah, this is not exactly runnable code. What the hell is i here?

But the central issue is something we can deal with:


for(unsigned int zz = 0; zz <=n; ++zz) {
  sum = 0;
  for(unsigned int aa = 0, bb = zz; aa <= zz; ++aa, --bb) {
      sum += arr[aa] * arr[bb]



In python, you'd do something like

[ [sum([arr[aa]*(arr[zz-aa]) for aa in range(zz+1) ] ) for zz in range (n+1)]


Obviously, this can be simplified.
Was This Post Helpful? 0
  • +
  • -

#13 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 116
  • View blog
  • Posts: 212
  • Joined: 11-January 13

Re: Converting c++ program to Python

Posted 13 February 2014 - 12:55 AM

Pure speculation, but I was wondering if something like this was the intention:
Spoiler

Implemented fairly simply in python:
Spoiler

If not, it still yields an interesting sequence:
[1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862]

Edit: Just discovered the numbers in this sequence are called Catalan numbers.
-Mek

This post has been edited by Mekire: 13 February 2014 - 01:02 AM

Was This Post Helpful? 0
  • +
  • -

#14 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,596
  • Joined: 16-October 07

Re: Converting c++ program to Python

Posted 13 February 2014 - 03:21 AM

I thought to do the same thing as Mekire. Mostly because dissecting the stillborn C++ was fun.

First, C++ code that kind of makes sense, based on what's given:
Spoiler


Now, some Python following the C++ as closely as reasonable:
Spoiler



And, to be fair, the way you'd actually do it in Python:
Spoiler

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1