46 simple exercises: 11-15!

11.-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.)

Here’s my code:

EJ11

12.-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:

****
*********
*******

Here’s my code:EJ12

 

13.-The function max() from exercise 1) and the function max_of_three() from exercise 2) will only work for two and three numbers, respectively. But suppose we have a much larger number of numbers, or suppose we cannot tell in advance how many they are? Write a function max_in_list() that takes a list of numbers and returns the largest one.

This is my code: EJ13

 

14.-Write a program that maps a list of words into a list of integers representing the lengths of the corresponding words.

This is my code:EJ14

 

15.-Write a function find_longest_word() that takes a list of words and returns the length of the longest one.

This is my code: EJ15

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s