Skip to content

Commit aab7042

Browse files
committed
ex 011 to 020
1 parent 66f2361 commit aab7042

File tree

17 files changed

+155
-17
lines changed

17 files changed

+155
-17
lines changed

exercises/013-sum_of_digits/test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ def test_for_functon_existence(capsys, app):
66

77
@pytest.mark.it('The function must return something')
88
def test_for_return(capsys, app):
9-
assert app.digits_sum(123) == None
9+
assert app.digits_sum(123) != None
1010

1111
@pytest.mark.it('The function must return a number')
1212
def test_for_output_type(capsys, app):
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#Complete the function to return the first digit to the right of the decimal point.
2+
import math
3+
def first_digit(num):
4+
return int(str(math.floor(num*10)/10)[-1])
5+
6+
7+
8+
9+
#Invoke the function with a positive real number. ex. 34.33
10+
print(first_digit(2.6))

exercises/014-digit_after_decimal_point/test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ def test_for_functon_existence(capsys, app):
66

77
@pytest.mark.it('The function must return something')
88
def test_for_return(capsys, app):
9-
assert app.first_digit(1.2) == None
9+
assert app.first_digit(1.2) != None
1010

1111
@pytest.mark.it('The function must return a number')
1212
def test_for_output_type(capsys, app):

exercises/015-car_route/README.es.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Un vehículo puede cubrir una distancia de `N` kilómetros por día ¿Cuántos d
99
## Ejemplo de entrada:
1010

1111
```py
12-
car_route(700,750)
12+
car_route(20,40)
1313
```
1414

1515
## Ejemplo de salida:

exercises/015-car_route/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ A car can cover distance of `N` kilometers per day. How many days will it take t
99
## Example input:
1010

1111
```py
12-
car_route(700)
12+
car_route(20,40)
1313
```
1414

1515
## Example output:
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#Complete the function to return the amount of days it will take to cover a route.
2+
#HINT: You may need to import the math module for this exercise.
3+
import math
4+
def car_route(n,m):
5+
return int(math.ceil(m/n))
6+
7+
8+
#Invoke the function with two intergers.
9+
print(car_route())

exercises/015-car_route/test.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,21 @@ def test_import_random():
1212
def test_for_functon_existence(capsys, app):
1313
assert callable(app.car_route)
1414

15-
@pytest.mark.it('We tried to pass 659 and 1857 as parameter and it did not return 3!')
15+
@pytest.mark.it('The function mus return something')
16+
def test_function_return(capsys, app):
17+
assert app.car_route(659, 1857) != None
18+
19+
@pytest.mark.it('The function must return a number')
20+
def test_function_return_type(capsys, app):
21+
assert type(app.car_route(659, 1857)) == type(1)
22+
23+
@pytest.mark.it('We tried to pass 20 and 40 as parameter and it did not return 2!')
1624
def test_for_file_output(capsys, app):
17-
assert app.car_route(659, 1857) == math.ceil(1857 / 659)
25+
assert app.car_route(20, 40) == 2
26+
27+
@pytest.mark.it('We tried to pass 20 and 900 as parameter and it did not return 45!')
28+
def test_for_file_output2(capsys, app):
29+
assert app.car_route(20, 900) == 45
1830

1931

2032

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#Complete the function to return the respective number of the century
2+
#HINT: You may need to import the math module.
3+
import math
4+
def century(year):
5+
if year % 100 == 0:
6+
return math.floor(year/100)
7+
else:
8+
return math.floor(year/100 +1)
9+
10+
11+
12+
#Invoke the function with any given year.
13+
print(century(2022))

exercises/016-century/test.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,23 @@ def test_import_random():
1313
def test_for_functon_existence(capsys, app):
1414
assert callable(app.century)
1515

16-
@pytest.mark.it('We tried to pass 1801 as parameter and it did not return 19!')
16+
@pytest.mark.it('The function must return something')
17+
def test_function_return(capsys, app):
18+
assert app.century(19001) != None
19+
20+
@pytest.mark.it('The function must return a number')
21+
def test_function_return_type(capsys, app):
22+
assert type(app.century(19001)) == type(1)
23+
24+
@pytest.mark.it('We tried to pass 2000 as parameter and it did not return 20!')
1725
def test_for_file_output(capsys, app):
18-
if app.century(1801) % 100 == 0:
19-
assert app.century(1801) == math.floor(1801/100)
20-
else:
21-
assert app.century(1801) == math.floor(1801/100 +1)
26+
assert app.century(2000) == 20
27+
28+
@pytest.mark.it('We tried to pass 2000 as parameter and it did not return 21!')
29+
def test_for_file_output2(capsys, app):
30+
assert app.century(2001) == 21
31+
32+
@pytest.mark.it('We tried to pass 2000 as parameter and it did not return 21!')
33+
def test_for_file_output3(capsys, app):
34+
assert app.century(2101) == 22
2235

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#Complete the function to return the total cost in dollars and cents of N cupcakes.
2+
#Remember you can return multiple parameters => return a, b
3+
def total_cost(d,c,n):
4+
return ((((d*100)+c)*n)//100, (((d*100)+c)*n)%100)
5+
6+
7+
8+
9+
#Invoke the function with three intergers: cost(dollars and cents) & number of cupcakes.
10+
print(total_cost(15,22,4))

exercises/017-total_cost/test.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,24 @@
66
def test_for_functon_existence(capsys, app):
77
assert callable(app.total_cost)
88

9+
@pytest.mark.it('The function must return something')
10+
def test_function_return(capsys, app):
11+
assert app.total_cost(15, 22, 4) != None
12+
13+
@pytest.mark.it('The function must return a tuple')
14+
def test_function_return_type(capsys, app):
15+
assert type(app.total_cost(15, 22, 4)) == type((60, 88))
16+
17+
@pytest.mark.it('The function must return a tuple of numbers')
18+
def test_function_return_type_parameters(capsys, app):
19+
result = app.total_cost(15, 22, 4)
20+
assert type(result[0]) == type(1) and type(result[1]) == type(1)
21+
922
@pytest.mark.it('We tried to pass 15, 22, 4 as parameters and it did not return (60, 88)!')
1023
def test_for_file_output(capsys, app):
11-
12-
assert app.total_cost(15, 22, 4) == ((((15*100)+22)*4)//100, (((15*100)+22)*4)%100)
24+
assert app.total_cost(15, 22, 4) == (60, 88)
25+
26+
@pytest.mark.it('We tried to pass 10, 15, 4 as parameters and it did not return (20, 30)!')
27+
def test_for_file_output2(capsys, app):
28+
assert app.total_cost(15, 22, 4) == (60, 88)
1329

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#Complete the function to return the number of day of the week for k'th day of year.
2+
def day_of_week(k):
3+
4+
return (3+k)%7
5+
6+
7+
8+
#Invoke function day_of_week with an interger between 0 and 6 (number for days of week)
9+
print(day_of_week(1))

exercises/018-day_of_week/test.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,19 @@
66
def test_for_functon_existence(capsys, app):
77
assert callable(app.day_of_week)
88

9-
@pytest.mark.it('Something went wrong! We tried to pass 56 as parameter and it did not return 3!Keep trying!')
9+
@pytest.mark.it('The function must return something')
10+
def test_function_return(capsys, app):
11+
assert app.day_of_week(1) != None
12+
13+
@pytest.mark.it('The function must return a number')
14+
def test_function_return_type(capsys, app):
15+
assert type(app.day_of_week(1)) == type(1)
16+
17+
@pytest.mark.it('Something went wrong! We tried to pass 1 as parameter and it did not return 4! Keep trying!')
1018
def test_for_file_output(capsys, app):
19+
assert app.day_of_week(1) == 4
1120

12-
assert app.day_of_week(56) == (3+56)%7
21+
@pytest.mark.it('Something went wrong! We tried to pass 46 as parameter and it did not return 4! Keep trying!')
22+
def test_for_file_output2(capsys, app):
23+
assert app.day_of_week(46) == 0
1324

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#Complete the function to return how many hrs and min are displayed on the 24th digital clock.
2+
def digital_clock(n):
3+
return ((n // 60), (n % 60))
4+
5+
#Invoke the function with any interger (seconds after midnight)
6+
print(digital_clock(150))

exercises/019-digital_clock/test.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,19 @@
66
def test_for_functon_existence(capsys, app):
77
assert callable(app.digital_clock)
88

9+
@pytest.mark.it('The function must return something')
10+
def test_function_return(capsys, app):
11+
assert app.digital_clock(194) != None
12+
13+
@pytest.mark.it('The function must return a tuple')
14+
def test_function_return_type(capsys, app):
15+
assert type(app.digital_clock(194)) == type((3, 14))
16+
917
@pytest.mark.it('We tried to pass 194 as parameter and it did not return (3, 14)!Keep Trying!')
1018
def test_for_file_output(capsys, app):
19+
assert app.digital_clock(194) == (3, 14)
1120

12-
assert app.digital_clock(194) == ((194 // 60), (194 % 60))
21+
@pytest.mark.it('We tried to pass 150 as parameter and it did not return (2, 50)! Keep Trying!')
22+
def test_for_file_output(capsys, app):
23+
assert app.digital_clock(150) == (2,30)
1324

exercises/020-factorial/solution.hide.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,14 @@ def fact(x):
44
return x * fact(x - 1)
55

66
x=int(input())
7-
print fact(x)
7+
print (fact(x))
8+
9+
10+
# or
11+
12+
# Your code here
13+
import math
14+
def factorial(num):
15+
return math.factorial(num)
16+
17+
print(factorial(8))

exercises/020-factorial/test.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ def test_factorial_exists(app):
99
except AttributeError:
1010
raise AttributeError("The function 'factorial' should exist on app.py")
1111

12+
@pytest.mark.it('The function must return something')
13+
def test_function_return(capsys, app):
14+
assert app.factorial(8) != None
15+
16+
@pytest.mark.it('The function must return a tuple')
17+
def test_function_return_type(capsys, app):
18+
assert type(app.factorial(8)) == type(1)
19+
1220
@pytest.mark.it('Testing the function factorial with the number 8, it should return 40320')
1321
def test_factorial_8(app):
1422
try:

0 commit comments

Comments
 (0)