def collect_vowels(s):
""" (str) -> str
Return the vowels (a, e, i, o, and u) from s.
>>> collect_vowels('Happy Anniversary!')
'aAiea'
>>> collect_vowels('xyz')
''
"""
vowels = ''
for char in s:
if char in 'aeiouAEIOU':
vowels = vowels + char
return vowels
doctest
After running the module containing collect_vowels, in the Python shell, import the doctest module, then call doctest.testmod():
>>> import doctest >>> doctest.testmod() TestResults(failed=0, attempted=2)
Consider this code:
def get_divisors(num, possible_divisors):
""" (int, list of int) -> list of int
Return a list of the values from possible_divisors
that are divisors of num.
>>> get_divisors(8, [1, 2, 3])
[1, 2]
>>> get_divisors(4, [-2, 0, 2])
[2]
"""
divisors = []
for item in possible_divisors:
if item != 0 and num % item == 0:
divisors.append(item)
return divisors
if __name__ == __main__:
import doctest
doctest.testmod()
class MyString(str):
def check(self):
""" (MyString) -> bool
>>> s = MyString("abc")
>>> s.check()
False
>>> d = MyString("aba")
>>> d.check()
True
"""
return self[0] == self[-1]
if __name__ == "__main__":
import doctest
doctest.testmod()
import unittest
import divisors
class TestDivisors(unittest.TestCase):
"""Example unittest test methods for get_divisors."""
def test_divisors_example_1(self):
"""Test get_divisors with 8 and [1, 2, 3]."""
actual = divisors.get_divisors(8, [1, 2, 3])
expected = [1, 2]
self.assertEqual(expected, actual)
def test_divisors_example_2(self):
"""Test get_divisors with 4 and [-2, 0, 2]."""
actual = divisors.get_divisors(4, [-2, 0, 2])
expected = [-2, 2]
self.assertEqual(expected, actual)
import Image, ImageDraw import math im = Image.new( 'RGB', (400,400), "white") # create a new black image draw = ImageDraw.Draw(im) draw.line((0, 0) + (400, 400), fill="grey", width = 1) draw.ellipse((20-5, 10-5, 20+5, 10+5), fill="green") draw.text((100, 200), "Text", "black") im.show()
import Image, ImageDraw im = Image.new( 'RGB', (400, 400), "white") # create a new black image draw = ImageDraw.Draw(im) point1 = raw_input().split() point2 = raw_input().split() x1, y1 = int(point1[0]), int(point1[1]) x2, y2 = int(point2[0]), int(point2[1]) xm = (x1 + x2) / 2 ym = (y1 + y2) / 2 draw.line((x1, y1) + (x2, y2), fill = "black", width = 3) draw.ellipse((xm - 5, ym - 5, xm + 5, ym + 5), fill = "green") im.show()