Professional Documents
Culture Documents
Statistical Programming
for
Data Science
2
Step
1:
Define
the
Problem
If
you
don t
have
a
clear
understanding
of
the
problem,
it
is
unlikely
that
you’ll
be
able
to
solve
it.
Steps
to
help
you
understand
the
problem:
Carefully
read
the
problem
un7l
you
understand
what
is
required.
Divide
the
problem
into:
Input
Output
Processing
-‐
List
of
steps/ac7ons
needed
to
produce
the
output
Detailed
Precise
Ordered
A
procedure
for
solving
a
problem
in
terms
of:
The
goal
of
an
algorithm
is
to
complete
some
task.
An
algorithm
is
wriYen
in
simple
English
(not
in
a
programming
language).
4
Step
2:
Develop
an
Algorithm
Correctly
specifying
the
order
in
which
the
ac7ons
are
to
be
executed
is
important.
Example:
Write
an
algorithm
to
get
out
of
bed
and
arrive
at
Uni:
Get
out
of
bed
Eat
breakfast
Take
off
pyjamas
Take
a
shower
Brush
teeth
Get
dressed
Drive
to
Uni
Suppose
the
steps
are
performed
in
a
slightly
different
order…
trouble!!!
Get
out
of
bed
Take
off
pyjamas
Get
dressed
Take
a
shower
Brush
teeth
Eat
breakfast
Drive
to
Uni
5
Step
2:
Develop
an
Algorithm
Design
a
solu7on
and
develop
into
an
algorithm.
You
may
like
to
work
backwards:
What
outputs
are
required?
What
data
do
you
need
to
be
able
to
calculate
the
outputs?
6
Step
2:
Develop
an
Algorithm
Pseudocode
is
an
informal
language
that
helps
you
develop
algorithms.
Is
a
tool
used
to
plan
your
program
before
you
start
coding.
It
helps
you
think
out
a
program
in
your
natural
language
before
aYemp7ng
to
write
it
in
a
programming
language
such
as
Python.
Pseudocode
is
a
verbal
descrip7on
of
your
plan:
Similar
to
programming
code.
syntax).
You
may
use
pseudocode
in
order
to
help
you
solve
your
problems.
You
can
design
solu7ons
without
knowing
a
programming
language.
Intended
to
help
you
create
beYer
computer
programs.
7
Step
2:
Pseudocode
Example
Control
Structures:
Sequence,
Selec7on,
Repe77on.
Pseudocode
uses
common
words
and
keywords
to
symbolise
opera7ons.
For
example:
Pseudocode:
Python
code:
IF time is greater than 7 if time > 7:
print 'time to go home' print('Time to go home')
k = 0 k = 0
WHILE k is less than 3 while k < 3:
k = k+1 k = k + 1
print k to screen print(k)
8
Step
2:
Develop
an
Algorithm
Summary:
Write
down
a
set
of
steps
to
solve
the
problem.
Take
the
processing
steps
from
step
1
(defining
the
problem).
Take
the
3
basic
control
constructs:
– Sequence
– Selec7on
– Repe77on
Determine
how
the
processing
will
be
performed.
– Some7mes
a
trial
and
error
process.
Each
processing
step
relates
to
1
or
more
steps
in
the
algorithm.
Once
an
algorithm
is
fully
developed
and
tested,
implemen7ng
it
in
a
par7cular
programming
language
is
rela7vely
trivial.
9
Step
3:
Test
Algorithm
We
then
need
to
test
the
algorithm
for
correctness.
To
detect
errors
early.
To make sure the algorithm is correct (produces the correct results)
Sanity
Check:
work
through
your
test
cases
by
hand
and
see
if
your
algorithm
handles
these
and
gives
the
right
answer
(more
on
the
next
slide).
Look
for
ambiguous
or
missing
steps.
Look
for
steps
that
do
a
lot
–
these
may
need
to
be
broken
down
further.
10
Step
3:
Test
Algorithm
To
test
if
your
approach
to
solving
the
problem
will
work,
you
need
test
cases
where
you
know
what
the
result
or
output
should
be.
You
need
one
or
more
situa7ons
that
are
'typical'
and
where
the
algorithm
should
work
and
you
are
able
to
specify
what
the
algorithm
should
do.
11
Step
3:
Test
Algorithm
Some
further
considera7ons:
Look
for
boundaries
Are
there
input
ranges
where
there
is
no
solu7on
or
the
algorithm
will
not
work?
These
are
o_en
associated
with
a
boundary,
such
as
first
or
last
value
of
an
input.
12
Step
4:
Implement
the
Algorithm
For
this
course,
this
means
we
need
to
create
a
solu7on
in
Python
or
R:
Convert
the
algorithm
into
a
Python
and/or
R
solu7on.
I.e.
build
up
your
program
in
small
steps
where
you
execute
and
test
the
code
you
have
just
added.
This
is
important!
13
Step
4:
Implement
the
Algorithm
Follow
good
coding
standards
as
you
go.
This
includes:
Tidy
up
your
code
as
you
go
–
keep
it
readable
and
take
out
redundant
test
code.
14
Step 5: Test & Verify
You
should
test
and
verify
the
program
and
the
solu7on.
15
Example
Given
a
wall
which
has
a
width
of
5
metres
and
a
height
of
10
metres,
write
a
program
that
computes
and
displays
the
amount
of
paint
required
to
paint
the
wall.
Paint
coverage
is
10
square
metres
per
litre.
16
Step
1:
Define
the
Problem.
Recall
the
sugges,on
(slide
3)
to
underline
and
capitalise
…
Given
a
wall
which
has
a
width
of
5
metres
and
a
height
of
10
metres,
write
a
program
that
COMPUTES
and
DISPLAYS
the
amount
of
paint
required
to
paint
the
wall.
Paint
coverage
is
10
square
metres
per
litre.
You
may
like
to
restate
the
problem
in
your
own
words…
Find
the
amount
of
paint
required
to
paint
a
wall.
17
Step
1:
Define
the
Problem.
Inputs
Wall
width
width
=
5
m
Wall
height
height
=
10
m
Output
Quan7ty
of
paint
(quan7ty
in
litres)
Processing
Compute
the
area
of
a
wall
Compute
amount
of
paint
required
Display
amount
of
paint
required
18
Step
2:
Develop
an
Algorithm
The
area
of
a
rectangle
can
be
calculated
using
the
following
formula:
area
=
width
x
height
Pseudocode:
the
algorithm
(detailed
set
of
instruc7ons)
is
as
follows:
Set
width
to
10
Set
height
to
5
Compute
area
of
wall
area
=
width
x
height
Compute
quan7ty
of
paint
quan7ty
=
area
/
10
Display
the
quan7ty
of
paint
to
the
screen
20
Step
3:
Test
Algorithm
Tes7ng
the
algorithm
for
correctness
is
an
important
step,
although
given
the
nature
of
the
current
example
is
not
needed
here
since
the
calcula7ons
are
straighlorward.
This
won’t
always
be
the
case!
When
you
write
repe77on
(while,
for
loops)
or
if-‐else,
if-‐elif-‐else
selec7on
statements,
you
should
rigorously
check
your
algorithm,
as
these
control
structures
are
o_en
a
source
of
error
(even
when
you
think
you
got
it
right!).
Implement
the
code
either
as
a
script
or
at
the
command
line
(i.e.
the
interpreter
window
with
the
chevron
>>>
prompts
in
Python,
or
single
>
in
R).
If
you
want,
you
can
improve
your
code
by
promp7ng
the
user
for
width
and
height
and
interest.
22
Step 5: Test & Verify
This
is
the
‘common
sense’
step
Compare
to
the
hand
solu7on
(if
you
have
one)
or
the
tes7ng
from
step
3.
23
Problem
Solving
Strategy
Summary:
Analyse
then
Design
then
Test
then
Implement
then
Test
(yes
we
test
a
lot!).