473,395 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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(backTrackAll(C, X, Y, i, j-1))
if C[i-1][j] >= C[i][j-1]:
R.update(backTrackAll(C, X, Y, i-1, j))
return R

Thanks!

Nov 13 '07 #1
4 2190
On Nov 13, 9:28 am, meyousikm...@yahoo.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(backTrackAll(C, X, Y, i, j-1))
if C[i-1][j] >= C[i][j-1]:
R.update(backTrackAll(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...@yahoo.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(backTrackAll(C, X, Y, i, j-1))
if C[i-1][j] >= C[i][j-1]:
R.update(backTrackAll(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...@yahoo.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(backTrackAll(C, X, Y, i, j-1))
if C[i-1][j] >= C[i][j-1]:
R.update(backTrackAll(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**********@yahoo.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(backTrackAll(C, X, Y, i, j-1))
if C[i-1][j] >= C[i][j-1]:
R.update(backTrackAll(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
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: ...
1
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...
11
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 ...
5
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...
3
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...
2
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...
29
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...
0
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...
0
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...
19
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...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.