EGR 103/Spring 2021/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

4.3 Getting Started


4.4 Group In-Lab Autograded Assignments

You should complete these during the lab, or at least by Sunday.

4.4.1 Inspired by P&E 2.1

As noted, there are several ways to think about this one. Come up with a couple of 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 your code to the autograder. Hint the smallest $$n$$ digit number is $$10^{(n-1)}$$. What is the largest $$n$$ digit number?

4.4.2 Multiplication Table

  • Pre-script: The autograder expects the newline "\n" to be at the end of your string so if you *start* each row with "\n" you will need to add a "\n" to the very end before returning the string. If you want to see the control characters, you can use:
    print(repr(stuff))
    
    For example, look at the difference between the following calls:
    In [1]: thing ="Hi\nthere\nfriends!"
    
    In [2]: print(thing)
    Hi
    there
    friends!
    
    In [3]: print(repr(thing))
    'Hi\nthere\nfriends!'
    
  • 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 Individual Gradescope Assignments

4.5.1 Inspired by P&E 2.3.8

The solution to this will require understanding recursion, which requires understanding recursion. To do that, read Chapter 16 (through 16.4) in the Runestone Academy Book. Your program must use recursion to get full credit.

4.5.2 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.5.3 Based on Chapra 3.14

The main concept here is to use logical masks to create piecewise functions. 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 code wrap.

4.6 Individual Lab Report

4.6.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.6.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. Here is the test code and graph from the lab document:

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