473,796 Members | 2,609 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Convert some Python code to C++

I am working on an implementation of the Longest Common Subsequence
problem (as I understand it, this problem can be used in spell
checking type activities) and have used this site to understand the
problem and its solution:

http://en.wikibooks.org/wiki/Algorit...on_subsequence
For those that understand algorithms and can talk Python, I want to
convert the Python code in the section "Reading out all LCSs" into C++
code but I don't understand some of the syntax. Can anyone give me a
hand?

Here is the code from that section, but is probably of little use
without understanding the entire problem setup given at the site
above:

def backTrackAll(C, X, Y, i, j):
if i == 0 or j == 0:
return set([""])
elif X[i-1] == Y[j-1]:
return set([Z + X[i-1] for Z in backTrackAll(C, X, Y, i-1,
j-1)])
else:
R = set()
if C[i][j-1] >= C[i-1][j]:
R.update(backTr ackAll(C, X, Y, i, j-1))
if C[i-1][j] >= C[i][j-1]:
R.update(backTr ackAll(C, X, Y, i-1, j))
return R

Thanks!

Nov 13 '07 #1
4 2206
On Nov 13, 9:28 am, meyousikm...@ya hoo.com wrote:
I am working on an implementation of the Longest Common Subsequence
problem (as I understand it, this problem can be used in spell
checking type activities) and have used this site to understand the
problem and its solution:

http://en.wikibooks.org/wiki/Algorit...trings/Longest...

For those that understand algorithms and can talk Python, I want to
convert the Python code in the section "Reading out all LCSs" into C++
code but I don't understand some of the syntax. Can anyone give me a
hand?

Here is the code from that section, but is probably of little use
without understanding the entire problem setup given at the site
above:

def backTrackAll(C, X, Y, i, j):
if i == 0 or j == 0:
return set([""])
elif X[i-1] == Y[j-1]:
return set([Z + X[i-1] for Z in backTrackAll(C, X, Y, i-1,
j-1)])
else:
R = set()
if C[i][j-1] >= C[i-1][j]:
R.update(backTr ackAll(C, X, Y, i, j-1))
if C[i-1][j] >= C[i][j-1]:
R.update(backTr ackAll(C, X, Y, i-1, j))
return R

Thanks!
You might try Shed Skin:

http://sourceforge.net/projects/shedskin/

It's been a while since I did C++. I would recommend going through a
basic C++ tutorial. I'm pretty sure the equivalence operators are
almost the same. You'll likely need to declare the types for the
arguments passed into the function as well.

I think lists are called arrays in C++. I don't know what the "set"
equivalent is though.

Mike

Nov 13 '07 #2
On 2007-11-13, ky******@gmail. com <ky******@gmail .comwrote:
On Nov 13, 9:28 am, meyousikm...@ya hoo.com wrote:
>I am working on an implementation of the Longest Common
Subsequence problem (as I understand it, this problem can be
used in spell checking type activities) and have used this
site to understand the problem and its solution:

http://en.wikibooks.org/wiki/Algorit...trings/Longest...

For those that understand algorithms and can talk Python, I
want to convert the Python code in the section "Reading out
all LCSs" into C++ code but I don't understand some of the
syntax. Can anyone give me a hand?

Here is the code from that section, but is probably of little
use without understanding the entire problem setup given at
the site above:

def backTrackAll(C, X, Y, i, j):
if i == 0 or j == 0:
return set([""])
elif X[i-1] == Y[j-1]:
return set([Z + X[i-1] for Z in backTrackAll(C, X, Y, i-1,
j-1)])
else:
R = set()
if C[i][j-1] >= C[i-1][j]:
R.update(backTr ackAll(C, X, Y, i, j-1))
if C[i-1][j] >= C[i][j-1]:
R.update(backTr ackAll(C, X, Y, i-1, j))
return R

Thanks!

You might try Shed Skin:

http://sourceforge.net/projects/shedskin/

It's been a while since I did C++. I would recommend going
through a basic C++ tutorial. I'm pretty sure the equivalence
operators are almost the same. You'll likely need to declare
the types for the arguments passed into the function as well.

I think lists are called arrays in C++. I don't know what the
"set" equivalent is though.
It is called set, oddly enough. ;)

There's an overload of the set::insert function that takes a
couple of iterators that will serve for Python's update method.

--
Neil Cerutti
Nov 13 '07 #3
On Nov 13, 12:51 pm, Neil Cerutti <horp...@yahoo. comwrote:
On 2007-11-13, kyoso...@gmail. com <kyoso...@gmail .comwrote:
On Nov 13, 9:28 am, meyousikm...@ya hoo.com wrote:
I am working on an implementation of the Longest Common
Subsequence problem (as I understand it, this problem can be
used in spell checking type activities) and have used this
site to understand the problem and its solution:
>http://en.wikibooks.org/wiki/Algorit...trings/Longest...
For those that understand algorithms and can talk Python, I
want to convert the Python code in the section "Reading out
all LCSs" into C++ code but I don't understand some of the
syntax. Can anyone give me a hand?
Here is the code from that section, but is probably of little
use without understanding the entire problem setup given at
the site above:
def backTrackAll(C, X, Y, i, j):
if i == 0 or j == 0:
return set([""])
elif X[i-1] == Y[j-1]:
return set([Z + X[i-1] for Z in backTrackAll(C, X, Y, i-1,
j-1)])
else:
R = set()
if C[i][j-1] >= C[i-1][j]:
R.update(backTr ackAll(C, X, Y, i, j-1))
if C[i-1][j] >= C[i][j-1]:
R.update(backTr ackAll(C, X, Y, i-1, j))
return R
Thanks!
You might try Shed Skin:
http://sourceforge.net/projects/shedskin/
It's been a while since I did C++. I would recommend going
through a basic C++ tutorial. I'm pretty sure the equivalence
operators are almost the same. You'll likely need to declare
the types for the arguments passed into the function as well.
I think lists are called arrays in C++. I don't know what the
"set" equivalent is though.

It is called set, oddly enough. ;)

There's an overload of the set::insert function that takes a
couple of iterators that will serve for Python's update method.

--
Neil Cerutti
I suspected as much, but I don't think they ever got that far into C++
in the classes I took. I'll have to file that little nugget for future
reference. Hopefully the OP is getting something out of this as well.

Mike

Nov 13 '07 #4
me**********@ya hoo.com a écrit :
For those that understand algorithms and can talk Python, I want to
convert the Python code in the section "Reading out all LCSs" into C++
code but I don't understand some of the syntax. Can anyone give me a
hand?

def backTrackAll(C, X, Y, i, j):
if i == 0 or j == 0:
return set([""])
elif X[i-1] == Y[j-1]:
return set([Z + X[i-1] for Z in backTrackAll(C, X, Y, i-1,
j-1)])
else:
R = set()
if C[i][j-1] >= C[i-1][j]:
R.update(backTr ackAll(C, X, Y, i, j-1))
if C[i-1][j] >= C[i][j-1]:
R.update(backTr ackAll(C, X, Y, i-1, j))
return R

Thanks!
just have a look at this tutorial: and look for Lists and Sets
http://docs.python.org/tut/tut.html

and look in the reference index for set() and list() in
http://docs.python.org/lib/genindex.html
or just pick the algo in another language in the url you give

or tell us precisely what part of the python algo you do not understant!
Nov 14 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4979
by: Peter Kwan | last post by:
Hi, I believe I have discovered a bug in Python 2.3. Could anyone suggest a get around? When I tested my existing Python code with the newly released Python 2.3, I get the following warning: FutureWarning: hex/oct constants > sys.maxint will return positive values in Python 2.4 and up It is because I used some constants such as 0x80ff3366, so I change it to 0x80ff3366L, hoping to get rid of the warning.
1
8540
by: Jinming Xu | last post by:
Hello everyone, While embedding my C++ program with Python, I am impeded by the conversion from a Python Tuple to a C++ array. I hope to get some assistance from you guys. I have a sequence of float numbers to be passed from Python to C++, with indefinite size. So I chose to use Tuple on the Python side. Since PyArg_ParseTuple() has no appropriate format to parse Tuple (The Tuple I am saying is not the Tuple of arguments) of...
11
6308
by: Grant Edwards | last post by:
I give up, how do I make this not fail under 2.4? fcntl.ioctl(self.dev.fileno(),0xc0047a80,struct.pack("HBB",0x1c,0x00,0x00)) I get an OverflowError: long int too large to convert to int ioctl() is expecting a 32-bit integer value, and 0xc0047a80 has the high-order bit set. I'm assuming Python thinks it's a signed value. How do I tell Python that 0xc0047a80 is an unsigned 32-bit value?
5
4443
by: jeremito | last post by:
I am extending python with C++ and need some help. I would like to convert a string to a mathematical function and then make this a C++ function. My C++ code would then refer to this function to calculate what it needs. For example I want to tell my function to calculate "x^2 + 3x +2", but later on I may want: "x + 3". Does anybody know how I can get an arbitrary string in Python (but proper mathematical function) turned into a C++...
3
13864
by: GM | last post by:
Dear all, Could you all give me some guide on how to convert my big5 string to unicode using python? I already knew that I might use cjkcodecs or python 2.4 but I still don't have idea on what exactly I should do. Please give me some sample code if you could. Thanks a lot Regards, Gary
2
2041
by: yinglcs | last post by:
Can you please tell me why the following code does not work in python? My guess is I need to convert 'count' from a string to an integer. How can I do that? And my understanding is python is a dynamic type language, should python convert it for me automatically? count = sys.argv for i in range(count): #do some stuff
29
5092
by: Harlin Seritt | last post by:
Hi... I would like to take a string like 'supercalifragilisticexpialidocius' and write it to a file in binary forms -- this way a user cannot read the string in case they were try to open in something like ascii text editor. I'd also like to be able to read the binary formed data back into string format so that it shows the original value. Is there any way to do this in Python? Thanks!
0
1112
by: John Krukoff | last post by:
-----Original Message----- One method which wouldn't require much python code, would be to run the XHTML through a simple identity XSL tranform with the output method set to HTML. It would have the benefit that you wouldn't have to worry about any of the specifics of the transformation, though you would need an external dependency. As far as I know, both 4suite and lxml (my personal favorite:
0
571
by: M.-A. Lemburg | last post by:
On 2008-07-01 20:31, Peter Bulychev wrote: You could write a codec which translates Unicode into a ASCII lookalike characters, but AFAIK there is no standard for doing this. I guess the best choice is to use the Unicode code point names as basis. These can be accessed via unicodedata.name(). You can then create a mapping which can be processed by the character map codec.
19
5348
by: est | last post by:
From python manual str( ) Return a string containing a nicely printable representation of an object. For strings, this returns the string itself. The difference with repr(object) is that str(object) does not always attempt to return a string that is acceptable to eval(); its goal is to return a printable string. If no argument is given, returns the empty string, ''.
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10239
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10190
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10019
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9057
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7555
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5579
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4122
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2928
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.