コード例 #1
0
def BreathFirstSearch(problem):
    visited = set()
    fringe = util.MyQueue()
    fringe.push((problem.getInitialState(), []))

    iteration = 0
    while not fringe.isEmpty():
        state, plan = fringe.pop()
        if problem.isGoal(state): return plan
        if tuple(sum(state, [])) not in visited:
            visited.add(tuple(sum(state, [])))
            for successor in problem.getSuccessors(state):
                action, next_state, _ = successor
                new_plan = plan + [action]
                fringe.push((next_state, new_plan))

        iteration += 1
        if not iteration % 20000:
            print(iteration, "nodes expanded.")
    else:
        return None
コード例 #2
0
On the other hand, operation push does not have to return anything.

An example behavior is as follows:
"""

s = util.MyStack()

assert (s.pop() == None)

s.push(1)
s.push(2)
s.push(3)
# should evaluate to
assert (s.pop() == 3)

q = util.MyQueue()

assert (q.pop() == None)
q.push(1)
q.push(2)
q.push(3)
# should evaluate to
assert (q.pop() == 1)
"""
Problem 3
For the two classes written in Problem 2, override __eq__, __ne__, and __str__
methods. You can read about these methods in detail here:

http://docs.python.org/reference/datamodel.html#basic-customization

Simply put, the above methods do the following:
コード例 #3
0
ファイル: HW0.py プロジェクト: leohentschker/HW0
import util
import types

# ### Problem 1
#
# Write a function called `matrix_multiply` which multiplies two 2-dimensional lists of real numbers. For instance,

mm = util.matrix_multiply([[1, 2], [3, 4]], [[4, 3], [2, 1]])
# should evaluate to
assert (mm == [[8, 5], [20, 13]])

# ### Problem 2
# Complete the definitions of the methods `init`, `push` and `pop` in the Python classes `MyQueue` and `MyStack`, using a deque data structure in Python. The operation pop should return, not print, the appropriate object in the structure. If empty, it should return None instead of throwing an error. On the other hand, operation push does not have to return anything. An example behavior is as follows:

q = util.MyQueue()
q.push(1)
q.push(2)
# should evaluate to
assert (q.pop() == 1)

s = util.MyStack()
s.push(1)
s.push(2)
assert q.pop() == 2

# ### Problem 3
# For the two classes written in Problem 2, override __eq__, __ne__, and __str__ methods. You can read about these methods in detail here:
#
# http://docs.python.org/reference/datamodel.html#basic-customization
#
# Simply put, the above methods do the following:
コード例 #4
0
ファイル: HW0.py プロジェクト: jgollub1/HW0
import util

# ### Problem 1
#
# Write a function called `matrix_multiply` which multiplies two 2-dimensional lists of real numbers. For instance,

mm = util.matrix_multiply([[1, 2], [3, 4]], [[4, 3], [2, 1]])
# should evaluate to
assert (mm == [[8, 5], [20, 13]])

# ### Problem 2
# Complete the definitions of the methods `init`, `push` and `pop` in the Python classes `MyQueue` and `MyStack`, using a deque data structure in Python. The operation pop should return, not print, the appropriate object in the structure. If empty, it should return None instead of throwing an error. On the other hand, operation push does not have to return anything. An example behavior is as follows:

q = util.MyQueue()
q.push(1)
q.push(2)
# should evaluate to
assert (q.pop() == 1)

# ### Problem 3
# For the two classes written in Problem 2, override __eq__, __ne__, and __str__ methods. You can read about these methods in detail here:
#
# http://docs.python.org/reference/datamodel.html#basic-customization
#
# Simply put, the above methods do the following:
#
# * __eq__(self, other) returns True if self and other are `equal`.
#
# * __ne__(self, other) returns True if self and other are `not equal`.
#
# * __str__(self) returns a string representation of self. You may decide on whatever representation you want, but ideally the method should at least print the elements.