Lecture 3, Part 2 Exercises

Before starting, please run the following cell

In [ ]:
from __future__ import division, print_function 

Note: This lab contains a collection of exercises to get more comfortable with writing functions. Exercises within the same question may not seem as related as exercises in previous labs.

Question 1

1.1

Create a string called my_string with value "addiscoder".

1.2

Print the lenght of my_string.

1.3

Given the list of strings below, use a for loop to print each string in the list.

In [ ]:
my_strings = ["tibs", "kitfo", "shiro", "injera"]

# Your code here:

1.4

Use a for loop to print the length of each string in my_strings.

1.5

Create a function that takes a list of strings and prints the length of the longest string in the list.

In [ ]:
def longest_string(lst):
    # Your code goes here
    
longest_string(my_strings)

# answer: 6

Question 2

2.1

Create a list called my_list containing 5 integers.

2.2

Find the length of my_list using len.

2.3

Use modulo to check that 4 is even and print True if it is.

2.4

Using a for loop and range, print every element in my_list

2.5

Write a function that takes a list of integers and prints the odd numbers in the list.

In [ ]:
def odd_numbers(lst):
    # Your code here
    
print(odd_numbers([1, 5, 23, 8, 9]))

# answer: 
# 1
# 5
# 23
# 9

2.6

Write a function that takes a list of integers and returns a list containing the elements in odd positions in the input list.

In [ ]:
def odd_positions(lst):
    # Your code here
    
print(odd_positions([1, 5, 23, 8, 9]))

# answer: [5, 8]

Question 3

3.1

Write a function that takes a list of integers as input and returns the sum of the numbers in the list. You can use the skeleton below if you are stuck.

In [ ]:
# Create a list called my_list that has 10 integer values.

# This is the function header for sum_list which takes in a list argument as lst.
def sum_list(lst):
    # Create a loop that sums the elements in lst
    
sum_list(my_list)

3.2

Write a function that takes an integer x and returns the sum of all numbers from 0 - x (inclusive).

In [ ]:
# This is the function header for sum_to_x which takes in an integer argument as x.
def sum_to_x(x):
    # Your code goes here

# This will check your function against the correct sums for some example inputs
print(sum_to_x(10))
print(sum_to_x(4))
print(sum_to_x(100))

# answer: 55
# answer: 10
# answer: 5050

Question 4

4.1

Write a function that takes two lists as input and combines them in alternating order.

Ex: [a,b,c], [1,2,3] → [a,1,b,2,c,3]

Note: The input lists will be the same length.

In [ ]:
# This is the function header for combine_lists which takes in two lists and returns the combined lists 
# in alternating order
def combine_lists(lst1, lst2):
    # Create new list to store values from both lists.
    
    # Create a loop to go through lists and combine into one list.
    
print(combine_lists([a, b, c], [1, 2, 3]))
print(combine_lists(['hello', 'pineapple', 'mango', 'strawberry'], [4, 7, 3, 5]))

# answer: [a, 1, b, 2, c, 3]
# answer: ['hello', 4, 'pineapple', 7, 'mango', 3, 'strawberry', 5]

4.2

Write a function that takes an integer x and returns a list of all multiples of 3 between 0 and x (inclusive).

In [ ]:
# This is the function header for mults_of_3, which takes in an integer x
def mults_of_3(x):
    # Your code goes here
    

mults_of_3(15)

# answer: [3, 6, 9, 12, 15]

Question 5

5.1

Write a function that takes a list of integers and returns the largest number in the list.

In [ ]:
# This is the function header for largest_in_list which takes in a list lst
def largest_in_list(lst):
    # Create a variable to store the largest number in the list
    
    # Find the largest number in list and store it
    
    # Return largest number in list
    
largest_in_list([1, 7, 2, 10])

# answer: 10

5.2

Write a function that takes a list of integers and returns True if the list is strictly increasing and False otherwise.

Note: A list is strictly increasing if every number in the list is larger than the number before it.

Ex: [1, 2, 3, 4] → True

Ex: [1, 2, 2, 3] → False

In [ ]:
# This is the function header for is_increasing which takes in a list lst
def is_increasing(lst):
    # Your code goes here
    
print(is_increasing([1, 2, 3, 4, 5]))
print(is_increasing([1, 3, 2, 5]))

# answer: True
# answer: False

Question 6

6.1

Write a function that takes in a string x and returns True if it is a palindrome and False otherwise.

In [ ]:
def is_num_palindrome_strings(x):
    # Your code goes here
    
    
print(is_num_palindrome_strings('1331'))
print(is_num_palindrome_strings('40000'))
print(is_num_palindrome_strings('10'))

# answer: True
# answer: False
# answer: False

6.2

Write a function that takes in an integer x and returns True if it is a palindrome and False otherwise.

In [ ]:
def is_num_palindrome(x):
    # Your code goes here
    
    
print(is_num_palindrome_strings(1331))
print(is_num_palindrome_strings(40000))
print(is_num_palindrome_strings(10))

# answer: True
# answer: False
# answer: False

Question 7

7.1

Write a function that takes an integer x and prints a right triangle with height x.

Ex:

build_right_triangle(4)  
#
##
###
####
In [ ]:
def build_right_triangle(x):
    # Your code goes here

build_right_triangle(4)    

7.2

Write a function that takes an integer x and prints an x by x checkerboard of hashes.

Ex:

build_checkerboard(4)  
# # # #
# # # #
# # # #
# # # #
In [ ]:
def build_checkerboard(x):
    # Your code goes here
    
build_checkerboard(4)

7.3

Write a function that takes an integer x and prints a pyramid with height x.

Ex:

build_pyramid(4)  
   #
  # #
 # # #
# # # #
In [ ]:
def build_pyramid(x):
    # Your code goes here
        
build_pyramid(4)