Objective Today, we're learning about the Array data structure. Check out the Tutorial tab for learning materials and an instructional video! Task Given an array, , of integers, print 's elements in reverse order as a single line of space-separated numbers. Input Format The first line contains an integer, (the size of our array). The second line contains space-separated integers describing array 's elements. Constraints , where is the integer in the array. Output Format Print the elements of array in reverse order as a single line of space-separated numbers. Sample Input 4 1 4 3 2 Sample Output 2 3 4 1 Solution : import java.io.*; import java.util.*; import java.math.*; public class Solution { public static void main(String[] args) { ...
Objective Today, we're discussing a simple sorting algorithm called Bubble Sort . Consider the following version of Bubble Sort: for ( int i = 0 ; i < n ; i ++) { // Track number of elements swapped during a single array traversal int numberOfSwaps = 0 ; for ( int j = 0 ; j < n - 1 ; j ++) { // Swap adjacent elements if they are in decreasing order if ( a [ j ] > a [ j + 1 ]) { swap ( a [ j ], a [ j + 1 ]); numberOfSwaps ++; } } // If no elements were swapped during a traversal, array is sorted if ( numberOfSwaps == 0 ) { break ; } } Task Given an array, , of size distinct elements, sort the array in ascending order using the Bubble Sort algorithm above. Once sorted, print the following lines: Array is sorted in numSwaps swaps. where is the number ...
This problem is all about unit testing. Your company needs a function that meets the following requirements: For a given array of integers, the function returns the index of the element with the minimum value in the array. If there is more than one element with the minimum value, the returned index should be the smallest one. If an empty array is passed to the function, it should raise an Exception. Note: The arrays are indexed from . A colleague has written that function, and your task is to design separated unit tests, testing if the function behaves correctly. The implementation in Python is listed below (Implementations in other languages can be found in the code template): def minimum_index ( seq ): if len ( seq ) == 0 : raise ValueError ( "Cannot get the minimum value index from an empty sequence" ) min_idx = 0 for i in range ( 1 , len ( seq )): if a [ i ] < a [ min_idx ]: mi...
Comments
Post a Comment