Needing help on printing mode in java - arraylist

I am given a task to make a method that takes a parameter of an ArrayList of Integer obj and print out the sum, average, and mode.
I can't seem to figure out how to find the mode. It should print out the number if there is only one mode, and it should print out "no single mode" if there is more than one (or none) mode. My method only prints out "no single mode". How can I fix my code to have the mode printed out?
This is what I have for my code:
public static void printStatistics(ArrayList<Integer> arr){
int sum = 0;
for(int i : arr){
sum += i;
}
System.out.println("Sum: "+sum);
System.out.println("Average: "+(double)sum/arr.size());
int temp = 0, counter = 0, max = 0;
for(int j = 0; j < arr.size() - 1; j++){
for(int k = j+1; k < arr.size(); k++){
if(arr.get(j) == arr.get(k)){
counter++;
if(counter > max){
max = counter;
temp = arr.get(j);
}
if(counter == max){
temp = -1;
}
}
}
}
if(temp > 0){
System.out.println("Mode: "+temp);
}
else if(temp < 0){
System.out.println("Mode: no single mode");
}
}

The problem lies here
if(counter > max){
max = counter;
temp = arr.get(j);
}
if(counter == max){
temp = -1;
}
You are assigning the value of counter to max in the first condition so the second if condition i.e., if(counter == max) will always be true, which results in temp having the value -1 which fulfills else if(temp < 0). This is why you are getting Mode: no single mode as the output every time.
Changing the condition should give you the desired output
if(counter < max){
temp = -1;
}

Related

find and remove element from array (solidity)

I've tackled a task: find a specific address in a sheet, move it to the end of the sheet, and remove it via a function pop! here is the code:
function removeAccount(address _account) external{
uint counter = arrayOfAccounts.length;
uint index;
for(uint i; i < counter; i++) {
if(arrayOfAccounts[i] == _account){
index = i;
break;
}
for(uint i = index; i < counter-1; i++){
arrayOfAccounts[i] = arrayOfAccounts[i + 1];
}
arrayOfAccounts.pop();
}
}
}
}
transact to Wote.removeAccount errored: VM error: revert.
revert
The transaction has been reverted to the initial state.
Note: The called function should be payable if you send value and the value you send should be less than your current balance.
Debug the transaction to get more information.
In case you dont care about addresses order
function removeAccount(address _account) external {
if(arrayOfAccounts.length == 1) {
arrayOfAccounts.pop();
}
else if(arrayOfAccounts[arrayOfAccounts.length - 1] == _account) {
arrayOfAccounts.pop();
}
else {
for (uint i = 0; i < arrayOfAccounts.length - 1; i++) {
if(_account == arrayOfAccounts[i]) {
arrayOfAccounts[i] = arrayOfAccounts[arrayOfAccounts.length - 1];
arrayOfAccounts.pop();
}
}
}
}
If order matters
function removeAccount(address _account) external{
uint counter = arrayOfAccounts.length;
for(uint i; i < counter; i++) {
if(arrayOfAccounts[i] == _account){
for(uint j = i; j < counter-1; j++){
arrayOfAccounts[j] = arrayOfAccounts[j + 1];
}
arrayOfAccounts.pop();
break;
}
}
}
}
Else if order doesn't matter
function removeAccount(address _account) external{
uint numAccounts = arrayOfAccounts.length;
for(uint i = 0; i < numAccounts; i++) {
if(arrayOfAccounts[i] == _account){ // if _account is in the array
arrayOfAccounts[i] = arrayOfAccounts[numAccounts - 1]; // move the last account to _account's index
arrayOfAccounts.pop(); // remove the last account
break;
}
}
}
The reason is just simple.
You used the second for loop inside the first for loop.
And also please initialize the index with counter;
uint256 index = counter;
And pop only when index is less than counter

How to write test cases using Equivalence Class, Boundary value, and Basis Path Testing

I have a method isPerfect(x) (with 4<=x<=10000) below, how to write test cases based on Equivalence Class, Boundary Value, and Basis Path Testing:
public boolean checkPerfectNumber(int x) {
if(x >= 4 && x <= 10000) {
int sum = 0;
for(int i = 1; i < x; i++) {
if(x % i == 0) {
sum += i;
}
}
if(sum == x) return true;
}
return false;
}

apps script: increase variable with each execution

Hi i have a list of values each value representing the output of a shift of packaging. I want to calculate the average output of 8 weeks. So each time a shift passes the average output changes. My idea is to trigger a function after each shift, which calculates the output. Now theres my problem, how do i get a varible (the one representing the row of the first value) to increase after each trigger of the function? What i tried is to declare the variable before the function and increase the variable of 1 inside the function. Buf ofc the starting value doesnt change this way.. Probably there is an easy way for this i just dont know yet (programming newbie here :)).
let i = 7;
let j = 126;
function schnitt() {
var summe = 0;
var counter = 0;
i++;
j++;
while(i <= j){
var aktuell = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Auswertung").getRange(i,6,1,1).getValue();
if(aktuell != ""){
summe = summe + aktuell;
counter++;
i++;
}
else{
i++
}
}
var durchschnitt = summe / counter;
var ausgabe = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Auswertung").getRange(8,7,1,1).setValue(durchschnitt);
}
I have found a work around. I just put var i and j into cells and do it like this:
function schnitt() {
var i = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Auswertung").getRange(3,14,1,1).getValue();
var j = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Auswertung").getRange(4,14,1,1).getValue();
var summe = 0;
var counter = 0;
while(i <= j){
var aktuell = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Auswertung").getRange(i,6,1,1).getValue();
if(aktuell != ""){
summe = summe + aktuell;
counter++;
i++;
}
else{
i++
}
}
var durchschnitt = summe / counter;
var ausgabe = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Auswertung").getRange(8,7,1,1).setValue(durchschnitt);
i = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Auswertung").getRange(3,14,1,1).getValue();
i++;
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Auswertung").getRange(3,14,1,1).setValue(i);
j = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Auswertung").getRange(4,14,1,1).getValue();
j++;
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Auswertung").getRange(4,14,1,1).setValue(j);
}

Getting indexOutOfBoundsException and I don't know why

I have been having this problem for a few hours. I don't know what it is, but I am having a hard time thinking clearly at the moment. This method displays a set of images. The first part of the method is just setting the gridbag constraints, whereas the next part in the if statement is creating jlabels and adding them to an arraylist of jlabels. The exception is being thrown when I try and add mouselisteners to the jlabels after they have been added to the arraylist (this is on line 112, and i have commented this on the code).
public void displayComplexStimulus(BufferedImage[] complexStimulus){
for(int i = 0; i < numberOfElements; i++){
if (i == 0 || i == 1 || i == 2){
c.gridx = i;
c.gridy = 0;
}
else if(i == 3 || i == 4 || i == 5){
c.gridx = i - 3;
c.gridy = 1;
}
else {
c.gridx = i - 6;
c.gridy = 2;
}
if(counter == 1){
if (phase1Trial.getPositionOfCorrectImage()!= i){
phase1IncorrectLabels.add(new JLabel(new ImageIcon(complexStimulus[i])));
phase1IncorrectLabels.get(i).addMouseListener(this); //line 112
add(phase1IncorrectLabels.get(i),c);
}
else if(phase1Trial.getPositionOfCorrectImage() == i){
correctLabel = new JLabel(new ImageIcon(complexStimulus[i]));
add(correctLabel, c);
correctLabel.addMouseListener(this);
}
}
}
}
If i==phase1Trial.getPositionOfCorrectImage() you're not adding an element to phase1IncorrectLabels. So in the next iteration after adding one element to the array it's at position i-1 and not i. You should replace your get(i) by get(phase1IncorrectLabels.size() - 1).

Codility extreme large Number error

I have a Codility test to take soon.
I was trying to find a modification in the code to avoid EXTREME LARGE NUMBERS ERROR by using LONG instead of INT... but this did not work.
Has anybody tried using CODILITY demo test and get a 100?
I went through previous posts but no solution to this particular problem.
MY CODE: COMPLEXITY O(N)... Still I got 94.
// you can also use includes for example:
// #include <algorithm>
#include<iostream>
#include<vector>
#include<math.h>
int equi ( const vector<int> &A ) {
if((int)A.size()==0)
return -1;
long int sum_l = A[0];
long int total_sum =0;
for(int i =0; i<(int)A.size();i++){
total_sum = total_sum + A[i];
}
int flag =0;
total_sum = total_sum -A[0];
if(total_sum == 0)
return 0;
for(int i=1; i<(int)A.size()-1;i++){
total_sum = total_sum - A[i];
if(sum_l ==total_sum){
flag=1;
return i;
}
sum_l= sum_l + A[i];
}
if(sum_l ==0)
return (int)A.size()-1;
if(flag ==0)
return -1;
}
I used long long, and I had not problem.
Try this one.
int left = A[0];
int right = 0;
for(int i: A){
right += i;
}
right -= left;
int diff = Math.abs(left - right);
for (int i = 1; i < A.length-1; i++) {
left += A[i];
right -= A[i];
int a = Math.abs(left - right);
if(diff > a){
diff = a;
}
}
return diff;