part 1 search
searchA search problem consists of:
▪ A state space
▪ A successor function(with actions, costs)
▪ A start state and a goal test
A solution is a sequence of actions (a plan) whichtransforms the start state to a goal state
State Space Graphs vs. Search TreesState space graph: A mathematicalrepresentation of a search problem
▪ Nodes are (abstracted) world configurations
▪ Arcs represent successors (action results)
▪ The goal test is a set of goal nodes (maybe only one)
▪ A search tree:
Possible fut ...
CS 61B Part 3 Inheritance, Implements, Iterators
prev:[[61B_Part_2_LinkList]]next:[[part4-Iterators]]Hug61B chapter4
Part 3 Inheritance, Implements, IteratorsoverloadSuppose we want to write a class WordUtilsthat includes functions we can run on lists of words, including a method that calculates the longest string in an SLList.
1234567891011public static String longest(SLList<String> list) { int maxDex = 0; for (int i = 0; i < list.size(); i += 1) { String longestString = list.get(maxDex); String thisStri ...
CS 61B Part 2 Linked List
prev: [[61B_Part_1_Java]]next: [[61B_Part_3_OOP]]
12345678public class IntList { public int first; public IntList rest; public IntList(int f, IntList r) { first = f; rest = r; }}
SLListWhile functional, “naked” linked lists like the one above are hard to use.Users of this class are probably going to need to know references very well, and be able to think recursively. Let’s make our users’ lives easier.
12345678910111213141516171819202122232425public class I ...
CS 61B Part 1 Java
prev: [[Basic_Java]]next: [[61B_Part_2_LinkList]]
Compiling systemJava source code –> (compiler) .class –> (interpreter) running file
why .class?
no need to open sourcecode
easier for computer to run
(more to explain in 61C)
Java compiling12javac Dog.javajava Dog
Class in Javain java, “self” in python is “this”.
Static vs. Non-Static Methods12345public class Dog { public static void makeNoise() { System.out.println("Bark!"); }}
Error!
(There ...
CS 61B Part 0.5 Java Debugging&Testing
prev: [[Basic_Java]]next: [[61B_Part_1_Java]]
Breakpointsprevious debugging experience (Print statements):
They require you to modify your code (to add print statements).
They require you to explicitly state what you want to know (since you have to say precisely what you want to print).
And they provide their results in a format that can be hard to read, since it’s just a big blob of text in the execution window.click the line, and now, we have breakpoint.click debugging , and the currently hig ...
CS 61B Part 0 Basic Java
next:[[61B_Part_1_Java]]
Java program skeletonno need to worry about the retract(缩进) (like in python).
Just like C++!
123456public class ClassNameHere { public static void main(String[] args) { }}
conditionsif12if (x < 10) x = x + 10;
other version:
12345if (x < 10) { System.out.println("I shall increment x by 10."); x = x + 10;}
else & else if1234567if (dogSize >= 50) { System.out.println("woof!"); ...
CS 61A calculators
parsingtext => lextical analysis => tokens => syntactic analysis => expression
123'(+ 1' ' (- 23)' ' (* 4 5.6))'
123 '(', '+', 1'(', '-', 23, ')''(', '*', 4, 5.6, ')', ')'
1Pair('+', Pair(1, ...))
reducereduce(, , …)
pass in the parameters(maybe in the form of a list) and apply them to the function
CS 61A errors
errorsassertassert , if body = False, end the program and print “reaction” e.g.:assert False, “Error”
raisetrigger errorsan example:
1234x = -1if x < 0: raise Exception("Sorry, no numbers below zero")
try statementtry: …except as : …example:
12345678910111213141516171819202122232425262728293031323334353637from operator import add, mul, truedivdef reduce(f, s, initial): """Combine elements of s pairwise using f, starting with initial. >>> ...
CS 61A Scheme
Schemesymbolsbooleanst truef falsestrings‘hello world!
numbersno need to say
calculations(+ 1 2) 3 (the same: +-*/<>=)
(modulo 35 4) 3
logic
(if [if-false])
Scheme:
123(if (> x 3) 1 2)
python:
1234if x > 3: 1else: 2
cond
1234(cond((> x 0) 'positive)((< x 0) 'negative)(else 'zero))
and & or
and (and x1 x2 …)
or (or x1 x2 …)
symbolsbooleanst truef falsestrings‘hello world!
numbersno need to say
calculations(+ 1 2) 3 (th ...
CS 61A lect11
CS61A lect11 data abstraction•rational(n, d) returns a rational number x•numer(x) returns the numerator of x•denom(x) returns the denominator of x(These functions implement an abstract representation for rational numbers)for whole data:
1234567891011def mul_rational(x, y): return rational(numer(x) * numer(y), denom(x) * denom(y))def add_rational(x, y): nx, dx = numer(x), denom(x) ny, dy = numer(y), denom(y) return rational(nx * dy + ny * dx, dx * dy)def print_rational(x): print(numer(x), ' ...
