Phyton to C++ ?

This is the place for queries that don't fit in any of the other categories.

Phyton to C++ ?

Postby raynox » Sat May 18, 2013 2:01 pm

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 !!
raynox
 
Posts: 4
Joined: Sat May 18, 2013 1:59 pm

Re: Phyton to C++ ?

Postby metulburr » Sat May 18, 2013 2:09 pm

New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1512
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Phyton to C++ ?

Postby Mekire » Sat May 18, 2013 3:05 pm

Great; now you have copied and pasted your post identically to three forums (that I know of) and haven't correctly used code tags in ANY of them.

The original source

raynox wrote: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 !!
How exactly should we know the indentation you want? Also you need to explain what the code is intended to do; where you got it from (assuming you didn't write it; which is indeed my assumption); and thirdly what incentive anyone here has for doing your work for you.

-Mek

Edit: Seriously, why not just google "C++ integer partitioning" :?
User avatar
Mekire
 
Posts: 1025
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Phyton to C++ ?

Postby raynox » Sat May 18, 2013 4:28 pm

Yes Mak, I didn't indented by mistake in the 3 forums I posted. My mistake. For that matter:

Code: Select all
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]


I don't see a problem about posting in several forums. Aren't them to get the community helping each other ? 8|

I admit I dunno phyton at all and it seems to me this is a simple piece that phyton gurus could help me achieving the C++ equivalent.

Lazyness (like you posted in the other forum) ? No. not at all. Unfortunatly I don't have time to get knowleage of phyton and the author of this code didn't present a fluxogram (http://homepages.ed.ac.uk/jkellehe/partitions.php).

Anyway thank you for your help.

PS - I do not expect someone to do the work for me...as I stated in my first line of the post I asked if anyone could help me...

Regards.
raynox
 
Posts: 4
Joined: Sat May 18, 2013 1:59 pm

Re: Phyton to C++ ?

Postby DrakeMagi » Sat May 18, 2013 4:33 pm

don 't get why this is added
Code: Select all
def ruleAsc(n):
    a = [0 for i in range(n + 1)]
    k = 1
    #a[0] = 0  #it does nothing useful
    a[1] = n


here my version
Code: Select all
#include <iostream>
#include <vector>

using namespace std;

struct Data
{
   vector<int> list;
   bool good;
};

struct FakeYield
{
   vector<int> a;
   int n;
   int k;
   
   FakeYield(int nvalue)
   {   
      n = nvalue;
      for(int i = 0; i < nvalue + 1; ++i)
         a.push_back(0);
      k = 1;
      a[1] = n;
   }
   
   Data yield()
   {
      Data data;
      int x, y;
      data.good = true;
      
      if( 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;
         for(int i = 0; i < k + 1; ++i)
            data.list.push_back(a[i]);
      }
      else
         data.good = false;
      
      return data;
   }
};

int main()
{
   FakeYield ruleAsc(10);
   Data data;
   
   while(true)
   {
      data = ruleAsc.yield();
      if(data.good)
      {
         for(unsigned int i=0; i < data.list.size(); ++i)
         {
            if(i == 0)
               cout << "[" << data.list[i];
            else
               cout << ", " << data.list[i];
         }
         cout << "]" << endl;
      }
      else
         break;
   }
   
   return 0;
}
Linux: won't find windows here.
Linux: the choice of a GNU generation.
https://github.com/DrakeMagi
DrakeMagi
 
Posts: 113
Joined: Sun May 12, 2013 8:36 pm

Re: Phyton to C++ ?

Postby Mekire » Sat May 18, 2013 4:40 pm

No one minds people posting in multiple forums. It was just that you didn't take the time to post correctly in any of them to which I took exception (as will most others). This wasn't restricted to you not using code tags, but the fact you didn't state your problem or any efforts you had previously taken. You posted a random piece of code with no other explanation of even what it was intended to do and asked for a translation. If there are specific things you need help understanding (like the yield statement), asking more specifically will find you much better results.

-Mek
User avatar
Mekire
 
Posts: 1025
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Phyton to C++ ?

Postby raynox » Sat May 18, 2013 4:45 pm

Ok Mek you're right.

I'm now going to check the code Drake posted with your explanation about yield. Maybe I could grasp some of python stuff so in the future I can do it myself and who knows helping others.

Best Regards.
raynox
 
Posts: 4
Joined: Sat May 18, 2013 1:59 pm

Re: Phyton to C++ ?

Postby raynox » Mon May 20, 2013 12:39 pm

I haven't say thank you Drake!

Your code works just fine and finally Im starting to relate the phyton function with C.

Than you very much !

Best Regards.
raynox
 
Posts: 4
Joined: Sat May 18, 2013 1:59 pm


Return to General Coding Help

Who is online

Users browsing this forum: Mikhailov and 2 guests

cron