Skip to content

Instantly share code, notes, and snippets.

@CompSciRocks
CompSciRocks / WordPairList.java
Created November 27, 2018 16:01
WordPair solution for the 2018 AP Computer Science Exam - https://compsci.rocks/wordpair-solution/
public WordPairList(String[] words) {
allPairs = new ArrayList<>();
for (int i=0; i<words.length; i++)
for (x=i+1; x<words.length; x++)
allPairs.add(new WordPair(words[i], words[x]);
}
@CompSciRocks
CompSciRocks / HiddenWord.java
Last active April 23, 2019 17:07
Solution for HiddenWord Free Response - https://compsci.rocks/hiddenword-solution/
public class HiddenWord {
private String word;
public HiddenWord(String w) {
word = w;
}
public String getHint(String guess) {
String out = "";
@CompSciRocks
CompSciRocks / colorImage.php
Last active December 5, 2018 14:42
Tiny script to create solid color images using PHP and GD Library for https://colorswatches.info - blog post at https://compsci.rocks/solid-color-image-in-php/
<?php
$red = 100;
$blue = 150;
$green = 200;
$my_img = imagecreate( 1000, 1000 );
$background = imagecolorallocate( $my_img, $red, $green, $blue );
header( "Content-type: image/png" );
imagepng( $my_img );
imagecolordeallocate( $my_img, $background );
imagedestroy( $my_img );
@CompSciRocks
CompSciRocks / arraySum.java
Created December 2, 2018 14:53
Solutions to DiverseArray problem on 2015 AP CompSci exam - https://compsci.rocks/diversearray-solution/
public static int arraySum( int[] arr ) {
int s = 0;
for (int n: arr)
s += n;
return s;
}
public static int arraySum( int[] arr ) {
int s = 0;
for (int i=; i<arr.length; i++) {
@CompSciRocks
CompSciRocks / testMethod.java
Last active November 29, 2018 17:09
How to wrap a catch for InvocationTargetException in a junit test - https://compsci.rocks/invocationtargetexception-fix/
public void test_MeetAtMidnight() throws Exception {
RouteCipher rc = new RouteCipher( 3, 5 );
setField( rc, "numRows", 3 );
setField( rc, "numCols", 5 );
Method m = RouteCipher.class.getDeclaredMethod( "fillBlock", new Class[]{ String.class } );
m.setAccessible( true );
// This is the problem line
m.invoke( rc, "Meet at midnight" );
@CompSciRocks
CompSciRocks / constructor.java
Created November 28, 2018 20:20
Solution for 2016 AP Computer Science Free Response LogMessage - https://compsci.rocks/logmessage-solution/
public LogMessage( String message ) {
int pos = message.indexOf(":");
machineId = message.substring(0, pos);
description = message.substring(pos + 1);
}
@CompSciRocks
CompSciRocks / LeftOverSpaces.java
Last active November 28, 2018 18:30
Working solution to the 2016 AP Computer Science Free Response Question String Formatter - https://compsci.rocks/string-formatter-solution/
public static int leftoverSpaces( List<String> wordList, int formattedLen ) {
/* Implementation not shown */
return formattedLen - totalLetters( wordList ) - ( basicGapWidth( wordList, formattedLen ) * wordList.size() - 1 );
}
@CompSciRocks
CompSciRocks / Crossword.java
Last active November 28, 2018 17:42
Working solution to the 2016 AP Computer Science Crossword free response question :: https://compsci.rocks/crossword-solution/
public class Crossword {
private Square[][] puzzle;
private boolean toBeLabeled( int r, int c, boolean[][] blackSquares ) {
return (!(blackSquares[r][c]) &&
( r == 0 || c == 0 || blackSquares[r-1][c] || blackSquares[r][c-1] );
}
public Crossword(boolean[][] blackSquares) {
String[] wordArray = {"wheels", "on", "the", "bus"};
RandomStringChooser sChooser = new RandomStringChooser(wordArray);
for (int k = 0; k < 6; k++) {
System.out.print(sChooser.getNext() + " ");
}
// Possible output
// bus the wheels on NONE NONE
public class MultPractice implements StudyPractice {
private int numOne;
private int numTwo;
public MultPractice(int a, int b) {
numOne = a;
numTwo = b;
}