Question

In: Computer Science

Write a method weave that takes two arrays of ints, a and b, and that returns...

Write a method weave that takes two arrays of ints, a and b, and that returns an array that contains the elements of a and b in the order a[0], b[0], a[1], b[1], etc.

If one of the arrays a or b is longer than the other, just add the extra elements at the end of the array.

In your solution, you can use only 3 arrays, namely the two arrays a, and b passed to the method and the array returned by the method.

You can't use any ArrayList, String, or Collection object.

You may assume that none of the arrays passed to the method are null, though one of them or both of them may be empty. If both arrays passed to the method are empty, just return an empty array.

Solutions

Expert Solution

Login to solve the question:

Step 1: As we have two arrays with different lengths, find the maximum length array size.

Step 2: Iterate a loop on the maximum length and start inserting elements in the result/final array, one by one.

Step 3: Check before appending the value whether the loop has reached the size of the array or not.

Step 4: Print the array.

Python Code, for the above logic:

#Weave method in python

----------------------------------

def weave(a, b):
result_array = []
for i in range(max(len(a),len(b))):
if(i<len(a)):
result_array.append(a[i])
if(i<len(b)):
result_array.append(b[i])
print(result_array)

----------------------------------

If we take an test case like:

a = [1, 2, 3, 4, 5, 6, 7, 8, 9]

b = [1, 2, 3]

Result:

[1, 1, 2, 2, 3, 3, 4, 5, 6, 7, 8, 9]

Snapshot of Code and result in Python 3.5 IDLE

Result:

Given that empty arrays are passed:

a=[]

b=[]

Result:

Note: The login is same whatever the programming language be. The above code is written in Python 3.5


Related Solutions

write the “largerComponents” method that takes in two integer arrays and returns true or false if...
write the “largerComponents” method that takes in two integer arrays and returns true or false if the first array’s components are strictly greater than the second array’s components. The arrays must be the same size or else return false. Clarification Note: Components meaning each value at a specific index. For instance, if we had the arrays {5,2,7} and {1,3,1} then this method would return false as the value “2” is not greater than “3”. here is a provided code //Do...
JAVA Arrays 4 Write a method called isPalindrome that takes a String as input and returns...
JAVA Arrays 4 Write a method called isPalindrome that takes a String as input and returns true if the String is a palindrome.
Using Java programming, Write a LinkedList method swap() that takes two ints as arguments and swaps...
Using Java programming, Write a LinkedList method swap() that takes two ints as arguments and swaps the elements at those two positions. The method should not traverse the list twice to find the elements, and it should not create or destroy any nodes.
Write a method that takes two Sorted Arrays of different sizes and merges them into one...
Write a method that takes two Sorted Arrays of different sizes and merges them into one sorted array, and use the method to write a full recursive Merge Sort Algorithm.
In C++ Write a function which takes two parameters: an array of ints and an int...
In C++ Write a function which takes two parameters: an array of ints and an int size of the array and prints every element greater than 5 to the screen. As an example, if the array has the following 10 elements: 2 5 8 9 7 1 0 2 6 3, your function should print out 8 9 7 6. You may assume that the parameters passed to the function are valid. Your function must have the following signature: void...
Write a method public static void minMax(int[] arr) that takes an array of unique ints of...
Write a method public static void minMax(int[] arr) that takes an array of unique ints of length at least two as an argument, and swaps the smallest value of the array into the 0th position and swaps the largest value of the array into the last position. For example, if int[] a = {4, 3, 2, 6, 1, 5}, the method call minMax(a) should modify the array so that it is {1, 3, 2, 5, 4, 6}. The method should...
In C++, write a function that takes in as inputs two arrays, foo and bar, and...
In C++, write a function that takes in as inputs two arrays, foo and bar, and their respective array sizes. The function should then output the concatenation of the two arrays as a singly linked list. You may assume that you are provided a singly linked list header file.
Write a Junit test method that takes 2 Arrays of type Integer[], and tests whether these...
Write a Junit test method that takes 2 Arrays of type Integer[], and tests whether these 2 Arrays are equal or not, and also if the elements are all even numbers. Describe under what conditions these 2 Arrays would be considered equal.
C Language - Programming Write a function that takes an array of ints, and the size...
C Language - Programming Write a function that takes an array of ints, and the size of the array – another int. It also returns a double. Call this one ‘average.’ Return a double that is the average of the values in the array. Demonstrate that it works by finding the average of an array with these values {78, 90, 56, 99, 88, 68, 92} Write a function that takes one double parameter, and returns a char. The parameter represents...
Write a java method that takes a string and returns an array of int that contains...
Write a java method that takes a string and returns an array of int that contains the corresponding alphabetic order of each letter in the received string: An illustration: the method takes: "Sara" the method returns: {4,1,3,2} another illustration: the method takes: "hey" the method returns: {2,1,3}
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT