Skip to content

Instantly share code, notes, and snippets.

@mrjeremyt
Created June 30, 2015 21:55
Show Gist options
  • Save mrjeremyt/1cc0e0b3b3d5da5bad32 to your computer and use it in GitHub Desktop.
Save mrjeremyt/1cc0e0b3b3d5da5bad32 to your computer and use it in GitHub Desktop.
BetterProgrammer certificate code
/**
* Created by MrJeremyT on 6/30/15.
*/
import java.util.ArrayList;
import java.util.List;
public class BetterProgrammerTask {
public static void main(String[] args){
// Change c = getCorrectChange(555);
// System.out.println("Dollars: " + c.getDollars());
// System.out.println("Quarters: " + c.getQuarters());
// System.out.println("Dimes: " + c.getDimes());
// System.out.println("Nickels: " + c.getNickels());
// System.out.println("Cents: " + c.getCents());
// System.out.println(getBinaryRepresentation(5));
System.out.println(transferFromAtoC(4));
}
public static List<String> transferFromAtoC(int n) {
/*
Towers Of Hanoi.
There are three pegs: A, B and C. There are n disks. All disks are different in size.
The disks are initially stacked on peg A so that they increase in size from the top to the bottom.
The goal is to transfer the entire tower from the A peg to the C peg.
One disk at a time can be moved from the top of a stack either to an empty peg or to
a peg with a larger disk than itself on the top of its stack.
The method should return a sequence of disk moves, each move is a String with two letters (A, B or C)
corresponding to the peg the disk moves from and the peg it moves to.
For example, the move "AC" means that a top disk from peg A should be moved to peg C.
*/
List<String> moves = new ArrayList<String>();
solve(n, "A", "B", "C", moves);
return moves;
}
public static void solve(int n, String start, String extra, String end, List<String> moves) {
if (n == 1) {
moves.add(start + end);
} else {
solve(n - 1, start, end, extra, moves);
moves.add(start + end);
solve(n - 1, extra, start, end, moves);
}
}
// Please do not change this interface
public static interface Node {
int getValue();
List<Node> getChildren();
}
public static double getAverage(Node root) {
/*
Please implement this method to
return the average of all node values (Node.getValue()) in the tree.
*/
List<Node> toEvaluate = new ArrayList<Node>();
toEvaluate.add(root);
int numNodes = 0;
double total = 0;
while(toEvaluate.size() > 0){
Node temp = toEvaluate.get(0);
toEvaluate.remove(0);
toEvaluate.addAll(temp.getChildren());
numNodes++;
total += temp.getValue();
}
return total/numNodes;
}
public static String getBinaryRepresentation(int n) {
/*
Please implement this method to
return a String with the binary representation of any number n, where n >= 0.
Example: "101" is a binary representation of 5
*/
return Integer.toBinaryString(n);
}
public static Change getCorrectChange(int cents) {
/*
Please implement this method to
take cents as a parameter
and return an equal amount in dollars and coins using the minimum number of
coins possible.
For example: 164 cents = 1 dollar, 2 quarters, 1 dime and 4 cents.
Return null if the parameter is negative.
*/
int dollars = 0;
int quarters = 0;
int dimes = 0;
int nickels = 0;
int pennies = 0;
if (cents >= 100){
dollars = cents / 100;
cents %= 100;
}
if (cents >= 25){
quarters = cents / 25;
cents %= 25;
}
if (cents >= 10){
dimes = cents / 10;
cents %= 10;
}
if (cents >= 5){
nickels = cents / 5;
cents %= 5;
}
if(cents > 0){
pennies = cents;
}
return new Change(dollars, quarters, dimes, nickels, pennies);
}
// Please do not change this class
static class Change {
private final int _dollars;
private final int _quarters; //25 cents
private final int _dimes; // 10 cents
private final int _nickels; // 5 cents
private final int _cents; // 1 cent
public Change(int dollars, int quarters, int dimes, int nickels, int cents) {
_dollars = dollars;
_quarters = quarters;
_dimes = dimes;
_nickels = nickels;
_cents = cents;
}
public int getDollars() {
return _dollars;
}
public int getQuarters() {
return _quarters;
}
public int getDimes() {
return _dimes;
}
public int getNickels() {
return _nickels;
}
public int getCents() {
return _cents;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment