Uploaded by blackshyguy

CMPUT 174 - Module 0 - Slides to present

advertisement
What is "Computational Problem Solving"?
1
Computational Problem Solving vs Programming
● Computational problem solving comes before Programming
● Computational problem solving is about finding an abstract,
computational solution for a problem or a task, e.g.:
○
○
○
finding the shortest route on a map between two locations
analyzing a sales data set and presenting the results on a screen
implementing a new game that you invented
● Programming is about taking a computational solution and describing
it in a particular programming language.
● Think of Computational Problem Solving activities as a Design Phase
that must come before the Programming Phase
2
Key Activities / Skills:
● Problem Decomposition
○ Break a bigger problem into smaller subproblems or subtasks that
are easier to understand, solve, implement, test, and maintain - or
adapt for other problems
● Abstraction & Modeling
○ Represent the essential information about a problem or task so
that the information can be stored and manipulated by a
computer.
● Algorithm Design
○ Find a sequence of well defined computational steps that, when
executed, achieve the solution/goal
3
A Simple Example
●
Turn-based strategy game
○
○
○
○
●
Player can move each turn on a playing field
Player has a circular field of vision
(how far a player can see may change during the game)
Player can only interact with game elements in its field of vision
…
Before implementing anything, you must
○
Problem Decomposition
Determine the "things" and their desired behaviour in the game
○
Abstraction & Modeling
Decide how to represent the things that occur in the game
○
Algorithm Design
Determine computational steps needed to achieve the desired game behaviour
4
A Simple Example (continued)
●
Part of your decomposition effort determines
that your game should include:
○ a playing field
○ a player character
○ items that the player can interact with
●
What should a representation of a player and items include, so that you can
compute whether or not an item is within the player's field of vision?
○
●
Assume that your playing field representation includes some kind of 2-dimensional
coordinate system.
Given you representation of player and items, what is an algorithm to
actually compute whether or not an item is within the player's field of vision?
5
y
A Simple Example (continued)
●
Representation
○
(xp, yp) coordinate for the (centre) position
radius r for the size of the field of vision
…
Items:
■
■
●
yp
yi
Player:
■
■
■
○
r
Xp
x
Xi
(xi, yi) coordinate for the (centre) position
…
Algorithm
○
○
For each item in the game: Compute the Euclidean distance d between player
centre and the item centre: d = √(xi - xp)2 + (yi - yp)2
If the distance is smaller than the radius r, then the item is within the player's
field of vision, otherwise it is not.
6
What is Programming?
7
Simplified Computer Architecture
CPU - Central Processing Unit
Main Memory
Sequence of "cells"
Input
Input: sequence
of bytes, e.g.,
generated by a
keyboard
Output
Output:
sequence of
bytes, e.g., sent
to a monitor
1
10110101
2
00100101
3
00010100
4
11110100
5
...
Typical cell size:
1 byte = 8 bits
Address of a cell:
Its unique number
in the sequence of cells
8
Simplified ComputerCUArchitecture
decodes
CPU - Central Processing Unit
CU - Control Unit
ALU - Arithmetic
Logic Unit
ALU performs basic
arithmetic and
logical operations
on binary data
Input
Input: sequence
of bytes, e.g.,
generated by a
keyboard
PC - Program Counter
PC "points" to the
next instruction to
be executed
Output
Output:
sequence of
bytes, e.g., sent
to a monitor
instruction and
Main Memory
controls other
parts of the
Program Code
CPU
Main Memory
Sequence of "cells"
1
10110101
2
00100101
3
00010100
Other reserved areas
4
11110100
----------------------------
5
...
Instruction 1
Instruction 2
Instruction 3
...
----------------------------
Free memory (Heap)
Typical cell size:
1 byte = 8 bits
Address of a cell:
Its unique number
in the sequence of cells
9
How computers "run"
● Simplified instruction cycle:
CPU
ALU
fetch instruction pointed to by PC
increment PC to the next instruction
Main Memory
CU
Instruction 1
Instruction 2
Instruction 3
...
PC
Input
Program Code
Output
decode & execute instruction
● Instructions are: very basic, low-level, and hardware specific
(e.g., load two numbers, perform an addition of two numbers, etc.)
● All complex tasks are composed of basic instructions
10
Programming a Computer
● Program = "Description of computations, written in a way that
a computer can execute them"
● Difficult to write complex programs in low-level languages!
● Programs are typically written using "higher level" languages
○ human readable and easier to understand
○ hardware independent ways of expressing common tasks
● Programs written in high-level languages are translated into
instructions at the lowest level before they can be executed
11
Translation of High Level Programs...
● ...depends on the programming language
● Compiler (e.g., C, C++, Pascal)
○ compile = translate a source code program completely into
"Object Code", which can then be executed
● Interpreter (e.g., Perl, Python, Java)
○ interpret = translate and immediately execute source code,
statement by statement, in the written order
○ often allow an interactive mode: "interactive shell" accepts and
executes a statement and then waits for the next statement
12
How computers "run"
Conceptual
Control flow
1
6
● Simplified instruction cycle:
fetch instruction pointed to by PC
increment PC to the next instruction
decode & execute instruction
Main Memory
...
Instruction i: code for call of a function f
Instruction i + 1
Instruction i + 2
...
2
3
4
5
Instruction 1 of function f code
Instruction 2 of function f code
Instruction 3 of function f code
Last instruction of function f code
...
● Instructions are: very basic, low-level, and hardware specific
(e.g., load two numbers, perform an addition of two numbers, etc.)
● All complex tasks are composed of basic instructions
● some instructions change the PC to a different location
○
e.g., to where the code for a called function is stored in memory
13
Writing & Running Python Programs
14
What is a (Python) Program?
● A sequence of statements in the Python programming
language that describe how to compute something.
● Python language has many different statement types for
○ creating data objects
(to represent the essential properties of your task/problem)
○ giving data object names
(so that you can easily to refer to an object by name in later statements)
○ create new objects as the result of certain computations
(by applying operators, calling functions, etc)
○ executing statements only conditionally, or repeatedly, ...
○ ...
15
Writing & Running Python Programs
1.
2.
Write a program in a "plain text" editor & save it
"Running" the Python program
= have the Python Interpreter evaluate it statement by statement
Can be done in several ways
○ Editor + Call the Python interpreter in a system shell
○ Use an IDE (Integrated Development Environment) that combines in one
interface/application
- a plain text editor (with Python syntax coloring)
- a debugger (plus possibly other tools)
- an interactive shell
- a shortcut to execute the file currently in the editor window
16
Demonstration of Editor, Command Line, and IDE
17
How does Python "Interpret" a Program
● Lexical Analysis (Tokenization)
○ parses the program file and identifies the "tokens" in the
sequence of characters
● Syntax Analysis (check for "grammatical" mistakes)
○ checks if statements are well-formed, given the identified tokens
● Semantics (the meaning of a program)
○ a program describes a computation; that computation
(as interpreted/executed by Python) is its meaning
18
Creating Objects:
Simple Expressions & Statements
19
Representing "things" in Python
● Different Kinds of Objects
○ Object Type
○ Object Identity
○ Object Value
20
Creating objects in Python: Literals
● int literals
○ e.g.: 0, 1, 45, -17
● float literals
○ e.g.: 0.0, 3.0, 0.578, -0.489, -1000.0
● string literals
○ e.g.: "Hello World", 'Barney', "123", "4.56"
21
Creating objects in Python: Expressions
● "Atomic" expressions are expressions.
○ These include literals and other things we will encounter in this
course such as identifiers and list displays.
● Enclosing an expression in parenthesis is an expression.
● The "application" of an operator to expressions is an
expression.
○ e.g.: 4 + 5, 3.45 * 7, age + 1
● There are many other kinds of expressions, including function
calls
● Executing\evaluating an expression results always in an object
22
Describing Computations in Python: Statements
● Simple statements fit in a single line
(as opposed to compound statements that we will encounter
later in this course, which require multiple lines to formulate)
● Expressions by themselves are simple statements,
so-called "expression statements"
■
useful only in interactive mode, unless they have a so-called
"side-effect" such as printing something to the screen.
● The assignment statement is a simple statement that allows
you to give names to objects.
23
Naming Objects:
The Assignment Statement
24
age | 140728674067120
Naming Objects
Memory Address:
Objects stored in
memory:
...
140728674067120
00001010
140728674067121
00001011
140728674067122
00001100
140728674067123
...
● The assignment statement with an identifier "target"
<identifier> = <expression that evaluates to an object>
e.g.: age = 10
Namespace
● Namespace vs Objectspace
○
Objects are created by Python in Main Memory
■
○
age
10
int
Think of them as "living" in an "objectspace"
Python also keeps track of the identifiers
and their association with objects
■
...
Objectspace
this information is called the "namespace"
"State Diagram"
age
...
10
25
Describing simple computations in Python - Examples
26
Practice
Q1 Trace through the following program and write the value of the identifiers as they change.
Example 1
first = 2
second = 3
third = first * second
second = third - first
first = first + second + third
third = second * first
Example 2
x = 10
x = x + x
x = x - 5
Q2 Write a program that swaps the values of two identifiers.
Q3 Write a program to print the id, type and value of an object.
27
Download