New User Special Price Expires in

Let's log you in.

Sign in with Facebook


Don't have a StudySoup account? Create one here!


Create a StudySoup account

Be part of our community, it's free to join!

Sign up with Facebook


Create your account
By creating an account you agree to StudySoup's terms and conditions and privacy policy

Already have a StudySoup account? Login here


by: Orrin Rutherford


Marketplace > Portland State University > ComputerScienence > CS 199 > SPST COMP SCI FOR BEGINNERS
Orrin Rutherford
GPA 3.91

Lois Delcambre

Almost Ready


These notes were just uploaded, and will be ready to view shortly.

Purchase these notes here, or revisit this page.

Either way, we'll remind you when they're ready :)

Preview These Notes for FREE

Get a free preview of these Notes, just enter your email below.

Unlock Preview
Unlock Preview

Preview these materials now for free

Why put in your email? Get access to more of this material and other relevant free materials for your school

View Preview

About this Document

Lois Delcambre
Class Notes
25 ?




Popular in Course

Popular in ComputerScienence

This 42 page Class Notes was uploaded by Orrin Rutherford on Tuesday September 1, 2015. The Class Notes belongs to CS 199 at Portland State University taught by Lois Delcambre in Fall. Since its upload, it has received 40 views. For similar materials see /class/168277/cs-199-portland-state-university in ComputerScienence at Portland State University.

Similar to CS 199 at PSU

Popular in ComputerScienence




Report this Material


What is Karma?


Karma is the currency of StudySoup.

You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!

Date Created: 09/01/15
Beyond programming CS I99 Computer Science for Beginners Spring 2009 Lois Delcambre Week Introduction 0 We ve covered 0 Python data types 0 Python expressions 0 Python statements 0 We ve written approximately 30 programs that allow students to practice the various data types expressions and statements 0 It s time to step back talk about this You ve learned a lot of Python 0 Look at the reserved word list p 30 o How many of these have you learned and del for is raise assert from lambda return break global not try class except or while continue exec import pass yield def nally in print if elif else for control structures You ve learned a lot of Python and del for is raise assert break try class except while continue exec yield finally def return import from used when you define functions and import functions in modules You ve learned a lot of Python del for is assert lambda break class except while continue exec pass yield finally in print Logical expressions that evaluate to true or false You ve learned a lot of Python class except l finally in V For loop and while loop break jump out past the loop continue jump to the bottom of this loop and keep going pass do nothing a placeholder F f PM quot3 3 l Militia You ve learned a lot of Python try yield exec pass finally print statement del and in used with iterables list set tuple l ZZGGQ Python statements documentation 0 6 Simple statements 0 7 Compound statements 0 6Expression statements 0 7The if statement 0 62Assignment statements 0 72The while statement 0 63The assert statement 0 73The for statement 0 64The pass statement 0 74The try statement 0 65The del statement 0 75The with statement 0 66The print statement 0 76 Function definitions 0 67The return statement 0 77 Class definitions 0 68The yield statement 0 8TQPIeveI components 0 69The raise statement 0 8 Compete python 0 60The break statement Programs 0 6 The continue statement 0 82 File input 0 62The import statement 0 83 Interactive input 0 6 3The global statement 0 84 Expression input 0 6 l4The exec statement You ve used modules 0 A module is a file 0 with your program in it 0 with a function definition in it 0 with a set of often related functions in it 0 You can 0 run a module using Run in IDLE 0 import a module 0 You ve seen 0 The string and math modules 0 There are dozens hundreds of others Many programming languages have the statements that you ve learned 0 Java has 0 assignment statements 0 for loops 0 while loops 0 dowhile loops 0 break 0 ifelse 0 print 0 FGtU rn We haven t talked about classes and objects objectoriented programming or oop Objectoriented programming is one paradigm for programming 0 Procedural programming is another paradigm for programming we have been using Python in a procedural way we write one Python statement after another 0 Functional programming is another paradigm where everything is a function no loop no assignment statements How is a program used o It might be run for a long time unchanged Have you heard about the Y2K problem o It might be part of something larger that evolves over time o It might be used by others exactly as it is written if you put it in a module o It might be used and modified by others eg in an open source setting Therefore we need it to be o It might be run for a long time unchanged Have you heard about the Y2K problem Correct for all inputs o It might be part of something larger that evolves over timeTestableagain amp again 0 It might be used by others exactly as it is written if you put it in a module Readable amp Understandable It might be used and modified by others eg in an open source setting Readable amp Understandable How do we know a program is correct for all inputs 0 In generalwe can t 0 Alan Turing proved in I936 that it is not possible to write a program that can take as input a program and an input and decide whether or not the program will finish or will run forever 0 This is called the halting problem 0 Thus we say it is undecidable Halting problem cont 0 So don t expect to generate a bunch of code and then expect some automatic procedure to figure out whether it s correct A general purpose automatic procedure can t even figure out whether or not a program will finish or not Shortest lie in computer sciencez it 9 works What about testing 0 Very important 0 You should be systematic in your testing Figure out your boundary conditions the smallestlargest input that your program should work for Then test 0 The smallest input 0 A range of typicalnormal inputsvariations o The largest input 0 An input smaller than the smallest 0 An input larger than the largest Testing cont 0 Testing can never completer identify all the defects within software Som software is released for use before it is bugfree in many cases 0 Some software is lifecritical so proper testing is really important 0 Note that for some programs it is possible to prove mathematically that they meet their specification So how should we develop software How do we begin o It is generally a bad idea to write one big program ie one big function called main that does everythingWhy Given what we know so far in Python how can we decompose a program into smaller pieces where each piece could be written AND tested separately That is what language feature should we use So how should we develop software How do we begin o It is generally a bad idea to write one bi program that does everythingWhy 0 People don t want to read long programs 0 Longer programs are usually more complex because some variables may be used in more than one place thus some parts of the program may depend on others 0 Given what we know so far in Python what language feature should we use 0 Functions Each function can be written and tested It doesn t see variables except those that are input and those that it returns So how do we decide which functions to write 0 Topdown design 0 Break the big program into a series of small ones Assume for the moment that you or someone else will be able to write the functions that you need Example of topdown design 0 Over years of playing racquetball Denny has noticed a strange quirk in the game He often competes with players who are just a little bit better than he is In the process he always seems to lose the vast majority of matches Susie suggests that they can write a computer program to figure out if it s just the nature of the game or not Racquetball To start the game one of the players puts the ball into play by serving The players then alternate hitting the ball to keep it in play in a rally The rally ends when one of the players fails to hit a legal shot The player who misses the shot loses the rally If the server wins the rally the server earns one point Players can only score points during their service If the server does NOT win the rally then NO point is awarded and service passes to the other player The first player to reach l5 points wins the game in the amateur version of scoring Book uses this Specification of a Racquetball Simulation Program Input The program rst prompts for and gets the service probabilities of the two players called Player A and Player B Then the program prompts for and gets the number of games to be simulated Output The program will provide a series of initial prompts such as the following What is the prob playerA wins a serve What is the prob player B wins a serve How many games to simulate The program will print out a nicely formatted report showing the number of games simulated and the number of wins and winning percentage for each player Here is an example Games Simulated 500 Wins forA 268 536 Wins for B 232 464 NotesAll inputs are assumed to be legal numeric values no error or validity checking is required In each simulated game playerA serves first How do we figure out who winsloses a serve We know the probability that playerA wins hisher serve We know the probability that player B wins hisher serve We need to flip a coin We need to use a random number generator Also called a pseudo random number generator It returns a value between 0 and l Python has a random module randrangestart stop step returns an integer in the requested range 7 i l l File Edit Shell Debug Options Windows Help Python 261 0251267517 Dec 4 2003 165100 MISC M1500 32 bit lntel on Win32 Type quotcopyright creditsquot or quotlicenseOquot for more information tti iiftt itis Personal rewall software may warn about the connection IDLE makes to its subprocess using this computer39s internal loopback interface This connection is not visible on any external interface and no data is sent to or received from the Internet aeseeeaeaaseeeaaseeeaeeaeeeeaeaaaaeeeeeeaaeeee IDLE 261 gtgtgt from random import rancirange gtgtgt randrange start stopNone step1 intlttype 39int39gt defaultNone mamiclth9 f199 Choose a random item from rangestart stopL stem random returns a floating point number between 0 and l gtgtgt import random as r gtgtgt rrandom 04066429l 80330725 gtgtgt rrandom 07324944665 324879 To determine whether playerA wins the rally when they serve if ltpayer wins servegt score score 1 This can be coded in Python like this if random lt prob for player A score score 1 Topdown design get started Print an Introduction Get the inputs probA probB n Simulate n games of racquetball using probA and probB Print a report on the wins for playerA and playerB def main printlnstructions probA probB n getlnputs winsA winsB simNGamesn probA probB printSummarywinsA winsB Notice at this point 0 You may not know how to write each of these functions Imagine that you will 0 You are choosing the inputs and outputs for each function This is a very important step You are defining the interface for the function Also called the signature 0 You can write the functions def printSummarya b pass Structure chart for what we have so far main printlntro getInputs sim ames printEummary At this point you might code the functions that you understand or instead focus on the ones you DON T know how to write You can continue doing topdown design Which ones do you know how to write Now consider each function We know how to write printlntro Here s one way def printlntro print quotThis program simulates a game of racquetball between two print players called quotAquot and quotBquot The abilities of each player is print quotindicated by a probability a number between 0 and 1 thatquot print quotthe player wins the point when serving Player A alwaysquot print quothas the first servequot What about getlnputso We know how to write this as well def getlnputs RETURNS the three simulation parameters probA probB and n a inputquotWhat is the prob player A wins a serve quot b inputquotWhat is the prob player B wins a serve quot n inputquotHow many games to simulate quot return a b n What about simNGames This is the hard one what shall we do Here s some pseudocode Initialize winsA and winsB to 0 loop n times simulate a game if playerA wins Add one to winsA else Add one to winsB Code for simNGames def simNGamesn probA probB Simulates n games and returns winsA and winsB winsA O winsB O for i in rangen scoreA scoreB simOneGameprobA probB We did it again we inventedimagined a function called simOneGame it needs to know the probability that player A wins hisher serve and the probability that player B wins hisher serve Accumulate wins based on simOneGame def simNGamesn probA probB winsA winsB O for i in rangen scoreA scoreB simOneGameprobA probB if scoreA gt scoreB winsA winsA 1 else winsB winsB 1 return winsA winsB Updated Structure Diagram F39mii F IP FVDF printlntrc getlnputs aim am s printEummary pzn39ti 5 1350133 501129 Ema Eim n am Continue topdown design How do we write simOneGame Pseudocode Initialize scores to 0 Set serving to quotAquot Loop while game is not over Simulate one serve of whichever player is serving update the status of the game Return scores Getting started simOneGame def simOneGameprobA probB scoreA O scoreB 0 serving quotAquot while ltconditiongt What is the condition We need to keep this function running until the game is over We can invent a function for that gameOver returns a Boolean def simOneGameprobA probB scoreA O scoreB 0 serving quotAquot while not gameOverscoreA scoreB New structure chart main 5 22009 printlnt r0 get Inputs E im amea paintsum pmh 5 11th 5mm BLMEE a imDnEGame Emma A WEE I truelfallse game ve r 39 Complete simOneGame def eim neGemeiprebA prebE eeeree U eeereE D serving quotAquot while geme et verieeeree eeereE if serving A if raneemij prebe eeereA eeereA 1 else eerving E else if rendem j e prebE eeereB eeereE 1 elee Serving quotAquot return eeereAI eeereE Another function gameOver Pseudocode def game veriabj a and b represent aeeree fer a racquetball game RETURNS true if the game is everI falee etherwiae Python code def game veriabi a and b repreaent aeeree fer a racquetball game RETURNS true if the game is ever falae etherwiee return a15 er b15 We also need to write the function printSummary but that s easy See pages 280282 for the complete program in your book Analysis and Design 0 Some jobs require that you ONLY do design 0 Some jobs require that you ONLY gather requirements analysis 0 Some jobs require that you ONLY test software 0 Some jobs require that you do all of these 0 Not everyone writes code all day long


Buy Material

Are you sure you want to buy this material for

25 Karma

Buy Material

BOOM! Enjoy Your Free Notes!

We've added these Notes to your profile, click here to view them now.


You're already Subscribed!

Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'

Why people love StudySoup

Jim McGreen Ohio University

"Knowing I can count on the Elite Notetaker in my class allows me to focus on what the professor is saying instead of just scribbling notes the whole time and falling behind."

Janice Dongeun University of Washington

"I used the money I made selling my notes & study guides to pay for spring break in Olympia, Washington...which was Sweet!"

Bentley McCaw University of Florida

"I was shooting for a perfect 4.0 GPA this semester. Having StudySoup as a study aid was critical to helping me achieve my goal...and I nailed it!"

Parker Thompson 500 Startups

"It's a great way for students to improve their educational experience and it seemed like a product that everybody wants, so all the people participating are winning."

Become an Elite Notetaker and start selling your notes online!

Refund Policy


All subscriptions to StudySoup are paid in full at the time of subscribing. To change your credit card information or to cancel your subscription, go to "Edit Settings". All credit card information will be available there. If you should decide to cancel your subscription, it will continue to be valid until the next payment period, as all payments for the current period were made in advance. For special circumstances, please email


StudySoup has more than 1 million course-specific study resources to help students study smarter. If you’re having trouble finding what you’re looking for, our customer support team can help you find what you need! Feel free to contact them here:

Recurring Subscriptions: If you have canceled your recurring subscription on the day of renewal and have not downloaded any documents, you may request a refund by submitting an email to

Satisfaction Guarantee: If you’re not satisfied with your subscription, you can contact us for further help. Contact must be made within 3 business days of your subscription purchase and your refund request will be subject for review.

Please Note: Refunds can never be provided more than 30 days after the initial purchase date regardless of your activity on the site.