def test_unique(): """Return True or False if the test of unique passes/fails""" TEST_PASSED = True # Assume the test will succeed numbers = [4, 5, 2, 6, 2, 3, 5, 8] nums = unique(numbers) counter = 0 for x in range(6): try: next(nums) counter += 1 except StopIteration: pass if counter == 6: return True else: return False things = unique(['dog', 'cat', 'bird', 'cat', 'fish']) counter = 0 for x in range(4): try: next(things) counter += 1 except StopIteration: pass if counter != 4: TEST_PASSED = False return TEST_PASSED
def test_unique(): """Return True/False if the test of unique passes/fails""" numbers = [4, 5, 2, 6, 7, 2, 3, 5, 8] expected = [4, 5, 2, 6, 7, 3, 8] # Save the results from calling next. result = [] nums = unique(numbers) # We know there should be 7 numbers, so call next() 7 times to get them. # Save them to compare later. # Use a try/except just in case something goes wrong and we get a # StopIteration error too early. try: result.append(next(nums)) result.append(next(nums)) result.append(next(nums)) result.append(next(nums)) result.append(next(nums)) result.append(next(nums)) result.append(next(nums)) except StopIteration: # We shouldn't get any exceptions here return False # We should have all the numbers in the result list at this point. # This call to next() should raise a StopIteration error. try: next(nums) except StopIteration: # Done, so now verify that the numbers we got back are correct. if result != expected: return False else: # We shouldn't get here if things work correctly. return False # Test 2, try with empty list nums = unique([]) # Should get error first time. try: next(nums) except StopIteration: # Nothing to do here, since this is what we want to happen. # Use pass to continue in case we add more tests after this one. # (Rather than return True here, which would be OK, # but I like having only one place for return True at the end.) pass else: # We shouldn't get here if things work correctly. return False # All tests passed if we get here return True
def test_unique(): i = 0 numbers = [2, 6, 5, 7, 7, 3, 9, 3, 2, 8] nums = unique(numbers) list1 = [2, 6, 5, 7, 3, 9, 8] while (i < len(list1)): if next(nums) == list1[i]: i = i + 1 continue else: return False try: next(nums) except StopIteration: return True else: return False
def test_unique(): """Return True/False if the test of unique passes/fails""" numbers = [4, 5, 2, 6, 2, 3, 5, 8] nums = unique(numbers) try: x = next(nums) y = next(nums) z = next(nums) m = next(nums) n = next(nums) q = next(nums) if x == 4: if y == 5: if z == 2: if m == 6: if n == 3: if q == 8: next(nums) except StopIteration: return True return False
assert (len_safe(7) == -1) assert (len_safe(None) == -1) assert (len_safe('cat') == 3) assert (len_safe('') == 0) animals = ['dog', 'cat', 'bird', 'cat', 'fish'] assert (len_safe(animals) == 5) assert (len_safe(math.pi) == -1) ### Part 3: unique numbers = [4, 5, 2, 6, 2, 3, 5, 8] nums = unique(numbers) assert (next(nums) == 4) assert (next(nums) == 5) assert (next(nums) == 2) assert (next(nums) == 6) assert (next(nums) == 3) assert (next(nums) == 8) try: next(nums) except: pass #print('StopIteation') things = unique(['dog', 'cat', 'bird', 'cat', 'fish']) assert (next(things) == 'dog') assert (next(things) == 'cat')