Skip to content

Commit 1964c64

Browse files
committed
ex 28, 29. 30, 32, 34, 36, 37 changed
1 parent dd62b9a commit 1964c64

File tree

14 files changed

+147
-153
lines changed

14 files changed

+147
-153
lines changed
Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
lines = []
2-
while True:
3-
s = input("")
4-
if s:
5-
lines.append(s.upper())
6-
else:
7-
break;
1+
def lines(text):
2+
return text.upper()
83

9-
for sentence in lines:
10-
print (sentence)
4+
print(lines('Hello world'))
Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +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 solution should return the expected output')
5-
def test_input_output(capsys, app):
4+
@pytest.mark.it('The function lines must exist')
5+
def test_function_existence(capsys, app):
6+
assert app.lines
67

7-
fake_input = ["Hello world Practice makes perfect"]
8-
with mock.patch('builtins.input', lambda x: fake_input.pop(0)):
9-
app()
10-
captured = capsys.readouterr()
11-
assert captured.out == "HELLO WORLD PRACTICE MAKES PERFECT\n"
8+
@pytest.mark.it('The function should return the expected output')
9+
def test_expected_output(capsys, app):
10+
assert app.lines("hello world") == "HELLO WORLD"
11+
12+
@pytest.mark.it('The function should return the expected output')
13+
def test_another_output(capsys, app):
14+
assert app.lines("LeT the WOrld know YoU") == "LET THE WORLD KNOW YOU"
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
s = input("")
2-
words = [word for word in s.split(" ")]
3-
print (" ".join(sorted(list(set(words)))))
1+
def remove_duplicate_words(text):
2+
words = text.split()
3+
return (" ".join(sorted(list(set(words)))))
Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,18 @@
11
import pytest, io, sys, json, mock, re, os
22

3-
@pytest.mark.it('Your solution should work as expected')
3+
@pytest.mark.it('The function remove_duplicate_words must exist')
4+
def test_function_existence(capsys, app):
5+
app.remove_duplicate_words
6+
7+
@pytest.mark.it('The function should return the expected output')
48
def test_expected_output(capsys, app):
5-
fake_input=['Hola como Hola']
6-
with mock.patch('builtins.input', lambda x: fake_input.pop()):
7-
app()
8-
captured = capsys.readouterr()
9-
assert captured.out == "Hola como\n"
9+
assert app.remove_duplicate_words("hello world and practice makes perfect and hello world again") == "again and hello makes perfect practice world"
1010

11-
@pytest.mark.it('Your solution should work as expected')
11+
@pytest.mark.it('The function should work with other entries')
1212
def test_expected_output_2(capsys, app):
13-
fake_input=['ayer como ayer es hoy']
14-
with mock.patch('builtins.input', lambda x: fake_input.pop()):
15-
app()
16-
captured = capsys.readouterr()
17-
assert captured.out == "ayer como es hoy\n"
13+
assert app.remove_duplicate_words("lets try this again with another try") == "again another lets this try with"
1814

15+
@pytest.mark.it('The function should work with other entries')
16+
def test_expected_output_3(capsys, app):
17+
assert app.remove_duplicate_words("Jacke was Named Jacke by his mother") == "Jacke Named by his mother was"
1918

20-
@pytest.mark.it('Your solution should work as expected')
21-
def test_expected_output_no_repetead(capsys, app):
22-
fake_input=['Hola como estas']
23-
with mock.patch('builtins.input', lambda x: fake_input.pop()):
24-
app()
25-
captured = capsys.readouterr()
26-
assert captured.out == "Hola como estas\n"
Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
value = []
2-
items=[x for x in input("").split(',')]
3-
for p in items:
4-
intp = int(p, 2)
5-
if not intp%5:
6-
value.append(p)
1+
def divisable_binary(text):
2+
value=[]
3+
items=[x for x in text.split(',')]
4+
for p in items:
5+
intp = int(p, 2)
6+
if not intp%5:
7+
value.append(p)
78

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

exercises/30-divisable-binary/test.py

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

3-
@pytest.mark.it('Your solution should work as expected')
3+
@pytest.mark.it('The function divisable_binary must exist')
4+
def test_function_existence(capsys, app):
5+
assert app.divisable_binary
6+
7+
@pytest.mark.it('The function should return the expected output')
48
def test_expected_output(capsys, app):
5-
fake_input=['0100,0011,1010,1001']
6-
with mock.patch('builtins.input', lambda x: fake_input.pop()):
7-
app()
8-
captured = capsys.readouterr()
9-
assert captured.out == "1010\n"
9+
assert app.divisable_binary("0100,0011,1010,1001") == "1010"
1010

11-
@pytest.mark.it('Your solution should work with other parameters')
11+
@pytest.mark.it('The function should work with other parameters. testing with 1111,1000,0101,0000')
1212
def test_expected_output_2(capsys, app):
13-
fake_input=['1111,1000,0101,0000']
14-
with mock.patch('builtins.input', lambda x: fake_input.pop()):
15-
app()
16-
captured = capsys.readouterr()
17-
assert captured.out == "1111,0101,0000\n"
13+
assert app.divisable_binary("1111,1000,0101,0000") == "1111,0101,0000"
14+
15+
@pytest.mark.it("The function should work with other parameters. Testing with 1000")
16+
def test_expected_output_3(capsys, app):
17+
assert app.divisable_binary("1000,1000,1000,1000") == ""
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
s = input("")
2-
d={"DIGITS":0, "LETTERS":0}
3-
for c in s:
4-
if c.isdigit():
5-
d["DIGITS"]+=1
6-
elif c.isalpha():
7-
d["LETTERS"]+=1
8-
else:
9-
pass
10-
print ("LETTERS", d["LETTERS"])
11-
print ("DIGITS", d["DIGITS"])
1+
def letters_and_digits(text):
2+
d={"DIGITS":0, "LETTERS":0}
3+
for c in text:
4+
if c.isdigit():
5+
d["DIGITS"]+=1
6+
elif c.isalpha():
7+
d["LETTERS"]+=1
8+
else:
9+
pass
10+
11+
return ("LETTERS {} DIGITS {}".format(d['LETTERS'], d['DIGITS']))
Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
import mock, pytest
1+
import mock, pytest, io, sys
22

3-
@pytest.mark.it('The solution should return the expected output')
3+
4+
@pytest.mark.it('The function letters_and_digits must exist')
5+
def test_function_existence(capsys, app):
6+
assert app.letters_and_digits
7+
8+
9+
@pytest.mark.it('The function should return the expected output')
410
def test_output(capsys, app):
5-
fake_input = ["hello world! 123"] #fake input
6-
with mock.patch('builtins.input', lambda x: fake_input.pop()):
7-
app()
8-
captured = capsys.readouterr()
9-
assert captured.out != "LETTERS 10 DIGITS 3\n"
11+
app.letters_and_digits("hello world! 123") == "LETTERS 10 DIGITS 3"
1012

11-
@pytest.mark.it('The solution should work with others inputs')
12-
def test_output_2(capsys, app):
13-
fake_input = ["How are you 11111"] #fake input
14-
with mock.patch('builtins.input', lambda x: fake_input.pop()):
15-
app()
16-
captured = capsys.readouterr()
17-
assert captured.out != "LETTERS 9 DIGITS 5\n"
13+
@pytest.mark.it('The solution should work with other parameters')
14+
def test_another_entry(capsys, app):
15+
assert app.letters_and_digits("My name is C200 and i live in apartment 3H") == "LETTERS 29 DIGITS 4"
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
a = input("")
2-
n1 = int( "%s" % a )
3-
n2 = int( "%s%s" % (a,a) )
4-
n3 = int( "%s%s%s" % (a,a,a) )
5-
n4 = int( "%s%s%s%s" % (a,a,a,a) )
6-
print (n1+n2+n3+n4)
1+
def computed_value(param):
2+
n1 = int( "%s" % param )
3+
n2 = int( "%s%s" % (param,param) )
4+
n3 = int( "%s%s%s" % (param,param,param) )
5+
n4 = int( "%s%s%s%s" % (param,param,param,param) )
6+
return (n1+n2+n3+n4)

exercises/34-a_aa_aaa_aaaa/test.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
import io, sys, pytest, os, re, mock
22

3-
@pytest.mark.it('The solution should return the expected output')
4-
def test_convert_inputs(capsys, app):
5-
6-
fake_input = ["9"] #fake input
7-
with mock.patch('builtins.input', lambda x: fake_input.pop()):
8-
app()
9-
captured = capsys.readouterr()
10-
assert captured.out == "11106\n"
3+
@pytest.mark.it('The function computed_value must exist')
4+
def test_function_existence(capsys, app):
5+
assert app.computed_value
6+
7+
@pytest.mark.it('The function should return the expected output')
8+
def test_expected_output(capsys, app):
9+
assert app.computed_value(9) == 11106
10+
11+
@pytest.mark.it('The function should work with other enties. Testing with 123')
12+
def test_another_output(capsys, app):
13+
assert app.computed_value(123) == 123246369492
14+
15+
@pytest.mark.it('The function should work with other enties. Testing with 0')
16+
def test_with_zero(capsys, app):
17+
assert app.computed_value(0) == 0
Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
1-
netAmount = 0
2-
while True:
3-
s = input("")
4-
if not s:
5-
break
6-
values = s.split(" ")
7-
operation = values[0]
8-
amount = int(values[1])
9-
if operation=="D":
10-
netAmount+=amount
11-
elif operation=="W":
12-
netAmount-=amount
13-
else:
14-
pass
15-
print (netAmount)
1+
def net_amount(param):
2+
netAmount = 0
3+
values = param.split()
4+
for x in range(len(values)):
5+
if values[x] == 'D':
6+
netAmount+=int(values[x+1])
7+
elif values[x] == 'W':
8+
netAmount-=int(values[x+1])
9+
return netAmount

exercises/36-net-amount/test.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
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 function net_amount must exist')
5+
def test_function_existence(capsys, app):
6+
assert app.net_amount
7+
8+
@pytest.mark.it('The function net_amount must exist')
9+
def test_function_existence(capsys, app):
10+
assert app.net_amount
11+
412
@pytest.mark.it('The solution should return the expected output')
513
def test_output(capsys, app):
6-
fake_input = ["D 300 D 300 W 200 D 100"] #fake input
7-
with mock.patch('builtins.input', lambda x: fake_input.pop()):
8-
app()
9-
captured = capsys.readouterr()
10-
assert captured.out == "500\n"
14+
assert app.net_amount("D 300 D 300 W 200 D 100") == 500
15+
16+
@pytest.mark.it('The solution should work with other parameters. Testing with "W 300 D 300 W 200 D 300"')
17+
def test_output_2(capsys, app):
18+
assert app.net_amount("W 300 D 300 W 200 D 300") == 100
1119

20+
@pytest.mark.it('The solution should work with other parameters. Testing with "W 300 D 300 W 200 D 300"')
21+
def test_output_negative(capsys, app):
22+
assert app.net_amount("W 300 D 300 W 200 W 300") == -500
Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
import re
2-
value = []
3-
items=[x for x in input("").split(',')]
4-
for p in items:
5-
if len(p)<6 or len(p)>12:
6-
continue
7-
else:
8-
pass
9-
if not re.search("[a-z]",p):
10-
continue
11-
elif not re.search("[0-9]",p):
12-
continue
13-
elif not re.search("[A-Z]",p):
14-
continue
15-
elif not re.search("[$#@]",p):
16-
continue
17-
elif re.search("\s",p):
18-
continue
19-
else:
20-
pass
21-
value.append(p)
22-
print (",".join(value))
2+
def valid_password(password):
3+
value = []
4+
items=[x for x in password.split(',')]
5+
for p in items:
6+
if len(p)<6 or len(p)>12:
7+
continue
8+
else:
9+
pass
10+
if not re.search("[a-z]",p):
11+
continue
12+
elif not re.search("[0-9]",p):
13+
continue
14+
elif not re.search("[A-Z]",p):
15+
continue
16+
elif not re.search("[$#@]",p):
17+
continue
18+
elif re.search("\s",p):
19+
continue
20+
else:
21+
pass
22+
value.append(p)
23+
return (",".join(value))
Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,19 @@
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('Your solution should work as expected')
4+
5+
@pytest.mark.it('The function valid_password must exist')
6+
def test_function_existence(capsys, app):
7+
assert app.valid_password
8+
9+
@pytest.mark.it('The function should return the expected output')
510
def test_expected_output(capsys, app):
6-
fake_input=['ABd1234@1,a F1#,2w3E*,2We3345']
7-
with mock.patch('builtins.input', lambda x: fake_input.pop()):
8-
app()
9-
captured = capsys.readouterr()
10-
assert captured.out == "ABd1234@1\n"
11+
assert app.valid_password("ABd1234@1,a F1#,2w3E*,2We3345") == "ABd1234@1"
1112

1213
@pytest.mark.it('Your solution should work as expected for valid passwords')
1314
def test_expected_another_output(capsys, app):
14-
fake_input=['Lmd4567@2,a F1#,2w3E*,2We3345']
15-
with mock.patch('builtins.input', lambda x: fake_input.pop()):
16-
app()
17-
captured = capsys.readouterr()
18-
assert captured.out == "Lmd4567@2\n"
15+
assert app.valid_password("Lmd4567@2,a F1#,2w3E*,2We3345") == "Lmd4567@2"
1916

2017
@pytest.mark.it('Your solution should work as expected when there is no valid password input')
2118
def test_expected_output_no_valid_entries(capsys, app):
22-
fake_input=['ABd12,a F1#,2w3E*,2We3345']
23-
with mock.patch('builtins.input', lambda x: fake_input.pop()):
24-
app()
25-
captured = capsys.readouterr()
26-
assert captured.out == "\n"
19+
assert app.valid_password("ABd12,a F1#,2w3E*,2We3345") == ""

0 commit comments

Comments
 (0)