943,741 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 1453
  • Python RSS
Oct 29th, 2008
0

C to Python Translation anyone?

Expand Post »
I've been reading an article but the code is written in C language which I don't get at all...

The code is:
Python Syntax (Toggle Plain Text)
  1. void traverse(Tptr p)
  2. { if (!p) return;
  3. traverse(p->lokid);
  4. if (p->splitchar)
  5. traverse(p->eqkid);
  6. else
  7. printf("%s/n", (char *) p->eqkid);
  8. traverse(p->hikid);

Someone please translate it to python...
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
blackrobe is offline Offline
52 posts
since Oct 2008
Oct 29th, 2008
1

Re: C to Python Translation anyone?

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.
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006
Oct 29th, 2008
0

Re: C to Python Translation anyone?

python Syntax (Toggle Plain Text)
  1. def traverse(p):
  2. try:
  3. p
  4. except NameError:
  5. traverse(p=lokid)
  6. if (p==splitchar):
  7. traverse(p=eqkid)
  8. else:
  9. print p
  10. traverse(p=hikid)

I think this is what you would want... someone correct me though... I thought it was good, but then I reread it and I'm a little drunk...

What I am confused about is, two lines into the function, it says restart the function...
Last edited by tyincali; Oct 30th, 2008 at 12:06 am.
Reputation Points: 31
Solved Threads: 7
Light Poster
tyincali is offline Offline
45 posts
since Oct 2008
Oct 30th, 2008
0

Re: C to Python Translation anyone?

Click to Expand / Collapse  Quote originally posted by blackrobe ...
I've been reading an article but the code is written in C language which I don't get at all...

The code is:
Python Syntax (Toggle Plain Text)
  1. void traverse(Tptr p)
  2. { if (!p) return;
  3. traverse(p->lokid);
  4. if (p->splitchar)
  5. traverse(p->eqkid);
  6. else
  7. printf("%s/n", (char *) p->eqkid);
  8. traverse(p->hikid);

Someone please translate it to python...
A word to word translation would go like this
python Syntax (Toggle Plain Text)
  1. def traverse(p):
  2. if not p:
  3. return
  4. traverse(p.lokid)
  5. if p.splitchar:
  6. traverse(p.eqkid)
  7. else:
  8. print(p.eqkid)
  9. traverse(p.hikid)
Reputation Points: 930
Solved Threads: 668
Posting Maven
Gribouillis is offline Offline
2,655 posts
since Jul 2008
Oct 30th, 2008
0

Re: C to Python Translation anyone?

So, looking at your translation, would you agree with me when I say this code is broken?
Reputation Points: 31
Solved Threads: 7
Light Poster
tyincali is offline Offline
45 posts
since Oct 2008
Oct 30th, 2008
0

Re: C to Python Translation anyone?

Click to Expand / Collapse  Quote originally posted by tyincali ...
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
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008
Oct 30th, 2008
0

Re: C to Python Translation anyone?

Click to Expand / Collapse  Quote originally posted by jlm699 ...
This code isn't broken it's a recursive function
yes, but because of the traverse(p->lokid); two lines into the function, it restarts itself over and over until the end when the fact that p is not defined forces a return. There is no way for it to get to the code after the second line, because it always restarts itself.
Reputation Points: 31
Solved Threads: 7
Light Poster
tyincali is offline Offline
45 posts
since Oct 2008
Oct 30th, 2008
0

Re: C to Python Translation anyone?

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.
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008
Oct 30th, 2008
0

Re: C to Python Translation anyone?

You, sir, just blew my mind.

Also, "Ternary"
Reputation Points: 31
Solved Threads: 7
Light Poster
tyincali is offline Offline
45 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: probel with pyExcelerator
Next Thread in Python Forum Timeline: WX.ID - Anywhere to get list?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC