Skip to content

Instantly share code, notes, and snippets.

@seanboe
Created January 11, 2023 18:35
Show Gist options
  • Save seanboe/3702074e2d62bb95101b06eacb4b8ec7 to your computer and use it in GitHub Desktop.
Save seanboe/3702074e2d62bb95101b06eacb4b8ec7 to your computer and use it in GitHub Desktop.
One True
public class StudentClass{
public static void oneTrue(boolean[] myBools){
//enter line of code to set last position/element to true//
myBools[myBools.length - 1] = true;
}
}
Duplicate
public class StudentClass{
public static void duplicate(int[] arr){
//given array arr, duplicate the first element to the last position
arr[arr.length - 1] = arr[0];
}
}
Set True
public class StudentClass{
public static void setTrue(boolean[] myBool){
//set all elements in myBool to true
// do not call any return lines.
for (int x = 0; x < myBool.length; x++) {
myBool[x] = true;
}
// do not return data
}
}
All ones
public class StudentClass{
public static void allOnes(int[] nums){
/* If you need to visit every index (positioon in array) then a loop
is generally required. Write a loop that generates values starting
at 0 and ending at length-1 */
for (int x = 0; x < nums.length; x++) {
nums[x] = 1;
}
}
}
Positions
public class StudentClass{
public static Tile[] makeTiles(int n){
//create the container to hold n number of Tiles
Tile[] output = new Tile[n];
for (int x = 0; x < output.length; x++) {
output[x] = new Tile(x);
}
return output;
}
}
Tiles
public class StudentClass{
public static Tile[] makeTiles(){
// 1 create the Tiles container
// 2 (most common mistake) - forgetting to create the objects!
// Don't hardcode.
// 3 return the data if necessary
// what part of the method header
Tile[] output = new Tile[10];
for (int x = 0; x < output.length; x++) {
output[x] = new Tile();
}
return output;
}
}
Make Chicken 2
public class StudentClass{
public static Chicken[] makeChicken2(){
//add code below
Chicken[] output = new Chicken[10];
for (int x = 0; x < output.length; x++) {
output[x] = new Chicken(2);
}
return output;
}
}
Candy swap
public class StudentClass{
public static void candySwap(Candy[] can){
Candy first = can[0];
can[0] = can[can.length - 1];
can[can.length - 1] = first;
}
}
make Chicken
public class StudentClass{
public static Chicken[] makeChicken(){
//enter solution below
Chicken[] output = new Chicken[10];
for (int x = 0; x < output.length; x++) {
output[x] = new Chicken();
}
return output;
}
}
countdown
public class StudentClass{
public static void countDown(int[] nums){
for (int x = nums.length - 1; x >= 0; x--) {
nums[nums.length - 1 - x] = x;
}
}
}
combineA
public class StudentClass{
public static int[] combineA(int[] arrA, int[] arrB){
//create a new array with a length that is equivalent to arrA and arrB
//combined. You do not need to copy over the values for this problem.
return new int[arrA.length + arrB.length];
//return the array you created
}
}
slideleft
public class StudentClass{
public static void slideLeft(int[] data){
//enter solution below
for (int x = 0; x < 20; x++) {
for (int a = 1; a < data.length; a++) {
if (data[a - 1] == 0) {
data[a - 1] = data[a];
data[a] = 0;
}
}
}
}
}
combinetwo
public class StudentClass{
public static int[] combineA(int[] arrA, int[] arrB){
//create a new array with a length that is equivalent to arrA and arrB
//combined. Copy over the contents of arrA and arrB to this new array.
int[] output = new int[arrA.length + arrB.length];
for (int x = 0; x < output.length; x++) {
if (x >= arrA.length) {
output[x] = arrB[x - arrA.length];
}
else {
output[x] = arrA[x];
}
}
return output;
}
}
positive slopes
public class StudentClass{
public static int positiveSlopes(APLine[] lines){
//enter solution below
int counter = 0;
for (int x = 0; x < lines.length; x++) {
if (lines[x].getSlope() > 0) {
counter++;
}
}
return counter;
}
}
merge
public class StudentClass{
public static int[] merge(int[] a, int[] b){
//enter solution below
int[] output = new int[a.length + b.length];
for (int x = 0; x < output.length; x++) {
if (x >= a.length) {
output[x] = b[x - a.length];
}
else {
output[x] = a[x];
}
}
for (int x = 0; x < 100; x++) {
for (int j = 1; j < output.length; j++) {
if (output[j] < output[j - 1]) {
int first = output[j];
output[j] = output[j - 1];
output[j - 1] = first;
}
}
}
return output;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment