Homework 4 (Due 6 pm, Monday, 11/06/2017)¶
Please submit your homework to your git repo by 6 pm, Monday, 11/06/2017.
Python provides a built-in function called
lenthat returns the length of a string, so the value of
len('dongwook')is 8. Write a function named
right_justifythat takes a string named
sas a parameter and prints the string with enough leading spaces so that the last letter of the string is in column 70 of the display:
>>> right_justify('yourFirstName') yourFirstName ^ # 70th column
Consider the following function example
do_twicethat takes a function object as an argument and calls it twice:
def do_twice(f): f() f()
Here is an example that uses
do_twiceto call a function named
def print_spam(): print 'spam' do_twice(print_spam)
- Type this example into a script and test it.
do_twiceso that it takes two arguments, a function object and a value, and calls the function twice, passing the value as an argument.
- Write a more general version of
print_twice, that takes a string as a parameter and prints it twice.
- Use the modified version of
print_twicetwice, passing spam as an argument.
Write a function called
is_sortedthat takes a list as a parameter and returns
Trueif the list is sorted in ascending order and
Falseotherwise. You can assume that the elements of the list can be compared with the relational operators
>, etc. For example,
Write a function that takes a list of numbers and returns the cumulative sum; that is, a new list where the i th element is the sum of the first i+1 elements from the original list. For example, the cumulative sum of
Write a function
print_yourNamethat takes first and last names as a list (e.g.,
['dongwook', 'lee']) and capitalizes the first characters of the first and last names:
>>> print_yourName(['dongwook','lee']) Dongwook Lee
Implement the function so that if a second argument
Falseis passed in as an extra argument, then it prints out the full name as Lee, Dongwook (there is a space between
>>> print_yourName(['dongwook','lee'], False) Lee, Dongwook
The default option for the second argument is either without it, or with
True, both of which should print out the full name as
>>> print_yourName(['dongwook','lee']) Dongwook Lee >>> print_yourName(['dongwook','lee'], True) Dongwook Lee
A word is called a palindrome if it is spelled the same backward and forward. Examples are noon, madam, redivider, bob, or racecar. Recursively, a word is a palindrome if the first and the last letters are the same and the middle part is a palindrome. Write a python routine that takes a string argument (i.e., words) and returns
Trueif it is a palindrome and
Write a Python routine that takes three real numbers and check if they can form a triangle. Your routine returns
Trueif they form a triangle and
Let’s now start learning LaTeX. This time, I am only asking you to read through the instructor’s online lecture note on LaTex, as offered as a part of AMS 200 this quarter. Please make sure you install a proper LaTeX distribution on your machine. Later, you’re going to write some homework reports and a final project report using LaTeX, so it is a good time to learn it. The link is found at:
Let’s make your Sphinx webpage look more interesting. One way to start with is to post your homework solutions of problems 1 to 7 to your webpage. Please make sure you post them 24 hours after the deadline, i.e., you can start posting your solutions after 6 pm, Tuesday, 11/7/17 and before 6 pm, Wednesday, 11/8/17. It is recommended that you post your codes in an organized way.