EGR 103/Fall 2020/Lab 4

From PrattWiki
Revision as of 16:32, 10 September 2020 by DukeEgr93 (talk | contribs) (4.4 Edfinity)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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 Edfinity

Here are some hints for some of the specific Edfinity problems. The number before the dot refers to the assignment number and the number after the dot refers to the problem within that assignment.

  • 2.3: Remember to convert the input from a string to an integer
  • 2.8: Be sure to use math versus numpy
  • 3.5: Note STRING.count(“x”) will return how often the character x appears in STRING
  • 3.6: STRING.join(LIST) will connect each of the strings contained in LIST by putting the characters in STRING between them. For example:
    In [1]: "BLAH".join(["a", "bee", "see"])
    Out[1]: 'aBLAHbeeBLAHsee'
    
  • 3.7: Be careful about how the subscripts (1-indexed) and list indexes (0-indexed) relate.
  • 3.8: Look at the CS 101 reference sheet for useful functions involving strings!
  • 4.3: You can ask if a character or substring is contained in another string using the "in" operator. For example:
    In [1]: "a" in "heart"
    Out[1]: True
    
    In [2]: "art" in "heart"
    Out[2]: True
    
    In [3]: "rat" in "heart"
    Out[3]: False
    
    In [4]: "hearth" in "heart"
    Out[4]: False
    
  • 4.8: See in again.

4.5 Individual In-Lab Autograded Assignments

You should complete these during the lab, or at least by Friday. The hard deadline is Sunday.

4.5.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.5.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.6 Individual Gradescope Assignments

4.6.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.6.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.6.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.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. 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