def histogram(li): for n in li: print generate_n_chars(n, '*')
Define a function generate_n_chars() that takes an integer n and a character c and returns a string, n characters long, consisting only of c:s. For example, generate_n_chars(5,"x") should return the string "xxxxx". (Python is unusual in that you can actually write an expression 5 * "x" that will evaluate to "xxxxx". For the sake of the exercise you should ignore that the problem can be solved in this manner.) """ def generate_n_chars(n, c): result = '' for i in range(n): result += c return result if __name__ == "__main__": print generate_n_chars(3, 'c') ###########ex12 #!/usr/bin/env python """ Define a procedure histogram() that takes a list of integers and prints a histogram to the screen. For example, histogram([4, 9, 7]) should print the following: **** ********* ******* """ from ex11 import generate_n_chars
def test_01(self): result = generate_n_chars(5, 'x') self.assertEqual(result, 'xxxxx')
def test_bad_type(self): data = 'banana' with self.assertRaises(TypeError): result = generate_n_chars(data)
def test_02(self): result = generate_n_chars(10, 'a') self.assertEqual(result, 'aaaaaaaaaa')