def test_rand(self): def known_diamond(n): if n % 2 == 0 or n < 0: return None result = [] def append(c, n, nl): for _ in range(n): result.append(c) if nl: result.append('\n') indent = n // 2 for i in range(indent, 0, -1): append(' ', i, False) append('*', n - 2 * i, True) append('*', n, True) for i in range(1, indent + 1): append(' ', i, False) append('*', n - 2 * i, True) return "".join(result) from random import randrange as rand for _ in range(25): n = rand(1, 20) if rand(4) else rand(-20, 5) exp = known_diamond(n) self.assertEqual(diamond(n), exp)
def get_road_mask(im: np.ndarray) -> np.ndarray: # Gaussian blur to make the road surface look more even im_blur = gaussian_filter(im, sigma=1) # K-Means clustering to get road im_c, kmeans = cluster_image(im_blur) mean_labels = np.mean(kmeans.cluster_centers_, axis=1) / 255 midtone_label = np.argsort(mean_labels)[1] midtone_value = kmeans.cluster_centers_[midtone_label] / 255 # midtone mask (assume midtone contains road) mask = (im_c == midtone_value) mask = mask.astype('float64').mean(axis=2) # Binary closing - close noise kernel = diamond(3) mask_closed = binary_closing(mask, kernel) # Binary opening - disconnect thinly connected parts mask_closed = binary_opening(mask, kernel) # Connected components cc = measure.label(mask_closed, background=0) # Assume the largest group is the road largest_cc = get_largest_component(cc) mask_main_cc = np.zeros(cc.shape) mask_main_cc[cc == largest_cc] = 1 # Get connected components of the reverse mask to fill in holes reverse_mask = 1 - mask_main_cc cc_rev = measure.label(reverse_mask, background=0) cc_rev_flat = cc_rev.reshape(-1) unique_labels = np.unique(cc_rev) counts = [] for label in unique_labels: counts.append(len(cc_rev_flat[cc_rev_flat == label])) max_size = np.max(counts) for label in unique_labels: if counts[label] < max_size: cc_rev[cc_rev == label] = 0 cc_rev[cc_rev > 0] = 1 final_mask = 1 - cc_rev final_mask = final_mask.astype(np.float) return final_mask
def test_diamond(len, expected): assert np.array_equal(diamond(len), expected) is True
pen1.circle(100) pen1.end_fill() pen2.write("KP", font=('Candara', 70)) pen1.up() pen1.goto(-250, -150) pen1.down() pen1.write("""1. Blackjack 2. Lucky 7 3. Pattern 1 4. Pattern 2""", font=("Candara", 20)) while True: k = input('Enter Choice ') if k == 1: blackjack.blackjack() elif k == 2: lucky7.lucky7() elif k == 3: triangle.triangle() else: diamond.diamond() print "" x = raw_input('You want to continue here (y/n)? ') if x == 'n': print "Thank You For Playing" break else: True
def test_B_diamond(self): self.assertEqual(diamond('B'), ' A \nB B\n A ')
def test_when_a_return_a(self): params = 'a' result = diamond(params) self.assertEquals(result, 'a\n')
def test_a_diamond(): assert diamond("A") == ["A"]
def test_c_diamond(): assert diamond("C") == [" A ", " B B ", "C C", " B B ", " A "]
def test_receive_D_and_return_a_diamond_with_A_B_C_D(self): returned = diamond.diamond('D') expected = ' A\n B B\n C C\nD D\n C C\n B B\n A' self.assertEquals(returned, expected)
def test_receive_B_and_return_a_diamond_with_A_and_B(self): returned = diamond.diamond('B') expected = ' A\nB B\n A' self.assertEquals(returned, expected)
def test_diamond_invalid_input(n, expected): """Check if function diamond returns None on an invalid input.""" assert diamond(n) == expected
def test_highher_n_value(): """Test for n = 9.""" assert diamond(9) == ' *\n ***\n *****\n *******\n*********\n' \ ' *******\n *****\n ***\n *\n'
def test_basic_shape(): """Test diamond production for n = 3.""" assert diamond(3) == ' *\n***\n *\n'
def test(self): self.assertEqual(diamond(3), " *\n***\n *\n") self.assertEqual(diamond(0), None) self.assertEqual(diamond(2), None) self.assertEqual(diamond(-1), None) self.assertEqual(diamond(-2), None)
def test_receive_A_and_return_A(self): returned = diamond.diamond('A') expected = 'A' self.assertEquals(returned, expected)
while True: print "Choices:" print "1 Diamond" print "2 Possible Sequences" print "3 Removing adjacent similar characters" print "4 Calculate Interest from Balance and Rate" print "5 Fibonacci Number" print "6 Stacks and Queues" print "7 Guess the jumbled word" print "0 Exit" choice = raw_input("Enter choice:") if choice == '1': print "Diamond:" diamond_input = raw_input("Enter Size:") diamond.diamond(diamond_input) elif choice == '2': print "Possible Sequences:" sequencer_input = raw_input("Enter sequence") sequencer.sequencer(sequencer_input) elif choice == '3': print "Remove adjacent similar Characters" adjacent_input = raw_input("Enter list of strings:") print(adjacent.cleaner(adjacent_input)) elif choice == '4': print "Balance growth" balance_start = int(raw_input("Enter Starting Balance:")) balance_rate = float(raw_input("Enter interest rate:")) balance_year = int(raw_input("Enter number of years:")) print balance.bal_growth(balance_start, balance_rate, balance_year) elif choice == '5':
from circle import circle if __name__ == '__main__': import argparse parser = argparse.ArgumentParser(description='Draws art') parser.add_argument('program', help='Choose the program', choices=['circle', 'diamond']) parser.add_argument('size', help='The size of the art', default=17, type=int) parser.add_argument('-o', '--out', help='Output to a file', type=argparse.FileType('w+'), default=sys.stdout) parser.add_argument('-f', '--filled', help='Character to use to fill a point', default='X') parser.add_argument('-e', '--empty', help='Character to use to fill empty points', default=' ') parser.add_argument('-w', '--width', help='Number of -e characters to put between points', default=1, type=int) parser.add_argument('-l', '--line-height', help='Number of newlines between each line', default=1, type=int) args = parser.parse_args() end = args.empty * args.width if (args.program == 'circle'): d = circle(args.size, args.filled, args.empty) if (args.program == 'diamond'): d = diamond(args.size, args.filled, args.empty) def p(c): print(c, end='', file=args.out, flush=True) for y in d: for x in y: p(x + end) p('\n' * args.line_height)
def test_d_diamond(): assert diamond("D") == [ " A ", " B B ", " C C ", "D D", " C C ", " B B ", " A " ]
def test_C_diamond(self): self.assertEqual(diamond('C'), ' A \n B B \nC C\n B B \n A ')
def test_b_diamond(): assert diamond("B") == [" A ", "B B", " A "]
def test_A_diamond(self): self.assertEqual(diamond('A'), 'A')
def test_when_c_return_diamond(self): params = 'c' result = diamond(params) self.assertEquals(result, ' a \n b b \nc c\n b b \n a \n')
def test_diamond(car, output): assert diamond(car) == output