Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The algorithm compares the median of the last m sales prices with the last price

ID: 3599172 • Letter: T

Question

The algorithm compares the median of the last m sales prices with the last price. If this median is greater than the last price, the algorithm recommends that you sell the stock; if the median is equal to the last price, the algorithm recommends that you hold; and if the median is less than the last prices, the algorithm recommends that you buy.

n indicates the number of prices in the input, and m indicates how many of the most recent values should be considered when calculating the median.

Sample Input

Sample Output

Explanation

For the first decision, the program calculates the median of <12, 10, 9> which is 10, and compares this with the last price 9. Since the median is greater than the last price, the program outputs "sell".

For the second decision, the program calculates the median of <10, 9, 9> which is 9, and compares this with the last price 9. Since the median is equal to the last price, the program outputs "hold".

For the third decision, the program calculates the median of <9, 9, 11> which is 9, and compares this with the last price 11. Since the median is less than the last price, the program outputs "buy".

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
import java.util.NoSuchElementException;


/*The interface should have five methods: first, enqueue, dequeue, size, and median. The median will method will return the median of the values in the queue, if the size is odd. It will return null otherwise.*/
interface iMedianQueue <T extends Comparable<T>> {
void enqueue(Object item);
Object dequeue();
int size();
void median();
}
  
class medianQueue implements iMedianQueue<Integer> {
private Object[] array;
private int size = 0;
private int head = 0; // index of the current front item, if one exists
private int tail = 0; // index of next item to be added

public medianQueue(int capacity) {
array = new Object[capacity];
}

public void enqueue(Object item) {
if (size == array.length) {
throw new IllegalStateException("Cannot add to full queue");
}
array[tail] = item;
tail = (tail + 1) % array.length;
size++;
}

public Object dequeue() {
if (size == 0) {
throw new NoSuchElementException("Cannot remove from empty queue");
}
Object item = array[head];
array[head] = null;
head = (head + 1) % array.length;
size--;
return item;
}

public int size() {
return size;
}
  
  
//Find with median with the set of m recent
public void median(){
//

}
}
  
  

public class Solution {

public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

String[] tokens = reader.readLine().split(" ");
int n = Integer.valueOf(tokens[0]); // n indicates the number of prices in the input
int m = Integer.valueOf(tokens[1]); //most recent tanscations //m will be odd
medianQueue q = new medianQueue (4);
  
  
for (int i = 0; i < n; i++) {
int lastPrice = Integer.valueOf(reader.readLine());
//read in new numbers
  

  
}
   }
}

Explanation / Answer

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
import java.util.NoSuchElementException;

interface iMedianQueue <T extends Comparable<T>> {
void enqueue(Object item);
Object dequeue();
int size();
void median(int x,int y);
}
  
class medianQueue implements iMedianQueue<Integer> {
private Object[] array;
private int size = 0;
private int head = 0; // index of the current front item, if one exists
private int tail = 0; // index of next item to be added
public medianQueue(int capacity) {
array = new Object[capacity];
}
public void enqueue(Object item) {
if (size == array.length) {
throw new IllegalStateException("Cannot add to full queue");
}
array[tail] = item;
tail = (tail + 1) % array.length;
size++;
}
public Object dequeue() {
if (size == 0) {
throw new NoSuchElementException("Cannot remove from empty queue");
}
Object item = array[head];
array[head] = null;
head = (head + 1) % array.length;
size--;
return item;
}
public int size() {
return size;
}
  
  
//Find with median with the set of m recent
public void median(int x,int y){
//
int median_pos=(x+y)/2;
int a=(int)array[median_pos];
int b=(int)array[(x+y-1)];
if(a==b)
System.out.println("hold");
else if(a>b)
System.out.println("sell");
else
System.out.println( "buy");
}
}
public class HelloWorld{

public static void main(String []args)throws IOException{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

String[] tokens = reader.readLine().split(" ");
int n = Integer.valueOf(tokens[0]); // n indicates the number of prices in the input
int m = Integer.valueOf(tokens[1]); //most recent tanscations //m will be odd
medianQueue q = new medianQueue (5);
  
  
for (int i = 0; i < n; i++) {
int lastPrice = Integer.valueOf(reader.readLine());
//read in new numbers
q.enqueue(lastPrice);
}
for(int i=0;i<n-m+1;i++)
{
q.median(i,m);
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote