[SOLVED] data structure math python COMP1730/COMP6730 Programming for Scientists

$25

File Name: data_structure_math_python_COMP1730/COMP6730_Programming_for_Scientists.zip
File Size: 668.82 KB

5/5 - (1 vote)

COMP1730/COMP6730 Programming for Scientists
Functional abstraction, with robots

Lecture outline
* The warehouse robot
* Functional abstraction
* The python language: First steps.

The robot

* Drive left/right along the shelf:
* Move lift up/down:

* Change position of the gripper:
folded open closed
* Moving sideways or down, the gripper may hit boxes if it is not folded.
* Folding/unfolding the gripper may hit boxes in adjacent stacks.

The robot simulator

>>> import robot
Start new simulation:
>>> robot.init()
Start simulation with larger area:
>>> robot.init(width = 11, height = 6)
Start simulation with random boxes:
>>> robot.init(width = 11, height = 6,
boxes = random)
Drive right/left one step:
>>> robot.drive right() >>> robot.drive left()

Move the lift up one step:
>>> robot.lift up()
Move the lift down one step:
>>> robot.lift down()
Change gripper position:
>>> robot.gripper to open() >>> robot.gripper to closed() >>> robot.gripper to folded()
* If the robot hits a box, no command works until a new simulation is started.

* How to pick up a box without hitting the box(es) next to it?

* How to pick up a box without hitting the box(es) next to it?
robot.lift up() robot.gripper to open() robot.lift down() robot.gripper to closed() robot.lift up()
* A program is a sequence of instructions.

Libraries, modules, namespaces
* Library is a generic term for a collection of (useful) functions, data structures, etc.
* In python, libraries are called modules. * Importing a module,
import math
import robot
makes its content available to use.

* Imported names are prefixed with the module name, as in math.pi, robot.lift up, etc.
They are placed in a separate namespace (more about namespaces later in the course).

* How does python find modules?
Standard modules (e.g., math) are installed in
a specific location on the file system.
Non-standard modules (e.g., robot) must be
in the current working directory (cwd).
>>> import os
>>> os.getcwd()
/home/patrik/teaching/python
* When running a program, the cwd is (normally) the directory where the file is.

Programming and functional abstraction

Problem: Building a tower

robot.init(width = 7, boxes = flat) robot.drive right()
robot.lift up()
robot.gripper to open()
robot.lift down() robot.gripper to closed() robot.lift up() robot.drive right() robot.drive right() robot.gripper to open() robot.lift down() robot.gripper to closed() robot.lift up() robot.drive right() robot.drive right() robot.gripper to open() robot.lift down()
.

Functional abstraction
* In programming, a function (also known as procedure or subroutine) is a piece of the program that is given a name.
The function is called by its name.
A function is defined once, but can be called any number of times.

* Why use functions?
Abstraction: To use a function, we only need
to know what it does, not how.
Break a complex problem into smaller parts.
Input Output
Engineering succeeds and fails
because of the black box Kuprenas & Frederick, 101 Things I Learned in Engineering School

Function definition in python
def move to next stack(): robot.drive right() robot.drive right()
name suite
* def is a python keyword (reserved word).
* The functions name is followed by a pair of parentheses and a colon.
Inside the parentheses are the functions parameters (more on this in coming lectures).
* The function suite is the sequence of statements that will be executed when the function is called.

Function definition in python
def grasp box on shelf(): robot.lift up()
4 robot.gripper to open() spacesrobot.lift down()
robot.gripper to closed() robot.lift up()
* In python, a suite is delimited by indentation.
All statements in the suite must be preceded
by the same number of spaces/tabs
(standard is 4 spaces).

Function definition in python
def release and pickup next(): robot.gripper to open() robot.lift down() robot.gripper to closed() robot.lift up()
* The def statement only defines the function it does not execute the suite.
* The whole definition is itself a statement.

Building a tower of 5 boxes
robot.init(width = 9, boxes = flat) robot.drive right()
grasp box on shelf()
move to next stack()
release and pickup next() move to next stack() release and pickup next() move to next stack() release and pickup next() move to next stack() robot.gripper to folded() robot.lift down()

The python language: First steps

Syntax
* The syntax of a (programming) language is the rules that define what is a valid program.
* A python program is a sequence of statements:
defining a function: calling a function:
def move twice(): robot.drive right() robot.drive right()
move twice() robot.lift up()
importing a module: import robot and a few more.

Whitespace
* Spaces, tabs and end-of-line are known as whitespace.
* The whitespace before a statement is called indentation.
* In python, whitespace has two special roles: end-of-line marks the end of a statement
(some exceptions, more later in the course); indentation defines the extent of a suite of
statements.
* Other than this, whitespace is ignored.

Permitted names in python
* A function name in python may contain letters, numbers and underscores ( ), but must begin with a letter or undescore.
Allowed
moverighttwice move right 2
is box red imPort
Not allowed
move right twice 2 steps right
is box red? import
* Reserved words cannot be used as names.
* Names are case sensitive: upper and lower
case letters are not the same.

Comments
* A hash sign (#) marks the beginning of a comment; it continues to end-of-line.
robot.init(width = 7) # use a wider shelf # grasp the first box:
robot.lift up()

* Comments are ignored by the interpreter.
Comments are for people.
Use comments to state what is not obvious.
* If it was hard to write, its probably hard to read. Add a comment. (Punch & Enbody, Rule 6)

* Write comments to describe what a function does, and when it should be expected to work.
# Pick up a box from the shelf, without
# hitting adjacent boxes.
# Assumptions: The robot (gripper) is in
# front of the box; the gripper is folded
# and the lift is down.
def grasp box on shelf():

Testing and debugging

Test, test, test
* How do we know our program works?
Specify the assumptions under which the
program (or function) is meant to work.
Test it with a variety of cases that fall under
those assumptions.
Particularly, edge cases.

Errors
Traceback (most recent call last):
File stack-3-v1.py, line 35, in
robot.lift up()
File //robot.py, line 40, in lift up
robot.lift up()
File //robot.py, line 600, in lift up
+ and cant go any higher!)
robot.RobotError: Robot Error: The lift is at
level 1 and cant go any higher!
* Errors will happen.
* Read the error message!

* Some common errors: SyntaxError:
You have broken the rules of python syntax.
NameError or AttributeError:
You have used a (function) name that doesnt exist. Check for typos.
IndentationError:
Too much or too little indentation.
All statements in a function suite must have the same indentation.
All statements outside function definitions must have no indentation.

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[SOLVED] data structure math python COMP1730/COMP6730 Programming for Scientists
$25