EGR 103/Spring 2023/Lab 4

From PrattWiki
Revision as of 23:54, 8 February 2023 by DukeEgr93 (talk | contribs) (Created page with "<div class="noautonum">__TOC__</div> == Errors / Additions == None yet! == 4.1 Introduction == The main purpose of this lab will be to look at selective and iterative struct...")
(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. 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 Assignments

4.4.1 APT

Once again, you are allowed to work on these in small groups. Remember that you cannot use numpy for these!

  • Laundry
    • The narrative for the third example is a little more complicated than it needs to be. You can fold the first load while the second load is drying and the third load is washing. Come up with a formula first, see if it works on the sample cases, then write the code.
  • DNAcgratio
    • The len() command may be useful for getting the total number of characters in a string.
    • Think about how to use a loop to check letters and keep track of how many times a "c" or "g" pops up.
  • Pancakes
    • This one tends to be "the complicated one to think about"
  • Bagels
    • Think about how to use the loop to go through the list.
    • What's a quick way to figure out how many extra bagels there might be for any given order?
  • ScoreIt
    • There are several different ways to solve this problem - the constraints (always 5 dice, always numbers [1, 6] should help.
    • There is a list.count(item) command that could come into play. For instance, [1, 3, 2, 5, 2, 4, 2, 1].count(2) returns 3 since the integer 2 appears thrice in the list.

4.4.2 Group Gradescope Problems

You really should work to complete these during the lab.

4.4.2.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? Think about using a loop and remember what mathematical operators like % and // do in Python.

4.4.2.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 and returning, but not printing, 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 ending"

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.4.3 Individual Lab Report

4.4.3.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.4.3.2 Based on P&E 1.35

  • Carefully consider what the equations need to look like for the angles before writing them.
  • Test your code with some obvious triangles (hint: once you import numpy as np, you can use np.sqrt()...and you probably know some things about a 1-1-1 triangle and a 1-1-np.sqrt(2) triangle!)
  • The input values for trig functions and the output values for inverse trig function in numpy are in radians.
  • Get the math working before trying the drawing, then spend some time figuring out how to create the line (hint: it takes four points to draw a closed triangle)
  • There should be a default case for whether or not to make the drawing.
  • Make sure your graphics are set to "Automatic" - see Python:Plotting#Python_Settings
  • Do not use the plt.show() or fig.show() command in your code!

4.4.3.3 Random Numbers

  • Note that you already have the code for getting a NetID, converting the NetID to a seed for the random number generator, and calculating the number of bins for the histogram. You will need to change the nums = line to get input from the user.
  • I also included the code for plotting histograms with a particular number of bins. A histogram will basically provide an accounting of how many values in your data set fall within a particular range. For example, we could take the birth dates of every person in the class, then bin them by which month in a year someone was born and create a histogram with 12 values: one for each month of the year. Or we could bin them by season and have four different values. The starter code automatically calculates how many bins to use based on the base-10 logarithm of the number of values you are looking at. See matplotlib.pyplot.hist from matplotlib.org for more information.
  • I did not include the code to make the distributions. They are in the np module, in the random groups, and the actual commands have names that completely make sense for generating uniform or normal distributions, respectively.
    • Oops, I've said too much...
  • I also did not include the lines about printing. Make sure you use the correct formats!
  • There are some commands in the script that we haven't completely covered in class - they are:
    • m.ceil: rounds a single number
      • We are going to use m.ceil() instead of np.ceil() because the NumPy version returns a float instead of an int and the number of bins has to be an integer for histograms to work.
    • m.log10: calculates the base 10 logarithm of a single number
On checking your random numbers

You can check your work by using mrg and 5000 for the input responses - you should get the values in the lab handout. However - this assumes you calculated the uniformly distributed numbers first and the normally distributed numbers second! Calculating them this way gives you:

Information for 5000 random numbers for mrg:
Uniform: min: +8.509e-05 avg: +5.020e-01 max: +9.998e-01
Normal:  min: -4.292e+00 avg: -7.668e-04 max: +3.634e+00

as listed in the lab manual. If you calculated them in the other order (normal and then uniform), you would get:

Information for 5000 random numbers for mrg:
Uniform: min: +6.652e-05 avg: +5.001e-01 max: +1.000e+00
Normal:  min: -3.272e+00 avg: -8.981e-03 max: +3.660e+00

and the histograms will be slightly different as well. We will accept either set of answers! Also - this has not been tested on different platforms - if the random number generators behave differently on OSX, Win, and Linux, I will note that here. So far - no evidence of differences.


General Concepts

This section is not in the lab report but rather has some items in it that span multiple problems in the lab. No content so far, but will be amended as needed!



Class Document Protection