Skip to content

Instantly share code, notes, and snippets.

@bytecodeman
Last active October 12, 2018 21:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bytecodeman/3af5eda32adcab36622fc2ee2ddf1687 to your computer and use it in GitHub Desktop.
Save bytecodeman/3af5eda32adcab36622fc2ee2ddf1687 to your computer and use it in GitHub Desktop.
CSC-220 In class Stack Exercise
import stack.*;
public class Palindrome {
public static void main(String[] args) {
// should return true
System.out.println(checkForPalindrome("abccba"));
// should return true
System.out.println(checkForPalindrome("Was it a car or a cat I saw?"));
// should return true
System.out.println(checkForPalindrome("I did, did I?"));
// should return false
System.out.println(checkForPalindrome("hello"));
// should return true
System.out.println(checkForPalindrome("Don't nod"));
}
public static boolean checkForPalindrome(String str) {
str = str.toLowerCase();
Stack<Character> charStack = new Stack<Character>();
String strForward = "";
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch >= 'a' && ch <= 'z') {
strForward += ch;
charStack.push(ch);
}
}
String strBackward = "";
while (!charStack.isEmpty())
strBackward += charStack.pop();
return strForward.equals(strBackward);
}
}
import stack.*;
public class Palindrome2 {
public static void main(String[] args) {
// should return true
System.out.println(checkForPalindrome("abccba"));
// should return true
System.out.println(checkForPalindrome("Was it a car or a cat I saw?"));
// should return true
System.out.println(checkForPalindrome("I did, did I?"));
// should return false
System.out.println(checkForPalindrome("hello"));
// should return true
System.out.println(checkForPalindrome("Don't nod"));
}
public static boolean checkForPalindrome(String str) {
str = str.toLowerCase();
Stack<Character> charStack = new Stack<Character>();
String strForward = "";
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch >= 'a' && ch <= 'z') {
strForward += ch;
charStack.push(ch);
}
}
int i = 0;
while (!charStack.isEmpty()) {
char stackChar = charStack.pop();
if (strForward.charAt(i) != stackChar)
return false;
i++;
}
return true;
}
}
public class PalindromeStarterCode {
public static void main(String[] args) {
// should return true
System.out.println(checkForPalindrome("abccba"));
// should return true
System.out.println(checkForPalindrome("Was it a car or a cat I saw?"));
// should return true
System.out.println(checkForPalindrome("I did, did I?"));
// should return false
System.out.println(checkForPalindrome("hello"));
// should return true
System.out.println(checkForPalindrome("Don't nod"));
}
public static boolean checkForPalindrome(String string) {
return false;
}
}
// Generic Code Derived from Example StackOfIntegers2b.java
package stack;
import java.util.*;
public class Stack<T> {
private ArrayList<T> list;
public Stack() {
list = new ArrayList<T>(16);
}
public T push(T item) {
list.add(item);
return item;
}
public T pop() {
if (getSize() <= 0)
throw new RuntimeException("Can't pop empty stack.");
T item = list.get(getSize() - 1);
list.remove(getSize() - 1);
return item;
}
public T peek() {
if (getSize() <= 0)
throw new RuntimeException("Can't peek empty stack.");
return list.get(getSize() - 1);
}
public boolean isEmpty() {
return list.isEmpty();
}
public int getSize() {
return list.size();
}
public int search(int i) {
return list.lastIndexOf(i);
}
/** Override the toString in the Object class */
public String toString() {
return "stack: " + list.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment