Skip to content

Instantly share code, notes, and snippets.

@ezragol
Last active January 18, 2023 16:07
Show Gist options
  • Save ezragol/78b774b9e95a14f5d139a73fb4104b62 to your computer and use it in GitHub Desktop.
Save ezragol/78b774b9e95a14f5d139a73fb4104b62 to your computer and use it in GitHub Desktop.
checkarray codecheck v1
/*
* CheckArray.java
*
* Implements a sorted method and several Boolean methods that determine
* whether the array fits a pattern.
*
* @author EZRA GOLDNER <24goldnere@brooklinek12.org>
*/
public class ArrayCheck
{
/////////////////////////////// FIELDS ///////////////////////////////
private int[] array;
//////////////////////////// CONSTRUCTORS ////////////////////////////
public ArrayCheck(int[] array) {
this.array = array;
}
////////////////////////////// METHODS ///////////////////////////////
/** Return a copy of this.array.
* @return a copy of array
*/
public int[] getArrayCopy() {
int[] result = new int[array.length];
for (int i = 0; i < array.length; i++)
result[i] = array[i];
return result;
}
/** Return copy of this.array sorted by increasing value.
* @return new array with elements of array sorted by increasing value
* @link https://www.toptal.com/developers/sorting-algorithms/selection-sort
*/
public int[] sorted() {
// Copy array to result so result can be returned sorted.
int[] result = getArrayCopy();
for (int i = 0; i < array.length - 1; i++) {
int next_index = i;
for (int j = i + 1; j < array.length; j++) {
if (result[j] < result[next_index])
next_index = j;
}
int old = result[next_index];
result[next_index] = result[i];
result[i] = old;
}
return result;
}
/** Return whether this.array is in increasing numerical order.
* @return true if this.array is in increasing numerical order, false otherwise
*/
public boolean isSorted() {
for (int i = 0; i < array.length - 1; i++) {
if (array[i] > array[i + 1])
return false;
}
return true;
}
/** Return whether array[i] == T(i) for all i on [0, array.length).
* @return true if array[i] == T(i) for all i on [0, array.length), false otherwise
*/
public boolean isTriangular() {
for (int i = 0; i < array.length; i++) {
int Ti = (int) ((Math.pow(i, 2) + i) / 2);
if (Ti != array[i])
return false;
}
return true;
}
/** Return whether array[i] == i! for all i on [0, array.length).
* @return true if array[i] == i! for all i on [0, array.length), false otherwise
*/
public boolean isFactorial() {
for (int i = 0; i < array.length; i++) {
int factorial = 1;
for (int j = i; j > 1; j--) {
factorial *= j;
}
if (factorial != array[i])
return false;
}
return true;
}
/** Return whether array is part of a Fibonacci sequence. That is:
* this.array[n] == this.array[n - 2] + this.array[n - 1] for all n.
* @return true if this.array is part of a Fibonacci sequence, false otherwise
* Precondition: this.array.length >= 3
*/
public boolean isFibonacci() {
assert this.array.length >= 3:
String.format("this.array.length (%s) < 3", this.array.length);
for (int i = 2; i < array.length; i++) {
if (array[i] != array[i - 2] + array[i - 1])
return false;
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment