Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Patterns CSC 161: The Art of Programming Prof. Henry Kautz 9/30/2009.

Similar presentations


Presentation on theme: "Programming Patterns CSC 161: The Art of Programming Prof. Henry Kautz 9/30/2009."— Presentation transcript:

1 Programming Patterns CSC 161: The Art of Programming Prof. Henry Kautz 9/30/2009

2 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 Reading assigned today: If statements: Textbook Sec. 7.1 and 7.3 While statements: Textbook Sec. 8.2 Pre-reading for Oct 7 th : Textbook Chapter 5 (Objects & Graphics) 2

3 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: 1. Task statement 2. Beginning, Middle, End 3. Main loop and stopping condition (if any) 4. Identify sub-tasks 5. Refine sub-tasks (repeat as necessary) 6. Write code 7. Read & revise 3

4 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 4

5 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) 5

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] 6

7 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!', '!', ' ') 7

8 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 8

9 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] 9

10 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] 10

11 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] 11

12 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 12

13 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] 13

14 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' 14

15 Using Patterns Problem: determine the average length of the words in a sentence typed by the user. 15


Download ppt "Programming Patterns CSC 161: The Art of Programming Prof. Henry Kautz 9/30/2009."

Similar presentations


Ads by Google