csc1015f – introduction to computer science and programming with python michelle kuttel...

44
CSC1015F – Introduction to Computer Science and Programming with Python Michelle Kuttel [email protected] Room 304.02

Upload: richard-jackson

Post on 01-Jan-2016

213 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: CSC1015F – Introduction to Computer Science and Programming with Python Michelle Kuttel mkuttel@cs.uct.ac.zamkuttel@cs.uct.ac.za Room 304.02

CSC1015F – Introduction to Computer Science and Programming

with PythonMichelle Kuttel

[email protected] Room 304.02

Page 2: CSC1015F – Introduction to Computer Science and Programming with Python Michelle Kuttel mkuttel@cs.uct.ac.zamkuttel@cs.uct.ac.za Room 304.02

What is Computer Science?

2

Page 3: CSC1015F – Introduction to Computer Science and Programming with Python Michelle Kuttel mkuttel@cs.uct.ac.zamkuttel@cs.uct.ac.za Room 304.02

What is Computer Science? – one expert’s opinion…

Computer Science is no more about computers than astronomy is about telescopes.

E. W. Dijkstra

3

Computer Science is hard to define. Some people use the term “Information Technology”.

Page 4: CSC1015F – Introduction to Computer Science and Programming with Python Michelle Kuttel mkuttel@cs.uct.ac.zamkuttel@cs.uct.ac.za Room 304.02

An easier question: What is a program?

4

Page 5: CSC1015F – Introduction to Computer Science and Programming with Python Michelle Kuttel mkuttel@cs.uct.ac.zamkuttel@cs.uct.ac.za Room 304.02

An easier question: What is a program? A program is a set of instructions

you write down directions to your house for a friend this is an program

your friend “executes” the program when they follow the instructions, in order

will she find your house???? (how good is your program?)

5

Page 6: CSC1015F – Introduction to Computer Science and Programming with Python Michelle Kuttel mkuttel@cs.uct.ac.zamkuttel@cs.uct.ac.za Room 304.02

ExerciseWrite down instructions for your lecturer to draw the picture below on

the black board. You must assume that: your lecturer cannot see or be shown the picture she can only follow verbal instructions

6

Page 7: CSC1015F – Introduction to Computer Science and Programming with Python Michelle Kuttel mkuttel@cs.uct.ac.zamkuttel@cs.uct.ac.za Room 304.02

What is a program? Every program is written in terms of a few

basic operations that the “executor” understands e.g. “turn left”, “go forward three blocks”

7

Page 8: CSC1015F – Introduction to Computer Science and Programming with Python Michelle Kuttel mkuttel@cs.uct.ac.zamkuttel@cs.uct.ac.za Room 304.02

Programs and algorithms The general rules in the “recipe” (or recipes)

in your program is called an algorithm(or recipe, process, method, technique,

procedure, routine)

The essence of the problem solution, with all executor-specific details removed.

Algorithm (Noun)a process or set of rules to be followed in

calculations or other problem-solving operations, esp. by a computer : a basic algorithm for division.

8

Page 9: CSC1015F – Introduction to Computer Science and Programming with Python Michelle Kuttel mkuttel@cs.uct.ac.zamkuttel@cs.uct.ac.za Room 304.02

Algorithms are independent of language and computerAnalogy: A recipe for fruit cake can be written in either

French or English and cooked in any well-equipped kitchen and will produce (approximately) the same cake.

All cooks are capable of the same basic operations: setting oven temperature, weighing ingredients

All kitchens have the same basic equipment Novice cooks may need more detail in the recipe, but

it is the same recipe the recipe is independent of the language and the

cook who implements it.

9

Page 10: CSC1015F – Introduction to Computer Science and Programming with Python Michelle Kuttel mkuttel@cs.uct.ac.zamkuttel@cs.uct.ac.za Room 304.02

Algorithms are independent of language and computer So, a program is an algorithm (or set of

algorithms) written in a particular language to be understood by a particular class of “computers” (people or machines or…)

10

Page 11: CSC1015F – Introduction to Computer Science and Programming with Python Michelle Kuttel mkuttel@cs.uct.ac.zamkuttel@cs.uct.ac.za Room 304.02

What exactly is an algorithm?

Everyday tasks require algorithms but we usually do not think about them.

E.g., running the washing machine, setting the burglar alarm, putting on your shoes…(!)

Algorithms must be precise so that they are Repeatable Have a predictable outcome Can be executed by different “computers” (people

or machines) Terminate!

11

Page 12: CSC1015F – Introduction to Computer Science and Programming with Python Michelle Kuttel mkuttel@cs.uct.ac.zamkuttel@cs.uct.ac.za Room 304.02

Check point: is this an algorithm?

Tidy your room!

12

Page 13: CSC1015F – Introduction to Computer Science and Programming with Python Michelle Kuttel mkuttel@cs.uct.ac.zamkuttel@cs.uct.ac.za Room 304.02

Check point: is this an algorithm?

Tidy your room:

Pick up all the stuff on your floor and put it away.

Make your bed.Vacuum the floor.

13

Page 14: CSC1015F – Introduction to Computer Science and Programming with Python Michelle Kuttel mkuttel@cs.uct.ac.zamkuttel@cs.uct.ac.za Room 304.02

Check point: is this an algorithm?Snakes and Ladders: This game is for 2 - 4 players. The first player to hit the last square wins and the game

ends. To start, each player takes his/her turn to roll the dice

and the one with the highest throw begins. Players then take turns to roll the die. They each move

the number of squares indicated by the die. If a player lands on a square depicting the bottom of the

ladder, she moves up to the square containing the top of the ladder.

If a player lands on the square containing the head of a snake, he slides down to the square containing the tail of the snake.

14

Page 15: CSC1015F – Introduction to Computer Science and Programming with Python Michelle Kuttel mkuttel@cs.uct.ac.zamkuttel@cs.uct.ac.za Room 304.02

Check point: is this an algorithm?

To find the sum of three numbers, a, b, c:d = a + b + c

15

Page 16: CSC1015F – Introduction to Computer Science and Programming with Python Michelle Kuttel mkuttel@cs.uct.ac.zamkuttel@cs.uct.ac.za Room 304.02

Check point: is this an algorithm?

Perfect Soft-Boiled Eggs: Heat med panful h2o to bare simmr; +3-6egg. Maintain at near simmring (no bubbles) uncvrd7m. Rmv from h2o.

16Reference: Maureen Evans, http://eat-tweet.com

Page 17: CSC1015F – Introduction to Computer Science and Programming with Python Michelle Kuttel mkuttel@cs.uct.ac.zamkuttel@cs.uct.ac.za Room 304.02

Algorithm to Boil Water in Kettle1. take the lid off kettle

2. If there is enough water in the kettle , go to step 7

3. Put kettle under tap

4. Open tap

5. While kettle is not full, Wait

6. Close tap

7. Replace lid on kettle

8. Plug kettle into power outlet

9. Turn kettle on

10. While water has not boiled, Wait 5 seconds

11. Turn kettle off

12. Remove plug from power outlet

17

Page 18: CSC1015F – Introduction to Computer Science and Programming with Python Michelle Kuttel mkuttel@cs.uct.ac.zamkuttel@cs.uct.ac.za Room 304.02

Algorithm to make a cup of tea…

18

Page 19: CSC1015F – Introduction to Computer Science and Programming with Python Michelle Kuttel mkuttel@cs.uct.ac.zamkuttel@cs.uct.ac.za Room 304.02

Exercise Write down an algorithm for finding the

maximum of a list of numbers

19

Page 20: CSC1015F – Introduction to Computer Science and Programming with Python Michelle Kuttel mkuttel@cs.uct.ac.zamkuttel@cs.uct.ac.za Room 304.02

find the maximum – class race plan your algorithm….

20

Page 21: CSC1015F – Introduction to Computer Science and Programming with Python Michelle Kuttel mkuttel@cs.uct.ac.zamkuttel@cs.uct.ac.za Room 304.02

find the minimum – class race

21

Page 22: CSC1015F – Introduction to Computer Science and Programming with Python Michelle Kuttel mkuttel@cs.uct.ac.zamkuttel@cs.uct.ac.za Room 304.02

find the maximum – class race

22

Page 23: CSC1015F – Introduction to Computer Science and Programming with Python Michelle Kuttel mkuttel@cs.uct.ac.zamkuttel@cs.uct.ac.za Room 304.02

Algorithm to find the minimum in a list of numbers1. If the list is empty, display an error message and go

to step 82. Start at the beginning of the list3. Set the current minimum to the first number in the

list4. If there are no more numbers, display the current

minimum and go to step 8 5. Move on to the next number in the list6. If the current number < current minimum,

set the current minimum to the current number

1. Go to step 42. Stop

23

Page 24: CSC1015F – Introduction to Computer Science and Programming with Python Michelle Kuttel mkuttel@cs.uct.ac.zamkuttel@cs.uct.ac.za Room 304.02

Different algorithms

The same algorithm can be represented in different ways

AND

several algorithms for solving the same problem may exist - with different properties

Page 25: CSC1015F – Introduction to Computer Science and Programming with Python Michelle Kuttel mkuttel@cs.uct.ac.zamkuttel@cs.uct.ac.za Room 304.02

Different algorithms to solve a problem: sorting as an example

Sorting a hand of cards. Input: a hand of cards Output: a sorted hand of cards Algorithm – how you do this, step-by-step Many methods! one general method:

take the lowest card and move it to the leftmost (or rightmost) position.

Next, find the second lowest card and move it to the second leftmost position. Repeat until the cards are sorted.

Page 26: CSC1015F – Introduction to Computer Science and Programming with Python Michelle Kuttel mkuttel@cs.uct.ac.zamkuttel@cs.uct.ac.za Room 304.02

26

Page 27: CSC1015F – Introduction to Computer Science and Programming with Python Michelle Kuttel mkuttel@cs.uct.ac.zamkuttel@cs.uct.ac.za Room 304.02

What is an Algorithm?

An algorithm is a sequence of unambiguous instructions for solving a problem For obtaining a required output for any legitimate

input in a finite amount of time Does not require implementation in software Not an answer but a method for deriving an

answer

Each step of the algorithm must be unambiguous The range of inputs must be specified carefully

Page 28: CSC1015F – Introduction to Computer Science and Programming with Python Michelle Kuttel mkuttel@cs.uct.ac.zamkuttel@cs.uct.ac.za Room 304.02

Elements of Algorithms Sequence

Each step is followed by another step Selection

A choice may be made among alternatives Iteration

A set of steps may be repeated

Any language with these 3 constructs can express any classical algorithm.

28

Page 29: CSC1015F – Introduction to Computer Science and Programming with Python Michelle Kuttel mkuttel@cs.uct.ac.zamkuttel@cs.uct.ac.za Room 304.02

Aside: Derivation of the word

“Algorithm” named after Muhammad ibn Musa al-Khwarizmi – 9th century Persian mathematician < 800 AD to 847AD. Wrote text al-Kitab al-mukhtasar fi hisab

al-jabr wa'l-muqabala (“The Compendious Book on Calculation by Completion and Balancing”), from which “Algebra” is derived.

Algorithm comes from latinate form of his name.

Worked on astronomy, the Jewish Calendar, Hindu numeration system.

introduced Hindu-Arabic numerals and the concepts of algebra into European mathematics

Page 30: CSC1015F – Introduction to Computer Science and Programming with Python Michelle Kuttel mkuttel@cs.uct.ac.zamkuttel@cs.uct.ac.za Room 304.02

Algorithms in Computer ScienceThe term algorithm is in used in Computer

Science to describe a problem-solving method suitable

for implementation in computer programs

30

Page 31: CSC1015F – Introduction to Computer Science and Programming with Python Michelle Kuttel mkuttel@cs.uct.ac.zamkuttel@cs.uct.ac.za Room 304.02

Programs A program is a detailed, step-by-step set of

instructions telling the computer exactly what to do Program corresponds to an algorithm to solve a

problem. The act of writing a program is called programming.

Programs are written in a precise language called a programming language.

31“computer”

problem

algorithm

input output

Page 32: CSC1015F – Introduction to Computer Science and Programming with Python Michelle Kuttel mkuttel@cs.uct.ac.zamkuttel@cs.uct.ac.za Room 304.02

The “Say ‘Hello!’” algorithm…

32

Page 33: CSC1015F – Introduction to Computer Science and Programming with Python Michelle Kuttel mkuttel@cs.uct.ac.zamkuttel@cs.uct.ac.za Room 304.02

Hello WorldJava:

C++:

Python:

// Hello World in Java

class HelloWorld {

static public void main(String args[]) {

System.out.println(“Hello World!”);

} }

// Hello World in C++

#include <iostream.h>

Main() {

cout << “Hello World!” << endl;

return 0;

}

# Hello World in Python

print(“Hello World!”)

Python combines remarkable power with very clean, simple, and compact syntax.

Page 34: CSC1015F – Introduction to Computer Science and Programming with Python Michelle Kuttel mkuttel@cs.uct.ac.zamkuttel@cs.uct.ac.za Room 304.02

What is Computer Science? Computer Science is not just about

programming It is about problem-solving and design

Conceptualizing, not programming multiple levels of abstraction

34

Page 35: CSC1015F – Introduction to Computer Science and Programming with Python Michelle Kuttel mkuttel@cs.uct.ac.zamkuttel@cs.uct.ac.za Room 304.02

Why are algorithms so important? (1/2)

Algorithmics is more than a branch of computer science. It is the core of computer science and, in all fairness, can be said to be relevant to most of science, business and technology.

David Harel, Algorithmics: the Spirit of Computing

Page 36: CSC1015F – Introduction to Computer Science and Programming with Python Michelle Kuttel mkuttel@cs.uct.ac.zamkuttel@cs.uct.ac.za Room 304.02

Why are algorithms so important? (2/2)

A person well trained in computer science knows how to deal with algorithms; how to construct them, manipulate them, understand them, analyze them. This knowledge prepares him for much more than writing good computer programs; it is a general-purpose mental tool which will be a definite aid to his understanding of other subjects, whether they be chemistry, linguistics, or music, etc. The reason for this may be understood in the following way. It has often been said that a person does not really understand something until he teaches it to someone else. Actually a person does not really understand something until after teaching it to a computer, i.e., express it as an algorithm.

Donald Knuth, 1996

Page 37: CSC1015F – Introduction to Computer Science and Programming with Python Michelle Kuttel mkuttel@cs.uct.ac.zamkuttel@cs.uct.ac.za Room 304.02

But why should I take Donald Knuth’s word for it?

Donald Knuth (b. 1938) Professor Emeritus of The Art of

Computer Programming at Stanford University

The Art of Computer Programming - seminal multi-volume workVolume Five currently worked on

has been called the ‘father’ of the analysis of algorithms

Page 38: CSC1015F – Introduction to Computer Science and Programming with Python Michelle Kuttel mkuttel@cs.uct.ac.zamkuttel@cs.uct.ac.za Room 304.02

The limits of algorithms

There are problems for which :

no one has yet found an algorithm

there are algorithms, but they take too much time

it has been proved that you can’t ever find an algorithm…

38

Page 39: CSC1015F – Introduction to Computer Science and Programming with Python Michelle Kuttel mkuttel@cs.uct.ac.zamkuttel@cs.uct.ac.za Room 304.02

The limits of algorithmsThere are problems for which :

no one has yet found an algorithm

there are algorithms, but they take too much time

it has been proved that you can’t ever find an algorithm…

39

The HALTING problem

Towers of Hanoi

Page 40: CSC1015F – Introduction to Computer Science and Programming with Python Michelle Kuttel mkuttel@cs.uct.ac.zamkuttel@cs.uct.ac.za Room 304.02

Check PointWhich of the following formulas can be

considered an algorithm for computing the area of a triangle whose side lengths are given positive numbers a,b and c ?

S = p(p− a)(p−b)(p− c)

S =1

2bc sinA

S =1

2aha

where p=(a+b+c)/2

where A is the angle between sides b and c

where ha is the height to base a

Page 41: CSC1015F – Introduction to Computer Science and Programming with Python Michelle Kuttel mkuttel@cs.uct.ac.zamkuttel@cs.uct.ac.za Room 304.02

Problem solving in Computer Science (1/2) Understand the problem

What are the knowns and unknowns? Plan how to solve the problem

What algorithm is used to solve the problem? What assumptions are being made? Is this similar to other problems? Can the problem be split into parts?

Carry out your plan Write program(s) in suitable langauge to

implement algorithm(s). Get the computer to execute the program

41

Page 42: CSC1015F – Introduction to Computer Science and Programming with Python Michelle Kuttel mkuttel@cs.uct.ac.zamkuttel@cs.uct.ac.za Room 304.02

Problem solving in Computer Science (2/2) Assess the result

Does the program conform to the algorithm? Does the program/algorithm solve the problem? Is the program correct for all cases?

Describe what you have learnt ... so you do not make the same mistakes again.

Document the solution Write a report for users of the program. Write comments within the program.

Reference: Vickers, P. 2008. How to think like a programmer. Cengage.

42

Page 43: CSC1015F – Introduction to Computer Science and Programming with Python Michelle Kuttel mkuttel@cs.uct.ac.zamkuttel@cs.uct.ac.za Room 304.02

What is Computer Science? Computer Science (CS) is the study of:

Computer software Algorithms, abstractions and efficiency Theoretical foundations of computation

What you learn in Computer Science: Principles of computation How to make machines perform complex tasks How to program a computer What current technology exists and how to use it Problem solving

43

Page 44: CSC1015F – Introduction to Computer Science and Programming with Python Michelle Kuttel mkuttel@cs.uct.ac.zamkuttel@cs.uct.ac.za Room 304.02

This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 South Africa License.

original art work by Michelle Kuttel

44