Skip to content

Commit 860e432

Browse files
committed
ex 38, 40
1 parent 1964c64 commit 860e432

File tree

5 files changed

+48
-64
lines changed

5 files changed

+48
-64
lines changed
Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
from operator import itemgetter, attrgetter
2-
3-
l = []
4-
while True:
5-
s = input("")
6-
if not s:
7-
break
8-
l.append(tuple(s.split(",")))
9-
10-
print (sorted(l, key=itemgetter(0,1,2)))
2+
def sort_tuples_ascending(tuples):
3+
l = []
4+
l.append(tuple(tuples.split(",")))
5+
return (sorted(l, key=itemgetter(0,1,2)))

exercises/38-sort-tuples-ascending/test.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22

33
path = os.path.dirname(os.path.abspath(__file__))+'/app.py'
44

5-
@pytest.mark.it('The solution should return the expected output')
6-
def test_convert_inputs(capsys, app):
7-
8-
fake_input = ["Tom,19,80 John,20,90 Jony,17,91 Jony,17,93 Json,21,85"] #fake input
9-
with mock.patch('builtins.input', lambda x: fake_input.pop()):
10-
app()
11-
captured = capsys.readouterr()
12-
assert captured.out == "[('John', '20', '90'), ('Jony', '17', '91'), ('Jony', '17', '93'), ('Json', '21', '85'), ('Tom', '19', '80')]\n"
5+
6+
@pytest.mark.it('The function sort_tuples_ascending must exist')
7+
def test_function_existence(capsys, app):
8+
assert app.sort_tuples_ascending
9+
10+
11+
@pytest.mark.it('The function should return the expected output')
12+
def etest_expected_output(capsys, app):
13+
app.sort_tuples_ascending("Tom,19,80 John,20,90 Jony,17,91 Jony,17,93 Json,21,85") == [('John', '20', '90'), ('Jony', '17', '91'), ('Jony', '17', '93'), ('Json', '21', '85'), ('Tom', '19', '80')]
14+
15+
@pytest.mark.it('The solution should work with other entries')
16+
def test_another_entry(capsys, app):
17+
app.sort_tuples_ascending("Martin,23,30 Tomas,25,27") == [('Martin', '23', '30', 'Tomas', '25', '27')]
Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +0,0 @@
1-
import math
2-
pos = [0,0]
3-
while True:
4-
s = input("")
5-
if not s:
6-
break
7-
movement = s.split(" ")
8-
direction = movement[0]
9-
steps = int(movement[1])
10-
if direction=="UP":
11-
pos[0]+=steps
12-
elif direction=="DOWN":
13-
pos[0]-=steps
14-
elif direction=="LEFT":
15-
pos[1]-=steps
16-
elif direction=="RIGHT":
17-
pos[1]+=steps
18-
else:
19-
pass
20-
21-
print (int(round(math.sqrt(pos[1]**2+pos[0]**2))))
Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
import math
2-
pos = [0,0]
3-
while True:
4-
s = input("")
5-
if not s:
6-
break
7-
movement = s.split(" ")
8-
direction = movement[0]
9-
steps = int(movement[1])
10-
if direction=="UP":
11-
pos[0]+=steps
12-
elif direction=="DOWN":
13-
pos[0]-=steps
14-
elif direction=="LEFT":
15-
pos[1]-=steps
16-
elif direction=="RIGHT":
17-
pos[1]+=steps
18-
else:
19-
pass
20-
21-
print (int(round(math.sqrt(pos[1]**2+pos[0]**2))))
2+
def compute_robot_distance(props):
3+
pos = [0,0]
4+
new_prop = props.split(" ")
5+
for x in range(len(new_prop)):
6+
if new_prop[x].upper() == 'UP':
7+
pos[0]+=int(new_prop[x+1])
8+
elif new_prop[x].upper() == 'DOWN':
9+
pos[0]-=int(new_prop[x+1])
10+
elif new_prop[x].upper() == 'LEFT':
11+
pos[1]-=int(new_prop[x+1])
12+
elif new_prop[x].upper() == 'RIGHT':
13+
pos[1]+=int(new_prop[x+1])
14+
else:
15+
None
16+
return (int(round(math.sqrt(pos[1]**2+pos[0]**2))))
17+
Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
import pytest, io, sys, json, mock, re, os
22

3-
@pytest.mark.it('Your solution should work as expected')
3+
4+
@pytest.mark.it('The function compute_robot_distance must exist')
5+
def test_function_existence(capsys, app):
6+
assert app.compute_robot_distance
7+
8+
9+
@pytest.mark.it('The function should return the expected output')
410
def test_expected_output(capsys, app):
5-
fake_input=['UP 5 DOWN 3 LEFT 3 RIGHT 2']
6-
with mock.patch('builtins.input', lambda x: fake_input.pop()):
7-
app()
8-
captured = capsys.readouterr()
9-
assert captured.out == "2\n"
11+
assert app.compute_robot_distance("UP 5 DOWN 3 LEFT 3 RIGHT 2") == 2
12+
13+
@pytest.mark.it('The solution should work with other entries')
14+
def test_another_output(capsys, app):
15+
assert app.compute_robot_distance("DOWN 20 UP 5 LEFT 5 RIGHT 2") == 15
1016

17+
@pytest.mark.it('The solution should work with other entries')
18+
def test_negative_inputs(capsys, app):
19+
assert app.compute_robot_distance("DOWN -1 UP -5 LEFT 50 RIGHT 20") == 30

0 commit comments

Comments
 (0)