Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

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.

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?

True

Problem 3

Please correct the logical error in the below code.

10
--- 6.175041198730469e-05 seconds ---
--- 5.1975250244140625e-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

'god yzal eht revo spmuj xof nworb kciuq e'

Problem 5

Define arrays for the following equations

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

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.