1.6. Exercises#

Note Run the bash script located in the root folder of the training repo to create a folder with interactive versions of these exercises.

These exercises cover basic Python skills.

1.6.1. Exercise 1#

Begin by importing the numpy Python package with the name “np” and the pylab package with the name “plt.” Also import the “os” package for reading and writing to files.

import numpy as np
import pylab as plt
import os

In this exercise, you will consider the Collatz conjecture, which is a famous unsolved problem in mathematics that asks whether repeating two simple arithmetic operations will eventually transform every positive integer into 1.
Write a Python function that takes any positive integer as an argument. Using loops and conditional statements, perform the following:

  • If the integer is even, divide by 2

  • If the integer is odd, multiply by 3 and add 1

  • Continue until convergence (i.e., the integer becomes 1)

  • Output the number of iterations required

def transformer(number):
    number_i = number
    n_iter = 0
    """
    Write code here
    """
    return number, n_iter

transformer(12)

Test your function using each inital guess in the set S = [2, 5, 10, 16, 24, 27, 35, 50]. Use Python plotting functions to plot the number of iterations required for each initial guess (i.e., initial guess on the x-axis and iterations on the y-axis). Lastly, save your plot as “Collatz_exercise.png” and write each initial guess with its corresponding number of iterations to a file called “Collatz.out.”

S = [2, 5, 10, 16, 24, 27, 35, 50]

"""
Write code here
"""

1.6.2. Exercise 2#

Write a Python function that takes any string of characters as an argument and displays characters that are present at an even index number. Your function should output an error if an empty string is passed to the function (i.e., use exception handling). As an example, the string “Python” would result in “y h n” being printed to the console.

def print_even(string_in):
    """
    Write code here
    """
        
print_even("Python")
print_even("")

Test your function using the following set of strings: S = [“Gojackets”, “Call me Ishmael”, “ILoveChBE”]. Also test your function on an empty string to check your error handling.

S = ["Gojackets", "Call me Ishmael", "ILoveChBE"]

1.6.3. Exercise 3#

Write a Python function to compute the income tax on a salary of $58,500 using the following scheme:

  • First $10,000: 0% tax rate

  • Next $15,000: 10% tax rate

  • Remaining: 23% tax rate

def get_tax(income):
    """
    Write code here
    """
    
    return tax

get_tax(58500)

1.6.4. Exercise 4#

Write a Python function that takes a positive integer and prints each digit from the integer in reverse order. As an example, 6572 would print 2756. Test your function with the set S = [1, 15, 658, 2940, 44112].

def print_reverse(number):
    """
    Write code here
    """
    
    print(number_reverse)

print_reverse(6572)

S = [1, 15, 658, 2940, 44112]
for number in S:
    print_reverse(number)

1.6.5. Exercise 5#

Use the Anaconda package manager to download the scikit-learn Python package (https://anaconda.org/anaconda/scikit-learn). Import the “mean_absolute_error” function and calcualte the MAE for the data below (MAE between x and y).

x = [-7,1,5,2,9,-2,0,1]
y = [-6,4,4.5,2,11,-2.1,1,3]