Skip to content

Commit a0a56a4

Browse files
authored
Merge pull request #56 from josemoracard/jose6-24-class-with-two-methods
exercises 24-class-with-two-methods to 27-sequence-of-words
2 parents 6c40b2f + 24ee78f commit a0a56a4

File tree

19 files changed

+207
-118
lines changed

19 files changed

+207
-118
lines changed
Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
# `24` Una clase con dos métodos
1+
# `24` Class with two methods
22

3-
Define una clase que tenga al menos dos métodos:
4-
Define a class which has at least two methods:
5-
getString: obtener un string desde la entrada de la consola.
6-
printString: imprimir el string en mayúscula.
7-
Por favor incluye una función simple de prueba para probar los métodos de la clase.
3+
## 📝 Instrucciones:
84

9-
Pistas:
10-
Usa el método __init__ para construir algunos parámetros.
5+
1. Define una clase llamada `InputOutString` que tenga al menos dos métodos:
6+
+ `get_string` para obtener un string de entrada desde la consola.
7+
+ `print_string` para imprimir el string obtenido en mayúsculas.
8+
9+
2. Prueba los métodos de tu clase.
10+
11+
## 💡 Pista:
12+
13+
+ Usa el método `__init__` para construir tus parámetros.
Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
# `24` Class with two methods
22

3-
Define a class which has at least two methods:
4-
getString: to get a string from console input
5-
printString: to print the string in upper case.
6-
Also please include simple test function to test the class methods.
3+
## 📝 Instructions:
74

8-
Hints:
9-
Use __init__ method to construct some parameters
5+
1. Define a class called `InputOutString` which has at least two methods:
6+
+ `get_string` to get a string from console input.
7+
+ `print_string` to print the string in upper case.
8+
9+
2. Test your class' methods.
10+
11+
## 💡 Hint:
12+
13+
+ Use the `__init__` method to construct your parameters.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Your code here
Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
class InputOutString(object):
1+
# Your code here
2+
class InputOutString:
23
def __init__(self):
3-
self.s = ""
4+
self.input_string = ""
45

5-
def getString(self):
6-
self.s = raw_input()
6+
def get_string(self):
7+
self.input_string = input("Enter a string: ")
78

8-
def printString(self):
9-
print self.s.upper()
9+
def print_string(self):
10+
print(self.input_string.upper())
1011

11-
strObj = InputOutString()
12-
strObj.getString()
13-
strObj.printString()
12+
string_object = InputOutString()
13+
string_object.get_string()
14+
string_object.print_string()
Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,33 @@
1-
# `25` Imprime la fórmula
1+
# `25` Print formula
22

3-
Escribe un programa que calcule e imprima el valor de acuerdo a la fórmula dada:
3+
## 📝 Instrucciones:
44

5-
Q = Square root of [(2 * C * D)/H]
5+
1. Escribe una función llamada `print_formula()`, con un parámetro que calcule e imprima el valor según la fórmula dada.
66

7-
A continuación encontrarás los valores fijos de C y H:
8-
C es 50. H es 30.
9-
D es la variable cuyos valores debiesen ser ingresados en tu
10-
Ejemplo:
11-
Digamos que le sentrega la siguiente secuencia separada por coma al programa:
12-
100,150,180
13-
El resultado del programa debiese ser:
14-
18,22,24
7+
```text
8+
Q = Square root of (2 * c * d) / h
9+
```
1510

16-
Pistas:
17-
Si el resultado recicido es un decimal, debería rendondearse a su valor más cercano (por ejemplo, si el resultado es 26.0, debiese imprimirse como 26)
18-
En el caso de que se le hayan entregado datos a la cuestión, deben asumirse como una entrada de la consola.
19-
11+
*A continuación encontrarás los valores fijos de `c` y `h`:*
12+
13+
+ `c` es 50.
14+
+ `h` es 30.
15+
+ `d` sería el parámetro de la función.
16+
17+
## 📎 Ejemplo de entrada:
18+
19+
```py
20+
print_formula(150)
21+
```
22+
23+
## 📎 Ejemplo de salida:
24+
25+
```py
26+
22
27+
```
28+
29+
## 💡 Pistas:
30+
31+
+ Si el resultado recibido es un decimal, debería redondearse a su valor más cercano (por ejemplo, si el resultado es 26.0, debiese imprimirse como 26).
32+
33+
+ Importa el módulo `math`.

exercises/25-print-formula/README.md

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,33 @@
11
# `25` Print formula
22

3-
Write a program that calculates and prints the value according to the given formula:
4-
Q = Square root of [(2 * C * D)/H]
5-
6-
Following are the fixed values of C and H:
7-
C is 50. H is 30.
8-
D is the variable whose values should be input to your program in a comma-separated sequence.
9-
Example
10-
Let us assume the following comma separated input sequence is given to the program:
11-
100,150,180
12-
The output of the program should be:
13-
18,22,24
14-
15-
Hints:
16-
If the output received is in decimal form, it should be rounded off to its nearest value (for example, if the output received is 26.0, it should be printed as 26)
17-
In case of input data being supplied to the question, it should be assumed to be a console input.
3+
## 📝 Instructions:
4+
5+
1. Write a function `print_formula()`, with one parameter that calculates and prints the value according to the given formula:
6+
7+
```text
8+
Q = Square root of (2 * c * d) / h
9+
```
10+
11+
*Following are the fixed values of `c` and `h`:*
12+
13+
+ `c` is 50.
14+
+ `h` is 30.
15+
+ `d` would be the parameter of the function.
16+
17+
## 📎 Example input:
18+
19+
```py
20+
print_formula(150)
21+
```
22+
23+
## 📎 Example output:
24+
25+
```py
26+
22
27+
```
28+
29+
## 💡 Hints:
30+
31+
+ If the output received is in decimal form, it should be rounded off to its nearest value (for example, if the output received is 26.0, it should be printed as 26).
32+
33+
+ Import the `math` module.

exercises/25-print-formula/app.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Your code here
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
# Your code here
12
import math
2-
def print_formula(num):
3-
return round(math.sqrt(2*50*num/30))
43

5-
print(print_formula(5))
4+
def print_formula(d):
5+
return round(math.sqrt(2 * 50 * d / 30))
6+
7+
print(print_formula(150))

exercises/25-print-formula/test.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
def test_function_existence(capsys, app):
66
assert app.print_formula
77

8-
@pytest.mark.it('The solution should work as expected')
8+
@pytest.mark.it('The solution should work as expected. Testing with 100')
99
def test_expected_output(capsys, app):
10-
assert app.print_formula(100,150,180) == "18,22,24"
10+
assert app.print_formula(100) == 18
1111

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

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
# `26` Array de dos dimensiones
1+
# `26` Two dimensional list
22

3-
Escribe un progrsms que reciba dos dígitos X,Y como entrada y genere un array de dos dimensiones. El valor del elemento en la fila i-th y en la columna j-th del array debiese ser i*j.
4-
Nota: i=0,1.., X-1; j=0,1,¡­Y-1.
5-
Ejemplo:
6-
Supongamos que se le entregan lasa siguientes entradas al programa:
7-
3,5
8-
Entonces, el resultado del programa debería ser:
9-
[[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]]
3+
## 📝 Instrucciones:
104

11-
Pistas:
12-
Nota: En el caso de que se le entreguen datos a la cuestión, debe asumirse como una entrada de la consola en un formulario separado por comas.
5+
1. Escribe una función llamada `two_dimensional_list()` que tome 2 dígitos (x, y) como entrada y genere una lista bidimensional o matriz.
136

7+
2. El valor del elemento en la fila `i` y columna `j` de la lista debería ser `i*j` (los valores de sus índices).
8+
9+
## 📎 Ejemplo de entrada:
10+
11+
```py
12+
two_dimensional_list(3,5)
13+
```
14+
15+
## 📎 Ejemplo de salida:
16+
17+
```py
18+
[[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]]
19+
```
Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
1-
# `26`Two dimensional array
2-
3-
Write a program which takes 2 digits, X,Y as input and generates a 2-dimensional array. The element value in the i-th row and j-th column of the array should be i*j.
4-
Note: i=0,1.., X-1; j=0,1,¡­Y-1.
5-
Example
6-
Suppose the following inputs are given to the program:
7-
3,5
8-
Then, the output of the program should be:
9-
[[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]]
10-
11-
Hints:
12-
Note: In case of input data being supplied to the question, it should be assumed to be a console input in a comma-separated form.
1+
# `26` Two dimensional list
2+
3+
## 📝 Instructions:
4+
5+
1. Write a function `two_dimensional_list()`, that takes 2 digits (x, y) as input and generates a 2-dimensional list or matrix.
6+
7+
2. The element value in the `i` row and `j` column of the list should be `i*j` (their index values).
8+
9+
## 📎 Example input:
10+
11+
```py
12+
two_dimensional_list(3,5)
13+
```
14+
15+
## 📎 Example output:
16+
17+
```py
18+
[[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]]
19+
```
20+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Your code here
Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
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)]
1+
# Your code here
2+
def two_dimensional_list(n_rows, n_columns):
3+
dimensions = [int(x) for x in "{},{}".format(n_rows, n_columns).split(',')]
4+
row_num = dimensions[0]
5+
col_num = dimensions[1]
6+
matrix = [[0 for col in range(col_num)] for row in range(row_num)]
67

7-
for row in range(rowNum):
8-
for col in range(colNum):
9-
multilist[row][col]= row*col
8+
for row in range(row_num):
9+
for col in range(col_num):
10+
matrix[row][col] = row * col
1011

11-
return (multilist)
12+
return matrix
13+
14+
print(two_dimensional_list(3,5))
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
21
import pytest,os,re,io,sys, mock, json
32

4-
@pytest.mark.it('The function two_dimensional_array must exist')
3+
@pytest.mark.it('The function two_dimensional_list must exist')
54
def test_function_existence(capsys, app):
6-
assert app.two_dimensional_array
5+
assert app.two_dimensional_list
76

8-
@pytest.mark.it('The function should return the expected output.')
7+
@pytest.mark.it('The function should return the expected output')
98
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]]
9+
assert app.two_dimensional_list(3,5) == [[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]]
1110

12-
@pytest.mark.it('The function should work with other entries. Testing with 2,7')
11+
@pytest.mark.it('The function should work with other entries. Testing with (2, 7)')
1312
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]]
13+
assert app.two_dimensional_list(2,7) == [[0, 0, 0, 0, 0, 0, 0], [0, 1, 2, 3, 4, 5, 6]]
1514

16-
@pytest.mark.it('The function should work with other entries. Testing with 2,7')
15+
@pytest.mark.it('The function should work with other entries. Testing with (1, 10)')
1716
def test_expected_output(capsys, app):
18-
assert app.two_dimensional_array(1,10) == [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
17+
assert app.two_dimensional_list(1,10) == [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
1-
# `27` Secuencia de palabras
1+
# `27` Sequence of words
22

3-
Escribe un programa que acepte una secuencia separada por comas como entrada e imprima las palabras en una secuencia separada por comas después de ordenarlas alfabéticamente.
4-
Supongamos que se le entrega la siguiente entrada al programa:
5-
without,hello,bag,world
6-
El resultado debiese ser:
7-
bag,hello,without,world
3+
## 📝 Instrucciones:
84

9-
Pistas:
10-
En el caso de que se le entreguen datos a la pregunta, deben considerarse como entradas de la consola.
5+
1. Escribe una función llamada `sequence_of_words()`, que acepte una secuencia de palabras separadas por comas como entrada (un string).
6+
7+
2. Imprime las palabras en una secuencia separada por comas después de ordenarlas alfabéticamente.
8+
9+
## 📎 Ejemplo de entrada:
10+
11+
```py
12+
sequence_of_words("without,hello,bag,world")
13+
```
14+
15+
## 📎 Ejemplo de salida:
16+
17+
```py
18+
bag, hello, without, world
19+
```
20+
21+
## 💡 Pista:
22+
23+
+ Recuerda que cada palabra debe estar separada por una coma y dentro de UN SOLO par de comillas.
Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
1-
# `27`Sequence of words
1+
# `27` Sequence of words
22

3-
Write a program that accepts a comma separated sequence of words as input and prints the words in a comma-separated sequence after sorting them alphabetically.
4-
Suppose the following input is supplied to the program:
5-
without,hello,bag,world
6-
Then, the output should be:
7-
bag,hello,without,world
3+
## 📝 Instructions:
84

9-
Hints:
10-
In case of input data being supplied to the question, it should be assumed to be a console input.
5+
1. Write a function `sequence_of_words`, that accepts a comma separated sequence of words as input (a string).
6+
7+
2. Print the words in a comma-separated sequence after sorting them alphabetically.
8+
9+
## 📎 Example input:
10+
11+
```py
12+
sequence_of_words("without,hello,bag,world")
13+
```
14+
15+
## 📎 Example output:
16+
17+
```py
18+
bag, hello, without, world
19+
```
20+
21+
## 💡 Hint:
22+
23+
+ Remember that each word must be separated by a comma and inside ONLY ONE pair of quotes.

exercises/27-sequence-of-words/app.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Your code here
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
# Your code here
12
def sequence_of_words(words):
23
items=[x for x in "{}".format(words).split(',')]
34
items.sort()
45
return (','.join(items))
6+
7+
print(sequence_of_words("this,is,sorted"))

0 commit comments

Comments
 (0)