weave
Category: Programming
Author: Stuart Reges
Book Chapter: 5.1
Problem: weave
Write a method weave that takes two integers as
parameters and that returns the result of weaving their digits together to
form a single number. Two numbers x and y are weaved together as follows.
The last pair of digits in the result should be the last digit of x followed
by the last digit of y. The second-to-the-last pair of digits in the result
should be the second-to-the-last digit of x followed by the
second-to-the-last digit of y. And so on.
For example, consider weaving 128 with 394. The last pair of digits in the
result should be 84 (because the original numbers end in 8 and 4). The
second-to-the-last pair of digits in the result should be 29 (because the
second-to-the-last digits of the original numbers are 2 and 9). The
third-to-the-last pair of digits in the result should be 13 (because the
third-to-the-last digits of the original numbers are 1 and 3). Thus:
weave(128, 394)
should return 132984. Notice that the order of the arguments is important.
The call weave(394, 128) would return 319248.
If one of the numbers has more digits than the other, you should imagine
that leading zeros are used to make the numbers of equal length. For
example, weave(2384, 12) should return 20308142 (as if it were a call on
weave(2384, 0012)). Similarly, weave(9, 318) should return 30198 (as if it
were a call on weave(009, 318)).
You may assume that the numbers passed to weave are non-negative. You may
not use Strings to solve this problem; you must solve it using integer
arithmetic. Write your solution to weave below.