Skip to content
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
29 changes: 15 additions & 14 deletions python2/koans/about_iteration.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@ def test_iterators_are_a_type(self):
for num in it:
fib += num

self.assertEqual(__, fib)
self.assertEqual(15, fib)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weird, but the name of the variable suggests a Fibonacci sum, but the algorithm is just the addition of the numbers xD.


def test_iterating_with_next(self):
stages = iter(['alpha', 'beta', 'gamma'])

try:
self.assertEqual(__, next(stages))
self.assertEqual('alpha', next(stages))
next(stages)
self.assertEqual(__, next(stages))
self.assertEqual('gamma', next(stages))
next(stages)
except StopIteration as ex:
err_msg = 'Ran out of iterations'

self.assertMatch(__, err_msg)
self.assertMatch('Ran out of iterations', err_msg)

# ------------------------------------------------------------------

Expand All @@ -38,7 +38,7 @@ def test_map_transforms_elements_of_a_list(self):
seq = [1, 2, 3]

mapped_seq = map(self.add_ten, seq)
self.assertEqual(__, mapped_seq)
self.assertEqual([11, 12, 13], mapped_seq)

def test_filter_selects_certain_items_from_a_list(self):
def is_even(item):
Expand All @@ -47,7 +47,7 @@ def is_even(item):
seq = [1, 2, 3, 4, 5, 6]

even_numbers = filter(is_even, seq)
self.assertEqual(__, even_numbers)
self.assertEqual([2, 4, 6], even_numbers)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In your words, what's the difference between filter and map?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

map applies the function in all members of the iterator, in contrast, filter do a comparison with the members and if that result is true, it's part of the final output.


def test_just_return_first_item_found(self):
def is_big_name(item):
Expand All @@ -57,12 +57,12 @@ def is_big_name(item):

# NOTE This still iterates through the whole names, so not particularly
# efficient
self.assertEqual([__], filter(is_big_name, names)[:1])
self.assertEqual(['Clarence'], filter(is_big_name, names)[:1])

# Boring but effective
for item in names:
if is_big_name(item):
self.assertEqual(__, item)
self.assertEqual('Clarence', item)
break

# ------------------------------------------------------------------
Expand All @@ -75,28 +75,29 @@ def multiply(self, accum, item):

def test_reduce_will_blow_your_mind(self):
result = reduce(self.add, [2, 3, 4])
self.assertEqual(__, result)
self.assertEqual(9, result)

result2 = reduce(self.multiply, [2, 3, 4], 1)
self.assertEqual(__, result2)
self.assertEqual(24, result2)

# Extra Credit:
# Describe in your own words what reduce does.


# Apply the function to every member of the list, the final return is what the final iteration of the function outputs
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, but it's a bit more specific than that, right? how does it differ from map?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The way in which reduce works and differs from is that it receives a function and a sequence(just as map) but it returns just a single value.

The way that this value is calculated is by taking the first two members of the sequence and then executing the function, after that, the result of that first execution is used with the next value of the sequence, this process gets repeated until the last member of the sequence is taken to account.

# ------------------------------------------------------------------

def test_use_pass_for_iterations_with_no_body(self):
for num in range(1, 5):
pass

self.assertEqual(__, num)
self.assertEqual(4, num)

# ------------------------------------------------------------------

def test_all_iteration_methods_work_on_any_sequence_not_just_lists(self):
# Ranges are an iterable sequence
result = map(self.add_ten, range(1, 4))
self.assertEqual(__, list(result))
self.assertEqual([11, 12, 13], list(result))

try:
f = open("example_file.txt")
Expand All @@ -105,7 +106,7 @@ def test_all_iteration_methods_work_on_any_sequence_not_just_lists(self):
def make_upcase(line):
return line.strip().upper()
upcase_lines = map(make_upcase, f.readlines())
self.assertEqual(__, list(upcase_lines))
self.assertEqual(['THIS', 'IS', 'A', 'TEST'], list(upcase_lines))
finally:
# Arg, this is ugly.
# We will figure out how to fix this later.
Expand Down