From 6f68e1818229e0d2dad760062e6b5bb137b88f5b Mon Sep 17 00:00:00 2001 From: zhang Date: Sun, 23 Jan 2022 12:42:38 +0800 Subject: =?UTF-8?q?=E9=9A=8F=E6=9C=BA=E6=B8=B8=E8=B5=B0=20&=20ortools=20de?= =?UTF-8?q?mo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- prob/random_walk_oo.py | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 prob/random_walk_oo.py (limited to 'prob/random_walk_oo.py') diff --git a/prob/random_walk_oo.py b/prob/random_walk_oo.py new file mode 100644 index 0000000..ce1baf5 --- /dev/null +++ b/prob/random_walk_oo.py @@ -0,0 +1,52 @@ + +import random + + +class Location(object): + + def __init__(self, x, y): + self.x = x + self.y = y + + def move(self, dx, dy): + return Location(self.x + dx, self.y + dy) + + def get_x(self): + return self.x + + def get_y(self): + return self.y + + def euclidean_distance_from(self, other): + x_dist = self.x - other.get_x() + y_dist = self.y - other.get_y() + return (x_dist ** 2 + y_dist **2)**0.5 + + def manhattan_distance_from(self, other): + x_dist = self.x - other.get_x() + y_dist = self.y - other.get_y() + return abs(x_dist) + abs(y_dist) + + def __str__(self): + return f'<{self.x}, {self.y}>' + + +class Drunk(object): + def __init__(self, name=None): + self.name = name + + def __str__(self): + if self.name is not None: + return self.name + return 'Anonymous' + + +def usual_take_step(): + return random.choice([[-1, 0], [1, 0], [0, -1], [0, 1]]) + + +def north_take_step(): + step_choices = [(0.0, 1.1), (0.0, -0.9), (1.0, 0.0), (-1.0, 0.0)] + return random.choice(step_choices) + + -- cgit v1.2.3