Wrox Programmer Forums
|
BOOK: Beginning Java 2, JDK 5 Edition
This is the forum to discuss the Wrox book Ivor Horton's Beginning Java 2, JDK 5 Edition by Ivor Horton; ISBN: 9780764568749
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Java 2, JDK 5 Edition section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 7th, 2010, 10:14 PM
Registered User
 
Join Date: Jan 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default [Help]Chapter 10 Practice 4

I downloaded the code, but I am having trouble to understand how the inner function works even though the author stated it. Any one who understand this can give me suggestions?

Comment By the Author:
Quote:
Creating all permutations of the words in the phrase is the most difficult part of the exercise.
If you can crack this, then writing the file is trivial :-)
This method visualizes the permutations of the words as a graph with a dummy node at the top.
The number of levels below the dummy node corresponds to the number of words in the phrase and each path
from the dummy node down through the levels defines one permutation of the words in the phrase.
The argument to the method is the index of the word in the permutation to be assigned. When this method is called with an argument of 0,
it traverses each of the paths in the graph and thus creates all permutations. When a path has been traversed,
it is written to the file. The path variable records the 'length' of a path as it is traversed,
where the maximum length is the number of words in the phrase. When path reaches the maximum, a complete
path has been traversed so it is written to the file.
 
Old October 25th, 2010, 12:40 PM
Registered User
 
Join Date: Oct 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Its recursion which maybe throwing you off
If I replace the for loop with a while...
Code:
private static void permute(int n) {
    permutation[n] = ++path == 0 ? null: words[path-1];
    if(path == words.length) { 
       write();
    } else {
       i=0
      while( i<words.length) {
        if(permutation[i] == null) 
           permute(i);
         // You return HERE
         ++i;
      }
    }
      path--;
      permutation[n] = null;
  }
Where I comment YOU RETURN HERE is where, by golly, you return after each recursion (see the method calls itself)
Now when it does the value of i in the loop & the original value of n (the argument) will return to what ever was pushed on the stack at that call, and so the while loop will continue, ++i is it still less?
yes is it a null yes push it all on the stack and call myself again
or no
path --
make n null
return
(and if it was a recursive call that called it it will again jump back into the while loop)

Hope that helps
 
Old October 30th, 2010, 07:02 AM
Registered User
 
Join Date: Jan 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you for your help. I am really glad that there is someone as nice as you.

However...

Sorry to say that this was solved long ago. I couldn't find any help here so I figure that out myself after some hard logics. It was too long ago(as indicated by the date), because right now I am already an oracle certified J2EE developer.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 10, listing 10-10-app kiwibrit BOOK: Professional ASP.NET 3.5 : in C# and VB ISBN: 978-0-470-18757-9 2 August 18th, 2009 04:21 AM
Chapter 10 JimSchubert BOOK: Ruby on Rails for Microsoft Developers 1 May 17th, 2009 11:35 PM
Chapter 10 gogeo BOOK: Beginning Access 2003 VBA 1 January 22nd, 2006 09:41 AM
Chapter 10 czambran BOOK: Beginning CSS: Cascading Style Sheets for Web Design ISBN: 978-0-7645-7642-3 2 March 29th, 2005 09:35 AM
Chapter 10 columbiasmiles JSP Basics 0 May 17th, 2004 08:09 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.