def test_insertion_naive(self): self.assertEqual( insert_naive(Rope("test"), "123", 2).to_string(), "te123st") self.assertEqual( insert_naive(Rope("test"), "123", 4).to_string(), "test123") self.assertEqual( insert_naive(Rope("test"), "123", 0).to_string(), "123test")
def test_that_the_rope_works_with_small_strings(self): testStr = "Hi" testRope = Rope(testStr) self.assertEqual(str(testRope), testStr) testRope.insert(1, "i, what's your name? My name is Benj") self.assertEqual(str(testRope), "Hi, what's your name? My name is Benji")
def test_deletion_naive(self): self.assertEqual( delete_range_naive(Rope("test"), 1, 2).to_string(), "tst") self.assertEqual( delete_range_naive(Rope("test"), 2, 4).to_string(), "te") self.assertEqual( delete_range_naive(Rope("test"), 0, 2).to_string(), "st")
def __init__(self, value=''): self.content = Rope(value) self.commands_to_function = { self.PRINT_COMMAND: self.print_string, self.INSERT_COMMAND: self.insert_string, self.DELETE_COMMAND: self.delete_string, self.APPEND_COMMAND: self.append_string }
def test_that_the_string_initialises_as_expected(self): testStr = "the quick brown fox jumped over the lazy dog" testRope = Rope(testStr, 10, 5, 15) self.assertEqual(str(testRope), testStr) self.assertEqual(testRope.node.leftLength, 22) self.assertEqual(testRope.node.left.leftLength, 11) self.assertEqual(testRope.node.left.left.value, "the quick b")
def test_that_deletes_happen_as_expected_and_rebalances(self): testStr = "the not so quick brown fox almost jumped over the lazy dog" testRope = Rope(testStr, 10, 5, 15) testRope.delete(27, 7) testRope.delete(4, 7) self.assertEqual(str(testRope), "the quick brown fox jumped over the lazy dog")
def test_that_inserts_happen_as_expected_and_rebalances(self): testStr = "the quick brown fox jumped over the lazy dog" testRope = Rope(testStr, 10, 5, 15) testRope.insert(4, "not so ") testRope.insert(27, "almost ") self.assertEqual( str(testRope), "the not so quick brown fox almost jumped over the lazy dog")
def test_that_the_rope_works_with_a_gigantic_string(self): testStr = "".join( choice("abcdefghijklmnopqrstuvwxyz") for i in range(100000)) testRope = Rope(testStr) self.assertEqual(str(testRope), testStr) testRope.insert(3000, "HELLO") testStr = testStr[:3000] + "HELLO" + testStr[3000:] self.assertEqual(str(testRope), testStr) testRope.delete(1000, 1000) testStr = testStr[:1000] + testStr[2000:] self.assertEqual(str(testRope), testStr)
def test_split_at_naive(self): test = Rope("test") self.assertEqual(split_at_naive(test, 2), (Rope("te"), Rope("st")))
def test_rope_basics(self): self.assertEqual(Rope("test").to_string(), "test") self.assertEqual(prepend(Rope("test"), "abc").to_string(), "abctest") self.assertEqual(append(Rope("test"), "abc").to_string(), "testabc")
# # For now just create one image pipeline to share with each image processor # LATER we will modify this to allow for a dictionary (switch-like) interface # to be injected into the image processors; this will allow the image processors # to have a selector for exclusion processor of different pipelines # # I.e., the idea is to create separate image processors when concurrent pipelines # are desired (e.g., look for faces AND pink elephants at the same time), and place # the exclusive options into a single processor (e.g., look for faces OR pink elephants) redBoiler = RedBoiler() blueBoiler = BlueBoiler() boiler = Boiler() gearLift = GearLift(bvTable) rope = Rope() nada = Nada() # NOTE: NOTE: NOTE: # # YOUR MILEAGE WILL VARY # The exposure values are camera/driver dependent and have no well defined standard (i.e., non-portable) # Our implementation is forced to use v4l2-ctl (Linux) to make the exposure control work because our OpenCV # port does not seem to play well with the exposure settings (produces either no answer or causes errors depending # on the camera used) FRONT_CAM_GEAR_EXPOSURE = 0 FRONT_CAM_NORMAL_EXPOSURE = -1 # Camera default frontCam = BucketCapture( name="FrontCam",
gameFont = pygame.font.SysFont('tlwgtypewriter', 25, False, False) # Set the width, height and position of the screen screen_size = (constants.WINDOW_WIDTH, constants.WINDOW_HEIGHT) screenInfo = pygame.display.Info() screen = pygame.display.set_mode(screen_size) pygame.display.set_caption("Ropes!!") # Loop until the user clicks the close button. done = False # Used to manage how fast the screen updates clock = pygame.time.Clock() instances = [Rope(200, 50, 300, 15, 0.1), Rope(400, 50, 300, 15, 0.1)] # -------- Main Program Loop ----------- while not done: # --- Main event loop events = pygame.event.get() for event in events: if event.type == pygame.QUIT: # If user clicked close done = True if event.type == pygame.KEYUP and event.key == pygame.K_ESCAPE: done = True # --- Game logic --- # # from random import randint # instances.append(PointMass(randint(100, 1200), 100, 1)) for inst in instances:
class StringEditor: PRINT_COMMAND = "PRINT" APPEND_COMMAND = "APPEND" DELETE_COMMAND = "DELETE" INSERT_COMMAND = "INSERT" AVAILABLE_COMMANDS = [ PRINT_COMMAND, APPEND_COMMAND, DELETE_COMMAND, INSERT_COMMAND ] def __init__(self, value=''): self.content = Rope(value) self.commands_to_function = { self.PRINT_COMMAND: self.print_string, self.INSERT_COMMAND: self.insert_string, self.DELETE_COMMAND: self.delete_string, self.APPEND_COMMAND: self.append_string } def command_controller(self): """ Take a command and act on it """ command = input() while not self._validate_command(command): print("Command is invalid! Valid commands are:\n\t{}".format( '\n\t'.join(self.AVAILABLE_COMMANDS))) command = input() arguments = command.split() command = arguments[0] args = arguments[1:] self.commands_to_function[command](args) def insert_string(self, *args): """ inserts given string at front of the text. Print "OK" as command result. """ str_to_insert = args[0][0] self.content.insert(0, str_to_insert) print('OK') def append_string(self, *args): """ append a given string to the end of the text. Print "OK" as command result. """ str_to_append = args[0][0] self.content.insert(len(self.content), str_to_append) print('OK') def delete_string(self, *args): """ deletes the specified substring. Print "OK" as command result in case of success. Print "ERROR" in case of invalid substring. """ try: start = int(args[0][0]) end = start + int(args[0][1]) self.content.remove(start, end) print('OK') except: print('ERROR') def print_string(self, *args): print(self.content) def _validate_command(self, command: str) -> bool: """ return a boolean indicating if the command is valid """ for valid_command in self.AVAILABLE_COMMANDS: if command.startswith(valid_command): return True return False
from time import time from rope import Rope, Vec, Collider #region pygame init pygame.init() size = (600, 600) screen = pygame.display.set_mode(size) screen.fill([255, 255, 255]) pygame.display.set_icon(screen) clock, fps = pygame.time.Clock(), 0 delta_time = 0 frame_start_time = 0 #endregion rope = Rope(x=size[0] / 2, y=10, length=500, resolution=50) mouse_collider = Collider(0, 0) mouse_collider.size = 50 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() frame_start_time = time() screen.fill(0) mpos = Vec(pygame.mouse.get_pos()) mouse_collider.pos.linear_interpolate(mpos, t=delta_time)
fps = 60 win = pygame.display.set_mode((800, 800)) clock = pygame.time.Clock() mid = pygame.display.get_surface().get_size()[0] // 2 start_y = pygame.display.get_surface().get_size()[1] // 2 r1 = 150 r2 = 220 a1 = math.pi / 4 a2 = math.pi m1 = 30 m2 = 20 g = 2 line_1 = Rope(mid, start_y, r1, a1, m1) line_2 = Rope(line_1.x2, line_1.y2, r2, a2, m2) paint_points = [] def swing(line_1, line_2): num1 = -g * (2 * line_1.m + line_2.m) * math.sin(line_1.a) num2 = -line_2.m * g * math.sin(line_1.a - 2 * line_2.a) num3 = -2 * math.sin(line_1.a - line_2.a) * line_2.m num4 = line_2.vel**2 * line_2.r + line_1.vel**2 * line_1.r * math.cos( line_1.a - line_2.a) den = line_1.r * (2 * line_1.m + line_2.m - line_2.m * math.cos(2 * line_1.a - 2 * line_2.a)) line_1.acc = (num1 + num2 + num3 * num4) / den num1 = 2 * math.sin(line_1.a - line_2.a)