This is a beta version of Practice-It. Give us feedback

logo Practice-It logo

parityMatches

Language/Type: Java Collections Stacks and Queues
Author: Stuart Reges (on 2020/11/06)

Write a method called parityMatches that takes two stacks of integers as parameters and that returns a count of the number of elements in corresponding positions that have the same parity. Two numbers are considered to have the same parity if they are either both even or both odd. Suppose, for example, that two stacks store these values:

        s1: bottom [3, 4, 5] top        s2: bottom [37, 14, 24] top

The method compares values in corresponding positions in the two stacks (3 and 37, 4 and 14, 5 and 24). Of these pairs, two have the same parity. The third pair (5 and 24) do not have the same parity. Therefore the method call parityMatches(s1, s2) would return 2.

Your method should throw an IllegalArgumentException if they have different numbers of elements. You may also assume that all of the numbers stored in the list are greater than or equal to 0. Your method is to examine the two stacks but must return them to their original state before terminating.

You are to use one queue as auxiliary storage to solve this problem. You may not use any other auxiliary data structures to solve this problem, although you can have as many simple variables as you like. You also may not solve the problem recursively. Your solution must run in O(n) time where n is the size of the stacks. Use the Stack and Queue structures described in the cheat sheet and obey the restrictions described there (recall that you can't use the peek method or a foreach loop or iterator).

Type your solution here:


This is a method problem. Write a Java method as described. Do not write a complete program or class; just the method(s) above.

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.