DOC PREVIEW
TAMU CSCE 110 - Continuation of Simulations
Type Lecture Note
Pages 4

This preview shows page 1 out of 4 pages.

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

Unformatted text preview:

CSCE 110 1nd EditionLecture 12Outline of Last Lecture:I. SimulationsA. DefinitionB. ExampleC. ExplanationOutline of Current Lecture:I. Continuation of SimulationsII. SetsA. IntroductionB. More properties of setsCurrent Lecture:I. Continuation of SimulationsLet's look at our simulation from the last lecture. Remember, this found the average number of times needed to roll a die in order to see all six sides appear. 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()These notes represent a detailed interpretation of the professor’s lecture. GradeBuddy is best used as a supplement to your own notes, not as a substitute.If you type this exact code into a Python program, you will find that as you increase the range in the function main(), the value printed will get closer to 14.7. 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. Looking back at our program, let's figure out different ways we can write our function single_trial():If we wanted to get rid of the very long "while" and make a cleaner, shorter command, we couldwrite: 1 def single_trial2():2 face = [0] * 73 count = 04 for i in range(1,7):5 while face[i] == 0:6 face[roll_die()] += 17 count += 18 return countLine 4 uses a for loop to check that each index 1-6 of the list, face, is not 0. We haven't learned anything new yet, this is just an example of how programs will always be different when written by different people. There are multiple ways to get the same solution.1 def single_trial3():2 face = [0] * 73 count = 04 while sum(face) != 6:5 face[roll_die()] = 16 count += 17 return countIn this function, each index will be set (and possibly reset) to 1 as they are rolled until all six sides have been rolled, which is signified by the sum of the list being 6 II. SetsA. What are they? Sets are very similar to lists. They are different in that they are put in parentheses, with the elements in brackets within, the elements of the set have no order, and there cannot be a multiple of the same element in a list (it will only be listed once). Say we have this in a Python Shell:>>> x = set()>>> x>>> set([])>>> x.add(14)>>> x>>> set([14])>>> x.add('python')>>> x>>> set(['python', 14])>>> x.add(14)>>> x>>> set(['python', 14])Notice we can add elements to a set with nameofset.add(elementyouwantadded). Also, notice how we tried to add 14 a second time to the set, and yet when we called the set after, it only had 14 as an element once. This is because once an element is in a set, it cannot beadded again. Now, let's write another function to perform the same action as single_trial() from earlier:1 def single_trial4():2 face = set()3 count = 04 while len(face) < 6:5 face.add(roll_die())6 count += 17 return countHere, we have a set, instead of a list, called face. Because sets only have each element once, we can use the loop to continue running until the length of the set is 6, meaning it has 6 different elements representing the six different sides of the die.B. More properties of setsThe intersection of two sets will be a new set that contains only the elements that are in both oftwo given sets. The union of two sets will be a new set that contains the elements that are in either of the two sets (but remember, no repeated elements). The difference of two sets will be a new set that contains elements that are only in the first set and not the second. 1 A = set(['apple', 'python', 'bear', 13.1, 14])2 B = set(['apple', 'tiger', 20])3 C = set(['hello'])45 print 'Intersection:', A.intersection(B)6 print 'Intersection:', B.intersection(A)7 print 'Union:', A.union(B)8 print 'Union:', B.union(A)9 print 'Difference (A - B):', A.difference(B)10 print 'Difference (B - A):', B.difference(A)11 print 'Intersection (A,C):', A.intersection(C)The output is below:Intersection: set(['apple'])Intersection: set(['apple'])Union: set(['apple', 'python', 13.1, 'bear', 'tiger', 20, 14])Union: set(['apple', 'python', 13.1, 14, 'tiger', 20, 'bear'])Difference (A - B): set(['python', 13.1, 'bear', 14])Difference (B - A): set(['tiger', 20])Intersection (A,C): set([])Note that unlike for intersection and union, the order of the sets does affect the output when using the difference operation. We can also look at membership. Here is an example in Python shell, but it isn't a new concept:>>> 'apple' in B>>>


View Full Document

TAMU CSCE 110 - Continuation of Simulations

Type: Lecture Note
Pages: 4
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 Continuation of Simulations
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 Continuation of Simulations 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 Continuation of Simulations 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?