Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Completed about_lists section #280

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
73 changes: 36 additions & 37 deletions koans/about_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class AboutLists(Koan):
def test_creating_lists(self):
empty_list = list()
self.assertEqual(list, type(empty_list))
self.assertEqual(__, len(empty_list))
self.assertEqual(0, len(empty_list))

def test_list_literals(self):
nums = list()
Expand All @@ -21,89 +21,88 @@ def test_list_literals(self):
self.assertEqual([1], nums)

nums[1:] = [2]
self.assertListEqual([1, __], nums)
self.assertListEqual([1, 2], nums)

nums.append(333)
self.assertListEqual([1, 2, __], nums)
self.assertListEqual([1, 2, 333], nums)

def test_accessing_list_elements(self):
noms = ['peanut', 'butter', 'and', 'jelly']

self.assertEqual(__, noms[0])
self.assertEqual(__, noms[3])
self.assertEqual(__, noms[-1])
self.assertEqual(__, noms[-3])
self.assertEqual('peanut', noms[0])
self.assertEqual('jelly', noms[3])
self.assertEqual('jelly', noms[-1])
self.assertEqual('butter', noms[-3])

def test_slicing_lists(self):
noms = ['peanut', 'butter', 'and', 'jelly']

self.assertEqual(__, noms[0:1])
self.assertEqual(__, noms[0:2])
self.assertEqual(__, noms[2:2])
self.assertEqual(__, noms[2:20])
self.assertEqual(__, noms[4:0])
self.assertEqual(__, noms[4:100])
self.assertEqual(__, noms[5:0])
self.assertEqual(['peanut'], noms[0:1])
self.assertEqual(['peanut', 'butter'], noms[0:2])
self.assertEqual([], noms[2:2])
self.assertEqual(['and', 'jelly'], noms[2:20])
self.assertEqual([], noms[4:0])
self.assertEqual(['jelly'], noms[4:100])
self.assertEqual([], noms[5:0])

def test_slicing_to_the_edge(self):
noms = ['peanut', 'butter', 'and', 'jelly']

self.assertEqual(__, noms[2:])
self.assertEqual(__, noms[:2])
self.assertEqual(['and', 'jelly'], noms[2:])
self.assertEqual(['peanut', 'butter'], noms[:2])

def test_lists_and_ranges(self):
self.assertEqual(range, type(range(5)))
self.assertNotEqual([1, 2, 3, 4, 5], range(1,6))
self.assertEqual(__, list(range(5)))
self.assertEqual(__, list(range(5, 9)))
self.assertNotEqual([1, 2, 3, 4, 5], list(range(1, 6)))
self.assertEqual([0, 1, 2, 3, 4], list(range(5)))
self.assertEqual([5, 6, 7, 8], list(range(5, 9)))

def test_ranges_with_steps(self):
self.assertEqual(__, list(range(5, 3, -1)))
self.assertEqual(__, list(range(0, 8, 2)))
self.assertEqual(__, list(range(1, 8, 3)))
self.assertEqual(__, list(range(5, -7, -4)))
self.assertEqual(__, list(range(5, -8, -4)))
self.assertEqual([5, 4], list(range(5, 3, -1)))
self.assertEqual([0, 2, 4, 6], list(range(0, 8, 2)))
self.assertEqual([1, 4, 7], list(range(1, 8, 3)))
self.assertEqual([5, 1, -3], list(range(5, -7, -4)))
self.assertEqual([5, 1, -3, -7], list(range(5, -8, -4)))

def test_insertions(self):
knight = ['you', 'shall', 'pass']
knight.insert(2, 'not')
self.assertEqual(__, knight)
self.assertEqual(['you', 'shall', 'not', 'pass'], knight)

knight.insert(0, 'Arthur')
self.assertEqual(__, knight)
self.assertEqual(['Arthur', 'you', 'shall', 'not', 'pass'], knight)

def test_popping_lists(self):
stack = [10, 20, 30, 40]
stack.append('last')

self.assertEqual(__, stack)
self.assertEqual([10, 20, 30, 40, 'last'], stack)

popped_value = stack.pop()
self.assertEqual(__, popped_value)
self.assertEqual(__, stack)
self.assertEqual('last', popped_value)
self.assertEqual([10, 20, 30, 40], stack)

popped_value = stack.pop(1)
self.assertEqual(__, popped_value)
self.assertEqual(__, stack)
self.assertEqual(20, popped_value)
self.assertEqual([10, 30, 40], stack)

# Notice that there is a "pop" but no "push" in python?

# Part of the Python philosophy is that there ideally should be one and
# only one way of doing anything. A 'push' is the same as an 'append'.

# To learn more about this try typing "import this" from the python
# To learn more about this, try typing "import this" from the Python
# console... ;)

def test_making_queues(self):
queue = [1, 2]
queue.append('last')

self.assertEqual(__, queue)
self.assertEqual([1, 2, 'last'], queue)

popped_value = queue.pop(0)
self.assertEqual(__, popped_value)
self.assertEqual(__, queue)
self.assertEqual(1, popped_value)
self.assertEqual([2, 'last'], queue)

# Note, popping from the left hand side of a list is
# Note, popping from the left-hand side of a list is
# inefficient. Use collections.deque instead.