EGR 103/Fall 2022/Lab 6

From PrattWiki
Jump to navigation Jump to search

The following document is meant as an outline of what is covered in this assignment.

Typographical Errors

None yet!

APT

Acronym

  • Note that you need to name the file CreateAcronym.py while the function is named acronym
  • How can you split a string up based on a particular character? (perhaps I've said too much?)

BagFitter

  • Be sure to do this by hand yourself first and figure out what process you are using.
  • You may be asking yourself, "Wouldn't it be great if there were a way to get a collection of the unique items in a list?" And the answer is - it would be great! And so there is. A set is a collection of elements where each element is unique. If you cast some other collection (for example, a list or a string) as a set, the recast version will have as many elements as the number of unique entries in the original. If the original contains all the same type, the set will be ordered.
In [1]: set([1, 2, 3, 4, 5, 2, 4, 2])
Out[1]: {1, 2, 3, 4, 5}

In [2]: set("hello")
Out[2]: {'e', 'h', 'l', 'o'}

In [3]: food = ['DAIRY', 'DAIRY', 'PRODUCE', 'PRODUCE', 'PRODUCE', 'MEAT']
In [4]: set(food)
Out[4]: {'DAIRY', 'MEAT', 'PRODUCE'}
  • A set is an iterable object
  • You may also be asking yourself, "Wouldn't it be great if there were a way to figure out how many times within a collection a particular value appears?" And the answer is - it would be great! And so there is! Iterables have a .count(VAL) method that you can use:
In [1]: nums = [1, 2, 3, 4, 5, 2, 4, 2]
In [2]: nums.count(2)
Out[2]: 3

In [3]: word = "hello"
In [4]: word.count("l")
Out[4]: 2

In [5]: state = "Mississippi"

In [6]: state.count("s")
Out[6]: 4

In [7]: state.count("ss")
Out[7]: 2

In [8]: state.count("sip")
Out[8]: 1

In [33]: food = ['DAIRY', 'DAIRY', 'PRODUCE', 'PRODUCE', 'PRODUCE', 'MEAT']

In [34]: food.count("PRODUCE")
Out[34]: 3

CarrotBoxes

  • You may be asking yourself, "Wouldn't it be great if there were a way to figure out where in a collection a particular value appears?" And the answer is - it would be great! And so there is! Iterables have a .index(VAL) method that you can use; note that it will only give the index of the first entry in the collection that matches the VAL:
In [1]: nums = [1, 2, 3, 4, 5, 2, 4, 2]
In [2]: nums.index(2)
Out[2]: 1

In [3]: word = "hello"
In [4]: word.index("l")
Out[4]: 2

In [5]: state = "Mississippi"

In [6]: state.index("s")
Out[6]: 2

In [7]: state.index("ss")
Out[7]: 2

In [8]: state.count("sip")
Out[8]: 5

In [33]: food = ['DAIRY', 'DAIRY', 'PRODUCE', 'PRODUCE', 'PRODUCE', 'MEAT']

In [34]: food.index("PRODUCE")
Out[34]: 2

MorselikeCode

If only we had done something similar to this in class... Wait!

Pikachu

  • There are several ways to tackle this problem! Try to think of two different methods and evaluate them for yourself before writing the code.
  • Be careful how you process things! "pkai" and "chpiu" should both yield NO, for instance, so you can't just start looking for and removing instances of "pi," "ka," and "chu."

Individual Lab Report

  • Be sure to put the appropriate version of the honor code -- if you use the examples from Pundit, the original author is either DukeEgr93 or Michael R. Gustafson II depending on how you want to cite things.
  • Note that you will need to add plt.ion() after you import matplotlib.pyplot to make sure your codes interactively update.

Chapra 2.22

  • Don't stare at the top half of the figure too long, or you will get sleepy. Very sleeeeeeepy…
  • Since you are adding two different kinds of axes, you will need to create the figure first and then create two different axis handles - a regular one for the top and a 3-D one for the bottom.
  • Use fig.set_size_inches(6, 8, forward=True) to make the graph the correct size.
  • Don't forget fig.tight_layout()

Chapra 3.9

  • To see all the colormaps, after importing the cm group just type
help(cm)
to see the names or go to Colormap Reference to see the color maps - only the ones listed with the help command are actually installed. Avoid the qualitative maps, flag, and prism.

Chapra 15.5

  • You will need to convert the original data sets into appropriately-shaped matrices before plotting. When you load the data set, you will get a 2D array of values that has the $$c$$ values in the first column, the $$T$$ values in the second column, and the $$OC$$ values in the third column. Conveniently, the values are in order as you traverse each row of the table in order. That is to say, Table P15.5 in the book is represented by the data set:
       0        0    14.60
      10        0    12.90
      20        0    11.40
       0        5    12.80
      10        5    11.30
      20        5    10.30
       0       10    11.30
      10       10    10.10
      20       10     8.96
       0       15    10.10
      10       15     9.03
      20       15     8.08
       0       20     9.09
      10       20     8.17
      20       20     7.35
       0       25     8.26
      10       25     7.46
      20       25     6.73
       0       30     7.56
      10       30     6.85
      20       30     6.20
Notice how $$c$$ goes from 0 to 10 to 20 and back again as you go through the first three rows of the data set, which represent the first three entries in the first row of the table. The $$T$$ value stays constant at 0 for those three items and the $$OC$$ value goes from 14.6 to 12.9 to 11.4.
To get this into an array, you will need to use the np.reshape() command. This command takes two arguments - an array or list and a tuple specifying what shape to make a new array. The shape must have the appropriate number of rows and columns for the original data set. Here is an example of a one-dimensional array of sixteen elements being reshaped into 5 different possible two-dimensional arrays:

Notice that the way Python fills the new array is to fill the first row first and then continue on to the next column. Fortunately, this is exactly what you need to do to reshape the information for $$c$$, $$T$$, and $$OC$$ into 7 by 3 two-dimensional arrays for graphing.
  • Here is an example loading a simple data set, grabbing information from each column, converting the information into arrays, and making a surface plot. The data set is a text file -- you can look at it in the second tab of the Trinket. This code will produce the graph at the end of Python:Plotting_Surfaces#Individual_Patches; the view command is there to make the patches easier to see.

  • Use the ax.set() command for labels and tick locations and remember the kwargs to set tick locations are xticks, yticks, and (though not needed here) zticks; those will take an array or list of where you want ticks.

Sphere

  • It should look like a sphere! Use fig.set_size_inches(6, 6, forward=True) to make the graph the correct size - if the figure window isn't square, the sphere will not actually look...spherical.
  • Don't forget to make it not blue!