C to Python Translation anyone?

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2008
Posts: 20
Reputation: blackrobe is an unknown quantity at this point 
Solved Threads: 0
blackrobe blackrobe is offline Offline
Newbie Poster

C to Python Translation anyone?

 
0
  #1
Oct 29th, 2008
I've been reading an article but the code is written in C language which I don't get at all...

The code is:
  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...
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,297
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 178
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: C to Python Translation anyone?

 
1
  #2
Oct 29th, 2008
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.
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 45
Reputation: tyincali is an unknown quantity at this point 
Solved Threads: 6
tyincali tyincali is offline Offline
Light Poster

Re: C to Python Translation anyone?

 
0
  #3
Oct 29th, 2008
  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.
I wish I had something cool to put here...
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 966
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 222
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Shark

Re: C to Python Translation anyone?

 
0
  #4
Oct 30th, 2008
Originally Posted by blackrobe View 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:
  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
  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)
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 45
Reputation: tyincali is an unknown quantity at this point 
Solved Threads: 6
tyincali tyincali is offline Offline
Light Poster

Re: C to Python Translation anyone?

 
0
  #5
Oct 30th, 2008
So, looking at your translation, would you agree with me when I say this code is broken?
I wish I had something cool to put here...
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,067
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 267
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: C to Python Translation anyone?

 
0
  #6
Oct 30th, 2008
Originally Posted by tyincali View Post
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
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 45
Reputation: tyincali is an unknown quantity at this point 
Solved Threads: 6
tyincali tyincali is offline Offline
Light Poster

Re: C to Python Translation anyone?

 
0
  #7
Oct 30th, 2008
Originally Posted by jlm699 View Post
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.
I wish I had something cool to put here...
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,067
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 267
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: C to Python Translation anyone?

 
0
  #8
Oct 30th, 2008
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.
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 45
Reputation: tyincali is an unknown quantity at this point 
Solved Threads: 6
tyincali tyincali is offline Offline
Light Poster

Re: C to Python Translation anyone?

 
0
  #9
Oct 30th, 2008
You, sir, just blew my mind.

Also, "Ternary"
I wish I had something cool to put here...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC