Rename year directories to allow natural ordering

This commit is contained in:
2023-12-20 03:57:27 +00:00
parent 0ab1f5ad3a
commit 1f7d812b98
1895 changed files with 0 additions and 7188 deletions

View File

@ -0,0 +1,69 @@
import javax.swing.JOptionPane;
/**
* Array implementation of Queue ADT
*/
public class ArrayQueue implements Queue
{
protected Object Q[]; // array used to implement the queue
protected int rear = -1; // index for the rear of the queue
protected int capacity; // The actual capacity of the queue array
public static final int CAPACITY = 1000; // default array capacity
public ArrayQueue() {
// default constructor: creates queue with default capacity
this(CAPACITY);
}
public ArrayQueue(int cap) {
// this constructor allows you to specify capacity
capacity = (cap > 0) ? cap : CAPACITY;
Q = new Object[capacity];
}
public void enqueue(Object n)
{
if (isFull()) {
JOptionPane.showMessageDialog(null, "Cannot enqueue object; queue is full.");
return;
}
rear++;
Q[rear] = n;
}
public Object dequeue()
{
// Can't do anything if it's empty
if (isEmpty())
return null;
Object toReturn = Q[0];
// shuffle all other objects towards 0
int i = 1;
while (i <= rear) {
Q[i-1] = Q[i];
i++;
}
rear--;
return toReturn;
}
public boolean isEmpty() {
return (rear < 0);
}
public boolean isFull() {
return (rear == capacity-1);
}
public Object front()
{
if (isEmpty())
return null;
return Q[0];
}
}

View File

@ -0,0 +1,63 @@
/** Array implementation of Stack ADT */
import javax.swing.JOptionPane;
public class ArrayStack implements Stack
{
protected int capacity; // The actual capacity of the stack array
protected static final int CAPACITY = 1000; // default array capacity
protected Object S[]; // array used to implement the stack
protected int top = -1; // index for the top of the stack
public ArrayStack() {
// default constructor: creates stack with default capacity
this(CAPACITY);
}
public ArrayStack(int cap) {
// this constructor allows you to specify capacity of stack
capacity = (cap > 0) ? cap : CAPACITY;
S = new Object[capacity];
}
public void push(Object element) {
if (isFull()) {
JOptionPane.showMessageDialog(null, "ERROR: Stack is full.");
return;
}
top++;
S[top] = element;
}
public Object pop() {
Object element;
if (isEmpty()) {
JOptionPane.showMessageDialog(null, "ERROR: Stack is empty.");
return null;
}
element = S[top];
S[top] = null;
top--;
return element;
}
public Object top() {
if (isEmpty()) {
JOptionPane.showMessageDialog(null, "ERROR: Stack is empty.");
return null;
}
return S[top];
}
public boolean isEmpty() {
return (top < 0);
}
public boolean isFull() {
return (top == capacity-1);
}
public int size() {
return (top + 1);
}
}

View File

@ -0,0 +1,21 @@
// class to implement Method 2
public class IVersusNMinusI implements PalindromeChecker {
// method 2 - comparing each element at index i to the element at n - i where n is the last index
@Override
public boolean checkPalindrome(String str) {
// looping through the first half of the String
NewPalindrome.operations[1]++;
for (int i = 0; i < Math.floor(str.length() / 2); i++) {
NewPalindrome.operations[1] += 1 + 1 + 1 + 1; // 1 for the getting str.length(), 1 for Math,floor, 1 for checking condition, 1 for incrementing
// returning false if the digits don't match
NewPalindrome.operations[1] += 1 + 1 + 1 + 1; // 1 for str.charAt(i), 1 for ((str.lenght() -1) - 1), 1 for the other str.charAt(), 1 for checking the condition
if (str.charAt(i) != str.charAt((str.length()-1) - i)) {
return false;
}
}
// returning true as default
return true;
}
}

View File

@ -0,0 +1,93 @@
import java.io.*;
public class NewPalindrome {
public static long[] operations = new long[4]; // array to contain the global operations count for each method
public static int[] decCount = new int[4]; // array to hold the count of decimal palindromes found using each method
public static int[] binCount = new int[4]; // array to hold the count of binary palindromes found using each method
public static int[] bothCount = new int[4]; // array to hold the count of numbers that are palindromes in both decimal & binary found using each method
public static long[] startTime = new long[4]; // array to hold the start time of each method's test loop
public static long[] totalTime = new long[4]; // array to hold the total time of each method's test loop
// array to hold all the String versions of the numbers so that they don't have to be generated for each method
// 0th column will be decimal, 1st column will be binary
public static String[][] strings = new String[1_000_001][2];
// array of StringBuilder objects used to hold the csv data (size of problem, number of operations) for each method
public static StringBuilder[] data = new StringBuilder[4];
// array of the four classes that will be tested
public static PalindromeChecker[] palindromeCheckers = {new ReverseVSOriginal(), new IVersusNMinusI(), new StackVSQueue(), new RecursiveReverse()};
public static void main(String args[]) {
// initialising the data array to StringBuilder objects
for (int i = 0; i < 4; i++) {
data[i] = new StringBuilder("operations,size\n");
}
// filling up the strings array
for (int i = 0; i <= 1_000_000; i++) {
strings[i][0] = Integer.toString(i, 10); // converting i to a String base 10
strings[i][1] = binary2string(strings[i][0]); // converting the decimal String to a binary String
}
// looping through each PalindromeChecker object in the palindromeCheckers array
for (int j = 0; j < 4; j++) {
// getting start time
startTime[j] = System.currentTimeMillis(); operations[j]++;
// looping through the numbers 0 to 1,000,000 and checking if their binary & decimal representations are palindromic
operations[j]++;
for (int i = 0; i <= 1_000_000; i++) {
// incrementing the operations count by 2, 1 for the loop condition check and 1 for incrementing i
operations[j] += 2;
// converting the number to a decimal or binary String and checking if is a palindrome
boolean isDecPalindrome = palindromeCheckers[j].checkPalindrome(strings[i][0]); operations[j]++;
boolean isBinPalindrome = palindromeCheckers[j].checkPalindrome(strings[i][1]); operations[j]++;
// incrementing the appropriate counter if the number is a palindrome in that base
decCount[j] = isDecPalindrome ? decCount[j] + 1 : decCount[j]; operations[j] += 1 + 1; // incremnting by 2, 1 for assignment, 1 for condition check
binCount[j] = isBinPalindrome ? binCount[j] + 1 : binCount[j]; operations[j] += 1 + 1;
bothCount[j] = isDecPalindrome && isBinPalindrome ? bothCount[j] + 1 : bothCount[j]; operations[j] += 1 + 1 +1; // 2 condition checks and one assignment, so incrementing by 3
// appending to the data StringBuilder at intervals of 50,000
if (i % 50_000 == 0) {
data[j].append(operations[j] + "," + i + "\n");
}
}
// calculating total time taken for method 1 and printing out the results
totalTime[j] = System.currentTimeMillis() - startTime[j]; operations[j] += 1 + 1; // incrementing by 2, 1 for getting current time and subtracting start time, 1 for assignment
System.out.println("Number of decimal palindromes found using Method " + j + ": " + decCount[j]);
System.out.println("Number of binary palindromes found using Method " + j + ": " + binCount[j]);
System.out.println("Number of palindromes in both decimal & binary found using Method " + j + ": " + bothCount[j]);
System.out.println("Number of primitive operations taken in Method " + j + ": " + operations[j]);
System.out.println("Time taken for Method " + j + ": " + totalTime[j] + " milliseconds");
System.out.println();
// outputting the data to separate csv files
try {
String filename = "method" + j + ".csv";
File csv = new File(filename);
// creating file if it doesn't already exist
csv.createNewFile();
FileWriter writer = new FileWriter(filename);
writer.write(data[j].toString());
writer.close();
} catch (IOException e) {
System.out.println("IO Error occurred");
e.printStackTrace();
System.exit(1);
}
}
}
// utility method to convert a decimal String to its equivalent binary String
public static String binary2string(String decimalStr) {
return Integer.toString(Integer.parseInt(decimalStr), 2); // parsing the String to an int and then parsing that int to a binary String
}
}

View File

@ -0,0 +1,320 @@
import java.io.*;
public class Palindrome {
// global operations count for each method
public static long operations1 = 0;
public static long operations2 = 0;
public static long operations3 = 0;
public static long operations4 = 0;
// String objects used to hold the csv data (size of problem, number of operations) for each method
public static StringBuilder data1 = new StringBuilder("operations,size\n");
public static StringBuilder data2 = new StringBuilder("operations,size\n");
public static StringBuilder data3 = new StringBuilder("operations,size\n");
public static StringBuilder data4 = new StringBuilder("operations,size\n");
public static void main(String args[]) {
// generating all the String versions of the numbers so that they don't have to be generated for each method
// 0th column will be decimal, 1st column will be binary
String[][] strings = new String[1_000_001][2];
for (int i = 0; i <= 1_000_000; i++) {
strings[i][0] = Integer.toString(i, 10); // converting i to a String base 10
strings[i][1] = binary2string(strings[i][0]); // converting the decimal String to a binary String
}
// variables for method 1 - reversed order String vs original String
int decCount1 = 0; operations1++; // count of decimal palindromes for use by method 1
int binCount1 = 0; operations1++; // count of binary palindromes for use by method 1
int bothCount1 = 0; operations1++; // count of numbers that are palindromic in both binary & decimal for use by method 1
long startTime1 = System.currentTimeMillis(); operations1 += 1 + 1;
// testing method 1 - reversed order String vs original String
// incrementing the operations counter for the initialisation of i below
operations1++;
for (int i = 0; i <= 1_000_000; i++) {
// incrementing the operations count by 2, 1 for the loop condition check and 1 for incrementing i
operations1 += 2;
// converting the number to a decimal or binary String and checking if is a palindrome
boolean isDecPalindrome = reverseVSoriginal(strings[i][0]); operations1++;
boolean isBinPalindrome = reverseVSoriginal(strings[i][1]); operations1++;
// incrementing the appropriate counter if the number is a palindrome in that base
decCount1 = isDecPalindrome ? decCount1 + 1 : decCount1; operations1 += 1 + 1; // incremnting by 2, 1 for assignment, 1 for condition check
binCount1 = isBinPalindrome ? binCount1 + 1 : binCount1; operations1 += 1 + 1;
bothCount1 = isDecPalindrome && isBinPalindrome ? bothCount1 + 1 : bothCount1; operations1 += 1 + 1 +1; // 2 condition checks and one assignment, so incrementing by 3
// appending to the data StringBuilder at intervals of 50,000
if (i % 50_000 == 0) {
data1.append(operations1 + "," + i + "\n");
}
}
// calculating total time taken for method 1 and printing out the results
long totalTime1 = System.currentTimeMillis() - startTime1; operations1 += 1 + 1; // incrementing by 2, 1 for getting current time and subtracting start time, 1 for assignment
System.out.println("Number of decimal palindromes found using Method 1: " + decCount1);
System.out.println("Number of binary palindromes found using Method 1: " + binCount1);
System.out.println("Number of palindromes in both decimal & binary found using Method 1: " + bothCount1);
System.out.println("Number of primitive operations taken in Method 1: " + operations1);
System.out.println("Time taken for Method 1: " + totalTime1 + " milliseconds");
// variables for method 2 - comparing each element at index i to the element at n - i where n is the last index
int decCount2 = 0; operations2++; // count of decimal palindromes for use by method 2
int binCount2 = 0; operations2++; // count of binary palindromes for use by method 2
int bothCount2 = 0; operations2++; // count of numbers that are palindromic in both binary & decimal for use by method 2
long startTime2 = System.currentTimeMillis(); operations2 += 1 + 1;
// testingmethod 2 - comparing each element at index i to the element at n - i where n is the last index
operations2++;
for (int i = 0; i <= 1_000_000; i++) {
operations2 += 1 + 1;
// converting the number to a decimal or binary String and checking if is a palindrome
boolean isDecPalindrome = iVSiMinusn(strings[i][0]); operations2++;
boolean isBinPalindrome = iVSiMinusn(strings[i][1]); operations2++;
// incrementing the appropriate counter if the number is a palindrome in that base
decCount2 = isDecPalindrome ? decCount2 + 1 : decCount2; operations2 += 1 + 1;
binCount2 = isBinPalindrome ? binCount2 + 1 : binCount2; operations2 += 1 + 1;
bothCount2 = isDecPalindrome && isBinPalindrome ? bothCount2 + 1 : bothCount2; operations2 += 1 + 1 +1;
// appending to the data StringBuilder at intervals of 50,000
if (i % 50_000 == 0) {
data2.append(operations2 + "," + i + "\n");
}
}
// calculating total time taken for method 2 and printing out the results
long totalTime2 = System.currentTimeMillis() - startTime2; operations2 += 1 + 1;
System.out.println();
System.out.println("Number of decimal palindromes found using Method 2: " + decCount2);
System.out.println("Number of binary palindromes found using Method 2: " + binCount2);
System.out.println("Number of palindromes in both decimal & binary found using Method 2: " + bothCount2);
System.out.println("Number of primitive operations taken in Method 2: " + operations2);
System.out.println("Time taken for Method 2: " + totalTime2 + " milliseconds");
// variables for method 3 - comparing each element at index i to the element at n - i where n is the last index
int decCount3 = 0; operations3++; // count of decimal palindromes for use by method 3
int binCount3 = 0; operations3++; // count of binary palindromes for use by method 3
int bothCount3 = 0; operations3++; // count of numbers that are palindromic in both binary & decimal for use by method 3
long startTime3 = System.currentTimeMillis(); operations3 += 1 + 1;
// testingmethod 3 - comparing each element at index i to the element at n - i where n is the last index
operations3++;
for (int i = 0; i <= 1_000_000; i++) {
operations3 += 1 + 1;
// converting the number to a decimal or binary String and checking if is a palindrome
boolean isDecPalindrome = stackVsqueue(strings[i][0]); operations3++;
boolean isBinPalindrome = stackVsqueue(strings[i][1]); operations3++;
// incrementing the appropriate counter if the number is a palindrome in that base
decCount3 = isDecPalindrome ? decCount3 + 1 : decCount3; operations3 += 1 + 1;
binCount3 = isBinPalindrome ? binCount3 + 1 : binCount3; operations3 += 1 + 1;
bothCount3 = isDecPalindrome && isBinPalindrome ? bothCount3 + 1 : bothCount3; operations3 += 1 + 1 + 1;
// appending to the data StringBuilder at intervals of 50,000
if (i % 50_000 == 0) {
data3.append(operations3 + "," + i + "\n");
}
}
// calculating total time taken for method 3 and printing out the results
long totalTime3 = System.currentTimeMillis() - startTime3; operations3 += 1 + 1;
System.out.println();
System.out.println("Number of decimal palindromes found using Method 3: " + decCount3);
System.out.println("Number of binary palindromes found using Method 3: " + binCount3);
System.out.println("Number of palindromes in both decimal & binary found using Method 3: " + bothCount3);
System.out.println("Number of primitive operations taken in Method 3: " + operations3);
System.out.println("Time taken for Method 3: " + totalTime3 + " milliseconds");
// variables for method 4 - comparing each element at index i to the element at n - i where n is the last index
int decCount4 = 0; operations4++; // count of decimal palindromes for use by method 4
int binCount4 = 0; operations4++; // count of binary palindromes for use by method 4
int bothCount4 = 0; operations4++; // count of numbers that are palindromic in both binary & decimal for use by method 4
long startTime4 = System.currentTimeMillis(); operations4 += 1 + 1;
// testingmethod 4 - comparing each element at index i to the element at n - i where n is the last index
operations4++;
for (int i = 0; i <= 1_000_000; i++) {
operations4 += 2;
// converting the number to a decimal or binary String and checking if is a palindrome
boolean isDecPalindrome = recursiveReverseVSoriginal(strings[i][0]); operations4++;
boolean isBinPalindrome = recursiveReverseVSoriginal(strings[i][1]); operations4++;
// incrementing the appropriate counter if the number is a palindrome in that base
decCount4 = isDecPalindrome ? decCount4 + 1 : decCount4; operations4 += 1 + 1;
binCount4 = isBinPalindrome ? binCount4 + 1 : binCount4; operations4 += 1 + 1;
bothCount4 = isDecPalindrome && isBinPalindrome ? bothCount4 + 1 : bothCount4; operations4 += 1 + 1 + 1;
// appending to the data StringBuilder at intervals of 50,000
if (i % 50_000 == 0) {
data4.append(operations4 + "," + i + "\n");
}
}
// calculating total time taken for method 4 and printing out the results
long totalTime4 = System.currentTimeMillis() - startTime4; operations4 += 1 + 1;
System.out.println();
System.out.println("Number of decimal palindromes found using Method 4: " + decCount4);
System.out.println("Number of binary palindromes found using Method 4: " + binCount4);
System.out.println("Number of palindromes in both decimal & binary found using Method 4: " + bothCount4);
System.out.println("Number of primitive operations taken in Method 4: " + operations4);
System.out.println("Time taken for Method 4: " + totalTime4 + " milliseconds");
// outputting the data to separate csv files
try {
File csv1 = new File("method1.csv");
File csv2 = new File("method2.csv");
File csv3 = new File("method3.csv");
File csv4 = new File("method4.csv");
// creating files if they don't already exist
csv1.createNewFile();
csv2.createNewFile();
csv3.createNewFile();
csv4.createNewFile();
FileWriter writer1 = new FileWriter("method1.csv");
writer1.write(data1.toString());
writer1.close();
FileWriter writer2 = new FileWriter("method2.csv");
writer2.write(data2.toString());
writer2.close();
FileWriter writer3 = new FileWriter("method3.csv");
writer3.write(data3.toString());
writer3.close();
FileWriter writer4 = new FileWriter("method4.csv");
writer4.write(data4.toString());
writer4.close();
} catch (IOException e) {
System.out.println("IO Error occurred");
e.printStackTrace();
System.exit(1);
}
}
// method 1 - reversed order String vs original String
public static boolean reverseVSoriginal(String str) {
String reversedStr = ""; operations1++;
// looping through each character in the String, backwards
// incrementing operations counter by 2, 1 for initialisating i, 1 for getting str.length()
operations1 += 1 + 1;
for (int i = str.length(); i > 0; i--) {
operations1 += 1 + 1; // for loop condition check & incrementing i
reversedStr += str.charAt(i-1); operations1 += 1 + 1;
}
// returning true if the Strings are equal, false if not
operations1 += str.length(); // the equals method must loop through each character of the String to check that they are equal so it is O(n)
return str.equals(reversedStr);
}
// method 2 - comparing each element at index i to the element at n - i where n is the last index
public static boolean iVSiMinusn(String str) {
// looping through the first half of the String
operations2++;
for (int i = 0; i < Math.floor(str.length() / 2); i++) {
operations2 += 1 + 1 + 1 + 1; // 1 for the getting str.length(), 1 for Math,floor, 1 for checking condition, 1 for incrementing
// returning false if the digits don't match
operations2 += 1 + 1 + 1 + 1; // 1 for str.charAt(i), 1 for ((str.lenght() -1) - 1), 1 for the other str.charAt(), 1 for checking the condition
if (str.charAt(i) != str.charAt((str.length()-1) - i)) {
return false;
}
}
// returning true as default
return true;
}
// method 3 - using a stack and a queue to do, essentially, what method 2 does
public static boolean stackVsqueue(String str) {
ArrayStack stack = new ArrayStack(); operations3 += 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1;
ArrayQueue queue = new ArrayQueue(); operations3 += 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1;
// looping through each character in the String and adding the character to the stack & queue
operations3++;
for (int i = 0; i < str.length(); i++) {
operations3 += 1 + 1 + 1;
stack.push(str.charAt(i)); operations3 += 1 + 1 + 1 + 1;
queue.enqueue(str.charAt(i)); operations3 += 1 + 1 + 1 + 1;
}
// looping through each character on the stack & queue and comparing them, returning false if they're different
operations3++;
for (int i = 0; i < str.length(); i++) {
operations3 += 1 + 1 + 1;
operations3 += 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1;
if (!stack.pop().equals(queue.front())) {
return false;
}
// the complexity of ArrayQueue.dequeue() is 3n+2, where n is the number of items in the queue when dequeue() is called.
// we need to determine the number of items in the queue so that we can determine the number of primitive operations performed when queue.dequeue() is called.
// to do this, we'll loop through the queue, dequeuing each object and enqueueing it in another ArrayQueue. once complete, we'll reassign the variable queue to point to the new ArrayQueue containing all the objects
ArrayQueue newQueue = new ArrayQueue(); // not counting the operations for this as it's not part of the algorithm, it's part of the operations counting
int n = 0; // n is the number of items in the ArrayQueue when dequeue() is called
while (!queue.isEmpty()) {
newQueue.enqueue(queue.dequeue());
n++;
}
queue = newQueue; // setting queue to point to the newQueue, which is just the state that queue would have been in if we didn't do this to calculate the primitive operations
newQueue = null; // don't need the newQueue object reference anymore
operations3 += 3*n + 2; // complexity of dequeue is 3n+2
queue.dequeue();
}
return true;
}
// method 4 - comparing the String reversed using recursion to the original String (essentially method 1 but with recursion)
public static boolean recursiveReverseVSoriginal(String str) {
// returning true if the original String is equal to the reversed String, false if not
operations4++;
return str.equals(reverse(str));
}
// method to reverse the characters in a String using recursion
public static String reverse(String str) {
// base case - returning an empty String if there is no character left in the String
operations4++;
if (str.length() == 0) {
return "";
}
else {
char firstChar = str.charAt(0); operations4 += 1 + 1;
String remainder = str.substring(1); operations4 += 1 + 1; // selecting the rest of the String, excluding the 0th character
// recursing with what's left of the String
String reversedRemainder = reverse(remainder); operations4++;
// returning the reversed rest of String with the first character of the String appended
return reversedRemainder + firstChar;
}
}
// utility method to convert a decimal String to its equivalent binary String
public static String binary2string(String decimalStr) {
return Integer.toString(Integer.parseInt(decimalStr), 2); // parsing the String to an int and then parsing that int to a binary String
}
}

View File

@ -0,0 +1,3 @@
public interface PalindromeChecker {
public boolean checkPalindrome(String str);
}

View File

@ -0,0 +1,14 @@
/*
* Abstract Queue interface
*/
public interface Queue {
// most important methods
public void enqueue(Object n); // add an object at the rear of the queue
public Object dequeue(); // remove an object from the front of the queue
// others
public boolean isEmpty(); // true if queue is empty
public boolean isFull(); // true if queue is full (if it has limited storage)
public Object front(); // examine front object on queue without removing it
}

View File

@ -0,0 +1,29 @@
// class to implement method 4
public class RecursiveReverse implements PalindromeChecker {
// comparing the String reversed using recursion to the original String (essentially method 1 but with recursion)
@Override
public boolean checkPalindrome(String str) {
// returning true if the original String is equal to the reversed String, false if not
NewPalindrome.operations[3]++;
return str.equals(reverse(str));
}
// method to reverse the characters in a String using recursion
public static String reverse(String str) {
// base case - returning an empty String if there is no character left in the String
NewPalindrome.operations[3]++;
if (str.length() == 0) {
return "";
}
else {
char firstChar = str.charAt(0); NewPalindrome.operations[3] += 1 + 1;
String remainder = str.substring(1); NewPalindrome.operations[3] += 1 + 1; // selecting the rest of the String, excluding the 0th character
// recursing with what's left of the String
String reversedRemainder = reverse(remainder); NewPalindrome.operations[3]++;
// returning the reversed rest of String with the first character of the String appended
return reversedRemainder + firstChar;
}
}
}

View File

@ -0,0 +1,21 @@
// class to implement Method 3
public class ReverseVSOriginal implements PalindromeChecker {
// method 1 - reversed order String vs original String
@Override
public boolean checkPalindrome(String str) {
String reversedStr = ""; NewPalindrome.operations[0]++;
// looping through each character in the String, backwards
// incrementing operations counter by 2, 1 for initialisating i, 1 for getting str.length()
NewPalindrome.operations[0] += 1 + 1;
for (int i = str.length(); i > 0; i--) {
NewPalindrome.operations[0] += 1 + 1; // for loop condition check & incrementing i
reversedStr += str.charAt(i-1); NewPalindrome.operations[0] += 1 + 1;
}
// returning true if the Strings are equal, false if not
NewPalindrome.operations[0] += str.length(); // the equals method must loop through each character of the String to check that they are equal so it is O(n)
return str.equals(reversedStr);
}
}

View File

@ -0,0 +1,13 @@
/** Abstract Stack interface */
public interface Stack
{
// most important methods
public void push(Object n); // push an object onto top of the stack
public Object pop(); // pop an object from top of the stack
// others
public Object top(); // examine top object on stack without removing it
public boolean isEmpty(); // true if stack is empty
public boolean isFull(); // true if stack is full (if it has limited storage)
}

View File

@ -0,0 +1,48 @@
// class to implement method 3
public class StackVSQueue implements PalindromeChecker {
// method 3 - using a stack and a queue to do, essentially, what method 2 does (compare the first index to the last index, etc.)
@Override
public boolean checkPalindrome(String str) {
ArrayStack stack = new ArrayStack(); NewPalindrome.operations[2] += 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1;
ArrayQueue queue = new ArrayQueue(); NewPalindrome.operations[2] += 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1;
// looping through each character in the String and adding the character to the stack & queue
NewPalindrome.operations[2]++;
for (int i = 0; i < str.length(); i++) {
NewPalindrome.operations[2] += 1 + 1 + 1;
stack.push(str.charAt(i)); NewPalindrome.operations[2] += 1 + 1 + 1 + 1;
queue.enqueue(str.charAt(i)); NewPalindrome.operations[2] += 1 + 1 + 1 + 1;
}
// looping through each character on the stack & queue and comparing them, returning false if they're different
NewPalindrome.operations[2]++;
for (int i = 0; i < str.length(); i++) {
NewPalindrome.operations[2] += 1 + 1 + 1;
NewPalindrome.operations[2] += 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1;
if (!stack.pop().equals(queue.front())) {
return false;
}
// the complexity of ArrayQueue.dequeue() is 3n+2, where n is the number of items in the queue when dequeue() is called.
// we need to determine the number of items in the queue so that we can determine the number of primitive operations performed when queue.dequeue() is called.
// to do this, we'll loop through the queue, dequeuing each object and enqueueing it in another ArrayQueue. once complete, we'll reassign the variable queue to point to the new ArrayQueue containing all the objects
ArrayQueue newQueue = new ArrayQueue(); // not counting the operations for this as it's not part of the algorithm, it's part of the operations counting
int n = 0; // n is the number of items in the ArrayQueue when dequeue() is called
while (!queue.isEmpty()) {
newQueue.enqueue(queue.dequeue());
n++;
}
queue = newQueue; // setting queue to point to the newQueue, which is just the state that queue would have been in if we didn't do this to calculate the primitive operations
newQueue = null; // don't need the newQueue object reference anymore
NewPalindrome.operations[2] += 3*n + 2; // complexity of dequeue is 3n+2
queue.dequeue();
}
return true;
}
}

View File

@ -0,0 +1,14 @@
public class Test {
public static void main(String[] args) {
String str = "1234567890";
ArrayQueue queue = new ArrayQueue();
for (int i = 0; i < str.length(); i++) {
queue.enqueue(str.charAt(i));
}
for (int i = 0; i < str.length(); i++) {
queue.dequeue();
}
}
}

View File

@ -0,0 +1,22 @@
operations,size
29,0
5716904,50000
11989234,100000
18683879,150000
25533879,200000
32383879,250000
39423164,300000
46523164,350000
53623164,400000
60723164,450000
67823164,500000
75051729,550000
82401729,600000
89751729,650000
97101729,700000
104451729,750000
111801729,800000
119151729,850000
126501729,900000
133851729,950000
141201734,1000000
1 operations size
2 29 0
3 5716904 50000
4 11989234 100000
5 18683879 150000
6 25533879 200000
7 32383879 250000
8 39423164 300000
9 46523164 350000
10 53623164 400000
11 60723164 450000
12 67823164 500000
13 75051729 550000
14 82401729 600000
15 89751729 650000
16 97101729 700000
17 104451729 750000
18 111801729 800000
19 119151729 850000
20 126501729 900000
21 133851729 950000
22 141201734 1000000

View File

@ -0,0 +1,22 @@
operations,size
15,0
1881959,50000
3768823,100000
5660295,150000
7552719,200000
9445167,250000
11337607,300000
13230031,350000
15122471,400000
17014895,450000
18907343,500000
20800207,550000
22693407,600000
24586615,650000
26479855,700000
28373063,750000
30266287,800000
32159487,850000
34052711,900000
35945943,950000
37839175,1000000
1 operations size
2 15 0
3 1881959 50000
4 3768823 100000
5 5660295 150000
6 7552719 200000
7 9445167 250000
8 11337607 300000
9 13230031 350000
10 15122471 400000
11 17014895 450000
12 18907343 500000
13 20800207 550000
14 22693407 600000
15 24586615 650000
16 26479855 700000
17 28373063 750000
18 30266287 800000
19 32159487 850000
20 34052711 900000
21 35945943 950000
22 37839175 1000000

View File

@ -0,0 +1,22 @@
operations,size
105,0
17250278,50000
36003532,100000
55780375,150000
75979194,200000
96178166,250000
116909827,300000
137812321,350000
158715267,400000
179617761,450000
200521097,500000
221777541,550000
243367974,600000
264958463,650000
286548836,700000
308139668,750000
329729920,800000
351320353,850000
372910963,900000
394501277,950000
416092270,1000000
1 operations size
2 105 0
3 17250278 50000
4 36003532 100000
5 55780375 150000
6 75979194 200000
7 96178166 250000
8 116909827 300000
9 137812321 350000
10 158715267 400000
11 179617761 450000
12 200521097 500000
13 221777541 550000
14 243367974 600000
15 264958463 650000
16 286548836 700000
17 308139668 750000
18 329729920 800000
19 351320353 850000
20 372910963 900000
21 394501277 950000
22 416092270 1000000

View File

@ -0,0 +1,22 @@
operations,size
29,0
6590279,50000
13847075,100000
21610649,150000
29560649,200000
37510649,250000
45687791,300000
53937791,350000
62187791,400000
70437791,450000
78687791,500000
87092069,550000
95642069,600000
104192069,650000
112742069,700000
121292069,750000
129842069,800000
138392069,850000
146942069,900000
155492069,950000
164042075,1000000
1 operations size
2 29 0
3 6590279 50000
4 13847075 100000
5 21610649 150000
6 29560649 200000
7 37510649 250000
8 45687791 300000
9 53937791 350000
10 62187791 400000
11 70437791 450000
12 78687791 500000
13 87092069 550000
14 95642069 600000
15 104192069 650000
16 112742069 700000
17 121292069 750000
18 129842069 800000
19 138392069 850000
20 146942069 900000
21 155492069 950000
22 164042075 1000000