1 / 15

Programming Patterns

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

kiri
Download Presentation

Programming Patterns

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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

  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: • 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

  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

  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)

  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]

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

  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

  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]

  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]

  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]

  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

  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]

  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'

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

More Related