-
Notifications
You must be signed in to change notification settings - Fork 0
Completed the about_iteration koan #17
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
base: devel
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,20 +14,20 @@ def test_iterators_are_a_type(self): | |
for num in it: | ||
fib += num | ||
|
||
self.assertEqual(__, fib) | ||
self.assertEqual(15, fib) | ||
|
||
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) | ||
|
||
# ------------------------------------------------------------------ | ||
|
||
|
@@ -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): | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In your words, what's the difference between There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
def test_just_return_first_item_found(self): | ||
def is_big_name(item): | ||
|
@@ -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 | ||
|
||
# ------------------------------------------------------------------ | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The way in which The way that this value is calculated is by taking the first two members of the |
||
# ------------------------------------------------------------------ | ||
|
||
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") | ||
|
@@ -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. | ||
|
There was a problem hiding this comment.
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.