List exercise

The slice operator can take a third argument that determines the step size, so t[::2] creates a list that contains every other element from t. If the step size is negative, it goes through the list backward, so t[::-1] creates a list of all the elements in t in reverse order. Use this idiom to write a one-line version of is_palindrome

 

                       

 

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 (as a precondition) that the elements of the list can be compared with the comparison operators <, >, etc.

For example, is sorted([1,2,2]) should return True and is sorted([’b’,’a’])

should return False

 

.

 

from Thinking in Python

posted @ 2014-07-13 19:55  平静缓和用胸音说爱  阅读(193)  评论(0编辑  收藏  举报