150 likes | 250 Views
Programming Patterns. CSC 161: The Art of Programming Prof. Henry Kautz 9/30/2009. Assignments. Assignment #4 (Design Lab) Due by Saturday 10 th October Electronic turn in Assignment #5 (Written exercises) Due Wednesday 7 th October (no class Oct 5 th ) Turn in hardcopy in class
E N D
Programming Patterns CSC 161: The Art of Programming Prof. Henry Kautz 9/30/2009
Assignments • Assignment #4 (Design Lab) • Due by Saturday 10th October • Electronic turn in • Assignment #5 (Written exercises) • Due Wednesday 7th October (no class Oct 5th) • Turn in hardcopy in class • Reading assigned today: • If statements: Textbook Sec. 7.1 and 7.3 • While statements: Textbook Sec. 8.2 • Pre-reading for Oct 7th: • Textbook Chapter 5 (Objects & Graphics)
Last Class • Top-Down Design • How to go from a problem statement to a program • Key idea: don't just start trying to write code! • Take small steps: • Task statement • Beginning, Middle, End • Main loop and stopping condition (if any) • Identify sub-tasks • Refine sub-tasks (repeat as necessary) • Write code • Read & revise
Today: Programming Patterns • Begin writing code when it is obvious how to implement it • What makes something obvious? • When you have seen something like it before! • Programming Patterns • Kinds of expressions, statements, and code fragments that appear over and over again • As your experience grows, your mental library grows • More and more things become obvious
Math Expression Patterns • Basic arithmetic • slope = (y2 – y1)/(x2 – x1) • Complete mathematics • import math • Dist = math.sqrt((x1 – x2)**2 + (y1 – y2)**2) • Random choice • import random • Roll = random.randint(1,6)
String Expression Patterns • Creating a string • S = 'Hello, World!' • Length of a string • len(S) • Picking out the K-th character in a string • S[K] • First character in a string • S[0] • Last character in a string • S[len(S) – 1]
String Expression Patterns • Concatenating (stringing together) strings • S = 'Hello,' • P = 'Sam' • S + ' ' + P == 'Hello, Sam' • Finding the position of a word or character in a string • import string • string.find('c', 'abcdefghijklmnopqrstuvwxyz') • Replacing a character or word in a string • S = string.replace('Hi! How are you!', '!', ' ')
String Expression Patterns • Translating back and forth between a integer and character, based on the position of the characters in a string • S = 'abcdefghijklmnopqrstuvwxyz' • S[3-1] == 'c' • string.find(S, 'c')+1 == 3
List Expression Patterns • Creating list • [0, 2, 4, 6, 8] • range(0,8,2) • Length of a list • len(L) • Picking out an element in a list • L = ['Fee', 'Fi', 'Fo', 'Fum'] • L[2] == 'Fo' • Last element of a list • L[len(L) – 1]
List Expression Patterns • Convert an integer value to a string using a list • Days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] • K = 4 • print 'Today is ' + Days[K]
Input Patterns • Numeric input • age = input('What is your age? ') • Text input (a single line) • name = raw_input('What is your name?') • Text input (individual words) • fullname = raw_input('What is your full name?') • L = string.split(fullname) • firstname = L[0] • lastname = L[1]
Iteration (Repeating) Patterns • Iterate over a range of numbers • Summing numbers from 1 to 5 • Sum = 0 • for X in range(1,5): • Sum = Sum + X • Iterate over members of a list • L = ['Sam', 'Fred', 'John'] • for X in L: • print 'Hello, ', X
Iteration (Repeating) Patterns • Iterate over members of a list • Summing numbers in a list • L = [25, 20, 22, 19] • Grade = 0 • for X in L: • Grade = Grade + X • Iterate over positions in a list • Summing numbers in a list • L = [25, 20, 22, 19] • Grade = 0 • for X in range(len(L)) • Grade = Grade + L[X]
Choice • Do something different depending on the value of a expression • if (Age > 18): • print 'Okay to enter' • elif (Age > 13): • print 'Okay to enter with guardian' • else: • print 'You may not enter'
Using Patterns • Problem: determine the average length of the words in a sentence typed by the user.