4.8. ExercisesΒΆ
-
In Robert McCloskey’s book Make Way for Ducklings, the names of the ducklings are Jack, Kack, Lack, Mack, Nack, Ouack, Pack, and Quack. This loop tries to output these names in order. Save your answer in a variable named
words
and make sure that this value doesn’t get overwritten later in your program.prefixes = "JKLMNOPQ" suffix = "ack" words = [first + suffix for first in prefixes]
Of course, that’s not quite right because Ouack and Quack are misspelled. Can you fix it?
-
Write a function that will return the number of digits in an integer.
-
Write a function named
reverse_str
that reverses its string argument.
-
Write a function that takes a string and mirrors its argument. For example the mirror of
"good"
is"gooddoog"
.
-
Write a function named
remove_char
that removes all occurrences of a given letter from a string. Do not use thereplace
string method.
-
Write a function that recognizes palindromes. (Hint: use your
reverse
function to make this easy!). Hint: Use all, reversed and zip.
-
Write a function that removes all occurrences of a string from another string. Do not use the
remove
string method in your solution.
-
Create a list containing 100 random integers between 0 and 1000 (use iteration, a comprehension, and the random module). Write a function called
average
that will take the list as a parameter and return the average.
-
Write a Python function that takes
n
andm
as input and returns the maximum value of a the list ofn
random integers between 0 andm
. (Note:. there is a builtin function namedmax
.)
-
Write a function called
mean_normal(n, m, sd)
that computes the mean ofn
randomly selected values taken from a normal distribution with meanm
and standard deviationsd
.
-
Write a function
sum_of_squares(xs)
that computes the sum of the squares of the numbers in the listxs
. For example,sum_of_squares([2, 3, 4])
should return 4+9+16 which is 29:
-
Write a function to count how many odd numbers are in a list.
-
Write a function called
sum_even
that sums up all the even numbers in a list.
-
Sum up all the negative numbers in a list.
-
Write a function called
num_greater_5
that counts how many words in a list have length 5.
-
Use the function from the last problem to count how many words in Emma have length of at least 5. Save your result in a variable named
num_words_emma
.Note: You should clean up such that
- Hyphenated words count as separate words.
- The words contracted with an apostrophe count as two words.
-
Create a function that takes a value n as input and constructs a multiplication table for whole numbers up to \(n\).
-
Print out a neatly formatted multiplication table, up to 12 x 12. You should do this by constructing a string. For full credit, each column should be right-justified and your solution should include only comprehensions and lambda functions. Hint: Write a lambda function pads a number with the right number of spaces.
-
Use list comprehensions to filter the hours table to include only managers. In SQL this would be performed using SELECT and WHERE. Hint: Start by creating a list of the names of all managers. .. actex:: select-where
- hours = [[“Alice”, 43],
- [“Bob”, 37], [“Fred”, 15]]
- titles = [[“Alice”, “Manager”],
- [“Betty”, “Consultant”], [“Bob”, “Assistant”]]
-
Use list comprehensions to decide if the following tables contain a manager that worked at least 40 hours.
-
Use list comprehensions to perform a right outer join on the following lists.
-
Use a list comprehension and lambda expression to create a sequence of functions that combine to average two matrices. A complete solution will provide functions for each level of abstraction.