Question: I am trying to override the add method of ArrayList to remember the last three digits added to the arraylist. I am to then take these three values and find the maximum value among them.
It seems that I can not get my add method to save the three elements without an out of bounds error.
import java.util.ArrayList;
public class MaxThreeArrayList extends ArrayList<Integer> {
MaxThreeArrayList maxlist = new MaxThreeArrayList();
int third_last = 0;
int second_last = 0;
int last = 0;
#Override public boolean add(Integer o){
if (maxlist.size() == 1){
last = maxlist.get(maxlist.size() - 1);
}
else if (maxlist.size() == 2){
last = maxlist.get(maxlist.size() - 1);
second_last = maxlist.get(maxlist.size() - 2);
}
else if (maxlist.size() >= 3){
last = maxlist.get(maxlist.size() - 1);
second_last = maxlist.get(maxlist.size() - 2);
third_last = maxlist.get(maxlist.size() - 3);
}
return super.add(o);
}
public int getMax(){
int maximum = 0;
if (third_last >= second_last){
if (third_last >= last){
maximum = third_last;
}
}
if (second_last >= third_last){
if (second_last >= last){
maximum = second_last;
}
}
if (last >= second_last){
if (last >= third_last){
maximum = last;
}
}
return maximum;
}
public static void main(String[] args) {
}
}
I think you should separate your concerns. Your overridden add should only worry about remembering the last three things you add to the list. This can be accomplished pretty easy with a deque. The method getMax should figure out which one is the largest, as its name suggests.
public class MaxThreeArrayList extends ArrayList<Integer> {
private final Deque<Integer> deque = new LinkedList<>();
#Override public boolean add(Integer i) {
deque.addFirst(i);
if (deque.size() > 3) { deque.removeLast(); }
return super.add(i);
}
public Integer getMax() {
Integer max = 0;
for (Integer i : deque) {
if (i > max) { max = i; }
}
return max;
}
}
As an additional exercise, you may note that this code can be made more general. It doesn't need to be hard-coded to remember the last 3. You could make that number configurable.
Related
As an example we're combing through the permutations of the integer 123456789. Inspired by Heap's algorithm, we have the following
public static ArrayList<String> comb(char[] seq, int n, ArrayList<String> box){
if(n == 1){
if (isSquare(Integer.valueOf(String.valueOf(seq)))) {
box.add(String.valueOf(seq));
}
} else {
for(int i=0; i<n; i++){
comb(seq,n-1, box);
int j;
if ((n%2)==0) {
j = i;
} else {
j = 0;
}
char temp = seq[n-1];
seq[n-1] = seq[j];
seq[j] = temp;
}
}
return box;
}
In the present case we're interested whether a particular permutation is a square of an integer. Realised by
public static boolean isSquare(int n) {
if ((n%10)==2 || (n%10) ==3 || (n%10)==7 || (n%10) == 8) {
return false;
} else if ( (Math.sqrt(n)) % 1 ==0) {
return true;
} else {
return false;
}
}
However, to be able to use comb I must initialise an empty array outside of the method. What should I do to avoid inducing the need for global variable? I would still like to obtain a box with all solutions. I realise my error is in the parametrisation of comb .
Create a function that "wraps" the original recursive function, provides it with every parameter it needs and creates copies of objects if necessary:
Let's say you renamed your comb(...) function to combRecursive(...) for the sake of convenient naming.
public static ArrayList<String> comb(char[] seq, int n){
char[] seqCopy = Arrays.copyOf(seq, seq.length);
return combRecursive(seqCopy, n, new ArrayList());
}
I'm trying to find the majority or leader, in a stack that isn't sorted, and i'm having trouble with my tos (top of stack variable). Below is my code with main included. The majority of an array or stack is any element which appears in the array more than half the time (arrSize/2).
public class findLeader {
static class ArrayStack{
private int[] stackArr;
private int tos;//top of stack
public ArrayStack(){
stackArr = new int[10];
tos = -1;
}
public ArrayStack(int size){
stackArr = new int[size];
tos = -1;
}
public ArrayStack(int[] arr, int tos){
stackArr = arr;
this.tos = tos;
}
public boolean isEmpty(){
return(tos == -1);
}
public int peek(){
if(isEmpty()){
return -999;
}
return stackArr[tos];
}
public void push(int x){
if(tos == stackArr.length - 1){
return;
}
stackArr[++tos] = x;
}
public int pop(){
if(isEmpty()){
return -999;
}
int popValue = stackArr[tos];
stackArr[tos] = 0;
--tos;
return popValue;
}
public void print(){
if(isEmpty()){
return;
}
for(int i = 0; i <= tos; ++i){
System.out.print(stackArr[i] + " ");
}
System.out.println();
}
}
public static int leader(ArrayStack myStack){
int initSize = myStack.tos + 1; //gets initial size of stack.
int leader; //initialize leader or majority.
while(!myStack.isEmpty()){
leader = myStack.peek();//set first leader variable to the element at the tos.
System.out.println("leader " + leader); //just for debugging
System.out.println("tos " + myStack.tos); //debugging
//System.out.println(isLeader(myStack, initSize, leader)); //debugging
if(isLeader(myStack, initSize, leader)){
return 1;
}
else{
myStack.pop();
}
System.out.println("after function tos " + myStack.tos); //debugging
}
return -1;
}
public static boolean isLeader(ArrayStack myStack, int initSize, int leader){
ArrayStack as = myStack;
int count = 0;
while(!as.isEmpty()){
if(as.peek() == leader){
as.pop();
++count;
}
else{
as.pop();
}
}
//System.out.println(count);
if(count > initSize / 2)
return true;
else{
return false;
}
}
public static void main(String[] args) {
int[] arr = {2, 5, 6, 2, 8, 2, 8, 2, 2};
ArrayStack stack = new ArrayStack();
stack.push(5);
stack.push(2);
stack.push(6);
stack.push(2);
stack.push(8);
stack.push(2);
stack.push(2);
stack.push(2);
stack.push(5);
System.out.println(leader(stack));
}
Where the problem arises is in the leader, and isLeader methods, after the initial call of isLeader, tos gets returned as tos = -1 as is demonstrated from my output below.
Output:
leader 5
tos 8
after function tos -1
-1
My intent is after every call to isLeader() if false is returned, i want to pop the tos variable from the top and call isLeader() once more with the new smaller stack.
Any help would really be appreciated, and I hope everyone is well!
Thank you
I'd personally add a method public Integer leader() to your ArrayStack that returns the stack's leader or null if the stack has no leader:
public Integer leader() {
final Map<Integer, Integer> counts = new HashMap<>();
final int neededLeaderCount = ((tos + 1) / 2) + 1;
for (int i = 0; i <= tos; ++i) {
int currentElementCount = counts.getOrDefault(stackArr[i], 0) + 1;
if (currentElementCount >= neededLeaderCount) {
return stackArr[i];
}
counts.put(stackArr[i], currentElementCount);
}
return null;
}
Your isLeader function could then be rewritten to:
public static boolean isLeader(ArrayStack myStack, int leader) {
// use Objects.equals as myStack.leader() may be null
return Objects.equals(myStack.leader(), leader);
}
This approach is much cleaner as it does not modify or require any internals of your ArrayStack outside of the class itself.
I was able to get it to work by also tracking a currSize of the stack to pass to the isLeader function. I feel like i am complicating things, if anyone has a simpler solution and would like to share it, I would be open to it. Below is my code if anyone is interested.
public static int leader(ArrayStack myStack){
int currSize = myStack.tos;
int initSize = myStack.tos + 1; //gets initial size of stack.
int leader; //initialize leader or majority.
while(!myStack.isEmpty()){
leader = myStack.peek();//set first leader variable to the element at the tos.
System.out.println("leader " + leader); //just for debugging
System.out.println("tos " + myStack.tos); //debugging
//System.out.println(isLeader(myStack, initSize, leader)); //debugging
if(isLeader(myStack, initSize, leader, currSize)){
return 1;
}
else{
--currSize;
myStack.pop();
}
System.out.println("after function tos " + myStack.tos); //debugging
}
return -1;
}
public static boolean isLeader(ArrayStack myStack, int initSize, int leader, int currSize){
ArrayStack as = myStack;
int count = 0;
while(as.tos > -1){
if(as.peek() == leader){
--as.tos; //Just move tos pointer down stack rather than popping
//to maintain original stack.
++count;
}
else{
--as.tos;
}
}
if(count > initSize / 2)
return true;
else{
as.tos = currSize;//reset the tos pointer back to the currSize of
//stack.
return false;
}
}
What I need to do is find the inventory item with the lowest cost from an array list. If there are none, return "N/A". So I can't really figure out how to find the smallest value out of all the inventory item costs. This is what I have so far. My plan was to set cost equal to the first inventory item and cycle through each item checking if it's cost is less but I'm confused on how to do that or if that is even the correct way of doing it.
EDIT: We have not covered for loops yet so I cannot use them. I can only use while loops
public InventoryItem itemWithLowestCost() {
if (inventoryList.size() > 0) {
int i = 0;
while (i < inventoryList.size()) {
double cost = inventoryList.get(i).getCost();
if (cost < )
}
}
else {
return "N/A";
}
}
Here is my InventoryItem constructor. I'm not sure if this is needed to understand the above code but here it is anyway
public InventoryItem(String descriptionIn, double costIn, double percentDepreciatedIn) {
description = descriptionIn.trim();
cost = costIn;
percentDepreciated = percentDepreciatedIn;
}
You can do it by checking the length to be non-zero, then picking the cost of element zero as "tentative best", and comparing it to costs of other elements on the list:
if (inventoryList.size() > 0) {
int best = 0;
int i = 1;
while (i < inventoryList.size()) {
double cost = inventoryList.get(i).getCost();
if (cost < inventoryList.get(best).getCost()) {
best = i;
}
i++;
}
return inventoryList.get(best);
} else {
return "N/A";
}
You can do it with a O(n) loop
public InventoryItem itemWithLowestCost() {
int size = (inventoryList == null || inventoryList.isEmpty()) ?
0 : inventoryList.size();
InventoryItem min = (size > 0) ? inventoryList.get(0) : null;
for (int i = 1; i < size; i++) {
InventoryItem ii = inventoryList.get(i);
if (ii.getCost() < min.getCost()) min = ii;
}
return min;
}
Note: That you can't return a String N/A as an InventoryItem instance (so this returns null).
You could use a comparitor to sort the inventory by cost, lowest to highets.
public InventoryItem itemWithLowestCost(Collection<InventoryItem> inventory) {
if(inventory != null && !inventory.isEmpty()) {
Collections.sort(inventory, new Comparator<InventoryItem>() {
#Override
public int compare(InventoryItem i1, InventoryItem i2) {
return i1.getCost() - i2.getCost();
}
});
return inventory.get(0);
}
return null;
}
Is there a way to implement a loop using final variables?
I mean a loop that would run for a specified number of iterations when you are not allowed to change anything after initialization!
Is recursion allowed, or do you literally need a loop construct like for or while? If you can use recursion, then:
void loop(final int n) {
if (n == 0) {
return;
} else {
System.out.println("Count: " + n);
loop(n-1);
}
}
One way is to create an Iterable<Integer> class representing an arbitrary range (without actually having to store all of the values in a list):
public static class FixedIntRange implements Iterable<Integer> {
private final int min;
private final int max;
public FixedIntRange(final int min, final int max) {
this.min = min;
this.max = max;
}
#Override
public Iterator<Integer> iterator() {
return new Iterator<Integer>() {
private Integer next = FixedIntRange.this.min;
#Override
public boolean hasNext() {
return next != null;
}
#Override
public Integer next() {
final Integer ret = next;
next = ret == max ? null : next + 1;
return ret;
}
#Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}
}
and then iterate over it normally:
for (final int i : new FixedIntRange(-10, 20)) {
// this will be run for each i in the range [-10, 20]
}
Create an array whose size is the required number of iterations, then use it in a for-each loop:
public class Test {
public static void main(String[] args) {
final int N = 20;
final int[] control = new int[N];
for(final int i : control){
System.out.println(i);
}
}
}
The trick here is that the iteration indexing is generated by the compiler as part of the enhanced for statement, and does not use any user-declared variable.
Something like this -
final int max = 5;
for(int i=0; i<max; i++) {}
Or another interesting one-
final boolean flag = true;
while(flag) {
// keep doing your stuff and break after certain point
}
One more-
List<String> list = ......
for(final Iterator iterator = list.iterator(); iterator.hasNext(); ) {
}
i have a task where i need to find the mode of an array. which means i am looking for the int which is most frequent. i have kinda finished that, but the task also says if there are two modes which is the same, i should return the smallest int e.g {1,1,1,2,2,2} should give 1 (like in my file which i use that array and it gives 2)
public class theMode
{
public theMode()
{
int[] testingArray = new int[] {1,1,1,2,2,2,4};
int mode=findMode(testingArray);
System.out.println(mode);
}
public int findMode(int[] testingArray)
{
int modeWeAreLookingFor = 0;
int frequencyOfMode = 0;
for (int i = 0; i < testingArray.length; i++)
{
int currentIndexOfArray = testingArray[i];
int frequencyOfEachInArray = howMany(testingArray,currentIndexOfArray);
if (frequencyOfEachInArray > frequencyOfMode)
{
modeWeAreLookingFor = currentIndexOfArray;
frequencyOfMode = modeWeAreLookingFor;
}
}
return modeWeAreLookingFor;
}
public int howMany(int[] testingArray, int c)
{
int howManyOfThisInt=0;
for(int i=0; i < testingArray.length;i++)
{
if(testingArray[i]==c){
howManyOfThisInt++;
}
}
return howManyOfThisInt;
}
public static void main(String[] args)
{
new theMode();
}
}
as you see my algorithm returns the last found mode or how i should explain it.
I'd approach it differently. Using a map you could use each unique number as the key and then the count as the value. step through the array and for each number found, check the map to see if there is a key with that value. If one is found increment its value by 1, otherwise create a new entry with the value of 1.
Then you can check the value of each map entry to see which has the highest count. If the current key has a higher count than the previous key, then it is the "current" answer. But you have the possibility of keys with similar counts so you need to store each 'winnning' answer.
One way to approach this is to check each map each entry and remove each entry that is less than the current highest count. What you will be left with is a map of all "highest counts". If you map has only one entry, then it's key is the answer, otherwise you will need to compare the set of keys to determine the lowest.
Hint: You're updating ModeWeAreLookingFor when you find a integer with a strictly higher frequency. What if you find an integer that has the same frequency as ModeWeAreLookingFor ?
Extra exercice: In the first iteration of the main loop execution, you compute the frequency of '1'. On the second iteration (and the third, and the fourth), you re-compute this value. You may save some time if you store the result of the first computation. Could be done with a Map.
Java code convention states that method names and variable name should start with a lower case character. You would have a better syntax coloring and code easier to read if you follow this convention.
this might work with a little modification.
http://www.toves.org/books/java/ch19-array/index.html#fig2
if ((count > maxCount) || (count == maxCount && nums[i] < maxValue)) {
maxValue = nums[i];
maxCount = count;
}
since it seems there are no other way, i did a hashmap after all. i am stuck once again in the logics when it comes to comparing frequencys and and the same time picking lowest integer if equal frequencys.
public void theMode()
{
for (Integer number: intAndFrequencyMap.keySet())
{
int key = number;
int value = intAndFrequencyMap.get(number);
System.out.println("the integer: " +key + " exists " + value + " time(s).");
int lowestIntegerOfArray = 0;
int highestFrequencyOfArray = 0;
int theInteger = 0;
int theModeWanted = 0;
if (value > highestFrequencyOfArray)
{
highestFrequencyOfArray = value;
theInteger = number;
}
else if (value == highestFrequencyOfArray)
{
if (number < theInteger)
{
number = theInteger;
}
else if (number > theInteger)
{
}
else if (number == theInteger)
{
number = theInteger;
}
}
}
}
Completed:
import java.util.Arrays;
public class TheMode
{
//Probably not the most effective solution, but works without hashmap
//or any sorting algorithms
public TheMode()
{
int[] testingArray = new int[] {2,3,5,4,2,3,3,3};
int mode = findMode(testingArray);
System.out.println(Arrays.toString(testingArray));
System.out.println("The lowest mode is: " + mode);
int[] test2 = new int[] {3,3,2,2,1};
int mode2=findMode(test2);
System.out.println(Arrays.toString(test2));
System.out.println("The lowest mode is: " +mode2);
int[] test3 = new int[] {4,4,5,5,1};
int mode3 = findMode(test3);
System.out.println(Arrays.toString(test3));
System.out.println(The lowest mode is: " +mode3);
}
public int findMode(int[] testingArray)
{
int modeWeAreLookingFor = 0;
int frequencyOfMode = 0;
for (int i = 0; i < testingArray.length; i++)
{
int currentIndexOfArray = testingArray[i];
int countIntegerInArray = howMany(testingArray, currentIndexOfArray);
if (countIntegerInArray == frequencyOfMode)
{
if (modeWeAreLookingFor > currentIndexOfArray)
{
modeWeAreLookingFor = currentIndexOfArray;
}
}
else if (countIntegerInArray > frequencyOfMode)
{
modeWeAreLookingFor = currentIndexOfArray;
frequencyOfMode = countIntegerInArray;
}
}
return modeWeAreLookingFor;
}
public int howMany(int[] testingArray, int c)
{
int howManyOfThisInt=0;
for(int i=0; i < testingArray.length;i++)
{
if(testingArray[i]==c){
howManyOfThisInt++;
}
}
return howManyOfThisInt;
}
public static void main(String[] args)
{
new TheMode();
}
}
Glad you managed to solve it. As you will now see, there is more than one way to approach a problem. Here's what I meant by using a map
package util;
import java.util.HashMap;
import java.util.Map;
public class MathUtil {
public static void main(String[] args) {
MathUtil app = new MathUtil();
int[] numbers = {1, 1, 1, 2, 2, 2, 3, 4};
System.out.println(app.getMode(numbers));
}
public int getMode(int[] numbers) {
int mode = 0;
Map<Integer, Integer> numberMap = getFrequencyMap(numbers);
int highestCount = 0;
for (int number : numberMap.keySet()) {
int currentCount = numberMap.get(number);
if (currentCount > highestCount) {
highestCount = currentCount;
mode = number;
} else if (currentCount == highestCount && number < mode) {
mode = number;
}
}
return mode;
}
private Map<Integer,Integer> getFrequencyMap(int[] numbers){
Map<Integer, Integer> numberMap = new HashMap<Integer, Integer>();
for (int number : numbers) {
if (numberMap.containsKey(number)) {
int count = numberMap.get(number);
count++;
numberMap.put(number, count);
} else {
numberMap.put(number, 1);
}
}
return numberMap;
}
}