Question

Suppose that each object in an n-object list L is colored either red or blue. Give...

Suppose that each object in an n-object list L is colored either red or blue. Give an efficient EREW algorithm to form two lists from the objects in L: one consisting of the blue objects and one consisting of the red objects.

Homework Answers

Answer #1
class MyObj {

    Property a;
    Property b;
    Property c;

    public boolean equals(Object o) {
        // use == if Property is primitive, like int or something
        return o instanceof MyObj && a.equals(o.a) && b.equals(o.b) && c.equals(o.c);
    }

    // edit - when you override equals, also override hashcode
    public int hashCode() {
        return a.hashCode() ^ b.hashCode() ^ c.hashCode();
    }

    public String toString() {
        return a.toString() + " " + b.toString() + " " + c.toString();
    }

}

// later in your main method
Set<MyObj> objSet = new HashSet<MyObj>();
for(MyObj o : list1) objSet.add(o);
for(MyObj o : list2) if(objSet.contains(o)) System.out.println(o + " is a match!");
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Suppose that each object in an n-object list L is colored either red or blue. Give...
Suppose that each object in an n-object list L is colored either red or blue. Give an efficient EREW algorithm to form two lists from the objects in L: one consisting of the blue objects and one consisting of the red objects.
Assume that each point on a circle is colored either red or blue. Prove that there...
Assume that each point on a circle is colored either red or blue. Prove that there are three points, say P, Q, R, that are colored the same and where at least two of the three distances between these points, d(P, Q), d(P, R), and d(Q, R), are equal.
In an ornamental plant, silver-colored flowers (s) are recessive to blue-colored flowers (S), red leaves (r)...
In an ornamental plant, silver-colored flowers (s) are recessive to blue-colored flowers (S), red leaves (r) are recessive to green leaves (R), and long stems (l) are recessive to short stems (L). All three genes involved are located on the same chromosome. Two true-breeding strains were crossed to produce an F1 plant. Then, this F1 plant was test-crossed to a plant with silver flowers, red leaves, and long stems, to produce the following progeny: progeny phenotype count of progeny silver...
Suppose L is a list of lists, where each element of L is a list of...
Suppose L is a list of lists, where each element of L is a list of integers. Use the map built-in function with a lambda expression to create a list P, where each element of P is a list of all non-negative integers in each list of L. For example, if L is the list [[1, -1, 2], [-1, -2, -3], [3, 2], [-5, 0, 5, 10]], then P is the list [[1, 2], [], [3, 2], [0, 5, 10]]....
Suppose an urn contains 50 balls: 25 are red, the rest are either blue or green...
Suppose an urn contains 50 balls: 25 are red, the rest are either blue or green in unknown proportion. A random ball will be drawn, and you must choose one of the following. 1A: you win $5 if the ball is red, 1B: you win $5 if the ball is blue. You also face a second choice: 2A: you win $5 if the ball is either blue or green, 2B: you win $5 if the ball is either red or...
Suppose that: Urn U1 contains 3 blue balls and six red balls, and Urn U2 contains...
Suppose that: Urn U1 contains 3 blue balls and six red balls, and Urn U2 contains 5 blue ball and 4 red balls Suppose we draw one ball at random from each urn. If the two balls drawn have different colors, what is the probability that the blue ball came from urn U1?
Let M be an n x n matrix with each entry equal to either 0 or...
Let M be an n x n matrix with each entry equal to either 0 or 1. Let mij denote the entry in row i and column j. A diagonal entry is one of the form mii for some i. Swapping rows i and j of the matrix M denotes the following action: we swap the values mik and mjk for k = 1,2, ... , n. Swapping two columns is defined analogously. We say that M is rearrangeable if...
Suppose three red and one blue marbles are placed inside a container that is opaque (you...
Suppose three red and one blue marbles are placed inside a container that is opaque (you can’t see into the container). If two marbles are selected at random without replacement answer the following questions: a) How many possible samples of size 2 can you create? b) List the set of all possible outcomes here (a tree structured diagram might prove helpful here): c) List the outcomes that make up the following event: “a red and a blue marble are selected”....
There are two urns. The first urn has three red, one blue and four yellow marbles....
There are two urns. The first urn has three red, one blue and four yellow marbles. The second urn has two red, four blue and two yellow marbles. If you draw one marble from each urn, what are the probabilities of the following events? What is the chance of getting two blue marbles? What is the chance of getting no blue marbles? What is the chance of getting at least one yellow marble? What is the chance that the marbles...
Suppose that there are two bags, each containing n − 1 red balls and one green...
Suppose that there are two bags, each containing n − 1 red balls and one green ball. You choose k balls at random from the first bag and put them into the second. You then choose k balls from the second bag and put them into the first. Find the probability (in terms of n and k) that both green balls end up in the same bag.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT