Skip to content

Instantly share code, notes, and snippets.

@CompSciRocks
CompSciRocks / colorImage.php
Last active Dec 5, 2018
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/
View colorImage.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 );
View HiddenWord.java
public class HiddenWord {
private String word;
public HiddenWord(String w) {
word = w;
}
public String getHint(String guess) {
String out = "";
@CompSciRocks
CompSciRocks / arraySum.java
Created Dec 2, 2018
Solutions to DiverseArray problem on 2015 AP CompSci exam - https://compsci.rocks/diversearray-solution/
View arraySum.java
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 Nov 29, 2018
How to wrap a catch for InvocationTargetException in a junit test - https://compsci.rocks/invocationtargetexception-fix/
View testMethod.java
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 Nov 28, 2018
Solution for 2016 AP Computer Science Free Response LogMessage - https://compsci.rocks/logmessage-solution/
View constructor.java
public LogMessage( String message ) {
int pos = message.indexOf(":");
machineId = message.substring(0, pos);
description = message.substring(pos + 1);
}
@CompSciRocks
CompSciRocks / LeftOverSpaces.java
Last active Nov 28, 2018
Working solution to the 2016 AP Computer Science Free Response Question String Formatter - https://compsci.rocks/string-formatter-solution/
View LeftOverSpaces.java
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 Nov 28, 2018
Working solution to the 2016 AP Computer Science Crossword free response question :: https://compsci.rocks/crossword-solution/
View Crossword.java
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) {
View ClientCode.java
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
View MultPractice.java
public class MultPractice implements StudyPractice {
private int numOne;
private int numTwo;
public MultPractice(int a, int b) {
numOne = a;
numTwo = b;
}
View Digits-doWhile.java
public Digits( int num ) {
digitList = new ArrayList<>();
int n = num;
do {
digitList.add(0, n % 10);
n /= 10;
} while (n > 0);
}