Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPhilip Withnall <philip@tecnocode.co.uk>2013-08-24 01:28:07 (GMT)
committer Philip Withnall <philip@tecnocode.co.uk>2013-08-24 01:28:07 (GMT)
commit05d38fa91ea1d3a4a674c14154a5a359e2d6bf7b (patch)
treed27128fb5d939231ac60e6d3a9fce5626e311537
parent009d7e7562392b9eaa88dd3b43e622d67a7b83fa (diff)
Add English versions of the exercises for the second part
These still need to be translated to Spanish.
-rw-r--r--Makefile6
-rw-r--r--exercises/en/10_def.py33
-rw-r--r--exercises/en/10_def_solution.py49
-rw-r--r--exercises/en/11_recursion.py28
-rw-r--r--exercises/en/11_recursion_solution.py33
-rw-r--r--exercises/en/12_create-class.py43
-rw-r--r--exercises/en/12_create-class_solution.py62
-rw-r--r--exercises/en/13_extend-class.py37
-rw-r--r--exercises/en/13_extend-class_solution.py72
-rw-r--r--exercises/en/14_property.py47
-rw-r--r--exercises/en/14_property_solution.py50
-rw-r--r--exercises/en/15_private-methods.py50
-rw-r--r--exercises/en/15_private-methods_solution.py53
13 files changed, 563 insertions, 0 deletions
diff --git a/Makefile b/Makefile
index a0cdfa7..aa37782 100644
--- a/Makefile
+++ b/Makefile
@@ -13,6 +13,12 @@ EN_EXERCISES = \
7_lists \
8_lists-min \
9_dicts \
+ 10_def \
+ 11_recursion \
+ 12_create-class \
+ 13_extend-class \
+ 14_property \
+ 15_private-methods \
$(NULL)
ES_EXERCISES = \
1_imprimir-nombres \
diff --git a/exercises/en/10_def.py b/exercises/en/10_def.py
new file mode 100644
index 0000000..6d7d6c6
--- /dev/null
+++ b/exercises/en/10_def.py
@@ -0,0 +1,33 @@
+#!/usr/bin/python
+# coding=utf-8
+
+import math
+
+"""Define a function to calculate the arithmetic mean of a list of integers.
+
+You must define a new function, arithmetic_mean, which takes a list of integers
+as its only parameter, and returns a single floating point number which is
+their arithmetic mean.
+
+Remember to document your function using a docstring!
+
+Extension: Define a second function, geometric_mean, which has the same
+parameter, but returns a single floating point number which is the geometric
+mean of the input integers: http://en.wikipedia.org/wiki/Geometric_mean.
+To do this, you’ll need the math.pow() function.
+"""
+
+# Define your functions here.
+
+
+# Code to check your functions. Do not modify anything below this line.
+assert(arithmetic_mean([5]) == 5)
+assert(arithmetic_mean([1, 2, 3]) == 2)
+assert(arithmetic_mean([2, 2, 2]) == 2)
+assert(arithmetic_mean.__doc__ is not None)
+
+if geometric_mean is not None:
+ assert(geometric_mean([5]) == 5)
+ assert(geometric_mean([2, 2]) == 2)
+ assert(geometric_mean([1, 2, 3]) == pow(6, 1.0 / 3.0))
+ assert(geometric_mean.__doc__ is not None)
diff --git a/exercises/en/10_def_solution.py b/exercises/en/10_def_solution.py
new file mode 100644
index 0000000..c929d73
--- /dev/null
+++ b/exercises/en/10_def_solution.py
@@ -0,0 +1,49 @@
+#!/usr/bin/python
+# coding=utf-8
+
+import math
+
+"""Define a function to calculate the arithmetic mean of a list of integers.
+
+You must define a new function, arithmetic_mean, which takes a list of integers
+as its only parameter, and returns a single floating point number which is
+their arithmetic mean.
+
+Remember to document your function using a docstring!
+
+Extension: Define a second function, geometric_mean, which has the same
+parameter, but returns a single floating point number which is the geometric
+mean of the input integers: http://en.wikipedia.org/wiki/Geometric_mean.
+To do this, you’ll need the math.pow() function.
+"""
+
+def arithmetic_mean(number_list):
+ """Calculate the arithmetic mean of the input integer list."""
+ total = 0
+ count = 0
+ for i in number_list:
+ total += i
+ count += 1
+ return float(total) / count
+
+def geometric_mean(number_list):
+ """Calculate the geometric mean of the input integer list."""
+ product = 1
+ count = 0
+ for i in number_list:
+ product *= i
+ count += 1
+ return pow(product, 1.0 / float(count))
+
+
+# Code to check your functions. Do not modify anything below this line.
+assert(arithmetic_mean([5]) == 5)
+assert(arithmetic_mean([1, 2, 3]) == 2)
+assert(arithmetic_mean([2, 2, 2]) == 2)
+assert(arithmetic_mean.__doc__ is not None)
+
+if geometric_mean is not None:
+ assert(geometric_mean([5]) == 5)
+ assert(geometric_mean([2, 2]) == 2)
+ assert(geometric_mean([1, 2, 3]) == pow(6, 1.0 / 3.0))
+ assert(geometric_mean.__doc__ is not None)
diff --git a/exercises/en/11_recursion.py b/exercises/en/11_recursion.py
new file mode 100644
index 0000000..371b432
--- /dev/null
+++ b/exercises/en/11_recursion.py
@@ -0,0 +1,28 @@
+#!/usr/bin/python
+# coding=utf-8
+
+"""Define a recursive function to calculate the greatest common denominator.
+
+You must define a new function, gcd (standing for “greatest common
+denominator”), which takes two integers as its parameters, and returns a single
+integer which is the greatest common denominator of the two. This is the
+largest integer which divides both numbers evenly.
+
+An easy way to implement this is using the Euclidean algorithm:
+ http://en.wikipedia.org/wiki/Euclidean_algorithm
+
+Your function must be implemented using recursion.
+
+Remember to document your function using a docstring!
+"""
+
+# Write your new function here.
+
+
+# Code to check your function. Do not modify anything below this line.
+assert(gcd(1, 1) == 1)
+assert(gcd(10, 12) == 2)
+assert(gcd(12, 10) == 2)
+assert(gcd(5, 0) == 5)
+assert(gcd(0, 5) == 5)
+assert(gcd.__doc__ is not None)
diff --git a/exercises/en/11_recursion_solution.py b/exercises/en/11_recursion_solution.py
new file mode 100644
index 0000000..ad8ae2e
--- /dev/null
+++ b/exercises/en/11_recursion_solution.py
@@ -0,0 +1,33 @@
+#!/usr/bin/python
+# coding=utf-8
+
+"""Define a recursive function to calculate the greatest common denominator.
+
+You must define a new function, gcd (standing for “greatest common
+denominator”), which takes two integers as its parameters, and returns a single
+integer which is the greatest common denominator of the two. This is the
+largest integer which divides both numbers evenly.
+
+An easy way to implement this is using the Euclidean algorithm:
+ http://en.wikipedia.org/wiki/Euclidean_algorithm
+
+Your function must be implemented using recursion.
+
+Remember to document your function using a docstring!
+"""
+
+def gcd(x, y):
+ """Compute the greatest common denominator of x and y."""
+ if y == 0:
+ return x
+ else:
+ return gcd(y, x % y)
+
+
+# Code to check your function. Do not modify anything below this line.
+assert(gcd(1, 1) == 1)
+assert(gcd(10, 12) == 2)
+assert(gcd(12, 10) == 2)
+assert(gcd(5, 0) == 5)
+assert(gcd(0, 5) == 5)
+assert(gcd.__doc__ is not None)
diff --git a/exercises/en/12_create-class.py b/exercises/en/12_create-class.py
new file mode 100644
index 0000000..7c8ebfe
--- /dev/null
+++ b/exercises/en/12_create-class.py
@@ -0,0 +1,43 @@
+#!/usr/bin/python
+# coding=utf-8
+
+"""Define a RegularPolygon class which implements some specified methods.
+
+You must define a new class, RegularPolygon, to represent an arbitrary regular
+polygon. It must have the following methods:
+ - __init__(self, sides): which stores the number of sides the polygon has
+ in a variable in the class
+ - get_name(self): which always returns 'Unknown polygon' (it will be used in
+ exercise 13)
+ - get_sides(self): which returns the number of sides in the polygon
+ - get_interior_angle(self): which returns the size (in degrees) of a single
+ internal angle in the polygon (e.g. 60 for a
+ triangle, 90 for a square, etc.)
+ - get_total_interior_angles(self): which returns the total internal angle in
+ the polygon in degrees (e.g. 180 for a
+ triangle, 360 for a square, etc.)
+
+Remember to document your class and all its methods using docstrings!
+"""
+
+# Write your class here.
+
+
+# Code to check your class. Do not modify anything below this line.
+triangle = RegularPolygon(3)
+assert(triangle.get_name() == 'Unknown polygon')
+assert(triangle.get_sides() == 3)
+assert(triangle.get_interior_angle() == 60)
+assert(triangle.get_total_interior_angle() == 180)
+
+square = RegularPolygon(4)
+assert(square.get_name() == 'Unknown polygon')
+assert(square.get_sides() == 4)
+assert(square.get_interior_angle() == 90)
+assert(square.get_total_interior_angle() == 360)
+
+assert(RegularPolygon.__doc__ is not None)
+assert(RegularPolygon.get_name.__doc__ is not None)
+assert(RegularPolygon.get_sides.__doc__ is not None)
+assert(RegularPolygon.get_interior_angle.__doc__ is not None)
+assert(RegularPolygon.get_total_interior_angle.__doc__ is not None)
diff --git a/exercises/en/12_create-class_solution.py b/exercises/en/12_create-class_solution.py
new file mode 100644
index 0000000..c67ad3e
--- /dev/null
+++ b/exercises/en/12_create-class_solution.py
@@ -0,0 +1,62 @@
+#!/usr/bin/python
+# coding=utf-8
+
+"""Define a RegularPolygon class which implements some specified methods.
+
+You must define a new class, RegularPolygon, to represent an arbitrary regular
+polygon. It must have the following methods:
+ - __init__(self, sides): which stores the number of sides the polygon has
+ in a variable in the class
+ - get_name(self): which always returns 'Unknown polygon' (it will be used in
+ exercise 13)
+ - get_sides(self): which returns the number of sides in the polygon
+ - get_interior_angle(self): which returns the size (in degrees) of a single
+ internal angle in the polygon (e.g. 60 for a
+ triangle, 90 for a square, etc.)
+ - get_total_interior_angles(self): which returns the total internal angle in
+ the polygon in degrees (e.g. 180 for a
+ triangle, 360 for a square, etc.)
+
+Remember to document your class and all its methods using docstrings!
+"""
+
+class RegularPolygon(object):
+ """A regular polygon with a specified number of sides."""
+ def __init__(self, sides):
+ self._sides = sides
+
+ def get_name(self):
+ """Get the human-readable name of the polyon."""
+ return 'Unknown polygon'
+
+ def get_sides(self):
+ """Get the number of sides in the polygon."""
+ return self._sides
+
+ def get_interior_angle(self):
+ """Get a single interior angle, in degrees."""
+ return 180 - (360 / self._sides)
+
+ def get_total_interior_angle(self):
+ """Get the sum of the interior angles in the polygon, in degrees."""
+ return self._sides * self.get_interior_angle()
+
+
+# Code to check your class. Do not modify anything below this line.
+triangle = RegularPolygon(3)
+assert(triangle.get_name() == 'Unknown polygon')
+assert(triangle.get_sides() == 3)
+assert(triangle.get_interior_angle() == 60)
+assert(triangle.get_total_interior_angle() == 180)
+
+square = RegularPolygon(4)
+assert(square.get_name() == 'Unknown polygon')
+assert(square.get_sides() == 4)
+assert(square.get_interior_angle() == 90)
+assert(square.get_total_interior_angle() == 360)
+
+assert(RegularPolygon.__doc__ is not None)
+assert(RegularPolygon.get_name.__doc__ is not None)
+assert(RegularPolygon.get_sides.__doc__ is not None)
+assert(RegularPolygon.get_interior_angle.__doc__ is not None)
+assert(RegularPolygon.get_total_interior_angle.__doc__ is not None)
diff --git a/exercises/en/13_extend-class.py b/exercises/en/13_extend-class.py
new file mode 100644
index 0000000..23849d5
--- /dev/null
+++ b/exercises/en/13_extend-class.py
@@ -0,0 +1,37 @@
+#!/usr/bin/python
+# coding=utf-8
+
+"""Define Triangle and Square classes which extend RegularPolygon.
+
+You must define two new classes, Triangle and Square, which represent regular
+triangles and squares. They must both inherit from RegularPolygon.
+
+In addition to having all the methods from RegularPolygon, they most both
+override the get_name() method from RegularPolygon so that it returns 'Triangle'
+or 'Square' instead of 'Unknown polygon'.
+
+Remember to document your classes and their methods using docstrings!
+"""
+
+# Copy the RegularPolygon class from the previous exercise here, unmodified.
+
+# Add your new child classes here.
+
+
+# Code to check your classes. Do not modify anything below this line.
+triangle = Triangle()
+assert(triangle.get_name() == 'Triangle')
+assert(triangle.get_sides() == 3)
+assert(triangle.get_interior_angle() == 60)
+assert(triangle.get_total_interior_angle() == 180)
+
+square = Square()
+assert(square.get_name() == 'Square')
+assert(square.get_sides() == 4)
+assert(square.get_interior_angle() == 90)
+assert(square.get_total_interior_angle() == 360)
+
+assert(Triangle.__doc__ is not None)
+assert(Triangle.get_name.__doc__ is not None)
+assert(Square.__doc__ is not None)
+assert(Square.get_name.__doc__ is not None)
diff --git a/exercises/en/13_extend-class_solution.py b/exercises/en/13_extend-class_solution.py
new file mode 100644
index 0000000..dca6ce2
--- /dev/null
+++ b/exercises/en/13_extend-class_solution.py
@@ -0,0 +1,72 @@
+#!/usr/bin/python
+# coding=utf-8
+
+"""Define Triangle and Square classes which extend RegularPolygon.
+
+You must define two new classes, Triangle and Square, which represent regular
+triangles and squares. They must both inherit from RegularPolygon.
+
+In addition to having all the methods from RegularPolygon, they most both
+override the get_name() method from RegularPolygon so that it returns 'Triangle'
+or 'Square' instead of 'Unknown polygon'.
+
+Remember to document your classes and their methods using docstrings!
+"""
+
+class RegularPolygon(object):
+ """A regular polygon with a specified number of sides."""
+ def __init__(self, sides):
+ self._sides = sides
+
+ def get_name(self):
+ """Get the human-readable name of the polyon."""
+ return 'Unknown polygon'
+
+ def get_sides(self):
+ """Get the number of sides in the polygon."""
+ return self._sides
+
+ def get_interior_angle(self):
+ """Get a single interior angle, in degrees."""
+ return 180 - (360 / self._sides)
+
+ def get_total_interior_angle(self):
+ """Get the sum of the interior angles in the polygon, in degrees."""
+ return self._sides * self.get_interior_angle()
+
+class Triangle(RegularPolygon):
+ """A regular, equilateral triangle."""
+ def __init__(self):
+ super(Triangle, self).__init__(3)
+
+ def get_name(self):
+ """Get the human-readable name of the triangle."""
+ return 'Triangle'
+
+class Square(RegularPolygon):
+ """A regular square."""
+ def __init__(self):
+ super(Square, self).__init__(4)
+
+ def get_name(self):
+ """Get the human-readable name of the square."""
+ return 'Square'
+
+
+# Code to check your classes. Do not modify anything below this line.
+triangle = Triangle()
+assert(triangle.get_name() == 'Triangle')
+assert(triangle.get_sides() == 3)
+assert(triangle.get_interior_angle() == 60)
+assert(triangle.get_total_interior_angle() == 180)
+
+square = Square()
+assert(square.get_name() == 'Square')
+assert(square.get_sides() == 4)
+assert(square.get_interior_angle() == 90)
+assert(square.get_total_interior_angle() == 360)
+
+assert(Triangle.__doc__ is not None)
+assert(Triangle.get_name.__doc__ is not None)
+assert(Square.__doc__ is not None)
+assert(Square.get_name.__doc__ is not None)
diff --git a/exercises/en/14_property.py b/exercises/en/14_property.py
new file mode 100644
index 0000000..8ada94a
--- /dev/null
+++ b/exercises/en/14_property.py
@@ -0,0 +1,47 @@
+#!/usr/bin/python
+# coding=utf-8
+
+"""Convert a pair of methods in a class to be a property instead.
+
+The class below uses a pair of methods, get_size() and set_size(), to read and
+write the size of the Pascal triangle it represents. You must add a new
+property, size, to the class, which wraps these two methods.
+
+You do not need to modify any of the existing methods in the class.
+
+Remember to document your additions using docstrings!
+"""
+
+class PascalTriangle(object):
+ """A representation of Pascal's triangle.
+
+ The size (number of cells on the base) of the triangle can be set. The
+ values of the cells are stored in a list of tuples of (cell index, value)
+ which is appended to whenever the user correctly fills out a cell.
+ """
+ def __init__(self, size):
+ self._size = size
+ self._cell_values = []
+
+ def set_cell_value(self, cell, val):
+ """Set the value of the given cell to one provided by the user."""
+ self._cell_values.append((cell, val))
+
+ def get_size(self):
+ """Get the triangle size."""
+ return self._size
+
+ def set_size(self, size):
+ """Set the triangle size and clear any user-entered cell values."""
+ self._size = size
+ self._cell_values = []
+
+
+# Code to check your classes. Do not modify anything below this line.
+triangle = PascalTriangle(4)
+assert(triangle.size == 4)
+triangle.size = 5
+assert(triangle.size == 5)
+
+assert(PascalTriangle.__doc__ is not None)
+assert(PascalTriangle.size.__doc__ is not None)
diff --git a/exercises/en/14_property_solution.py b/exercises/en/14_property_solution.py
new file mode 100644
index 0000000..b535d54
--- /dev/null
+++ b/exercises/en/14_property_solution.py
@@ -0,0 +1,50 @@
+#!/usr/bin/python
+# coding=utf-8
+
+"""Convert a pair of methods in a class to be a property instead.
+
+The class below uses a pair of methods, get_size() and set_size(), to read and
+write the size of the Pascal triangle it represents. You must add a new
+property, size, to the class, which wraps these two methods.
+
+You do not need to modify any of the existing methods in the class.
+
+Remember to document your additions using docstrings!
+"""
+
+class PascalTriangle(object):
+ """A representation of Pascal's triangle.
+
+ The size (number of cells on the base) of the triangle can be set. The
+ values of the cells are stored in a list of tuples of (cell index, value)
+ which is appended to whenever the user correctly fills out a cell.
+ """
+ def __init__(self, size):
+ self._size = size
+ self._cell_values = []
+
+ def set_cell_value(self, cell, val):
+ """Set the value of the given cell to one provided by the user."""
+ self._cell_values.append((cell, val))
+
+ def get_size(self):
+ """Get the triangle size."""
+ return self._size
+
+ def set_size(self, size):
+ """Set the triangle size and clear any user-entered cell values."""
+ self._size = size
+ self._cell_values = []
+
+ # size property
+ size = property(get_size, set_size)
+
+
+# Code to check your classes. Do not modify anything below this line.
+triangle = PascalTriangle(4)
+assert(triangle.size == 4)
+triangle.size = 5
+assert(triangle.size == 5)
+
+assert(PascalTriangle.__doc__ is not None)
+assert(PascalTriangle.size.__doc__ is not None)
diff --git a/exercises/en/15_private-methods.py b/exercises/en/15_private-methods.py
new file mode 100644
index 0000000..ce5853d
--- /dev/null
+++ b/exercises/en/15_private-methods.py
@@ -0,0 +1,50 @@
+#!/usr/bin/python
+# coding=utf-8
+
+"""Factor out some common code from two methods into a new private method.
+
+Some of the code in the class below is repeated. Find the two lines of repeated
+code and move them into a new private method which should be called from the
+locations the duplicated code was removed from.
+
+Remember to document your new method using a docstring!
+"""
+
+class Counter(object):
+ """A counter which can be incremented or reset.
+
+ This could be used for counting people passing through an entry gate, for
+ example. Whenever the counter value changes, the same message is printed.
+ """
+ def __init__(self):
+ self._counter = 0
+
+ def get_counter(self):
+ """Return the counter value."""
+ return self._counter
+
+ def increment_counter(self):
+ """Increment the counter value and print a message."""
+ self._counter += 1
+ print('Counter is now: %i' % self._counter)
+
+ def reset_counter(self):
+ """Reset the counter value to 0 and print a message."""
+ self._counter = 0
+ print('Counter is now: %i' % self._counter)
+
+
+# Code to check your classes. Do not modify anything below this line.
+counter = Counter()
+assert(counter.get_counter() == 0)
+counter.increment_counter()
+assert(counter.get_counter() == 1)
+counter.increment_counter()
+assert(counter.get_counter() == 2)
+counter.reset_counter()
+assert(counter.get_counter() == 0)
+
+assert(Counter.__doc__ is not None)
+assert(Counter.get_counter.__doc__ is not None)
+assert(Counter.increment_counter.__doc__ is not None)
+assert(Counter.reset_counter.__doc__ is not None)
diff --git a/exercises/en/15_private-methods_solution.py b/exercises/en/15_private-methods_solution.py
new file mode 100644
index 0000000..418c2c1
--- /dev/null
+++ b/exercises/en/15_private-methods_solution.py
@@ -0,0 +1,53 @@
+#!/usr/bin/python
+# coding=utf-8
+
+"""Factor out some common code from two methods into a new private method.
+
+Some of the code in the class below is repeated. Find the two lines of repeated
+code and move them into a new private method which should be called from the
+locations the duplicated code was removed from.
+
+Remember to document your new method using a docstring!
+"""
+
+class Counter(object):
+ """A counter which can be incremented or reset.
+
+ This could be used for counting people passing through an entry gate, for
+ example. Whenever the counter value changes, the same message is printed.
+ """
+ def __init__(self):
+ self._counter = 0
+
+ def get_counter(self):
+ """Return the counter value."""
+ return self._counter
+
+ def __set_counter(self, counter):
+ """Set the value of the counter and print a message."""
+ self._counter = counter
+ print('Counter is now: %i' % self._counter)
+
+ def increment_counter(self):
+ """Increment the counter value and print a message."""
+ self.__set_counter(self._counter + 1)
+
+ def reset_counter(self):
+ """Reset the counter value to 0 and print a message."""
+ self.__set_counter(0)
+
+
+# Code to check your classes. Do not modify anything below this line.
+counter = Counter()
+assert(counter.get_counter() == 0)
+counter.increment_counter()
+assert(counter.get_counter() == 1)
+counter.increment_counter()
+assert(counter.get_counter() == 2)
+counter.reset_counter()
+assert(counter.get_counter() == 0)
+
+assert(Counter.__doc__ is not None)
+assert(Counter.get_counter.__doc__ is not None)
+assert(Counter.increment_counter.__doc__ is not None)
+assert(Counter.reset_counter.__doc__ is not None)