Skip to content

Instantly share code, notes, and snippets.

@dmaii
Created January 31, 2014 03:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dmaii/8726092 to your computer and use it in GitHub Desktop.
Save dmaii/8726092 to your computer and use it in GitHub Desktop.
Coderbyte Problems for Reshad
import java.util.*;
import java.io.*;
class Function {
String CoinDeterminer(int sum) {
int[] coins = {1,5,7,9,11};
List<Integer> knapsack = new ArrayList<Integer>();
knapsack.add(0);
knapsack.add(1);
for (int i = 2; i <= sum; i++) {
int minCoin = 11;
int minValue = i;
for (int denom : coins) {
if (denom <= i && knapsack.get(i - denom) < minValue) {
int nextValue = knapsack.get(i - denom);
minCoin = denom;
minValue = nextValue;
}
}
knapsack.add(i, knapsack.get(i - minCoin) + 1);
}
return knapsack.get(sum).toString();
}
}
public class CoinDeterminer {
public static void main (String[] args) {
Function f = new Function();
System.out.println(f.CoinDeterminer(35));
}
}
import java.util.*;
import java.io.*;
class Function {
static boolean FibEquations(int x, boolean sign) {
double signed = sign ? 4.0 : -4.0;
double val = Math.sqrt(5.0 * Math.pow(x, 2) + signed);
return val % 1 == 0 ? true : false;
}
String FibonacciChecker(int num) {
return (FibEquations(num, true) || FibEquations(num, false)) ? "yes" : "no";
}
}
public class FibonacciChecker {
public static void main (String[] args) {
Function f = new Function();
System.out.println(f.FibonacciChecker(986));
}
}
import java.util.*;
import java.io.*;
class Function {
String MostFreeTime(String[] slots) {
List<Integer> beginnings = new ArrayList<Integer>();
List<Integer> endings = new ArrayList<Integer>();
for (String slot : slots) {
String[] split = slot.split("-");
for (int index = 0; index <= 1; index++) {
String s = split[index];
String[] hm = s.split(":");
int hours = Integer.parseInt(hm[0]);
int minutes = Integer.parseInt(hm[1].substring(0, 2));
String ampm = hm[1].substring(2, 4);
if (ampm.equals("PM") && hours >= 1 && hours <= 11) {
hours = hours + 12;
} else if (ampm.equals("AM") && hours == 12) {
hours = hours - 12;
}
int timeInMinutes = hours * 60 + minutes;
if (index == 0) {
beginnings.add(timeInMinutes);
} else {
endings.add(timeInMinutes);
}
}
}
Collections.sort(beginnings);
Collections.sort(endings);
int largest = 0;
for (int i = 1; i < beginnings.size(); i++) {
int diff = beginnings.get(i) - endings.get(i - 1);
if (diff > largest) {
largest = diff;
}
}
return String.format("%02d", new Integer(largest / 60)) + ":" + String.format("%02d", new Integer(largest % 60));
}
}
public class MostFreeTime {
public static void main (String[] args) {
Function f = new Function();
String[] slots = {"06:00AM-08:00PM","05:00AM-05:45AM","08:02PM-08:04PM","08:10PM-09:00PM","09:09PM-09:11PM"};
System.out.println(f.MostFreeTime(slots));
}
}
import java.util.*;
import java.io.*;
class Function {
String NumberSearch(String str) {
char[] chars = str.toCharArray();
float letters = 0;
float sum = 0;
for (Character c : chars) {
if (Character.isLetter(c)) {
letters += 1;
} else if (Character.isDigit(c)) {
sum += Character.digit(c, 10);
}
}
return (new Integer(Math.round(sum / letters))).toString();
}
}
public class NumberSearch {
public static void main (String[] args) {
Function f = new Function();
System.out.println(f.NumberSearch("Hel4 e4ee"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment