Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions _sources/Containers/dynamic_supp.rst
Original file line number Diff line number Diff line change
@@ -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()