init
This commit is contained in:
BIN
in_class/cse123-inclass11/.DS_Store
vendored
Normal file
BIN
in_class/cse123-inclass11/.DS_Store
vendored
Normal file
Binary file not shown.
79
in_class/cse123-inclass11/ScrabbleHelper/Scrabble.java
Normal file
79
in_class/cse123-inclass11/ScrabbleHelper/Scrabble.java
Normal file
@@ -0,0 +1,79 @@
|
||||
import java.util.*;
|
||||
import java.io.*;
|
||||
|
||||
public class Scrabble {
|
||||
public static void main(String[] args) {
|
||||
Set<String> dictionary = loadDictionary();
|
||||
List<Character> letters = readLetters();
|
||||
|
||||
System.out.println("Searching " + letters);
|
||||
findWords(letters, dictionary);
|
||||
}
|
||||
|
||||
public static void findWords(List<Character> letters, Set<String> dict) {
|
||||
findWords(letters, dict, "");
|
||||
}
|
||||
|
||||
private static void findWords(List<Character> letters, Set<String> dict, String soFar) {
|
||||
if (dict.contains(soFar)) {
|
||||
System.out.println(soFar);
|
||||
}
|
||||
if (!letters.isEmpty()) {
|
||||
for (int i = 0; i < letters.size(); i++) {
|
||||
char letter = letters.remove(i);
|
||||
findWords(letters, dict, soFar+letter);
|
||||
letters.add(i, letter);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// HELPER METHODS
|
||||
|
||||
// does not check if word is legal
|
||||
private static int scoreWord(String word) {
|
||||
// A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z
|
||||
int[] values = {1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10};
|
||||
|
||||
int score = 0;
|
||||
word = word.toUpperCase();
|
||||
for (int i = 0; i < word.length(); i++) {
|
||||
char ch = word.charAt(i);
|
||||
score += values[ch - 'A'];
|
||||
}
|
||||
|
||||
return score;
|
||||
}
|
||||
|
||||
private static List<Character> readLetters() {
|
||||
List<Character> result = new ArrayList<>();
|
||||
|
||||
Scanner input = new Scanner(System.in);
|
||||
System.out.print("Enter letters separated by whitespace: ");
|
||||
Scanner letters = new Scanner(input.nextLine());
|
||||
while (letters.hasNext()) {
|
||||
String letter = letters.next();
|
||||
if (letter.length() > 1) {
|
||||
System.out.println(" Skipping " + letter + " - not one letter");
|
||||
} else {
|
||||
result.add(letter.toUpperCase().charAt(0));
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static Set<String> loadDictionary() {
|
||||
try {
|
||||
Scanner input = new Scanner(new File("dictionary.txt"));
|
||||
Set<String> dict = new HashSet<>();
|
||||
|
||||
while (input.hasNextLine()) {
|
||||
dict.add(input.nextLine());
|
||||
}
|
||||
|
||||
return dict;
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
178691
in_class/cse123-inclass11/ScrabbleHelper/dictionary.txt
Normal file
178691
in_class/cse123-inclass11/ScrabbleHelper/dictionary.txt
Normal file
File diff suppressed because it is too large
Load Diff
67
in_class/cse123-inclass11/ScrabbleHelperv2/Scrabble.java
Normal file
67
in_class/cse123-inclass11/ScrabbleHelperv2/Scrabble.java
Normal file
@@ -0,0 +1,67 @@
|
||||
import java.util.*;
|
||||
import java.io.*;
|
||||
|
||||
public class Scrabble {
|
||||
public static void main(String[] args) {
|
||||
Set<String> dictionary = loadDictionary();
|
||||
List<Character> letters = readLetters();
|
||||
|
||||
String best = findBestWord(letters, dictionary);
|
||||
System.out.println(best + " (" + scoreWord(best) + ")");
|
||||
}
|
||||
|
||||
public static String findBestWord(List<Character> letters, Set<String> dict) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
// HELPER METHODS
|
||||
|
||||
// does not check if word is legal
|
||||
private static int scoreWord(String word) {
|
||||
// A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z
|
||||
int[] values = {1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10};
|
||||
|
||||
int score = 0;
|
||||
word = word.toUpperCase();
|
||||
for (int i = 0; i < word.length(); i++) {
|
||||
char ch = word.charAt(i);
|
||||
score += values[ch - 'A'];
|
||||
}
|
||||
|
||||
return score;
|
||||
}
|
||||
|
||||
private static List<Character> readLetters() {
|
||||
List<Character> result = new ArrayList<>();
|
||||
|
||||
Scanner input = new Scanner(System.in);
|
||||
System.out.print("Enter letters separated by whitespace: ");
|
||||
Scanner letters = new Scanner(input.nextLine());
|
||||
while (letters.hasNext()) {
|
||||
String letter = letters.next();
|
||||
if (letter.length() > 1) {
|
||||
System.out.println(" Skipping " + letter + " - not one letter");
|
||||
} else {
|
||||
result.add(letter.toUpperCase().charAt(0));
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static Set<String> loadDictionary() {
|
||||
try {
|
||||
Scanner input = new Scanner(new File("dictionary.txt"));
|
||||
Set<String> dict = new HashSet<>();
|
||||
|
||||
while (input.hasNextLine()) {
|
||||
dict.add(input.nextLine());
|
||||
}
|
||||
|
||||
return dict;
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
178691
in_class/cse123-inclass11/ScrabbleHelperv2/dictionary.txt
Normal file
178691
in_class/cse123-inclass11/ScrabbleHelperv2/dictionary.txt
Normal file
File diff suppressed because it is too large
Load Diff
25
in_class/cse123-inclass9/IsPalindromeArray.java
Normal file
25
in_class/cse123-inclass9/IsPalindromeArray.java
Normal file
@@ -0,0 +1,25 @@
|
||||
public class IsPalindromeArray {
|
||||
public static void main(String[] args) {
|
||||
System.out.println(isPalindrome(new int[] { 1, 2, 3, 4, 5, 4, 3, 2, 1 }));
|
||||
System.out.println(isPalindrome(new int[] { 10, 20, 20, 10 }));
|
||||
System.out.println(isPalindrome(new int[] { 1 }));
|
||||
System.out.println(isPalindrome(new int[] {}));
|
||||
System.out.println(isPalindrome(new int[] { 1, 2, 3, 4, 5 }));
|
||||
System.out.println(isPalindrome(new int[] { 1, 2, 5, 3 }));
|
||||
System.out.println(isPalindrome(new int[] { 10, 20, 30, 10 }));
|
||||
}
|
||||
|
||||
public static boolean isPalindrome(int[] array) {
|
||||
return isPalindrome(array, 0, array.length - 1);
|
||||
}
|
||||
|
||||
private static boolean isPalindrome(int[] array, int lower, int upper) {
|
||||
if (upper - lower < 1) {
|
||||
return true;
|
||||
} else if (array[lower] != array[upper]) {
|
||||
return false;
|
||||
} else {
|
||||
return isPalindrome(array, lower+1,upper-1);
|
||||
}
|
||||
}
|
||||
}
|
||||
19
in_class/cse123-inclass9/IsPalindromeString.java
Normal file
19
in_class/cse123-inclass9/IsPalindromeString.java
Normal file
@@ -0,0 +1,19 @@
|
||||
public class IsPalindromeString {
|
||||
public static void main(String[] args) {
|
||||
System.out.println(isPalindrome("racecar"));
|
||||
System.out.println(isPalindrome("tacocat"));
|
||||
System.out.println(isPalindrome("ABBA"));
|
||||
System.out.println(isPalindrome("x"));
|
||||
System.out.println(isPalindrome(""));
|
||||
System.out.println(isPalindrome("banana"));
|
||||
System.out.println(isPalindrome("abcbba"));
|
||||
System.out.println(isPalindrome("bramb"));
|
||||
}
|
||||
|
||||
public static boolean isPalindrome(String s) {
|
||||
if (s.length() <= 1 || s.equals(ReverseString.reverse(s))) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
38
in_class/cse123-inclass9/RecursiveMystery.java
Normal file
38
in_class/cse123-inclass9/RecursiveMystery.java
Normal file
@@ -0,0 +1,38 @@
|
||||
public class RecursiveMystery {
|
||||
public static void main(String[] args) {
|
||||
// mystery1(3);
|
||||
// mystery2(4);
|
||||
mystery3("taco");
|
||||
}
|
||||
|
||||
public static void mystery1(int n) {
|
||||
if (n <= 1) {
|
||||
System.out.print(n);
|
||||
} else {
|
||||
mystery1(n / 2);
|
||||
System.out.print(", " + n);
|
||||
}
|
||||
}
|
||||
|
||||
public static void mystery2(int n) {
|
||||
if (n <= 0) {
|
||||
System.out.print("*");
|
||||
} else if (n % 2 == 0) {
|
||||
System.out.print("(");
|
||||
mystery2(n - 1);
|
||||
System.out.print(")");
|
||||
} else {
|
||||
System.out.print("[");
|
||||
mystery2(n - 1);
|
||||
System.out.print("]");
|
||||
}
|
||||
}
|
||||
|
||||
public static void mystery3(String str) {
|
||||
if (!str.isEmpty()) {
|
||||
System.out.print(str.charAt(0));
|
||||
mystery3(str.substring(1));
|
||||
System.out.print(str.charAt(0));
|
||||
}
|
||||
}
|
||||
}
|
||||
22
in_class/cse123-inclass9/ReverseString.java
Normal file
22
in_class/cse123-inclass9/ReverseString.java
Normal file
@@ -0,0 +1,22 @@
|
||||
public class ReverseString {
|
||||
public static void main(String[] args){
|
||||
System.out.println(reverse("Hello"));
|
||||
System.out.println(reverse("nathan"));
|
||||
System.out.println(reverse("racecar"));
|
||||
System.out.println(reverse("taco"));
|
||||
System.out.println(reverse("ABBA"));
|
||||
System.out.println(reverse(""));
|
||||
System.out.println(reverse("a"));
|
||||
}
|
||||
|
||||
public static String reverse(String str){
|
||||
if (str.isEmpty()) {
|
||||
return "";
|
||||
} else if (str.length() == 1) {
|
||||
return str;
|
||||
}
|
||||
|
||||
String backwards = reverse(str.substring(1)) + str.charAt(0);
|
||||
return backwards;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user