EGR 103/Spring 2020/Lab 4

From PrattWiki
Jump to navigation Jump to search

Errors / Additions

None yet!

4.1 Introduction

The main purpose of this lab will be to look at selective and iterative structures and to learn about logical masks. The following is a companion piece to the lab handout itself. The sections in this guide will match those in the handout.

4.2 Resources

See main EGR 103 page for links to these

4.3 Getting Started

EGR_103/Getting_Started

BASH Shortcuts

During lab, we will set up your account to have two new Unix shortcuts to make working with labs a little easier. To make this work, you will need to follow the instructions at BASH Shortcuts to:

  • Create a ~/.bashrc file in your home directory with the contents described on BASH Shortcuts
  • Create a ~/.bash file in your home directory with the two functions (get103 and ltx) described on BASH Shortcuts

From this point forward, every time you log in those shortcuts will be available. The files will be in tar file you copied for this week; after you expand the tar file, you can type:

cp -i .bash_profile ~
cp -i .bashrc ~

to copy the files into your home directory. To make them available immediately without having to log out and log back in, you will need to type

source ~/.bashrc

4.4 Ungraded

These problems are good ones to look at and to solve but are not a part of the graded work. Given that, you can absolutely consult with classmates about how to get the answers to these ungraded problems but you may not share those answers outside of the members of the class.

4.5 Group-Based In-Lab Autograded Assignments

You can work in groups of two or three people are are physically present in the lab for these. Remember that whoever submits the program should also add the group members to that submission; group members can get a copy of the program from Gradescope by looking at the submission and downloading the code.

4.5.1 Based on P&E 2.1 * Due in lab

As noted, there are several ways to think about this one. Come up with a couple different ways that you the human would solve the problem and then figure out how to get the computer to take the same steps. You should write several test cases for yourself before submitting to the autograder. Hint the smallest $$n$$ digit number is $$10^{(n-1)}$$. What is the largest $$n$$ digit number?

4.5.2 Based on P&E 4.48 * Due in lab

  • The heart of this code will be nested for loops and proper format specifiers to make sure things fit. The spacing needs to be exact to pass the tests. You will basically be creating one giant string - the good news is you can keep appending to a variable that has a string in it with code like
big_thing = big_thing + "new thing"

or

big_thing += "new ending"
  • '{:5}'.format(x) will create a string with the number x in it, reserving 5 spaces for it
  • Try to get the core of the table before figuring out how to add row or column indices
  • A double for loop is very useful here; the general structure is:
# Stuff to do before any rows
for row in ROWS:
    # Stuff to do at the start of each row
    for col in COLS:
        #Stuff to do in each column of the row
    # Stuff to do at the end of each row
# Stuff to do after all rows

Here is a trinket that might help illuminate how all this works; you may want to move the divider between the code and the output all the way to the left to see things print properly.

4.5.3 Based on Chapra 3.14 * Due Friday 2/14

The main concept here is to use logical masks to create piecewise functions; we will go over that in lab. The function itself actually only needs to be one line of code! One really, really long "line" of code, but still... Also - all the parentheses are important - each relational operator needs a set, the logical operators need a set, and the products need a set. And if you go over one line, you will need another set on the outside to get Python to codewrap.

4.6 Individual Autograded Work

4.6.1 Based on P&E Project 2.4

Once again, work a few examples by hand before trying to write the function. Also put test cases in your code - for instance, the specific case given in the problem might be a good one to check, especially since your function should be returning the evolution of the A and B values. There are different ways to keep track of all the things you need to report back - when you write things out by hand, note how many different "variables" you create on paper.

4.6.2 P&E 2.3.8

The solution to this will require understanding recursion, which requires understanding recursion. To do that, read pages 687-694 in the Punch & Enbody Book and play with the programs provided. Your program must use recursion to get full credit.

4.6.3 Geometric Progressions

Among other things, this problem looks at input validation. You will definitely want to check out the section on While Loops on the Python:Iterative_Structures for help in identifying type errors with the input. That section is much more complicated than what you need, however, because you are not taking inputs nor are you giving a second chance! Your code will likely resemble the code in the following, which is a one argument function that first sees if the value given can be turned into a float. If it can, the function returns the number squared; if it cannot, the function returns -1.

def fun(a):
    try:
        a = float(a)
    except Exception as uhoh:
        print('Not floatable')
        return -1

    return a**2

Here are sample runs:

In [1]: fun(3)
Out[1]: 9.0

In [2]: fun('hi')
Not floatable
Out[2]: -1

You will also want to make sure you think about how you will be checking for valid inputs and then write and test the code for each problem case. For valid arguments, work the problem by hand and once again think about what you are storing and how on your paper.

4.7 Individual Lab Report

4.7.1 Sinusoidal Functions

See the Python:Plotting page as well as the matplotlib.pyplot.plot page for information about the additional pieces of information you can give the plt.plot() (or, in our case, ax.plot()) command to change sizes and colors.

4.7.2 Chapra 3.10 / Beam Deflection

The main concepts here are using logical masks to create piecewise functions, using different sets of points for mathematical analysis versus graphing, and determining and locating extrema as discussed in Plotting.

General Concepts

This section is not in the lab report but rather has some items in it that span multiple problems in the lab.

PythonTutor is your friend

If you are having a hard time tracing how your program is executing, you may want to put your function in [pythontutor.com] to get a sense of what the various variables are doing.

Determining and Locating Extrema

See Python:Plotting#Using_Different_Scales for some examples.

Logical Masks

See Python:Logical Masks.


Class Document Protection