Skip to content

Commit dd62b9a

Browse files
committed
ex 03, 04, 22, 25, 26, 27 changed
1 parent df75263 commit dd62b9a

File tree

10 files changed

+85
-98
lines changed

10 files changed

+85
-98
lines changed

exercises/03-sum_of_three_numbers/test.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,32 @@ def test_use_print():
88
regex = re.compile(r"print(\s)*\(")
99
assert bool(regex.search(content)) == True
1010

11-
@pytest.mark.it('Almost there! But you need to convert the incoming input from string to integer')
12-
def test_convert_inputs(capsys, app):
11+
@pytest.mark.it('The solution should return the expected output. Tested with 2, 3, 6')
12+
def test_sum_expected_inputs(capsys, app):
13+
14+
fake_input = ["2","3","6"] #fake input
15+
with mock.patch('builtins.input', lambda x: fake_input.pop()):
16+
app()
17+
captured = capsys.readouterr()
18+
assert captured.out == "11\n"
19+
20+
@pytest.mark.it('The solution must sum all entries. Tested with 0, 3, 7')
21+
def test_sum_another_inputs(capsys, app):
1322

1423
fake_input = ["0","3","7"] #fake input
1524
with mock.patch('builtins.input', lambda x: fake_input.pop()):
1625
app()
1726
captured = capsys.readouterr()
1827
assert captured.out == "10\n"
1928

20-
@pytest.mark.it('Sum all three input numbers and print on the console the result')
21-
def test_add_variables(capsys, app):
29+
@pytest.mark.it('The solution must sum all entries. Tested with 0, 0, 0')
30+
def test_sum_with_zero(capsys, app):
2231

23-
fake_input = ["2","3","4"] #fake input
32+
fake_input = ["0","0","0"] #fake input
2433
with mock.patch('builtins.input', lambda x: fake_input.pop()):
2534
app()
2635
captured = capsys.readouterr()
27-
assert captured.out == "9\n"
36+
assert captured.out == "0\n"
2837

2938

3039

exercises/04-area_of_right_triangle/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ def area_of_triangle(arg1, arg2):
44
return None
55

66
# Testing your function
7-
print(area_of_triangle(4, 5))
7+
print(area_of_triangle(3, 5))
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
n=int(input("Insert a number"))
2-
d=dict()
3-
for i in range(1,n+1):
4-
d[i]=i*i
1+
def generate_dict(n):
2+
d=dict()
3+
for i in range(1,n+1):
4+
d[i]=i*i
5+
return d
56

6-
print (d)
7+
print(generate_dict(n))

exercises/22-Integral/test.py

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
11
import pytest,os,re,io,sys, mock, json
22

3+
@pytest.mark.it('The function generate_dict should exist')
4+
def test_function_existence(capsys, app):
5+
assert app.generate_dict
36

4-
@pytest.mark.it('The solution should return the expected output')
5-
def test_convert_inputs(capsys, app):
7+
@pytest.mark.it('The function should return a dictionary')
8+
def test_typeof_return(capsys, app):
9+
assert type(app.generate_dict(7)) == type({})
610

7-
fake_input = ["8"] #fake input
8-
with mock.patch('builtins.input', lambda x: fake_input.pop()):
9-
app()
10-
captured = capsys.readouterr()
11-
assert captured.out == "{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64}\n"
11+
@pytest.mark.it('The function should return the expected output')
12+
def test_expected_output(capsys, app):
13+
assert app.generate_dict(8) == {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64}
1214

13-
14-
@pytest.mark.it('The solution should work with other entries')
15-
def test_convert_inputs_2(capsys, app):
16-
17-
fake_input = ["5"] #fake input
18-
with mock.patch('builtins.input', lambda x: fake_input.pop()):
19-
app()
20-
captured = capsys.readouterr()
21-
assert captured.out == "{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}\n"
15+
@pytest.mark.it('The function should work with other entries')
16+
def test_another_entry_5(capsys, app):
17+
assert app.generate_dict(5) == {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
2218

2319

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
#!/usr/bin/env python
22
import math
3-
c=50
4-
h=30
5-
value = []
6-
items=[x for x in input("").split(',')]
7-
for d in items:
8-
value.append(str(int(round(math.sqrt(2*c*float(d)/h)))))
3+
def print_formula(n1,n2,n3):
4+
value = []
5+
items=[x for x in "{},{},{}".format(n1,n2,n3).split(',')]
6+
for d in items:
7+
value.append(str(int(round(math.sqrt(2*50*float(d)/30)))))
98

10-
print (','.join(value))
9+
return (','.join(value))

exercises/25-print-formula/test.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
11
import pytest, io, sys, json, mock, re, os
22
path = os.path.dirname(os.path.abspath(__file__))+'/app.py'
33

4-
@pytest.mark.it('The solution should work as expected')
5-
def test_convert_inputs(capsys, app):
4+
@pytest.mark.it('The function print_formula must exist')
5+
def test_function_existence(capsys, app):
6+
assert app.print_formula
67

7-
fake_input = ["100,150,180"] #fake input
8-
with mock.patch('builtins.input', lambda x: fake_input.pop()):
9-
app()
10-
captured = capsys.readouterr()
11-
assert captured.out == "18,22,24\n"
8+
@pytest.mark.it('The solution should work as expected')
9+
def test_expected_output(capsys, app):
10+
assert app.print_formula(100,150,180) == "18,22,24"
1211

13-
@pytest.mark.it('The solution should work with other parameters')
14-
def test_convert_inputs_2(capsys, app):
12+
@pytest.mark.it('The solution should work as expected')
13+
def test_another_output(capsys, app):
14+
assert app.print_formula(200,90,300) == "26,17,32"
1515

16-
fake_input = ["200,90,300"] #fake input
17-
with mock.patch('builtins.input', lambda x: fake_input.pop()):
18-
app()
19-
captured = capsys.readouterr()
20-
assert captured.out == "26,17,32\n"
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
input_str = input("")
2-
dimensions=[int(x) for x in input_str.split(',')]
3-
rowNum=dimensions[0]
4-
colNum=dimensions[1]
5-
multilist = [[0 for col in range(colNum)] for row in range(rowNum)]
1+
def two_dimensional_array(nList, nElements):
2+
dimensions=[int(x) for x in "{},{}".format(nList,nElements).split(',')]
3+
rowNum=dimensions[0]
4+
colNum=dimensions[1]
5+
multilist = [[0 for col in range(colNum)] for row in range(rowNum)]
66

7-
for row in range(rowNum):
8-
for col in range(colNum):
9-
multilist[row][col]= row*col
7+
for row in range(rowNum):
8+
for col in range(colNum):
9+
multilist[row][col]= row*col
1010

11-
print (multilist)
11+
return (multilist)
Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,18 @@
11

22
import pytest,os,re,io,sys, mock, json
33

4-
@pytest.mark.it('The solution must return the expected value for an input like "3,5"')
5-
def test_for_output_3_5(capsys, app):
6-
fake_input = ["3,5"]
7-
with mock.patch('builtins.input', lambda x: fake_input.pop()):
8-
app()
9-
captured = capsys.readouterr()
10-
assert captured.out == "[[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]]\n"
4+
@pytest.mark.it('The function two_dimensional_array must exist')
5+
def test_function_existence(capsys, app):
6+
assert app.two_dimensional_array
117

12-
13-
14-
@pytest.mark.it('Your solution should work with others input values, testing with: 2,7')
15-
def test_for_output_2_7(capsys, app):
16-
fake_input = ["2,7"]
17-
with mock.patch('builtins.input', lambda x: fake_input.pop()):
18-
app()
19-
captured = capsys.readouterr()
20-
assert captured.out == "[[0, 0, 0, 0, 0, 0, 0], [0, 1, 2, 3, 4, 5, 6]]\n"
8+
@pytest.mark.it('The function should return the expected output.')
9+
def test_expected_output(capsys, app):
10+
assert app.two_dimensional_array(3,5) == [[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]]
2111

22-
@pytest.mark.it('The input must be two numbers separate by comma, like this: 7,8')
23-
def test_input(capsys, app):
24-
fake_input = ["2,7"]
25-
with mock.patch('builtins.input', lambda x: fake_input.pop()):
26-
app()
27-
captured = capsys.readouterr()
28-
assert captured.out == "[[0, 0, 0, 0, 0, 0, 0], [0, 1, 2, 3, 4, 5, 6]]\n"
12+
@pytest.mark.it('The function should work with other entries. Testing with 2,7')
13+
def test_expected_output(capsys, app):
14+
assert app.two_dimensional_array(2,7) == [[0, 0, 0, 0, 0, 0, 0], [0, 1, 2, 3, 4, 5, 6]]
15+
16+
@pytest.mark.it('The function should work with other entries. Testing with 2,7')
17+
def test_expected_output(capsys, app):
18+
assert app.two_dimensional_array(1,10) == [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
items=[x for x in input("").split(',')]
2-
items.sort()
3-
print (','.join(items))
1+
def sequence_of_words(words):
2+
items=[x for x in "{}".format(words).split(',')]
3+
items.sort()
4+
return (','.join(items))
Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
import pytest, io, sys, json, mock, re, os
22
path = os.path.dirname(os.path.abspath(__file__))+'/app.py'
33

4-
@pytest.mark.it('The output should be the expected')
5-
def test_output(capsys, app):
6-
fake_input = ["without,hello,bag,world"] #fake input
7-
with mock.patch('builtins.input', lambda x: fake_input.pop()):
8-
app()
9-
captured = capsys.readouterr()
10-
assert captured.out == "bag,hello,without,world\n"
4+
@pytest.mark.it('The function sequence_of_words must exist')
5+
def test_function_existence(capsys, app):
6+
assert app.sequence_of_words
117

12-
@pytest.mark.it('Your solution should work with other entries')
13-
def test_other_inputs(capsys, app):
14-
fake_input = ["hola,como,estas"] #fake input
15-
with mock.patch('builtins.input', lambda x: fake_input.pop()):
16-
app()
17-
captured = capsys.readouterr()
18-
assert captured.out == "como,estas,hola\n"
8+
@pytest.mark.it('The function should return the expected output')
9+
def test_function_existence(capsys, app):
10+
assert app.sequence_of_words("bag,hello,without,world") == "bag,hello,without,world"
11+
12+
@pytest.mark.it('The function should return the expected output')
13+
def test_function_existence(capsys, app):
14+
assert app.sequence_of_words("No,pain,gain") == "No,gain,pain"

0 commit comments

Comments
 (0)