Created
          October 20, 2016 02:21 
        
      - 
      
- 
        Save jjlumagbas/aa5c7080d802d11e72d574b458cb5985 to your computer and use it in GitHub Desktop. 
    Lists and for-loops
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | # sum | |
| def sum(lst): | |
| """[Number] -> Number | |
| Returns the sum of all elements of the list | |
| """ | |
| result = 0 | |
| for el in lst: | |
| result = result + el | |
| return result | |
| print(sum([1, 2, 3, 4, 5, 8])) | |
| print("---") | |
| # join | |
| def join(lst): | |
| """[String] -> String | |
| Concatenates all the elements of the list | |
| into a single string | |
| """ | |
| result = "" | |
| for el in lst: | |
| result = result + el | |
| return result | |
| print(join(["abc", "wxyz", "def"])) # "abcwxyzdef" | |
| print("---") | |
| # reverse | |
| def reverse(lst): | |
| """[a] -> [a] | |
| Returns a list that is a reverse of lst | |
| """ | |
| result = [] | |
| for el in lst: | |
| result = [el] + result | |
| return result | |
| print(reverse([1, 2, 3, 4, 5])) # [5, 4, 3, 2, 1] | |
| print(reverse(["abc", "wxyz", "def"])) # ["def", "wxyz", "abc"] | |
| # min | |
| def min(lst): | |
| smallest = lst[0] | |
| for el in lst: | |
| if (el < smallest): | |
| smallest = el | |
| return smallest | |
| print(min([5, 3, 2, 8, 7])) | |
| def display_lengths(lst): | |
| for el in lst: | |
| print(len(el)) | |
| display_lengths(["abc", "wxyz", "def"]) | |
| def display(lst): | |
| for el in lst: | |
| print(el) | |
| display([1, 2, 3, 4, 5]) | |
| display(["abc", "wxyz", "def"]) | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment