Commit 1b4d56bf authored by Aitor Perez's avatar Aitor Perez

Added some exercises. Now there are exercises for lists, dictionaries, fileIO...

Added some exercises. Now there are exercises for lists, dictionaries, fileIO and two numpy exercises
parent 13ecb45a
No preview for this file type
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Exercise 1: Lists - Solution
#
# Complete the filter_positive_oneliner function to achieve the same
# functionality as filter_positive but only in one line, using list
# comprehension.
def filter_positive(numbers):
positive_numbers = []
......@@ -11,16 +17,13 @@ def filter_positive(numbers):
return positive_numbers
def filter_positive_2(numbers):
return [x for x in numbers if x > 0] # <- a list created using list comprehension
def filter_positive_oneliner(numbers):
return [x for x in numbers if x > 0]
###########################
measurements = [-7.43, 11.09, -10.50, 6.62, -3.84, 9.98, -1.64, -7.16, 5.42, -31.48]
#positive_measurements = filter_positive(measurements)
positive_measurements = filter_positive_2(measurements)
positive_measurements = filter_positive_oneliner(measurements)
print(positive_measurements)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Exercise 1: Lists
#
# Complete the filter_positive_oneliner function to achieve the same
# functionality as filter_positive but only in one line, using list
# comprehension.
def filter_positive(numbers):
positive_numbers = []
......@@ -11,16 +17,13 @@ def filter_positive(numbers):
return positive_numbers
def filter_positive_2(numbers):
return [] # <- a list created using list comprehension
def filter_positive_oneliner(numbers):
return []
###########################
measurements = [-7.43, 11.09, -10.50, 6.62, -3.84, 9.98, -1.64, -7.16, 5.42, -31.48]
positive_measurements = filter_positive(measurements)
#positive_measurements = filter_positive_2(measurements)
positive_measurements = filter_positive_oneliner(measurements)
print(positive_measurements)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Exercise 2: Dictionaries - Solution
#
# Complete the filter_nice function to return only the nice geometric
# figures, which are the following:
# - Green triangles
# - Circles with diameter <= 3
# - Yellow rectangles
# - Rectangles with area >= 7
def is_nice(figure):
if figure['name'] == 'triangle' and figure['color'] == 'green':
return True
if figure['name'] == 'circle' and 2*figure['radius'] <= 3:
return True
if figure['name'] == 'square' and (figure['color'] == 'yellow' or figure['side']**2 >= 7):
return True
if figure['name'] == 'rectangle' and (figure['color'] == 'yellow' or figure['sides'][0]*figure['sides'][1] >= 7):
return True
return False
def filter_nice(figures):
nice_figures = []
for figure in figures:
if is_nice(figure):
nice_figures.append(figure)
return nice_figures
figures = [
{'name': 'triangle',
'sides': [3, 4, 5],
'color': 'blue'},
{'name': 'circle',
'radius': 1,
'color': 'red'},
{'name': 'square',
'side': 2,
'color': 'green'},
{'name': 'rectangle',
'sides': [3, 4],
'color': 'yellow'}
]
nice_figures = filter_nice(figures)
print(nice_figures)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Exercise 2: Dictionaries
#
# Complete the filter_nice function to return only the nice geometric
# figures, which are the following:
# - Green triangles
# - Circles with diameter <= 3
# - Yellow rectangles
# - Rectangles with area >= 7
def filter_nice(figures):
nice_figures = []
for figure in figures:
pass # Your code here
return nice_figures
figures = [
{'name': 'triangle',
'sides': [3, 4, 5],
'color': 'blue'},
{'name': 'circle',
'radius': 1,
'color': 'red'},
{'name': 'square',
'side': 2,
'color': 'green'},
{'name': 'rectangle',
'sides': [3, 4],
'color': 'yellow'}
]
nice_figures = filter_nice(figures)
print(nice_figures)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Exercise 3: FileIO - Solution
#
# Read the data from the file measurements.dat and write only the
# positive values to a file named positive_measurements.dat
def filter_positive(numbers):
return [x for x in numbers if x > 0]
###########################
# Read data from measurements.dat
input_file = open("measurements.dat", "r")
measurements = [float(line) for line in input_file]
......
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Exercise 3: FileIO
#
# Read the data from the file measurements.dat and write only the
# positive values to a file named positive_measurements.dat
def filter_positive(numbers):
return [x for x in numbers if x > 0]
###########################
# Read data from measurements.dat
# measurements = ...
measurements = []
positive_measurements = filter_positive(measurements)
# Write data to positive_measurements.dat
# ...
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Exercise 4.1: Numpy chessboard - Solution
#
# Create a 8x8 matrix with a chessboard pattern with numbers 1 and 0.
import numpy as np
def chessboard():
M = np.zeros((8,8))
for i in np.arange(0,8):
for j in np.arange(0,8):
M[i, j] = (i + j) % 2
return M
print(chessboard())
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Exercise 4.1: Numpy chessboard
#
# Create a 8x8 matrix with a chessboard pattern with numbers 1 and 0.
import numpy as np
def chessboard():
pass # Your code here
print(chessboard())
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Exercise 4.2: Numpy matrix order - Solution
#
# Complete the function order to find the order of a 2x2 matrix modulo n.
# Assume that the matrix has nonzero determinant.
# Use the function np.array_equal(M1, M2) for matrix comparison.
import numpy as np
def order(M, n):
k = 1
Mk = M % n
while not np.array_equal(Mk, np.eye(2)):
Mk = np.dot(Mk, M) % n
k += 1
return k
A = 3 * np.eye(2)
B = np.array([[0, 1], [1, 0]])
C = np.array([[1, 2], [3, 4]])
print(order(A, 2))
print(order(B, 3))
print(order(C, 7))
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Exercise 4.2: Numpy matrix order
#
# Complete the function order to find the order of a 2x2 matrix modulo n.
# Assume that the matrix has nonzero determinant.
# Use the function np.array_equal(M1, M2) for matrix comparison.
import numpy as np
def order(M, n):
pass # Your code here
A = 3 * np.eye(2)
B = np.array([[0, 1], [1, 0]])
C = np.array([[1, 2], [3, 4]])
print(order(A, 2))
print(order(B, 3))
print(order(C, 7))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment