Week 10: Search
-
String type: common operations.
-
Working with files: File I/O
-
Type conversions
-
Parsing
-
Searching: Linear search, Binary search.
2. String practice
// write a function which returns true if a string contains the
// letter 'a' and false otherwise
boolean containsLetterA(String input) {
boolean foundA = false;
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if (c == 'a') {
foundA = true;
}
}
return foundA;
}
void setup() {
String s1 = "Hello";
println(s1);
char[] data = {'h', 'e', 'l', 'l', 'o'};
String s2 = new String(data);
println(s2);
char first = s1.charAt(0);
println(first);
first = s2.charAt(0);
println(first);
String story = "A quick brown fox jumps over the lazy dog";
int idx = story.indexOf("fox");
println(story);
println("Found fox at: ", idx);
String substr = story.substring(idx, idx+3);
println(substr);
// Print out every letter in s1 on its own line
for (int i = 0; i < s1.length(); i++) {
char c = s1.charAt(i);
println(c);
}
String apples = "apples";
String house = "house";
println(apples, containsLetterA(apples));
println(house, containsLetterA(house));
}
3. Shopping List
A simple example showing how to load from files
void setup() {
String[] lines = loadStrings("shoppingList.txt");
for (int i = 0; i < lines.length; i++) {
String line = lines[i];
println(line);
}
}
4. Average grade
void setup() {
String[] lines = loadStrings("grades.txt");
float total = 0;
for (int i = 0; i < lines.length; i++) {
String line = lines[i];
int value = int(line);
println(line, value);
total += value;
}
total = total/lines.length;
println("The average is "+ total);
}
5. Load balances
Customer[] customers = null;
void setup() {
String[] lines = loadStrings("balances.csv");
customers = new Customer[lines.length];
for (int i = 0; i < lines.length; i++) {
String line = lines[i];
String[] tokens = line.split(",");
// we know there are two tokens per line
String name = tokens[0]; // first token is the name
float balance = float(tokens[1]); // second token is an integer balance
println(name, balance);
customers[i] = new Customer(name, balance);
}
}
And here is the Customer class
class Customer {
String mName;
float mBalance;
Customer(String name, float balance) {
mName = name;
mBalance = balance;
}
}
6. Linear search
int indexOf(int value, int[] values) {
for (int i = 0; i < values.length; i++) {
int testValue = values[i];
if (testValue == value) {
return i;
}
}
return -1;
}
void setup() {
int[] numbers1 = {-3, 8, 0, 11, 13};
int val = 21;
int idx = indexOf(val, numbers1);
println("Index of "+val+" is "+idx);
}
7. Binary search
int binarySearch(int value, int[] values) {
int low = 0;
int high = values.length-1;
while(low <= high) {
int mid = (low + high)/2;
int testValue = values[mid];
if (value < testValue) { // look left
high = mid - 1;
}
else if (value > testValue) { // look right
low = mid + 1;
}
else if (value == testValue) { // found it!
return mid;
}
}
return -1;
}
void setup() {
int[] numbers1 = {-3, 8, 0, 11, 13};
int val = -5;
int idx = binarySearch(val, numbers1);
println("Index of "+val+" is "+idx);
}
8. Comparing strings
void setup() {
String str1 = "apples";
String str2 = "bananas";
String str3 = "orange";
String str4 = "orange";
if (str1.compareTo(str2) < 0) { // str1 < str2
println(str1, "is less than (comes before)", str2);
}
if (str3.compareTo(str2) > 0) { // str3 > str2
println(str3, "is greater than (comes after)", str2);
}
if (str3.compareTo(str4) == 0) { // str3 == str4
println(str3, "is equal to (the same)", str4);
}
}