Skip to content

Instantly share code, notes, and snippets.

@mattcunningham
Last active January 17, 2017 16:55
Show Gist options
  • Save mattcunningham/5a3f3945549b5735f91ce72cf30393b4 to your computer and use it in GitHub Desktop.
Save mattcunningham/5a3f3945549b5735f91ce72cf30393b4 to your computer and use it in GitHub Desktop.
public class FlipFlop {
public static String findFlipFlop(int testNm) {
String retString = "";
for (int i = 1; i <= testNm; i++) {
boolean three = i % 3 == 0;
boolean five = i % 5 == 0;
if (three || five) {
if (three) {
retString += "Flip";
}
if (five) {
retString += "Flop";
}
} else {
retString += "Nah";
}
retString += ((testNm != i) ? " " : "");
}
return retString;
}
public boolean checkForFlop(int testNm) {
return findFlipFlop(testNm).indexOf("Flop") > -1;
}
}
public class ArrayMinMax {
public static int findMax(int[] numberArray) {
int max = 0;
for (int num : numberArray) {
if (num > max) {
max = num;
}
}
return max;
}
public static int findMin(int[] numberArray) {
int min = numberArray[0];
for (int num : numberArray) {
if (num < min) {
min = num;
}
}
return min;
}
public int totalMaxMin(int[] numberArray) {
int min = numberArray[0];
int max = 0;
for (int num : numberArray) {
if (num < min) {
min = num;
}
if (num > max) {
max = num;
}
}
return max + min;
}
}
import java.util.ArrayList;
public class NumberProperties {
// Constructor not shown
public ArrayList<Integer> factorsOf(int testNum) {
ArrayList<Integer> factors = new ArrayList<Integer>();
for (int i = 1; i <= testNum; i++) {
if (testNum % i == 0) {
factors.add(i);
}
}
return factors;
}
public int[] multiplesIn(int testNum, int start, int finish) {
int[] multiples = new int[finish-start];
for (int i = start; i <= finish; i++) {
multiples[start-i] = testNum * i;
}
return multiples;
}
}
import java.util.ArrayList;
public class NutsNBolts {
public int sumArray(int[] numberArray, int start, int end) {
int total = 0;
for (int i = start; i <= end; i++) {
total += numberArray[i];
}
return total;
}
public int productArray(int[] numberArray, int start, int end) {
int total = 1;
for (int i = start; i <= end; i++) {
total *= numberArray[i];
}
return total;
}
public boolean isIncreasing(int[] numberArray, int start, int end) {
int last = 0;
for (int i = start; i < end; i++) {
if (last > numberArray[i]) {
return false;
}
last = numberArray[i];
}
return true;
}
public int[] getDivisibles(int[] numberArray, int start, int end) {
ArrayList<Integer> retList = new ArrayList<Integer>();
if (isIncreasing(numberArray, start, end)) {
for (int num : numberArray) {
if (num % 3 == 0) retList.add(num);
}
} else {
for (int num : numberArray) {
if (num % 2 == 0) retList.add(num);
}
}
int[] retArray = new int[retList.size()];
for (int i = 0; i < retList.size(); i++) {
retArray[i] = retList.get(i);
}
return retArray;
}
public int concatenateArray(int[] numberArray) {
int ret = 0;
for (int i = 0; i < numberArray.length; i++) {
ret += numberArray[numberArray.length-i-1]*(int)(Math.pow(10.0, (double)i));
}
return ret;
}
}
public class TimeGaps {
public int getShortestTimeJump(int[] startYears, int[] endYears) {
int shortest = Integer.MAX_VALUE;
for (int i = 0; i < startYears.length; i++) {
int val = endYears[i] - startYears[i];
if (val < shortest) shortest = val;
}
return shortest;
}
public int getGalacticID(String galacticName) {
final String ALPHABET = " abcdefghijklmnopqrstuvwxyz";
int galacticID = 0;
for (char c : galacticName.toLowerCase().toCharArray()) {
galacticID += ALPHABET.indexOf(c);
}
return galacticID;
}
}
import java.util.Random;
public class Dice {
private Random dice = new Random();
public Dice() { }
public int diceRoll() {
return dice.nextInt(6) + 1;
}
public double averageDiceRoll(int numberOfRolls) {
double ret = 0.0;
for (int i = 0; i < numberOfRolls; i++) {
ret += (double) this.diceRoll();
}
return ret/(double)numberOfRolls;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment