HW06: Debugging and Documentation#

Purposes of this assignment is to get more practice with the following:

  • If statements

  • For loops

  • Debugging

  • Function Calls

  • Using objects (class instance) and make a calculation using the objects attributes

Problem 1#

We have learned about functions and loops. In this problem, you will write a function that takes a list of numbers and returns the sum of all the numbers in the list, except for the last number. Below is a start to this problem. Please complete it using a loop.

listofnumbers = [1,3,5,7,9,11,13,15,17,19]
def loopsum(list):
    sum = 0
    for i in list:
        sum += i
    return sum

Can you fix the logical error in the above code?

Problem 2#

Explain how the below code is working. What is the purpose of the if statement? What is the purpose of the for loop? What is the purpose of the return statements?

def is_prime(n):
    if n <= 1:
        return False
    for i in range(2, n):
        if n % i == 0:
            return False
    return True
is_prime(7)
True

Problem 3#

Please correct the logical error in the below code.

def max_of_list(list):
    max_value = list[0]
    for i in range(1, len(list)):
        if list[i] > list[i-1]:
            max_value = list[i]
    return max_value
arrayexample = [3, 4, 5, 6, 7, 8, 9, 10] #this is an example test, you may want to run others
max_of_list(arrayexample)
10
#this returns how much CPU time the function took to run as compared to the internal max function
import time
start_time = time.time()
max_of_list(arrayexample)
print("--- %s seconds ---" % (time.time() - start_time))
start_time = time.time()
max(arrayexample)
print("--- %s seconds ---" % (time.time() - start_time))
--- 4.315376281738281e-05 seconds ---
--- 3.600120544433594e-05 seconds ---

It’s good to discover the documentation of available packages so you don’t reinvent the wheel less efficiently. There already is an internal max function that is faster. (At least when done right it is.)

Problem 4#

Fix the below code to reverse the order of the string

def reverse_string(str):
    reversed_str = ""
    for i in range(len(str) - 1, 1, -1):
        reversed_str += str[i]
    return reversed_str
reverse_string("The quick brown fox jumps over the lazy dog")
'god yzal eht revo spmuj xof nworb kciuq e'

Problem 5#

Define arrays for the following equations

\[\begin{split} \begin{align} y1 &= 2x−3 + 2x−1 \\ y2 &= 2x−3 \\ y3 &= 2x−1 \\ \end{align} \end{split}\]

where x ∈ [1E−2, 1E2] with at least 100 points in each array. Hint: Use the numpy function logspace. Plot x versus y1, y2, and y3 with the following formatting specifications, and save the plot as power law.pdf.

  • Make the plot a log-log plot.

  • Make the default font size 16.

  • Plot y1 as a black solid line of width 2, y2 as a blue dashed line of default width and y3 as a red dotted line of default width.

  • Label the x-axis with a symbol, σ.

  • Label the y-axis with the word “Energy”

  • Title the figure “Power-Law Crossover”

  • Change the x-axis limits to span from 1E−2 to 1E2 and the y-axis from 1E−5 to 1E5

  • Add a legend where y1 is labeled as “exact”, y2 is labeled as “large σ” and y3 is labeled as “small σ”. Hint. You are going to have to read the online documentation to learn how to do some of this formatting. I did this on purpose, so you can get comfortable doing this because it is an important skill.