Homework 4 (Due 6 pm, Monday, 11/06/2017)ΒΆ

Please submit your homework to your git repo by 6 pm, Monday, 11/06/2017.

  1. Python provides a built-in function called len that returns the length of a string, so the value of len('dongwook') is 8. Write a function named right_justify that takes a string named s as 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')
                                                                                # 70th column
  2. Consider the following function example do_twice that takes a function object as an argument and calls it twice:

    def do_twice(f):

    Here is an example that uses do_twice to call a function named print_spam twice:

    def print_spam():
          print 'spam'
    1. Type this example into a script and test it.
    2. Modify do_twice so that it takes two arguments, a function object and a value, and calls the function twice, passing the value as an argument.
    3. Write a more general version of print_spam, called print_twice, that takes a string as a parameter and prints it twice.
    4. Use the modified version of do_twice to call print_twice twice, passing spam as an argument.
  3. Write a function called is_sorted that takes a list as a parameter and returns True if the list is sorted in ascending order and False otherwise. You can assume that the elements of the list can be compared with the relational operators <, >, etc. For example, is_sorted([1,2,2]) should return True and is_sorted(['b','a']) should return False.

  4. 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 [1,2,3] is [1,3,6].

  5. Write a function print_yourName that 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 False is passed in as an extra argument, then it prints out the full name as Lee, Dongwook (there is a space between , and Dongwook):

    >>> 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 Dongwook Lee:

    >>> print_yourName(['dongwook','lee'])
     Dongwook Lee
    >>> print_yourName(['dongwook','lee'], True)
    Dongwook Lee
  6. 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 True if it is a palindrome and False otherwise.

  7. Write a Python routine that takes three real numbers and check if they can form a triangle. Your routine returns True if they form a triangle and False otherwise.

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

  9. 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.