Question

In: Computer Science

I'm trying to do some pratice problems in the book and here is one of them....

I'm trying to do some pratice problems in the book and here is one of them.

THIS IS FOR JAVA.

Write a method cleanCorruptData that accepts an ArrayList of integers and removes any adjacent pair of integers in the list if the left element of the pair is smaller than the right element of the pair. Every pair's left element is an even-numbered index in the list, and every pair's right element is an odd index in the list. For example, suppose a variable called list stores the following element values: [3, 7, 5, 5, 8, 5, 6, 3, 4, 7]

We can think of this list as a sequence of pairs: (3, 7), (5, 5), (8, 5), (6, 3), (4, 7). The pairs (3, 7) and (4, 7) are "bad" because the left element is smaller than the right one, so these pairs should be cleaned (or removed). So the call of cleanCorruptData(list); would change the list to store the following element values: [5, 5, 8, 5, 6, 3]

If the list has an odd length, the last element is not part of a pair and is also considered "corrupt;" it should therefore be cleaned by your method. If an empty list is passed in, the list should still be empty at the end of the call. You may assume that the list passed is not null. You may not use any other arrays, lists, or other data structures to help you solve this problem.

THIS IS FOR JAVA.

Solutions

Expert Solution

ArrayList<Integer> cleanCorruptData(ArrayList<Integer> list)
{
   int l = list.length, j = 0;
   ArrayList<Integer> result; // contains the resultant list
   for (int i = 0; i < l; i+=2)
   {
       int firstElement = list[i];
       if (i+1 < l) // checking for odd and even length
       {
           int secondElement = list[i+1];
      
           if (firstElement >= secondElement) // if the condition is true, then put it in the result list else remove
           {
               result[j++] = firstElement;
               result[j++] = secondElement;
           }
       }
   }
   return list;
}


Related Solutions

I'm working on a to-do list program in Python 2. I'm trying to delete an item...
I'm working on a to-do list program in Python 2. I'm trying to delete an item from the list and I'm not sure what I'm missing to do that. I haven't been able to get it to delete by string or by index number. Also, I'm trying to get the menu to run again after the user completes the add/delete/etc options. Do I need to put menu() menu_option = int(input("Welcome to your To-Do List. Please choose and option to continue:...
I'm having trouble with my do while loop. I'm trying to get it where if the...
I'm having trouble with my do while loop. I'm trying to get it where if the user enter's 3 after whatever amount of caffeinated beverages they've entered before then the loop will close and the rest of my code would proceed to execute and calculate the average price of all the caffeinated beverages entered. Can someone please help me with this? Here's my Code: import java.util.Scanner; public class Main { public static void main(String[] args) { CaffeinatedBeverage[] inventory = new...
I'm trying to write a program that reads 3 heights of kids and puts them in...
I'm trying to write a program that reads 3 heights of kids and puts them in ascending order using if and else statements but i'm having trouble #include<stdio.h> int main() { int k1,k2,k3; printf("Enter height of kid #1 >"); scanf("%d",&k1); printf("Enter height of kid #2 >"); scanf("%d",&k2); printf("Enter height of kid #3 >"); scanf("%d",&k3); if(k3>k2>k1) { printf("In ascending order %d %d %d",k3,k2,k1); } else if(k3>k1>k2) { printf("In ascending order %d %d %d",k3,k1,k2); } else if(k2>k3>k1) { printf("In ascending order %d...
Please show how the solution was arrived at I'm trying to understand how to do the...
Please show how the solution was arrived at I'm trying to understand how to do the problem. 4.(2) The fern is defined as the unit of force required to accelerate a unit of mass, called the bung, with the gravitational acceleration on the surface of the moon, which is one-sixth the normal gravitational acceleration on earth. a) What is the conversion factor that would be used to convert a force from the natural unit to the derived unit in this...
I'm trying to make a study sheet, what are the formula for Kinematics in one dimension,...
I'm trying to make a study sheet, what are the formula for Kinematics in one dimension, Vectors, kinematics in two dimensions, Dynamics, Newton’s laws of motion. Applications of Newton’s laws, gravity, Work and energy, Conservation of energy, Linear momentum and collisions, Rotational motion, Static equilibrium, elasticity, fracture, Fluids Heat and temperature; kinetic theory of gases, Thermodynamics
Please answer them correctly. Here are short 3 problems. Please solve all 3 problems. I would...
Please answer them correctly. Here are short 3 problems. Please solve all 3 problems. I would really appreciate your effort. Thanks. 1. Linda Williams is looking to invest in a three-year bond that makes semi-annual coupon payments at a rate of 5.475 percent. If these bonds have a market price of $982.63, what yield to maturity can she expect to earn? (Round intermediate calculations to 5 decimal places, e.g. 1.25145 and final answer to 2 decimal places, e.g. 15.25%.) Excel...
Please answer them correctly. Here are short 3 problems. Please solve all 3 problems. I would...
Please answer them correctly. Here are short 3 problems. Please solve all 3 problems. I would really appreciate your effort. Thanks. 1. You invest $260 in a mutual fund today that pays 6.30 percent interest annually. How long will it take to double your money? (If you solve this problem with algebra round intermediate calculations to 6 decimal places, in all cases round your final answer to 0 decimal place, e.g. 545) Number of Years ______? 2. You decide to...
Please answer them correctly. Here are short 3 problems. Please solve all 3 problems. I would...
Please answer them correctly. Here are short 3 problems. Please solve all 3 problems. I would really appreciate your effort. Thanks. 1. Cullumber Corp had sales of $336,000 in 2017. If management expects its sales to be $476,450 in 6 years, what is the rate at which the company’s sales are expected to grow? (If you solve this problem with algebra round intermediate calculations to 4 decimal places, in all cases round your final answer to 2 decimal places, e.g....
Please answer them correctly. Here are short 4 problems. Please solve all 4 problems. I would...
Please answer them correctly. Here are short 4 problems. Please solve all 4 problems. I would really appreciate your effort. Thanks. 1. Margaret Moore is a sales executive at a Baltimore firm. She is 25 years old and plans to invest $3,900 every year in an IRA account, beginning at the end of this year until she reaches the age of 65. If the IRA investment will earn 11.35 percent annually, how much will she have in 40 years, when...
Please answer them correctly. Here are 2 short problems. Please solve all 2 problems correctly. Make...
Please answer them correctly. Here are 2 short problems. Please solve all 2 problems correctly. Make sure the answers are correct. I would really appreciate your effort. Thanks. 1). Sunland, Inc., is considering investing in a new production line for eye drops. Other than investing in the equipment, the company needs to increase its cash and cash equivalents by $10,000, increase the level of inventory by $27,000, increase accounts receivable by $25,000, and increase accounts payable by $5,000 at the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT