C Puzzles

yet another place for C puzzles

Thursday, August 04, 2005

 

SET Puzzle : Week9 Question ; Azealots

Say, you have a set of weights.
and some operations on this set are readily available.

(1) int get_total_weight(Set * s) ; // return the total weight of the set s

(2) void remove_weights( Set * s, int k) ;
// remove all the elements from the set s,
whose individual weight equals 'k'

(3) int get_max_weight( Set * s) ;
// return the maximum of the weights

(4) add_to_set( Set * s, int k) ; // Add a new weight k to the set s

(5) int is_divided_to_two_equal_parts( Set *s);
// returns TRUE if and only if set of weights can be divided
into two parts such that both parts weigh the same

All you have to do is write a function
int is_subset( Set * s, int k);
which will return TRUE if and only if set s of weights has a subset
that weighs exactly k.

Some conditions ,
1) MUST call is_divided_to_two_equal_parts() , at least once.
2) MUST not have any loops , recursion or goto
3) MUST have less than 10 statements

Comments:
Here is the Answer

BOOLEAN bar(WeightSet *ws, int k)
{
int x = getTotalWeight(ws) - (k * 2) ;
x = abs(x) ;
add (ws, x) ;
return foo (ws) ;
}
 
Vinod,

Thanks for pointing it out. it was the original answer from the topper.

Here is the corrected one.

int is_subset(Set *ws, int k)
{
int x = get_total_weight(ws) - (k * 2) ;
x = abs(x) ;
add_to_set(ws, x) ;
return is_divided_to_two_equal_parts (ws) ;
}
 
Some more interesting C puzzles

http://freeprogrammingresources.googlepages.com
 
Post a Comment

Subscribe to Post Comments [Atom]





<< Home

Archives

July 2005   August 2005   October 2005   December 2005   March 2006   June 2006   July 2006   December 2006   February 2007   June 2007   March 2010   May 2010  

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]