def test_palendrome(): input = "Madam I'm Adam" palindrome = String(input).is_palindrome() it.assertEqual(palindrome, True) input = "asdf" palindrome = String(input).is_palindrome() it.assertEqual(palindrome, False) input = "Madam I'm Adam" palindrome = String(input).is_palindrome_manual() it.assertEqual(palindrome, True) input = "asdf" palindrome = String(input).is_palindrome_manual() it.assertEqual(palindrome, False)
def test_reverse_substrings_at_token(): input = String("abc def hi") expected = "cba fed ih" result = input.reverse_substring_at_token(" ") it.assertEqual(result, expected) input = String("abc/def/hi") expected = "cba/fed/ih" result = input.reverse_substring_at_token("/") it.assertEqual(result, expected)
def __init__(self, Lx=40, Ly=40, boundary={ 'h': 'periodic', 'v': 'periodic' }, size=[5, 4, 10, 12], plot=True, plot_surface=True, save_image=False, save_video=False, filename_image="", filename_video="", frames=1000, beta=2., interval=1, weight_const=0.5, strings=None, pre_function=None, post_function=None): """Init function of Main class. Lx (int (even)): 格子のx方向(グラフではy軸)の格子点数 Ly (int (even)): 格子のy方向(グラフではx軸)の格子点数 """ # Create triangular lattice with given parameters # self.lattice = LT(np.zeros((Lx, Ly), dtype=np.int), # scale=float(max(Lx, Ly)), boundary=boundary) self.lattice = LT(np.zeros((Lx, Ly), dtype=np.int), scale=float(max(Lx, Ly)), boundary=boundary) self.lattice_X = self.lattice.coordinates_x.reshape( self.lattice.Lx, self.lattice.Ly) self.lattice_Y = self.lattice.coordinates_y.reshape( self.lattice.Lx, self.lattice.Ly) self.occupied = np.zeros((Lx, Ly), dtype=np.bool) self.number_of_lines = Lx if strings is None: # Put the strings to the lattice self.strings = self.create_random_strings(len(size), size) else: self.strings = [String(self.lattice, **st) for st in strings] for string in self.strings: if string.loop: raise AttributeError("string can't be loop.") self.occupied[string.pos_x, string.pos_y] = True self.plot = plot self.plot_surface = plot_surface self.save_image = save_image self.save_video = save_video if self.save_image: if filename_image == "": raise AttributeError("`filename_image` is empty.") else: self.filename_image = filename_image if self.save_video: if self.plot: raise AttributeError( "`save` and `plot` method can't be set both True.") if filename_video == "": raise AttributeError("`filename_video` is empty.") else: self.filename_video = filename_video self.interval = interval self.frames = frames self.beta = beta self.weight_const = weight_const self.bonding_pairs = {i: {} for i in range(len(self.strings))} for key in self.bonding_pairs.keys(): value = self.get_bonding_pairs(s=self.strings[key], index_start=0, index_stop=len( self.strings[key].pos)) # TODO: 隣接点がないとき,全体のシミュレーションを終了する # if len(value) == 0: # return False self.bonding_pairs[key] = value # pp.pprint(self.bonding_pairs) # print(self.strings[0].pos) # pp.pprint(self.bonding_pairs[0]) # pp.pprint(self.bonding_pairs) # return None self.pre_function = pre_function self.post_function = post_function self.pre_func_res = [] self.post_func_res = [] # Plot triangular-lattice points, string on it, and so on if self.plot: self.plot_all() self.start_animation() elif self.save_video: self.plot_all() self.start_animation(filename=self.filename_video) else: t = 0 while t < self.frames: try: self.update() t += 1 except StopIteration: break if self.save_image: if not self.__dict__.has_key('fig'): self.plot_all() self.fig.savefig(self.filename_image) plt.close()
def create_random_strings(self, N=3, size=[10, 5, 3]): """Create N strings of each size is specified with 'size'. This process is equivalent to self-avoiding walk on triangular lattice. """ strings = [] n = 0 while n < N: # set starting point x = random.randint(0, self.lattice.Lx - 1) y = random.randint(0, self.lattice.Ly - 1) if self.occupied[x, y]: # reset print_debug("(%d, %d) is occupied already. continue." % (x, y)) continue self.occupied[x, y] = True S = size[n] pos = [(x, y, np.random.choice(6))] trial, nmax = 0, 10 double = 0 while len(pos) < S: if trial > nmax: for _x, _y, vec in pos: self.occupied[_x, _y] = False print_debug("All reset") break X = self.get_next_xy(x, y, pos[-1][2]) if not X: if len(pos) == 1: print_debug("len(pos) == 1") double = 0 trial += 1 break else: print_debug("back one step") print_debug(pos) if double == 1: print_debug("two time. back two step") print_debug(pos) oldx, oldy, oldvec = pos[-1] del pos[-1] self.occupied[oldx, oldy] = False oldx, oldy, oldvec = pos[-1] del pos[-1] self.occupied[oldx, oldy] = False x, y, vector = pos[-1] trial += 1 print_debug(pos) break oldx, oldy, oldvec = pos[-1] del pos[-1] self.occupied[oldx, oldy] = False x, y, vector = pos[-1] trial += 1 print_debug(pos) continue else: double = 0 x, y, vector = X self.occupied[x, y] = True pos.append((x, y, vector)) print_debug("add step normally") print_debug(pos) else: print_debug("Done. Add string") vec = [v[2] for v in pos][1:] strings.append( String(self.lattice, n, pos[0][0], pos[0][1], vec=vec)) n += 1 return strings
def __init__(self, Lx=40, Ly=40, boundary={ 'h': 'periodic', 'v': 'periodic' }, size=[5, 4, 10, 12], plot=True, plot_surface=True, save_image=False, save_video=False, filename_image="", filename_video="", frames=1000, beta=2., interval=0, weight_const=0.5, strings=None, pre_function=None, post_function=None): self.lattice = LT(np.zeros((Lx, Ly), dtype=np.int), scale=float(max(Lx, Ly)), boundary=boundary) ## randomize randomize(self.lattice) ## if the lattice size exceeds 200, don't draw triangular lattice. if max(self.lattice.Lx, self.lattice.Ly) < 200: self.triang_standard = tri.Triangulation( self.lattice.coordinates_x, self.lattice.coordinates_y) self.triang_random = tri.Triangulation( self.lattice.coordinates_x, self.lattice.coordinates_y, triangles=self.triang_standard.triangles) self.lattice_X = self.lattice.coordinates_x.reshape( self.lattice.Lx, self.lattice.Ly) self.lattice_Y = self.lattice.coordinates_y.reshape( self.lattice.Lx, self.lattice.Ly) self.occupied = np.zeros((Lx, Ly), dtype=np.bool) self.number_of_lines = Lx if strings is None: # Put the strings to the lattice self.strings = self.create_random_strings(len(size), size) else: self.strings = [String(self.lattice, **st) for st in strings] for string in self.strings: self.occupied[string.pos_x, string.pos_y] = True self.plot = plot self.plot_surface = plot_surface self.save_image = save_image self.save_video = save_video if self.save_image: if filename_image == "": raise AttributeError("`filename_image` is empty.") else: self.filename_image = filename_image if self.save_video: if self.plot: raise AttributeError( "`save` and `plot` method can't be set both True.") if filename_video == "": raise AttributeError("`filename_video` is empty.") else: self.filename_video = filename_video self.interval = interval self.frames = frames # 逆温度 self.beta = beta self.weight_const = weight_const self.bonding_pairs = {i: {} for i in range(len(self.strings))} for key in self.bonding_pairs.keys(): value = self.get_bonding_pairs( s=self.strings[key], # indexes=[[0, len(self.strings[key].pos)]] index_start=0, index_stop=len(self.strings[key].pos)) self.bonding_pairs[key] = value self.pre_function = pre_function self.post_function = post_function self.pre_func_res = [] self.post_func_res = [] # Plot triangular-lattice points, string on it, and so on if self.plot: self.plot_all() self.start_animation() elif self.save_video: self.plot_all() self.start_animation(filename=self.filename_video) else: t = 0 while t < self.frames: try: self.update() t += 1 except StopIteration: break if self.save_image: if not self.__dict__.has_key('fig'): self.plot_all() self.fig.savefig(self.filename_image) plt.close()
def test_reverse_substrings_in_groups(): input = String("abcdefhi") expected = "cbafedih" result = input.reverse_substring_in_group(3) it.assertEqual(result, expected)
def test_remove_duplicates(): input = String("aabccdddefggaa") expected = "abcdefg" it.assertEqual(input.dedupe(), expected)