Wrox Programmer Forums
|
BOOK Programming Interviews Exposed: Secrets to Landing Your Next Job 3rd Edition
This is the forum to discuss the Wrox book Programming Interviews Exposed: Secrets to Landing Your Next Job, 3rd Edition by John Mongan, Noah Kindler, Eric Giguere; ISBN: 978-1-118-26136-1
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK Programming Interviews Exposed: Secrets to Landing Your Next Job 3rd 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 25th, 2014, 05:00 PM
mjd mjd is offline
Registered User
 
Join Date: Jan 2014
Posts: 1
Thanks: 0
Thanked 1 Time in 1 Post
Default Combinations Solution - Pg. 118

Hi,

Great book, really enjoying it so far.

On page 118, you describe the small "loop partitioning" optimization that can be used to eliminate the conditional within the loop.

I believe the conditional will always evaluate to true since the expression matches the one in the 'test' component of the for loop that surrounds it (and the value of i is not changed in the body of the for loop).

Looks like the right-hand side of the expression in the if statement needs to be changed to 'in.length() - 1' to have any effect (if the intended effect is to eliminate recursive calls that result in no additional work being done).

Thanks,
-Matt
The Following User Says Thank You to mjd For This Useful Post:
 
Old June 22nd, 2014, 10:32 PM
Registered User
 
Join Date: May 2014
Posts: 4
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
Originally Posted by mjd View Post
Hi,

Great book, really enjoying it so far.

On page 118, you describe the small "loop partitioning" optimization that can be used to eliminate the conditional within the loop.

I believe the conditional will always evaluate to true since the expression matches the one in the 'test' component of the for loop that surrounds it (and the value of i is not changed in the body of the for loop).

Looks like the right-hand side of the expression in the if statement needs to be changed to 'in.length() - 1' to have any effect (if the intended effect is to eliminate recursive calls that result in no additional work being done).

Thanks,
-Matt
Yeah you are right, it seems that even if we remove "if" line alltogether the function will still work, it will just have an extra function call where the loop will not be executed.
The Following User Says Thank You to yevvi For This Useful Post:
 
Old June 22nd, 2014, 10:52 PM
Registered User
 
Join Date: May 2014
Posts: 4
Thanks: 0
Thanked 1 Time in 1 Post
Default alternative solution

Actually when working on this problem my first solution was different from the author's:

public void combine(int i)
{
if (i == in.length())
System.out.println(out);
else {
combine(i + 1);
out.append(in.charAt(i));
combine(i + 1);
out.setLength(out.length() - 1);
}
}

In this case the function combine(i) should print all combinations in the substring (i, in.length()).

From the example "wxyz" the set of all combinations of "wxyz" is the set of all combinations of "xyz" plus the same set where each combinations is prepended with "w".

The only difference is that the function above will also print empty string as one of the outputs (i was thinking in terms of power sets and power set includes empty set as one of its elements).

Last edited by yevvi; June 22nd, 2014 at 10:59 PM..





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 14 Exercise 3 Solution pg 747 arodrigu12 BOOK: Beginning ASP.NET 4 : in C# and VB 2 April 21st, 2011 07:58 PM
Pg 118 DyerOppenheimer BOOK: ASP.NET MVC Website Programming Problem Design Solution ISBN: 9780470410950 6 February 18th, 2011 12:31 AM
Sharepoint files - client.stp - PG 118 step #1 Chris Sutherland BOOK: Beginning SharePoint 2007: Building Team Solutions with MOSS 2007 ISBN: 978-0-470-12449-9 0 January 31st, 2008 04:37 PM
Create All Possible Combinations From Varying Sets DolphinBay ASP.NET 1.0 and 1.1 Professional 7 April 26th, 2005 10:55 AM
Mouse-Keyboard Combinations shadowpug VB.NET 2002/2003 Basics 4 February 11th, 2004 06:21 PM





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