DOC PREVIEW
TAMU CSCE 110 - Exam 2 Study Guide
Type Study Guide
Pages 10

This preview shows page 1-2-3 out of 10 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CSCE 110 1nd EditionExam # 2 Study Guide Lectures: 11 - 17Lecture 11 (October 10)SimulationsHow many times will we have to roll a die before all six faces are seen? 14.7We can support this both mathematically and with a simulation on Python, which portrays a realistic situation/experiment based on random rolls. If the program below is run, we will get a number around 14.7 (the higher the range is in line 13, the closer we get, as accuracy and precision is increased with a higher number of simulated experiments. 1 >>> import random2 >>> def roll_die():3 >>> return random.randint(1,6)4 >>> def single_trial:5 >>> face = [0]*76 >>> count = 07 >>> while face[1] == 0 or face[2] == 0 or face[3] == 0 or face[4] == 0 or face[5] == 0 or face[6] == 0:8 >>> face[roll_die()] += 19 >>> count += 110 >>> return count11 >>> def main():12 >>> total = 013 >>> for i in range(0,1000):14 >>> total += single_trial()15 >>> print "Average number of rolls: ", total/100016 >>> main()This can be backed up mathematically, by adding the theoretical number of rolls needed:Number of Unseen Faces Number of Rolls Needed:6 6/6, or 15 6/54 6/4, or 3/23 6/3, or 22 6/3, or 31 6/1, 6Notice that the number of rolls needed should be 6 (the number of sides on the die) divided by the number faces that haven't been seen yet. Lecture 12 (October 15) Properties of sets1 def f1(s1, s2, op):2 if op == ’i’:3 result = s1.intersection(s2)4 elif op == ’u’:5 result = s1.union(s2)6 elif op == ’d’:7 result = s1.difference(s2)8 else:9 result = s1.issubset(s2)10 return result11 def main():12 set1 = set([’apple’, 5, ’banana’, 7])13 set2 = set([’banana’, 5])14 print len(set2) # Line A15 print f1(set1 , set2 , ’i’) # Line B16 print f1(set2 , set1 , ’i’) # Line C17 print f1(set1 , set2 , ’u’) # Line D18 print f1(set2 , set1 , ’u’) # Line E19 print f1(set1 , set2 , ’d’) # Line F20 print f1(set2 , set1 , ’d’) # Line G21 print f1(set1 , set2 , ’p’) # Line H22 print f1(set2 , set1 , ’p’) # Line I23 main()a) What output does Line A produce? 2This simply prints the length of the set 2, or the number of elements in set 2.b) What output does Line B produce? set (['banana', 5])The 'i' in line 15 signifies that we will take an intersection in the user-defined function "f1()". Because an intersection makes a set that includes only the elements which are in BOTH sets given (does not rely on the order of the sets), the output of line B is set (['banana', 5]). Remember, it creates a new set, with the elements in brackets, within parentheses.c) What output does Line C produce? set (['banana', 5])Remember, the built-in intersection function for sets does not change when you switch the order of the two sets given. Therefore, the output is the same output we got from Line B.d) What output does Line D produce? set ([5, 7, 'banana', 'apple'])The 'u' in line 17 signifies that we will take a union of the two sets in the user-defined function "f1()". This does not depend on the order of the two sets. It will make a new set with the elements that are in either set, but will not add it to the new set twice if it is an element of bothsets. e) What output does Line E produce? set ([5, 7, 'banana', 'apple'])Union does not depend on the order of the two sets given, so we get the same output that we got from Line D.f) What output does Line F produce? set (['apple', 7])The 'd' in line 17 signifies that we will take the difference of the two sets in the user-defined function "f1()". This function does depend on the ordering. Line 19 signifies that we will make a set that contains all the elements of set 1 that are NOT in set 2 as well. g) What output does Line G produce? set ([])Because this function does depend on the ordering, we will get a different output than we did from Line F. Line 20 signifies that we will make a set that contains all the elements of set 2 that are NOT in set 1 as well. As set 2 is a subset of set 1, we will get the empty set here.h) What output does Line H produce? FalseThe 'p' in line 21 signifies that we will check a Boolean expression stating that set 1 is a subset ofset 2. Because not all elements of set 1 are in set 2, set 1 is not a subset of set 2, and we get False. This also depends on the order of the two sets in the function. i) What output does Line I produce? TrueThe 'p' in line 22 signifies that we will check a Boolean expression stating that set 2 is a subset ofset 1. All elements of set 2 are in set 1, so set 2 is a subset of set 1, and we get True.>>> x = set()>>> x.add(14)>>> x.add('python')>>> x.add(14)>>> print xWhat will be the output?>>> set(['python', 14])Remember, sets only contain one of each element. We successfully added 14 and 'python' to the set, but when we tried adding 14 again, nothing happened. Lecture 13 (October 17) Plotting graphs1 import matplotlib.pyplot as plot2 def draw_plot(x_axis , y_axis , x_ticks , y_ticks):3 plot.plot(x_axis , y_axis , marker=’o’, label=’a simple line’)4 plot.xlabel(’x axis’)5 plot.ylabel(’y axis’)6 plot.title(’a simple plot’)7 plot.xticks(x_ticks)8 plot.yticks(y_ticks)9 plot.legend()10 plot.grid(True)11 plot.show()12 def main():13 step = 1 # Line A14 x_list = range(5)15 y_list = [3, 2, 5, 0, 2]16 xticks_list = [0, 1, 2, 3, 4]17 yticks_list = range(min(y_list), max(y_list) + 2, step)18 print yticks_list # Line B19 draw_plot(x_list , y_list , xticks_list , yticks_list) # Line C20 main()a) What output does Line B produce?[0, 1, 2, 3, 4, 5, 6]b) What output does Line C produce? The graph will look like this:c) What is the effect on the program if Line A is changed to step = 2Notice that the only thing on the graph that changed was the ticks on the y-axis and the resulting grid size. The output of line B will also change to be [0, 2, 4, 6].Lecture 14 (October 22)Importing comma-separated values for a graph.This is a program that will graph the results of the annual hotdog-eating contest, with the year vs. the winning number of hotdogs.1 import matplotlib.pyplot as plot2 def plot_results(x_axis, y_axis):3 plot.plot(x_axis, y_axis, label='curve label', marker='o')4 plot.xlabel('Year')5 plot.ylabel('number of hotdogs eaten')6 plot.title('Nathan\'s Hotdog Eating Contest')7 plot.grid()8 plot.legend(loc='upper left')9 plot.savefig('hotdogs-eaten.pdf')10 plot.show() 11 def main():12 filename = raw_input('Enter filename: ') 13 open_file = open(filename, 'r')14 #data = [open_file.read()]15 data =


View Full Document

TAMU CSCE 110 - Exam 2 Study Guide

Type: Study Guide
Pages: 10
Documents in this Course
06-IO

06-IO

29 pages

21-OOP

21-OOP

8 pages

key

key

6 pages

21-OOP

21-OOP

8 pages

Load more
Download Exam 2 Study Guide
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Exam 2 Study Guide and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Exam 2 Study Guide 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?