Skip to content

Instantly share code, notes, and snippets.

@mattcunningham
Last active January 11, 2017 18:23
Show Gist options
  • Save mattcunningham/38b05680295e63193ff1eb1d16cc5103 to your computer and use it in GitHub Desktop.
Save mattcunningham/38b05680295e63193ff1eb1d16cc5103 to your computer and use it in GitHub Desktop.
public class ArrayMadness {
public static int findMax(int [] numbersArray) {
int max = numbersArray[0];
for (int number : numbersArray) {
if (number > max) {
max = number;
}
}
return max;
}
public static int findMin(int [] numbersArray) {
int min = numbersArray[0];
for (int number : numbersArray) {
if (number < min) {
min = number;
}
}
return min;
}
public static int totalMaxMin(int [] numbersArray) {
return findMax(numbersArray) + findMin(numbersArray);
}
public static void main(String[] args) {
int [] testValues = {6, 20, 31, 2, 7, 14};
if (findMax(testValues) == 31) {
System.out.println("Part A is correct.");
}
else {
System.out.println("Part A is incorrect.");
}
if (findMin(testValues) == 2) {
System.out.println("Part B is correct.");
}
else {
System.out.println("Part B is incorrect.");
}
if (totalMaxMin(testValues) == 33) {
System.out.println("Part C is correct.");
}
else {
System.out.println("Part C is incorrect.");
}
}
}
public class Encrypter {
// lowercase letters of the alphabet
private char[] normalAlphabet = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z'};
private char[] encryptAlphabet = new char[26];
private int shiftAmount;
private boolean shiftToLeft;
public Encrypter(int shiftAmount, boolean shiftToLeft) {
this.shiftAmount = shiftAmount;
this.shiftToLeft = shiftToLeft;
if (shiftToLeft) {
shiftLeft();
} else {
shiftRight();
}
}
public String encrypt(String normalWord) {
String newString = "";
for (char c : normalWord.toCharArray()) {
for (int i = 0; i < this.normalAlphabet.length; i++) {
if (this.normalAlphabet[i] == c) {
newString += this.encryptAlphabet[i];
break;
}
}
}
return newString;
}
public void shiftLeft() {
for (int i = 0; i < this.normalAlphabet.length; i++) {
int newPos = i + this.shiftAmount;
if (newPos < this.normalAlphabet.length) {
this.encryptAlphabet[newPos] = this.normalAlphabet[i];
} else {
this.encryptAlphabet[newPos - 26] = this.normalAlphabet[i];
}
}
}
public void shiftRight() {
for (int i = 0; i < this.normalAlphabet.length; i++) {
int newPos = i - this.shiftAmount;
if (newPos < 0) {
this.encryptAlphabet[newPos + 26] = this.normalAlphabet[i];
} else {
this.encryptAlphabet[newPos] = this.normalAlphabet[i];
}
}
}
}
public class NumberChanger {
public static int [] changeNumbers(int[] numberArray, int changeFrom, int changeTo) {
for (int i = 0; i < numberArray.length; i++) {
if (numberArray[i] == changeFrom) {
numberArray[i] = changeTo;
}
}
return numberArray;
}
public static void printArray(int[] numberArray) {
for (int i : numberArray) {
System.out.print(i + " ");
}
System.out.println();
}
public static void main(String args[]) {
int [] testArray = {1, 3, 5, 3, 7, 7, 8, 1, 5, 3};
int [] changedArray = changeNumbers(testArray, 3, 5);
System.out.println("Expected output: 1 5 5 5 7 7 8 1 5 5");
System.out.print("Your output: ");
printArray(changedArray);
}
}
import java.util.ArrayList;
public class Quizzes {
public int[] getScores(int[] scoresArray, String dayOfWeek){
String[] daysOfTheWeek = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
int num = 0;
int[] ret = new int[scoresArray.length/5];
for (int i = 0; i < daysOfTheWeek.length; i++) {
if (daysOfTheWeek[i].equals(dayOfWeek)) {
num = i;
break;
}
}
for (int i = 0; i < scoresArray.length/5; i++) {
ret[i] = scoresArray[(i*5)+num];
}
return ret;
}
public String bestDay(int[] scoresArray){
String[] daysOfTheWeek = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
String best = "";
double max = 0.0;
for (String day : daysOfTheWeek) {
double average = 0.0;
int[] scores = getScores(scoresArray, day);
for (double score : scores) {
average += score;
}
average /= scores.length;
if (average > max) {
max = average;
best = day;
}
}
return best;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment