1 CSC 221: Introduction to Programming Fall 2011 List comprehensions & objects building strings & lists comprehensions conditional comprehensions example:

Slides:



Advertisements
Similar presentations
TWO STEP EQUATIONS 1. SOLVE FOR X 2. DO THE ADDITION STEP FIRST
Advertisements

Finding The Unknown Number In A Number Sentence! NCSCOS 3 rd grade 5.04 By: Stephanie Irizarry Click arrow to go to next question.
Slide 1 Insert your own content. Slide 2 Insert your own content.
Copyright © 2011, Elsevier Inc. All rights reserved. Chapter 5 Author: Julia Richards and R. Scott Hawley.
1 Copyright © 2010, Elsevier Inc. All rights Reserved Fig 2.1 Chapter 2.
By D. Fisher Geometric Transformations. Reflection, Rotation, or Translation 1.
Factors, Primes & Composite Numbers
Business Transaction Management Software for Application Coordination 1 Business Processes and Coordination.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Title Subtitle.
0 - 0.
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
MULTIPLYING MONOMIALS TIMES POLYNOMIALS (DISTRIBUTIVE PROPERTY)
ADDING INTEGERS 1. POS. + POS. = POS. 2. NEG. + NEG. = NEG. 3. POS. + NEG. OR NEG. + POS. SUBTRACT TAKE SIGN OF BIGGER ABSOLUTE VALUE.
MULTIPLICATION EQUATIONS 1. SOLVE FOR X 3. WHAT EVER YOU DO TO ONE SIDE YOU HAVE TO DO TO THE OTHER 2. DIVIDE BY THE NUMBER IN FRONT OF THE VARIABLE.
SUBTRACTING INTEGERS 1. CHANGE THE SUBTRACTION SIGN TO ADDITION
MULT. INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
Addition Facts
ZMQS ZMQS
Learning Objectives for Section 7.2 Sets
BT Wholesale October Creating your own telephone network WHOLESALE CALLS LINE ASSOCIATED.
1 CSC 222: Computer Programming II Spring 2004 HW1 review arrays vs. vectors class design object-oriented design data objects + functionality example:
Chapter 11: Models of Computation
Table 22.1 Stakeholder summary for the Odd Shoe Company
Python Mini-Course University of Oklahoma Department of Psychology
ABC Technology Project
Lilian Blot Recursion Autumn 2012 TPOP 1. Lilian Blot Recursion Autumn 2012 TPOP 2.
1 / / / /. 2 (Object) (Object) –, 10 (Class) (Class) –, –, – (Variables) [ Data member Field Attribute](, ) – (Function) [ Member function Method Operation.
© Glenn Rowe AC Lab 2 A simple card game (twenty- one)
Introduction Recall that the imaginary unit i is equal to. A fraction with i in the denominator does not have a rational denominator, since is not a rational.
4.6 Perform Operations with Complex Numbers
1 University of Utah – School of Computing Computer Science 1021 "Thinking Like a Computer"
© S Haughton more than 3?
© Charles van Marrewijk, An Introduction to Geographical Economics Brakman, Garretsen, and Van Marrewijk.
© Charles van Marrewijk, An Introduction to Geographical Economics Brakman, Garretsen, and Van Marrewijk.
© Charles van Marrewijk, An Introduction to Geographical Economics Brakman, Garretsen, and Van Marrewijk.
Progam.-(6)* Write a program to Display series of Leaner, Even and odd using by LOOP command and Direct Offset address. Design by : sir Masood.
Quadratic Inequalities
Squares and Square Root WALK. Solve each problem REVIEW:
Problem Solving and Algorithm Design
© 2012 National Heart Foundation of Australia. Slide 2.
Lets play bingo!!. Calculate: MEAN Calculate: MEDIAN
Past Tense Probe. Past Tense Probe Past Tense Probe – Practice 1.
Chapter 5 Test Review Sections 5-1 through 5-4.
GG Consulting, LLC I-SUITE. Source: TEA SHARS Frequently asked questions 2.
Addition 1’s to 20.
25 seconds left…...
Copyright © Cengage Learning. All rights reserved.
Test B, 100 Subtraction Facts
11 = This is the fact family. You say: 8+3=11 and 3+8=11
Week 1.
We will resume in: 25 Minutes.
1 Unit 1 Kinematics Chapter 1 Day
9.2 Absolute Value Equations and Inequalities
CHAPTER 11 FILE INPUT & OUTPUT Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
1 PART 1 ILLUSTRATION OF DOCUMENTS  Brief introduction to the documents contained in the envelope  Detailed clarification of the documents content.
Chapter 13 – Introduction to Classes
Copyright © Cengage Learning. All rights reserved.
Index Student Activity 1: Questions to familiarise students with the
1 CSC 221: Computer Programming I Spring 2008 Lists, data storage & access  ArrayList class  methods: add, get, size, remove, contains, set, indexOf,
1 CSC 221: Introduction to Programming Fall 2013 Big data  building lists list comprehensions, throwaway comprehensions conditional comprehensions  processing.
1 CSC 222: Object-Oriented Programming Spring 2013 Objects and classes: a first pass  221 review  software objects, classes, object-oriented design 
1 CSC 221: Introduction to Programming Fall 2011 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice.
1 CSC 221: Computer Programming I Fall 2006 Lists, data storage & access  ArrayList class  methods: add, get, size, remove, contains, set, indexOf, toString.
1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice.
Chapter 8: Arrays Gator Engineering One-dimensional array Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Move the first element to the.
CSC 222: Object-Oriented Programming
CSC 222: Object-Oriented Programming
CSC 221: Introduction to Programming Fall 2018
Presentation transcript:

1 CSC 221: Introduction to Programming Fall 2011 List comprehensions & objects building strings & lists comprehensions conditional comprehensions example: deck of cards object-oriented programming, classes & objects example: deck of cards

Review: building strings we have seen numerous examples of building strings 2

Similarly: building lists since lists & strings are both sequences, can similarly build lists 3

List comprehensions Python has an alternative mechanism for building lists: comprehensions inspired by math notation for defining sets squares(N) = in Python: [EXPR_ON_X for X in LIST] 4

More comprehensions… 5

Even more… 6

Exercises: use list comprehensions to build the following lists of numbers: a list of all odd numbers from 1 to N a list of all cubes from 1 to N a list of the first N powers of 2 7 given a list of words, use list comprehensions to build: a list containing all of the same words, but capitalized a list containing all of the same words, but each word reversed a list containing the lengths of all of the same words

Throwaway comprehensions comprehensions can be useful as part of bigger tasks 8

Another example 9 note: the string method join appends the contents of a list into a single string, using the specified divider

Conditional comprehensions comprehensions can include conditions in general: [EXPR_ON_X for X in LIST if CONDITION] 10

Another example 11

12 HW6 application Skip-3 Solitaire: cards are dealt one at a time from a standard deck and placed in a single row if the rank or suit of a card matches the rank or suit either 1 or 3 cards to its left, then that card (and any cards beneath it) can be moved on top goal is to have the fewest piles when the deck is exhausted

Modeling cards there are many ways we could represent individual cards card = "Ace of Hearts" rank = "Ace" suit = "Hearts" to keep things simple (and consistent), we will use a 2-char string: ranks are from " TJQKA" suits are from "CDHS" card1 = "4C" card2 = "AS" card3 = "TH" 13

Modeling a deck of cards we can represent a deck of cards as a list deck = ["2C", "3C", "4C", … ] extremely tedious to build! 14 better solution using nested loops: even better solution using a comprehension:

Modeling a deck of cards (cont.) 15

Problems… this approach is far from ideal since the deck is represented as a list, any list methods can be applied we would like to limit the actions on the list to those that make sense for a deck e.g., should only be able to deal from the top should not be able to insert cards in the middle 16

Object-oriented approach 17 the dominant approach to (large-scale) software development today is object-oriented programming (OOP) in order to solve a problem, you identify the objects involved in the real-world solution and model them in software the software model should package all of the data & allowed operations (methods) for that type of object in OOP, a class is a new type definition that encapsulates data & methods e.g., a DeckOfCards class would include data in the form of a list of cards (2-char strings) methods such as dealCard, addCard, numCards, … once a class is defined, can create and manipulates objects of that class e.g., str = "foo" num = [4, 2, 7]deck = DeckOfCards() foo.upper() nums.sort()deck.shuffle()

OO view of Skip-3 can identify the objects involved in a Skip-3 game Card: models a playing card, with rank & suit DeckOfCards: models a deck, with methods shuffle, dealCard, numCards, … RowOfCards: models a row, with methods addAtEnd, moveCard, numCards, … using these objects, can build an interactive game 18

cards.py 19 each method has an implicit parameter: self __init__ and __str__ are special purpose methods self.cards is a list that belongs to this object MORE DETAILS ABOUT CLASS DEFINITIONS LATER

DeckOfCards example 20

HW 6: RowOfCards class similarly, we could define a class that represents a row of cards you will complete the class definition for the first part of HW 6 21

HW 6: skip3 game in PART 2, extend the basic framework add help command display # of cards in row at "quit" add error checking to handle illegal & malformed commands 22