Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How to do this please help ASAP don't use Any Java library class (e.g., ArrayList, Arrays) /* * Input Parameters: * - `seq`: a non-empty

How to do this please help ASAP

don't use Any Java library class (e.g., ArrayList, Arrays)

/* * Input Parameters: * - `seq`: a non-empty array of integers * * Assumptions: * - Input array `seq` is not empty. * * What to return? * - Return an array that represents an encoding of `seq`, by adopting the following principle: * Element at each even index (e.g., at index 0, at index 2, and so on) of the return array specifies * how many times the element at the next odd index (e.g., at index 1, at index 3, and so on) repeats * in the input array `seq`. * * For example, {4, 2, 3, 1} encodes that value 2 (at odd index 1) should repeat 4 times (as specified at even index 0), * and similarly, value 1 should repeat 3 times. * * Say `seq` is {2, 2, 2, 2, 1, 1, 1}. * Then the method should return an encoded array {4, 2, 3, 1}. * * See the JUnit tests related to this method. */ public static int[] task4(int[] seq) { int[] result = null; /* Your task is to implement this method, * so that running TestUtilities.java will pass all JUnit tests. * * Recall from Week 1's tutorial videos: * 1. No System.out.println statements should appear here. * Instead, an explicit, final `return` statement is placed for you. * 2. No Scanner operations should appear here (e.g., input.nextInt()). * Instead, refer to the input parameters of this method. * 3. Do not re-assign any of the parameter/input variables. */ // Your implementation of this method starts here.

// Do not modify this return statement. return result; } }

the unit tests:

/* * Tests related to Task 4 */ @Test public void test_task4_1() { int[] seq = {3, 1, 1, 1, 2, 2, 2, 2}; int[] expected = {1, 3, 3, 1, 4, 2}; assertArrayEquals(expected, Utilities.task4(seq)); }

@Test public void test_task4_2() { int[] seq = {1, 1, 1, 2, 2, 2, 2}; int[] expected = {3, 1, 4, 2}; assertArrayEquals(expected, Utilities.task4(seq)); } @Test public void test_task4_3() { int[] seq = {2, 2, 2, 2, 1, 1, 1}; int[] expected = {4, 2, 3, 1}; assertArrayEquals(expected, Utilities.task4(seq)); } }

the answer I got:

Please modify thise to make it better:

if (seq.length == 0) { result = new int[0]; } else { int count = 1; for (int i = 1; i < seq.length; i++) { if (seq[i] != seq[i - 1]) { ++count; } } count *= 2;

result = new int[count]; int number = seq[0], seqCount = 1, index = 0; for (int i = 1; i < seq.length; i++) { if (seq[i] != seq[i - 1]) { result[index++] = seqCount; result[index++] = number; number = seq[i]; seqCount = 1; } else { seqCount++; } } result[index++] = seqCount; result[index] = number; }

// Do not modify this return statement. return result; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Relational Database And SQL

Authors: Lucy Scott

3rd Edition

1087899699, 978-1087899695

More Books

Students also viewed these Databases questions

Question

What is horizontal analysis and ratio analysis in accounting?

Answered: 1 week ago