Skip to content

Commit a8f3b5e

Browse files
committed
for loop exercises
1 parent b3cfd13 commit a8f3b5e

File tree

10 files changed

+100
-52
lines changed

10 files changed

+100
-52
lines changed

exercises/10-Find_avg/README.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
# `28` Find average
1+
# `10` Find average
22

3-
Another way to loop a list with the for loop will be using the IN statement like this:
4-
5-
```py
6-
for index in my_list:
7-
print(myArray[index])
8-
```
93

104

115
# 📝Instructions
12-
Calculate the average value of all the items in the array and print it on the console.
6+
1. Calculate the average value of all the items in the list and print it on the console.
7+
2.
138

149
# 💡HINT:
15-
To print the average, you have to add all the values and divide the result by the total length of the array.
10+
To print the average, you have to add all the values and divide the result
11+
by the total length of the list.
12+
13+
```py
14+
The result have to be like:
15+
27278.8125
16+
```

exercises/10-Find_avg/app.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
#Your code here:
44

55

6-
total = 0
7-
6+
# total = 0
87
for val in my_list:
9-
total += val
10-
avg = total / len(my_list)
8+
# total += val
9+
# avg = total / len(my_list)
1110
print(avg)

exercises/10-Find_avg/test.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import io
2+
import os
3+
import sys
4+
sys.stdout = buffer = io.StringIO()
5+
6+
import app
7+
import pytest
8+
9+
10+
@pytest.mark.it("Print the minimum value from the list")
11+
def test_min():
12+
captured = buffer.getvalue()
13+
assert "27278.8125\n" in captured
14+
15+
@pytest.mark.it("Use the for loop and len function")
16+
def test_if_loo():
17+
f = open(os.path.dirname(os.path.abspath(__file__))+'/app.py')
18+
content = f.read()
19+
assert content.find("for") > 0
20+
assert content.find("len") > 0
Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
# `10.1` And one and two and three
2+
3+
Dictionaries (or dict in Python) are a way of storing elements just like you
4+
would in a Python list. But, rather than accessing elements using its index,
5+
you assign a fixed key to it and access the element using the key. What you now
6+
deal with is a "key-value" pair, which is sometimes a more appropriate data structure
7+
for many problem instead of a simple list.
8+
9+
210
# 📝Instruction
311
1. Given a contact object, please `loop all its properties and values` and print them on the console.
412
2. You will have to loop its properties to be able to print them
513

614
```py
7-
Expected console output:
15+
Example console output:
816
fullname : John Doe
917
phone : 123-123-2134
1018
email : test@nowhere.com
1119
```
1220

1321
💡Hints
14-
MDN for in loop reference
15-
<!-- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration#for...in_statement -->
22+
-contact.keys() #['fullname', 'phone', 'email']
23+
-contact.values() #['Jane Doe', '321-321-4321', 'test@test.com']
24+
-contact.items() #[('fullname', 'Jane Doe'), ('phone', '321-321-4321'), ('email', 'test@test.com')]

exercises/10.1-And_One_and_a_Two_and_a_Three/app.py

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,6 @@
44
"email": "test@test.com"
55
}
66
#Your code here:
7-
8-
# declare and aux variable and assign the value of the key
9-
k = contact.keys()
10-
11-
12-
# loop the entire dict and concactenate the items in one output
137
for k, v in contact.items():
14-
print("{0}: {1}".format(k, v))
15-
16-
17-
#another form:
18-
x = contact.keys()
19-
20-
for x, e in contact.items():
21-
print(x, ":", e)
8+
print(k, ":", v)
229

23-
#loop without declaration:
24-
for key, value in contact.items():
25-
print(key, ":", value)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import io
2+
import os
3+
import sys
4+
sys.stdout = buffer = io.StringIO()
5+
6+
import app
7+
import pytest
8+
9+
10+
@pytest.mark.it("Print the items in the console")
11+
def test_dict():
12+
captured = buffer.getvalue()
13+
assert "fullname : Jane Doe\nphone : 321-321-4321\nemail : test@test.com\n" in captured
14+
15+
@pytest.mark.it("Loop the all values")
16+
def test_if_loo():
17+
f = open(os.path.dirname(os.path.abspath(__file__))+'/app.py')
18+
content = f.read()
19+
assert content.find("for") > 0
20+
21+

exercises/11-Nested_list/README.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
1-
Instructions from your teacher:
2-
# `29` Nested list
1+
2+
# `11` Nested list
3+
34
It is possible to find an list comprised of other lists (it is called a two-dimension list or matrix).
45

56
In this example, we have a list of coordinates that you can access by doing the following:
67

78
//longitude = []
89
// for loop in coordinate longitude
9-
longitude = coordinatesArray[0][1];
10+
longitude = coordinatesList[0][1];
1011

1112

12-
Instructions:
13-
Loop through the array printing only the longitudes.
14-
13+
#📝Instructions:
14+
Loop through the list printing only the longitudes.
15+
```py
1516
The result should be something like this:
1617
-112.633853
1718
-63.987
1819
-81.901693
1920
-71.653268
21+
```
22+
23+
💡Hint:
24+
Remember the index of the position 1 is list[0]

exercises/11-Nested_list/app.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11

2-
coordinatesArray = [[33.747252,-112.633853],[-33.867886, -63.987],[41.303921, -81.901693],[-33.350534, -71.653268]]
2+
coordinatesList = [[33.747252,-112.633853],[-33.867886, -63.987],[41.303921, -81.901693],[-33.350534, -71.653268]]
33

44
# Your code go here:
5-
"""from here the student have to decleare the function"""
6-
"""declare the empty variable"""
7-
# longitude = []
85

9-
# for grades in coordinatesArray:
10-
# longitude.append(grades[1])
11-
# grades += grades
12-
# print(longitude)
6+
for grades in coordinatesList:
7+
print(grades[1])

exercises/11-Nested_list/test.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,21 @@
11
import io
2+
import os
23
import sys
34
sys.stdout = buffer = io.StringIO()
45

6+
import app
7+
import pytest
8+
9+
10+
@pytest.mark.it("Print the longitudes")
11+
def test_dict():
12+
captured = buffer.getvalue()
13+
assert "-112.633853\n-63.987\n-81.901693\n-71.653268\n" in captured
14+
15+
@pytest.mark.it("Loop the all values using for loop")
16+
def test_if_loo():
17+
f = open(os.path.dirname(os.path.abspath(__file__))+'/app.py')
18+
content = f.read()
19+
assert content.find("for") > 0
20+
21+

exercises/12-Map_and_list/app.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
Celsius_values = [-2,34,56,-10]
22

3-
fahrenheit_values = list(map(lambda x : x * 5 / 9 + 32, Celsius_values))
4-
print(fahrenheit_values)
5-
6-
73

84

95
def fahrenheitValues(x):
10-
return x * 5/9 + 32
6+
#the magic go here:
7+
# return x * 5/9 + 32
118
result = list(map(fahrenheit_values, Celsius_values))
129
print(result)

0 commit comments

Comments
 (0)