Implement the squeeze() method in the provided ArraySqueeze class so that it performs as indicated in the comment. The main() method in this class runs some test cases on squeeze(). You should also add a few nontrivial and interesting test cases of your own at the end of main().
package A1;
/**
* The purpose of this class is to squeeze an array of ints.
*
* The main method runs some tests.
*
* @author andy
*
*/
public class ArraySqueeze {
/**
* squeeze() takes an array of ints. On completion the array contains the
* same numbers, but wherever the array had two or more consecutive
* duplicate numbers, they are replaced by one copy of the number. Hence,
* after squeeze() is done, no two consecutive numbers in the array are the
* same.
*
* Any unused elements at the end of the array are set to -1.
*
* For example, if the input array is [ 4 , 1 , 1 , 3 , 3 , 3 , 1 , 1 ], it
* reads [ 4 , 1 , 3 , 1 , -1 , -1 , -1 , -1 ] after squeeze() completes.
*
* @param ints
* the input array.
*/
public static void squeeze(int[] ints) {
// TODO: Fill in your solution here. Ours takes linear time and is less than 10 lines long,
// not counting blank/comment lines or lines already present in this file.
}
/**
* main() runs test cases on your squeeze() method. Prints summary
* information on basic operations and halts with an error (and a stack
* trace) if any of the tests fail.
*/
public static void main(String[] args) {
String result;
System.out.println("Let's squeeze arrays!\n");
int[] test1 = {3, 7, 7, 7, 4, 5, 5, 2, 0, 8, 8, 8, 8, 5};
System.out.println("squeezing " + TestHelper.stringInts(test1) + ":");
squeeze(test1);
result = TestHelper.stringInts(test1);
System.out.println(result + "\n");
TestHelper.verify(result.equals(
"[ 3 , 7 , 4 , 5 , 2 , 0 , 8 , 5 , -1 , -1 , -1 , -1 , -1 , -1 ]"),
"BAD SQEEZE!!! No cookie.");
int[] test2 = {6, 6, 6, 6, 6, 3, 6, 3, 6, 3, 3, 3, 3, 3, 3};
System.out.println("squeezing " + TestHelper.stringInts(test2) + ":");
squeeze(test2);
result = TestHelper.stringInts(test2);
System.out.println(result + "\n");
TestHelper.verify(result.equals(
"[ 6 , 3 , 6 , 3 , 6 , 3 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ]"),
"BAD SQEEZE!!! No cookie.");
int[] test3 = {4, 4, 4, 4, 4};
System.out.println("squeezing " + TestHelper.stringInts(test3) + ":");
squeeze(test3);
result = TestHelper.stringInts(test3);
System.out.println(result + "\n");
TestHelper.verify(result.equals("[ 4 , -1 , -1 , -1 , -1 ]"),
"BAD SQEEZE!!! No cookie.");
int[] test4 = {0, 1, 2, 3, 4, 5, 6};
System.out.println("squeezing " + TestHelper.stringInts(test4) + ":");
squeeze(test4);
result = TestHelper.stringInts(test4);
System.out.println(result + "\n");
TestHelper.verify(result.equals("[ 0 , 1 , 2 , 3 , 4 , 5 , 6 ]"),
"BAD SQEEZE!!! No cookie.");
System.out.println("\nAdditional tests done by the student or TA:\n");
// Insert your additional test cases here.
}
}This solution take linear time.
public static void squeeze(int[] ints) {
int c=0, i = 0, p;
int arr[] = new int[ints.length];
for(i=0; i<ints.length-1; ++i)
p = (ints[i] == ints[i+1]) ? arr[i+1] = -1 : -1;
for(i=0; i<arr.length; ++i)
p = (arr[i] != -1) ? ints[c++] = ints[i] : -1;
for(i=c; i<ints.length; ++i)
ints[c++] = -1;
}
===========================================
SEE OUTPUT
![52 33 340 35 public static void squeeze(int[] ints) { 36 // TODO: Fill in your solution here. Ours takes linear time and is l](http://img.homeworklib.com/questions/38207660-0e8d-11ec-8a8c-1104bc9f5c18.png?x-oss-process=image/resize,w_560)
PLEASE COMMENT if there is any concern.
==========================================
Implement the squeeze() method in the provided ArraySqueeze class so that it performs as indicated in...
Consider a non-empty int array ints. A contiguous subarray ints[ start .. start + len -1 ] (with starting index start and length len) is called a flat if all elements of that subarray are equal. Furthermore, such a subarray is called a plateau if it is flat and each of the elements ints[start -1] and ints[start + len] that immediately proceed/succeed the subarray are either nonexistent (i.e., out of array’s index range) or are strictly smaller than the elements...
Write a MyString class that stores a (null-terminated) char* and a length and implements all of the member functions below. Default constructor: empty string const char* constructor: initializes data members appropriately Copy constructor: prints "Copy constructor" and endl in addition to making a copy Move constructor: prints "Move constructor" and endl in addition to moving data Copy assignment operator: prints "Copy assignment" and endl in addition to making a copy Move assignment operator: prints "Move assignment" and endl in addition...
C++ and/or using Xcode... Define a class of rational numbers. A rational number is a number that can be represented as the quotient of two integers. For example, 1/2, 3/4, 64/2, and so forth are all rational numbers. (By 1/2, etc., we mean the everyday meaning of the fraction, not the integer division this expression could produce in a C++ program). Represent rational numbers as two values of type int, one for the numerator and one for the denominator. Call...
You are tasked with implementing a recursive function void distanceFrom(int key) on the IntList class (provided). The function will first search through the list for the provided key, and then, recursively, change all previous values in the list to instead be their distance from the node containing the key value. Do not update the node containing the key value or any nodes after it. If the key does not exist in the list, each node should contain its distance from...
Lab 3 Step One First, create an empty directory for lab3. There is no starter code for this lab. You will be throwing and catching exceptions in this exercise. Create a file called RuntimeException.h and put the following code in it. #include <string> class RuntimeException { private: string errorMsg; public: RuntimeException(const string& err) { errorMsg = err; } string getMessage() const { return errorMsg; } } Step Two In a new .cpp file in your directory, write a main function...
Lab 3 Step One First, create an empty directory for lab3. There is no starter code for this lab. You will be throwing and catching exceptions in this exercise. Create a file called RuntimeException.h and put the following code in it. #include <string> class RuntimeException { private: string errorMsg; public: RuntimeException(const string& err) { errorMsg = err; } string getMessage() const { return errorMsg; } } Step Two In a new .cpp file in your directory, write a main function...
In C++, develop a class that supports array rotation. Rotating an array is an operation where you shift all elements of the array some number of positions left or right, and elements that are shifted off of the left or right end of the array "wrap around" to the right or left end, respectively. For example, if we rotate the array [1, 2, 3, 4, 5] to the right by 1, we get the array [5, 1, 2, 3, 4]....
package week_3; /** Write a method called countUppercase that takes a String array argument. You can assume that every element in the array is a one-letter String, for example String[] test = { "a", "B", "c", "D", "e"}; This method will count the number of uppercase letters from the set A through Z in the array, and return that number. So for the example array above, your method will return 2. You will need to use some Java library methods....
Identify a logical error in the following code and fix it. public class test1 { public static void main(String[] args){ int total; float avg; int a, b, c; a=10; b=5; c=2; total=a+b+c; avg=total/3; System.out.println("Average: "+avg); } } Answer: Write the output of the program below. import java.util.Scanner; public class test2 { public static void main(String[] arg){ int psid; String name; Scanner input=new Scanner(System.in); System.out.println("Enter your PSID"); psid=input.nextInt(); System.out.println("Enter your...
Arrays and Methods Worksheet 2 (10 pts) Copy the following into a new file called ArrayMethods2.java. Write the methods one-by-one. Compile and run your code after you write each method to make sure it was written properly. When your methods pass all of the tests, upload your file to Canvas. /** * Assignment 20.2 * @author Your Name * CIS 36A */ public class ArrayMethods2 { /** * Given an array of ints, return the index of the...