Exemple #1
0
def histogram(li):
    for n in li:
        print generate_n_chars(n, '*')
Exemple #2
0
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

Exemple #3
0
	def test_01(self):
		result = generate_n_chars(5, 'x')
		self.assertEqual(result, 'xxxxx')
Exemple #4
0
	def test_bad_type(self):
		data = 'banana'
		with self.assertRaises(TypeError):
			result = generate_n_chars(data)
Exemple #5
0
	def test_02(self):
		result = generate_n_chars(10, 'a')
		self.assertEqual(result, 'aaaaaaaaaa')
Exemple #6
0
def histogram(li):
    for n in li:
        print generate_n_chars(n, '*')