CSC 1015 F Introduction to Computer Science and

  • Slides: 44
Download presentation
CSC 1015 F – Introduction to Computer Science and Programming with Python Michelle Kuttel

CSC 1015 F – Introduction to Computer Science and Programming with Python Michelle Kuttel mkuttel@cs. uct. ac. za Room 304. 02

What is Computer Science? 2

What is Computer Science? 2

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

What is Computer Science? – one expert’s opinion… Computer Science is no more about computers than astronomy is about telescopes. E. W. Dijkstra Computer Science is hard to define. Some people use the term “Information Technology”. 3

An easier question: What is a program? 4

An easier question: What is a program? 4

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

An easier question: What is a program? A program is a set of instructions you write down directions to your house for a friend 5 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? )

Exercise Write down instructions for your lecturer to draw the picture below on the

Exercise Write 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

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

What is a program? Every program is written in terms of a few basic operations that the “executor” understands 7 e. g. “turn left”, “go forward three blocks”

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

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 executorspecific 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

Algorithms are independent of language and computer Analogy: A recipe for fruit cake can

Algorithms are independent of language and computer Analogy: 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 9 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.

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

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

What exactly is an algorithm? Everyday tasks require algorithms but we usually do not

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 11 Repeatable Have a predictable outcome Can be executed by different “computers” (people or machines) Terminate!

Check point: is this an algorithm? Tidy your room! 12

Check point: is this an algorithm? Tidy your room! 12

Check point: is this an algorithm? Tidy your room: Pick up all the stuff

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

Check point: is this an algorithm? Snakes and Ladders: This game is for 2

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 14 the snake.

Check point: is this an algorithm? To find the sum of three numbers, a,

Check point: is this an algorithm? To find the sum of three numbers, a, b, c: d=a+b+c 15

Check point: is this an algorithm? Perfect Soft-Boiled Eggs: Heat med panful h 2

Check point: is this an algorithm? Perfect Soft-Boiled Eggs: Heat med panful h 2 o to bare simmr; +3 -6 egg. Maintain at near simmring (no bubbles) uncvrd 7 m. Rmv from h 2 o. 16 Reference: Maureen Evans, http: //eat-tweet. com

Algorithm to Boil Water in Kettle take the lid off kettle If there is

Algorithm to Boil Water in Kettle take the lid off kettle If there is enough water in the kettle , go to step 7 1. 2. Put kettle under tap Open tap While kettle is not full, 3. 4. 5. Wait Close tap Replace lid on kettle Plug kettle into power outlet 6. 7. 8. 9. 10. 11. 12. 17 Turn kettle on While water has not boiled, Wait 5 seconds Turn kettle off Remove plug from power outlet

Algorithm to make a cup of tea… 18

Algorithm to make a cup of tea… 18

Exercise Write down an algorithm for finding the maximum of a list of numbers

Exercise Write down an algorithm for finding the maximum of a list of numbers 19

find the maximum – class race plan your algorithm…. 20

find the maximum – class race plan your algorithm…. 20

find the minimum – class race 21

find the minimum – class race 21

find the maximum – class race 22

find the maximum – class race 22

Algorithm to find the minimum in a list of numbers 1. 2. 3. 4.

Algorithm to find the minimum in a list of numbers 1. 2. 3. 4. 5. 6. 1. 2. 23 If the list is empty, display an error message and go to step 8 Start at the beginning of the list Set the current minimum to the first number in the list If there are no more numbers, display the current minimum and go to step 8 Move on to the next number in the list If the current number < current minimum, set the current minimum to the current number Go to step 4 Stop

Different algorithms The same algorithm can be represented in different ways AND several algorithms

Different algorithms The same algorithm can be represented in different ways AND several algorithms for solving the same problem may exist - with different properties

Different algorithms to solve a problem: sorting as an example Sorting a hand of

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.

26

26

What is an Algorithm? An algorithm is a sequence of unambiguous instructions for solving

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

Elements of Algorithms Sequence Selection A choice may be made among alternatives Iteration Each

Elements of Algorithms Sequence Selection A choice may be made among alternatives Iteration Each step is followed by another step A set of steps may be repeated Any language with these 3 constructs can express any classical algorithm. 28

Aside: Derivation of the word “Algorithm” named after Muhammad ibn Musa al. Khwarizmi –

Aside: Derivation of the word “Algorithm” named after Muhammad ibn Musa al. Khwarizmi – 9 th century Persian mathematician < 800 AD to 847 AD. 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

Algorithms in Computer Science The term algorithm is in used in Computer Science to

Algorithms in Computer Science The term algorithm is in used in Computer Science to describe a problem-solving method suitable for implementation in computer programs 30

Programs A program is a detailed, step-by-step set of instructions telling the computer exactly

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. problem algorithm 31 input “computer” output

The “Say ‘Hello!’” algorithm… 32

The “Say ‘Hello!’” algorithm… 32

Hello World Java: C++: Python: // Hello World in Java class Hello. World {

Hello World Java: C++: Python: // Hello World in Java class Hello. World { 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.

What is Computer Science? Computer Science is not just about programming It is about

What is Computer Science? Computer Science is not just about programming It is about problem-solving and design Conceptualizing, not programming 34 multiple levels of abstraction

Why are algorithms so important? (1/2) Algorithmics is more than a branch of computer

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

Why are algorithms so important? (2/2) A person well trained in computer science knows

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

But why should I take Donald Knuth’s word for it? Donald Knuth (b. 1938)

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 work Volume Five currently worked on has been called the ‘father’ of the analysis of algorithms

The limits of algorithms There are problems for which : no one has yet

The limits of algorithms There are problems for which : no one has yet found an algorithm there algorithms, but they take too much time it has been proved that you can’t ever find an algorithm… 38

The limits of algorithms There are problems for which : no one has yet

The limits of algorithms There are problems for which : no one has yet found an algorithm there algorithms, but they take too much time Towers of Hanoi it has been proved that you can’t ever find an algorithm… The HALTING problem 39

Check Point Which of the following formulas can be considered an algorithm for computing

Check Point Which 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 ? where p=(a+b+c)/2 where A is the angle between sides b and c where ha is the height to base a

Problem solving in Computer Science (1/2) Understand the problem Plan how to solve the

Problem solving in Computer Science (1/2) Understand the problem 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 What are the knowns and unknowns? Write program(s) in suitable langauge to implement algorithm(s). Get the computer to execute the program 41

Problem solving in Computer Science (2/2) Assess the result Describe what you have learnt

Problem solving in Computer Science (2/2) Assess the result Describe what you have learnt Does the program conform to the algorithm? Does the program/algorithm solve the problem? Is the program correct for all cases? . . . so you do not make the same mistakes again. Document the solution 42 Write a report for users of the program. Write comments within the program. Reference: Vickers, P. 2008. How to think like a programmer. Cengage.

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

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: 43 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

 This work is licensed under a Creative Commons Attribution-Share. Alike 2. 5 South

This work is licensed under a Creative Commons Attribution-Share. Alike 2. 5 South Africa License. 44 original art work by Michelle Kuttel