5 Replies - 392 Views - Last Post: 18 May 2013 - 09:09 AM Rate Topic: -----

#1 raynox  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 18-May 13

Python to C++ ?

Posted 18 May 2013 - 07:38 AM

Hello guys !

Is there any guru out there that could help me translating this piece of python code to C++ ?

def ruleAsc(n):
a = [0 for i in range(n + 1)]
k = 1
a[0] = 0
a[1] = n
while k != 0:
x = a[k - 1] + 1
y = a[k] - 1
k -= 1
while x <= y:
a[k] = x
y -= x
k += 1
a[k] = x + y
yield a[:k + 1]

Thank you very much !!

Is This A Good Question/Topic? 0
  • +

Replies To: Python to C++ ?

#2 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,691
  • Joined: 13-March 10

Re: Python to C++ ?

Posted 18 May 2013 - 08:15 AM

Indenting is very important in Python so make sure you post the code in a correct format.
Was This Post Helpful? 0
  • +
  • -

#3 raynox  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 18-May 13

Re: Python to C++ ?

Posted 18 May 2013 - 08:23 AM

Yes you're right !...sorry about that. Here it goes:

def accelAsc(n):
    a = [0 for i in range(n + 1)]
    k = 1
    a[0] = 0
    y = n - 1
    while k != 0:
        x = a[k - 1] + 1
        k -= 1
        while 2*x <= y:
            a[k] = x
            y -= x
            k += 1
        l = k + 1
        while x <= y:
            a[k] = x
            a[l] = y
            yield a[:k + 2]
            x += 1
            y -= 1
        a[k] = x + y
        y = x + y - 1
        yield a[:k + 1]



The previous was smaller as I pasted here incorrectly.

Thanks a lot !!
Was This Post Helpful? 0
  • +
  • -

#4 Mekire  Icon User is offline

  • D.I.C Head

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

Re: Python to C++ ?

Posted 18 May 2013 - 08:45 AM

This looks a bit familiar:
Generating Integer Partitions

Simply pasting in someone else's code and asking for a translation generally won't get you very far. And anyway, if you really are that lazy, you know the name of the algorithm (ascending composition integer partitioning). Why not search for something on it in the language you want?

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

#5 raynox  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 18-May 13

Re: Python to C++ ?

Posted 18 May 2013 - 09:01 AM

View PostMekire, on 18 May 2013 - 08:45 AM, said:

This looks a bit familiar:
Generating Integer Partitions

Simply pasting in someone else's code and asking for a translation generally won't get you very far. And anyway, if you really are that lazy, you know the name of the algorithm (ascending composition integer partitioning). Why not search for something on it in the language you want?

-Mek


Mekire:

It's not about lazyness. I don't know Python and I've tried to find out about the YIELD function but haven't got it yet.

Of course I've searched the web for this algorithm in it's C++ equivalent but couldn't find it..that's the reason I posted this here in the first place... !!

I'm glad you know what is Integer Composition (in this particular case the ascending version of it by Jerome Kelleher) but sadly you're probably in the same boat as I regarding python...
Was This Post Helpful? 0
  • +
  • -

#6 Mekire  Icon User is offline

  • D.I.C Head

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

Re: Python to C++ ?

Posted 18 May 2013 - 09:09 AM

Using yeild as opposed to return makes a function a generator. This means that you can get 1 value from it and then the next time you use it it will start back in the same place. This contrasts to standard returning functions in that they don't maintain any memory of their previous call.

This is a method of making a function more performance and memory efficient. In python 2.x xrange is a generator; standard range returns a list. If you want to iterate over a huge sequence xrange performs far better as it only generates the one value you need at a time.

What is it about yield that you need to know?

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

Page 1 of 1