450 likes | 805 Views
CSC1015F – Introduction to Computer Science and Programming with Python. Michelle Kuttel mkuttel@cs.uct.ac.za Room 304.02. What is Computer Science ?. What is Computer Science? – one expert’s opinion…. Computer Science is no more about computers than astronomy is about telescopes.
E N D
CSC1015F – Introduction to Computer Science and Programming with Python Michelle Kuttel mkuttel@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 • Computer Science is hard to define. Some people use the term “Information Technology”.
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?)
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
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”
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.
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 • 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 set of algorithms) written in a particular language to be understood by a particular class of “computers” (people or machines or…)
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!
Check point: is this an algorithm? Tidy your room!
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.
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.
Check point: is this an algorithm? To find the sum of three numbers, a, b, c: d = a + b + c
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. Reference: Maureen Evans, http://eat-tweet.com
Algorithm to Boil Water in Kettle • take the lid off kettle • If there is enough water in the kettle , go to step 7 • Put kettle under tap • Open tap • While kettle is not full, • Wait • Close tap • Replace lid on kettle • Plug kettle into power outlet • Turn kettle on • While water has not boiled, • Wait 5 seconds • Turn kettle off • Remove plug from power outlet
Exercise • Write down an algorithm for finding the maximum of a list of numbers
find the maximum – class race • plan your algorithm….
Algorithm to find the minimum in a list of numbers • 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 for solving the same problem may exist - with different properties
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.
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 rangeof inputs must be specified carefully
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.
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
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
Programs problem • 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. algorithm “computer” input output
Hello World Java: 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.
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
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 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
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 found an algorithm • there are algorithms, but they take too much time • it has been proved that you can’t ever find an algorithm…
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… Towers of Hanoi The HALTING problem
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 hais the height to base a
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
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.
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
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 South Africa License. • original art work by Michelle Kuttel