C to Python Translation anyone?
I've been reading an article but the code is written in C language which I don't get at all...
The code is:
void traverse(Tptr p)
{ if (!p) return;
traverse(p->lokid);
if (p->splitchar)
traverse(p->eqkid);
else
printf("%s/n", (char *) p->eqkid);
traverse(p->hikid);
Someone please translate it to python...
blackrobe
Junior Poster in Training
52 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
Oh sweet Jesus! Years ago they used to pester us with stuff like this. C syntax can be real ugly, and your example is one of the best examples of ugly C syntax.
sneekula
Nearly a Posting Maven
2,427 posts since Oct 2006
Reputation Points: 961
Solved Threads: 212
I've been reading an article but the code is written in C language which I don't get at all...
The code is:
void traverse(Tptr p)
{ if (!p) return;
traverse(p->lokid);
if (p->splitchar)
traverse(p->eqkid);
else
printf("%s/n", (char *) p->eqkid);
traverse(p->hikid);
Someone please translate it to python...
A word to word translation would go like this
def traverse(p):
if not p:
return
traverse(p.lokid)
if p.splitchar:
traverse(p.eqkid)
else:
print(p.eqkid)
traverse(p.hikid)
Gribouillis
Posting Maven
2,781 posts since Jul 2008
Reputation Points: 1,024
Solved Threads: 691
So, looking at your translation, would you agree with me when I say this code is broken?
This code isn't broken it's a recursive function
jlm699
Veteran Poster
1,112 posts since Jul 2008
Reputation Points: 355
Solved Threads: 292
You must think recursively! These calls are all stacked so once it traverses to the outer-most or lower-most children, p becomes none, so it returns. When it returns, the function that called it can then proceed past that traverse(p->lokid) call.
You see this thing travels to all of the children, and then starts doing what it needs to do. And it looks like it's a tertiary (triary? I'm not sure which word is correct) search as opposed to binary search.
jlm699
Veteran Poster
1,112 posts since Jul 2008
Reputation Points: 355
Solved Threads: 292