Assignment 1
Design Recipe
Break
Contest
Strings
Conditional Statements
CSCA08 W INTER 2017
W EEK 3 - D ESIGN R ECIPE , C ONDITIONAL S TATEMENTS AND
S TRINGS
Ilir Dema
University of Toronto Scarborough
January 17, 2017
Assignment 1
Design Recipe
Break
Contest
C ROSSWORD P UZZLES
Example of a correct crossword puzzle:
Strings
Conditional Statements
Assignment 1
Design Recipe
Break
Contest
C ROSSWORD P UZZLES
Example of an incorrect crossword puzzle:
Strings
Conditional Statements
Assignment 1
Design Recipe
Break
C ROSSWORD P UZZLES
Puzzle representation:
Contest
Strings
Conditional Statements
Assignment 1
Design Recipe
Break
Contest
Strings
Conditional Statements
D ESIGN R ECIPE
• Why have a Design Recipe?
• Standard: Makes it easy to read other people’s code
• Enforces good commenting
• Gives us a step-by-step process to follow when we’re
writing a function
Assignment 1
Design Recipe
Break
Contest
Strings
Conditional Statements
D ESIGN R ECIPE
• 8 steps in the recipe
1 Header: The function definition
2 Type Contract: What types come in, what type is returned
3 Requirements: Limits on the inputs other than type
4 Examples: Some example inputs/outputs
5 Description: Explanation of what the function does
6 Internal Comments: Plan of what your code will do
7 Code: The body of the function
8 Test: Make sure that your function works
Assignment 1
B REAK
Design Recipe
Break
Contest
Strings
Conditional Statements
Assignment 1
Design Recipe
Break
Contest
Strings
S TEP 1: H EADER
• Function header, using def
• Function name should be descriptive
• Parameter names should also be meaningful
Conditional Statements
Assignment 1
Design Recipe
Break
Contest
Strings
Conditional Statements
S TEP 2: T YPE C ONTRACT
• A contract between you and whoever is using the function
• “If you give me parameters of the types I define, I will give
you output of the defined type (and I won’t crash)
• If you don’t follow the contract, I’m not responsible for what
happens
• Form:
• (parameter type list) -> output type
• Inside function, in triple quotes
Assignment 1
Design Recipe
Break
Contest
Strings
Conditional Statements
S TEP 3: R EQUIREMENTS
• Any other requirements that the user must obey
• Similar to type contract, but for things other than type
• Sometimes referred to a preconditions
• Ex: REQ : value1 > 0 - Tells the user that value1 must be
positive
• Goes below type contract
Assignment 1
Design Recipe
Break
Contest
Strings
Conditional Statements
S TEP 4: E XAMPLES
• Write a few example calls to your function, including
expected output
• Standard cases
• Don’t worry about tricky/border cases yet
• go after REQs
• This step is not necessary for functions which use random
or i/o (cases where couldn’t predict output given input)
Assignment 1
Design Recipe
Break
Contest
Strings
Conditional Statements
S TEP 5: D ESCRIPTION
• A description of what the function does
• Should be understandable to anyone reading it
• They shouldn’t have to know anything about the internals of
your function
• Should mention every parameter by name
• Goes after type contract, but before REQs
Assignment 1
Design Recipe
Break
Contest
Strings
Conditional Statements
S TEP 6: I NTERNAL C OMMENTS
• This is where you plan what your function will do
• Skipping this step = recipe for disaster
• Use indentation of comments to plan code
• Look at examples to help you
• May want to go back to examples to add tricky/boundary
cases at this point
Assignment 1
Design Recipe
Break
Contest
Strings
Conditional Statements
S TEP 7: C ODE
• This is the bit that actually gets executed
• Goal of the design recipe is that this step should be trivial
• Don’t want to be focusing on algorithm and implementation
details at the same time
Assignment 1
Design Recipe
Break
Contest
Strings
Conditional Statements
S TEP 8: T EST THE F UNCTION
• Run all of your example cases from step 1 & 6
• If there are any problems, go back to earlier steps and
repeat
Assignment 1
Design Recipe
Break
Contest
Strings
Conditional Statements
D ESIGN R ECIPE
• Review: 8 steps in the recipe
1 Header: The function definition
2 Type Contract: What types come in, what type is returned
3 Requirements: Limits on the inputs other than type
4 Examples: Some example inputs/outputs
5 Description: Explanation of what the function does
6 Internal Comments: Plan of what your code will do
7 Code: The body of the function
8 Test: Make sure that your function works
Assignment 1
Design Recipe
Break
Contest
D ESIGN R ECIPE
• How can we remember all the steps?
1 Header
2 Type Contract
3 Requirements
4 Examples
5 Description
6 Internal Comments
7 Code
8 Testing
Strings
Conditional Statements
Assignment 1
Design Recipe
Break
Contest
Strings
Conditional Statements
S TRINGS
A STRING IS A COLLECTION OF CHARACTERS
• Strings represent text - often, for humans to read.
• Strings are surrounded by single (or double) quotes
• Special characters in strings are set off with a slash
Assignment 1
Design Recipe
Break
Contest
Strings
Conditional Statements
C ONCEP T EST: S TRING O PERATORS
W HAT IS THE VALUE OF S AFTER THE FOLLOWING CODE ?
s
s
s
s
=
=
=
=
" abc "
" d "∗3 + s
s + ’’ ∗ 3
s + "q"
( A ) "abcddd q"
( B ) "abcddd”””q"
( C ) "abcdddq"
( D ) "qdddabc"
( E ) "dddabcq"
Assignment 1
Design Recipe
Break
Contest
Strings
Conditional Statements
I NDICES
• Strings are sequences: they are composed of an ordered
sequence of characters
• Each item in the sequence has a position - an index
• s[i] is used to extract the string at position i in s
• The indices start from 0 (zero indexing) and are counted
from the left.
• Python allows negative indices. Negative values start
counting from the right. (-1 is the rightmost index.)
Assignment 1
Design Recipe
Break
Contest
Strings
Conditional Statements
C ONCEP T EST: I NDICES
W HICH OF THE FOLLOWING EVALUATE TO THE LAST CHARACTER
IN S ?
( A ) s[-1]
( B ) s[len(s)]
( C ) s[len(s)-1]
( D ) A and B
( E ) A and C
Assignment 1
Design Recipe
Break
Contest
Strings
Conditional Statements
S LICING
• Strings can be sliced into sub-strings uses indices.
• s[i: j] is used to extract the string start from index i and
ending at (but not including) index j from string s.
• If an index is omitted, then the beginning (or end) of the
string is assumed.
• So s[:] is the entire string, and s[:-2] is every character in
the string except for the last two.
Assignment 1
Design Recipe
Break
Contest
Strings
Conditional Statements
C ONCEP T EST: S LICING
W HAT IS THE OUTPUT OF THE FOLLOWING CODE ?
game = " l o s t v i k i n g s "
p r i n t ( game [ 5 : − 1 ] )
( A ) kings
( B ) king
( C ) viking
( D ) vikings
( E ) None of the above: the correct answer is surrounded by
apostrophes because it is a string
Assignment 1
Design Recipe
Break
Contest
Strings
C ONCEP T EST: S LICING
W HAT IS THE OUTPUT OF THE FOLLOWING CODE ?
game = " l o s t v i k i n g s "
p r i n t ( game [ 2 : − 6 ] )
( A ) st v
( B ) ost v
( C ) iking
( D ) st vi
( E ) viking
Conditional Statements
Assignment 1
Design Recipe
Break
Contest
Strings
C ONCEP T EST: S LICING
W HAT IS THE OUTPUT OF THE FOLLOWING CODE ?
game = " l o s t v i k i n g s "
p r i n t ( game[ − 6 : 1 1 ] )
( A ) ost vikings
( B ) ost viking
( C ) ikings
( D ) iking
( E ) vikings
Conditional Statements
Assignment 1
Design Recipe
Break
Contest
Strings
Conditional Statements
M ETHODS
• Methods are similar to functions but are bound to a specific
type (or object).
• You can tell the difference visually by looking for the dot (.)
operator:
s = " Baseball "
p r i n t ( s . lower ( ) )
lower ( s )
# p r i n t s baseball
# s does n o t change
# error !
Assignment 1
Design Recipe
Break
Contest
Strings
C ONCEP T EST: LEN AND FIND
W HAT IS THE OUTPUT OF THE FOLLOWING CODE ?
s = " Mississauga "
t = l e n ( s . r e p l a c e ( " ss " , " a " ) )
print ( t )
( A ) 11
( B ) ss
( C ) 10
( D ) Miaiaauga
( E ) None of the above
Conditional Statements
Assignment 1
Design Recipe
Break
Contest
Strings
IF STATEMENT
S IMPLE IF STATEMENT
i f expression :
body
How is it executed:
• Evaluate the expression
• If the result is True, then execute the body
• If the result is False, skip the body
Conditional Statements
Assignment 1
Design Recipe
Break
Contest
Strings
Conditional Statements
IF EXAMPLE
i f t e m pe r a tu r e < 0 :
print (" Brrrrr , i t is freezing ! " )
• The expression must be boolean expression
• The body can be any number of statements. It can even
include other if statements.
• The termination of the body is signaled by the restored
indentation.
Assignment 1
Design Recipe
Break
Contest
Strings
ELSE STATEMENT
i f expression :
body1
else :
body2
How is it executed:
• Evaluate the expression
• If the result is True, then execute the body1
• If the result is False, then execute body2
Conditional Statements
Assignment 1
Design Recipe
Break
Contest
Strings
Conditional Statements
C ONCEP T EST: IF - ELSE
W HAT IS THE VALUE OF X AFTER THE FOLLOWING CODE ?
x = 5
if x >
x =
if x >
x =
else :
x =
( A ) -3
(B) 1
(C) 2
(D) 3
(E) 5
2:
−3
2:
1
3
Assignment 1
Design Recipe
Break
Contest
Strings
Conditional Statements
C ONCEP T EST: IF - ELSE
W HAT IS THE VALUE OF X AFTER THE FOLLOWING CODE ?
x = 0
if x <
x =
if x >
x =
else :
x =
(A) 0
(B) 1
(C) 4
(D) 3
(E) 5
5:
4
2:
1
3
Assignment 1
Design Recipe
Break
Contest
Strings
Conditional Statements
N ESTED IF STATEMENTS
i f e x p re s s io n 1 :
body1
i f e x p re s s i on 2 :
body2
How is it executed:
• Evaluate the expression1
• If the result is True, then execute the body1
• Since body1 contains expression2, evaluate it. If true, then
execute body2.
Assignment 1
Design Recipe
Break
Contest
Strings
Conditional Statements
C HAINING IF STATEMENTS
i f e x p re s s io n 1 :
body1
e l i f e x p re s s io n 2 :
body2
e l i f e x p re s s io n 3 :
body3
How is it executed:
• Evaluate the expressions in order
• For the first evaluated to True, execute respective body.
• Skip any remaining conditions.
Assignment 1
Design Recipe
Break
Contest
C HAINING IF STATEMENTS
Compare this:
i f sunny :
p r i n t ( " I l o v e sunshine ! " )
e l i f warm :
p r i n t ( " Yay , n i c e and warm ! " )
to:
i f sunny :
p r i n t ( " I l o v e sunshine ! " )
i f warm :
p r i n t ( " Yay , n i c e and warm ! " )
Strings
Conditional Statements
© Copyright 2026 Paperzz