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
len
that returns the length of a string, so the value oflen('dongwook')
is 8. Write a function namedright_justify
that takes a string nameds
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') yourFirstName ^ # 70th column
Consider the following function example
do_twice
that takes a function object as an argument and calls it twice:def do_twice(f): f() f()
Here is an example that uses
do_twice
to call a function namedprint_spam
twice:def print_spam(): print 'spam' do_twice(print_spam)
- Type this example into a script and test it.
- 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. - Write a more general version of
print_spam
, calledprint_twice
, that takes a string as a parameter and prints it twice. - Use the modified version of
do_twice
to callprint_twice
twice, passing spam as an argument.
Write a function called
is_sorted
that takes a list as a parameter and returnsTrue
if the list is sorted in ascending order andFalse
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 returnTrue
andis_sorted(['b','a'])
should returnFalse
.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]
.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,
andDongwook
):>>> 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 asDongwook Lee
:>>> 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
True
if it is a palindrome andFalse
otherwise.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 andFalse
otherwise.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.