Commit 1b4d56bf by 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)
No preview for this file type
 #!/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)