Skip to content

Commit 6360ee3

Browse files
committed
triangle, iteration
1 parent c0f3e8a commit 6360ee3

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

python3/koans/about_iteration.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,20 @@ def test_iterators_are_a_type(self):
1313
for num in it:
1414
total += num
1515

16-
self.assertEqual(__ , total)
16+
self.assertEqual(15 , total)
1717

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

2121
try:
22-
self.assertEqual(__, next(stages))
22+
self.assertEqual('alpha', next(stages))
2323
next(stages)
24-
self.assertEqual(__, next(stages))
24+
self.assertEqual('gamma', next(stages))
2525
next(stages)
2626
except StopIteration as ex:
2727
err_msg = 'Ran out of iterations'
2828

29-
self.assertRegex(err_msg, __)
29+
self.assertRegex(err_msg, "Ran out of iterations")
3030

3131
# ------------------------------------------------------------------
3232

@@ -40,14 +40,14 @@ def test_map_transforms_elements_of_a_list(self):
4040
mapping = map(self.add_ten, seq)
4141

4242
self.assertNotEqual(list, mapping.__class__)
43-
self.assertEqual(__, mapping.__class__)
43+
self.assertEqual(map, mapping.__class__)
4444
# In Python 3 built in iterator funcs return iterable view objects
4545
# instead of lists
4646

4747
for item in mapping:
4848
mapped_seq.append(item)
4949

50-
self.assertEqual(__, mapped_seq)
50+
self.assertEqual([11,12,13], mapped_seq)
5151

5252
# Note, iterator methods actually return objects of iter type in
5353
# python 3. In python 2 map() would give you a list.
@@ -62,7 +62,7 @@ def is_even(item):
6262
for item in filter(is_even, seq):
6363
even_numbers.append(item)
6464

65-
self.assertEqual(__, even_numbers)
65+
self.assertEqual([2,4,6], even_numbers)
6666

6767
def test_just_return_first_item_found(self):
6868
def is_big_name(item):
@@ -77,7 +77,7 @@ def is_big_name(item):
7777
except StopIteration:
7878
msg = 'Ran out of big names'
7979

80-
self.assertEqual(__, name)
80+
self.assertEqual('Clarence', name)
8181

8282

8383
# ------------------------------------------------------------------
@@ -94,13 +94,13 @@ def test_reduce_will_blow_your_mind(self):
9494
# to the functools module.
9595

9696
result = functools.reduce(self.add, [2, 3, 4])
97-
self.assertEqual(__, result.__class__)
97+
self.assertEqual(int, result.__class__)
9898
# Reduce() syntax is same as Python 2
9999

100-
self.assertEqual(__, result)
100+
self.assertEqual(9, result)
101101

102102
result2 = functools.reduce(self.multiply, [2, 3, 4], 1)
103-
self.assertEqual(__, result2)
103+
self.assertEqual(24, result2)
104104

105105
# Extra Credit:
106106
# Describe in your own words what reduce does.
@@ -111,14 +111,14 @@ def test_use_pass_for_iterations_with_no_body(self):
111111
for num in range(1,5):
112112
pass
113113

114-
self.assertEqual(__, num)
114+
self.assertEqual(4, num)
115115

116116
# ------------------------------------------------------------------
117117

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

123123
try:
124124
file = open("example_file.txt")
@@ -127,7 +127,7 @@ def test_all_iteration_methods_work_on_any_sequence_not_just_lists(self):
127127
def make_upcase(line):
128128
return line.strip().upper()
129129
upcase_lines = map(make_upcase, file.readlines())
130-
self.assertEqual(__, list(upcase_lines))
130+
self.assertEqual(['THIS', 'IS', 'A', 'TEST'], list(upcase_lines))
131131
finally:
132132
# Arg, this is ugly.
133133
# We will figure out how to fix this later.

python3/koans/triangle.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,17 @@ def is_positive(a):
2121
return False
2222
return True
2323

24+
def is_triangle_possible(a,b,c):
25+
if (a >= b + c) or (b >= a + c) or (c >= a + b):
26+
return False
27+
return True
28+
2429

2530
def triangle(a, b, c):
26-
# DELETE 'PASS' AND WRITE THIS CODE
2731
if (not is_positive(a) or not is_positive(b) or not is_positive(c)):
2832
raise TriangleError
33+
if not is_triangle_possible(a, b, c):
34+
raise TriangleError
2935
# if not is_positive(a):
3036
# raise TriangleError
3137
# if not is_positive(b):

0 commit comments

Comments
 (0)