Card Tricks - Introduction to Computing Using Python - Lecture Slides, Slides of Introduction to Computing

Key points in this Introduction to Computing Using Python lecture are: Card Tricks, Quick Poker Primer, Probabilities, Monte Carlo Methods, Start Implementing, Python Features, Multi-Way Comparisons, Boolean, Special Methods, Boolean to Integer Conversion . Objectives of this course are: 1.Fluency in (Python) procedural programming 2. Competency in object-oriented programming 3. Knowledge of searching and sorting algorithms

Typology: Slides

2012/2013

Uploaded on 08/17/2013

bakul
bakul 🇮🇳

4.6

(16)

69 documents

1 / 12

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Quick poker primer!
Basic (straight) version: 5 random cards in your hand!
§2 of same rank: pair (e.g., 3C 3D 5D 6H 7S)!
§3 of same rank: three of a kind (5C 5D 5H 6H 7S)!
§4 of same rank: four of a kind (6C 6D 6H 6S 7S)!
§Two separate pairs: two pair (5C 5D 6D 6H 7S)!
§3 of one rank, 2 of another: full house (5C 5D 6D 6H 6S)!
§All cards in rank sequence: straight (5C 6D 7D 8H 9S)!
§All cards of same suit: flush (5H 7H 8H 10H KH)!
§All in seq. and same suit: straight flush (5D 6D 7D 8D 9D)!
Less probable hands beat more probable hands!
Some games have > 5 cards; you choose 5 cards!
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Card Tricks - Introduction to Computing Using Python - Lecture Slides and more Slides Introduction to Computing in PDF only on Docsity!

Quick poker primer

• Basic (straight) version: 5 random cards in your hand

§ 2 of same rank: pair (e.g., 3C 3D 5D 6H 7S) § 3 of same rank: three of a kind ( 5C 5D 5H 6H 7S) § 4 of same rank: four of a kind ( 6C 6D 6H 6S 7S) § Two separate pairs: two pair ( 5C 5D 6D 6H 7S) § 3 of one rank, 2 of another: full house ( 5C 5D 6D 6H 6S ) § All cards in rank sequence: straight ( 5C 6D 7D 8H 9S ) § All cards of same suit: flush ( 5H 7H 8H 10H KH ) § All in seq. and same suit: straight flush ( 5D 6D 7D 8D 9D )

• Less probable hands beat more probable hands

• Some games have > 5 cards; you choose 5 cards

Goal for this lecture

  • Plan: randomly deal out N hands (like maybe a million) and see how many are full houses, straights, etc. The ratio of the number of straights dealt to N is an estimate of the probability of being dealt a straight.
  • This requires us to extend our Hand class so that it can recognize all eight poker hands. We’ll do this, develop testing code along the way, and then answer the question with this Monte Carlo experiment.

What are the probabilities of all the

various poker hands, with varying

numbers of cards in the hand?

multi-way comparisons x < y < z means x < y and y < z w == x == y == z means w == x and x == y and y == z One difference: middle operands are not evaluated twice. conversion to Boolean in conditionals 4.0 + 2 automatically converts 2 to float if x: … automatically converts x to bool. if n: means if n != 0: —for numeric types:  zero is false, nonzero true if lst: means if len(list) != 0: —for sequence types:  empty is false, nonempty true

Stage 2: start testing

• Form a plan for testing the whole class

• Start by setting up tests for the first methods

• Python features:

§ positional and keyword arguments

§ assert statement

• New patterns:

§ init with multiple disjoint ways to initialize

§ streamlining test cases with lists

Stage 3: complete implementation

• Counting suit and rank histograms is useful

• Also simplifies generalization to more cards

• Python features:

§ augmented assignment (+= and friends)

§ str vs. repr

• new patterns:

§ computing sums using a for loop

§ computing several sums in parallel

§ computing a maximum using a for loop

augmented assignment x += y is shorthand for x = x + y One difference: expression on the left is not evaluated twice. special methods str and repr We have seen the special method str that is called to generate a readable string representation of an object. There is also repr which does something similar. str is used by str() and print. Meant for the user to read. repr is used by repr(), the interactive prompt, and back- quotes (x). Should be unambigous, for the programmer. Ideally, repr() returns exactly what you would put in your program to create that value—recall how strings work.

Boolean to integer conversion Conversions involving bool go both ways. When you convert a bool to a numeric type, True is 1 and False is 0. Example: x += (y > 3) increments x by 1 if y is greater than 3.

Monte Carlo methods

• In CS, “Monte Carlo” is the idea

of a program that runs a random

experiment many times to

estimate some value.

• It can estimate many quantities

for which it is very hard or

impossible to derive formulas.

• A nice example of trading long

computation time for ease of

programming!

Casino de Monte Carlo in Monaco. Wikimedia commons / Wigulf "