diff --git a/_sources/Containers/dynamic_supp.rst b/_sources/Containers/dynamic_supp.rst new file mode 100644 index 0000000..712fd99 --- /dev/null +++ b/_sources/Containers/dynamic_supp.rst @@ -0,0 +1,51 @@ +Toggle Questions +================= + +.. parsonsprob:: exp1_pp1a + :adaptive: + :numbered: left + + Put the blocks in order to define the function ``has22`` to return ``True`` if there are at least two items in the list nums that are adjacent and both equal to 2, otherwise return ``False``. For example, return ``True`` for ``has22([1, 2, 2])`` since there are two adjacent items equal to 2 (at index 1 and 2) and ``False`` for ``has22([2, 1, 2])`` since the 2’s are not adjacent. + ----- + def has22(nums): + ===== + for i in range(len(nums)-1): + ===== + for i in range(len(nums)): #paired + ===== + if nums[i] == 2 and num[i+1] == 2: + ===== + if nums[i] == 2 and num[i-1] == 2: #paired + ===== + return True + ===== + return true #paired + ===== + return False + + +.. activecode:: exp1_q1_write + :autograde: unittest + + Finish the function ``has22`` below to return ``True`` if there are at least two items in the list ``nums`` that are adjacent and both equal to 2, otherwise return ``False``. For example, return ``True`` for ``has22([1, 2, 2])`` since there are two adjacent items equal to 2 (at index 1 and 2) and ``False`` for ``has22([2, 1, 2])`` since the 2's are not adjacent. + + ~~~~ + def has22(nums): + + ==== + from unittest.gui import TestCaseGui + + class myTests(TestCaseGui): + + def testOne(self): + self.assertEqual(has22([1, 2, 2]), True, 'has22([1, 2, 2])') + self.assertEqual(has22([1, 2, 1, 2]), False, 'has22([1, 2, 1, 2])') + self.assertEqual(has22([2, 1, 2]), False, 'has22([2, 1, 2])') + self.assertEqual(has22([2, 2, 1]), True, 'has22([2, 2, 1])') + self.assertEqual(has22([3, 4, 2]), False, 'has22([3, 4, 2])') + self.assertEqual(has22([2]), False, 'has22([2])') + self.assertEqual(has22([]), False, 'has22([])') + self.assertEqual(has22([3, 3, 1]), False, 'has22([3, 3, 1])') + self.assertEqual(has22([1, 4, 4]), False, 'has22([1, 4, 4])') + + myTests().main()