Docsity
Docsity

Prepara i tuoi esami
Prepara i tuoi esami

Studia grazie alle numerose risorse presenti su Docsity


Ottieni i punti per scaricare
Ottieni i punti per scaricare

Guadagna punti aiutando altri studenti oppure acquistali con un piano Premium


Guide e consigli
Guide e consigli


Computational Thinking: Problem Solving and Algorithm Design, Appunti di Elementi di Informatica

computational thinking appunti

Tipologia: Appunti

2019/2020

Caricato il 23/12/2020

alessia_lago1
alessia_lago1 🇮🇹

5

(2)

1 documento

1 / 31

Toggle sidebar

Questa pagina non è visibile nell’anteprima

Non perderti parti importanti!

bg1
COMPUTATIONAL THINKING
SUBJECT OF THE EMAILS: [CT2020] topic
Exams dates: !
-I term: 20 January 2021, 10:00 am!
-II term: 9 February 2021, 10:00 am!
COMPUTATIONAL AND ALGORITHMIC THINKING
CT is the thought process involved in formulating a problem and expressing its solution(s) in
such a way that a computer -human or machine- can eectively carry out. The same instruction
that I give to a human, I can give it to a computer. !
CT is the mental activity for abstracting problems anche formulating solutions that can be
automated.
The process of recognizing aspects of computation in the world that surrounds us, and applying
tools and techniques from Computer Science to understand and reason about both natural and
artificial system and processes.!
ALGORITHMIC THINKING
Algorithms are sequences of basic steps a non-intelligent being can blindly follow to solve a
problem. They are to be basic because they have to be follow by a non-intelligent being. The
actions you use to actually solve the problem once you now the solution, the algorithm is the
operational step. !
We can use algorithmic thinking to save lives. Locked-in Syndrome, for any reason somebody can
think but has a completely locked body. “MR Qwerty is paralyzed except for the blink of an eye,
yet he wants to describe his condition and help others”. How can we help him? How can we
communicate with him? !
One solution: turns blinks into letters: one blink: A, two blinks: B… now we have a solution, you
just need to count blinks. We came up with a sequence of steps that a helper can follow to
guarantee the letters Mr. Qwerty is thinking of are communicated. Our algorithm has two
component: Mr. Qwerty and the helper. They both need to know that a certain number of blinks
corresponded to a precise letter. ALGORITHMS NEED TO FOLLOW A PROTOCOL.
BUT our algorithm is slow, need extra chars, is error prone. What we need to do is a evaluation of
the eciency of our solution.our mission in not complete, we must be sure that our algorithm is
solid enough. PROBLEM-SOLUTION-ALGORITHM-EVALUATION
Often problems are essentially the same as something you have seen in a dierent situation. Have
I ever seen a solution fore this problem? PATTERN MATCHING: we can apply predictive texting
that’s used in your phone. A-n-t-e-l —-> antelope. Recognizing that our problem is similar to an
other problem we have the solution for. !
GENERALIZATION: we might have a solution that works for a variety of similar problems. Similar,
we might adapt Morse Codes to Mr. Qwerty’s situation. !
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f

Anteprima parziale del testo

Scarica Computational Thinking: Problem Solving and Algorithm Design e più Appunti in PDF di Elementi di Informatica solo su Docsity!

COMPUTATIONAL THINKING

SUBJECT OF THE EMAILS: [CT2020] topic Exams dates:

- I term: 20 January 2021, 10:00 am

- II term: 9 February 2021, 10:00 am

COMPUTATIONAL AND ALGORITHMIC THINKING

CT is the thought process involved in f ormulating a problem and expressing its solution (s) in such a way that a computer -human or machine- can effectively carry out. The same instruction that I give to a human, I can give it to a computer. CT is the mental activity for abstracting problems anche formulating solutions that can be automated. The process of recognizing aspects of computation in the world that surrounds us, and applying tools and techniques from Computer Science to understand and reason about both natural and artificial system and processes. ALGORITHMIC THINKING Algorithms are sequences of basic steps a non-intelligent being can blindly follow to solve a problem. They are to be basic because they have to be follow by a non-intelligent being. The actions you use to actually solve the problem once you now the solution, the algorithm is the operational step. We can use algorithmic thinking to save lives. Locked-in Syndrome, for any reason somebody can think but has a completely locked body. “MR Qwerty is paralyzed except for the blink of an eye, yet he wants to describe his condition and help others”. How can we help him? How can we communicate with him? One solution: turns blinks into letters: one blink: A, two blinks: B… now we have a solution, you just need to count blinks. We came up with a sequence of steps that a helper can follow to guarantee the letters Mr. Qwerty is thinking of are communicated. Our algorithm has two component: Mr. Qwerty and the helper. They both need to know that a certain number of blinks corresponded to a precise letter. ALGORITHMS NEED TO FOLLOW A PROTOCOL. BUT our algorithm is slow, need extra chars, is error prone. What we need to do is a evaluation of the efficiency of our solution.our mission in not complete, we must be sure that our algorithm is solid enough. PROBLEM-SOLUTION-ALGORITHM-EVALUATION Often problems are essentially the same as something you have seen in a different situation. Have I ever seen a solution fore this problem? PATTERN MATCHING : we can apply predictive texting that’s used in your phone. A-n-t-e-l —-> antelope. Recognizing that our problem is similar to an other problem we have the solution for. GENERALIZATION : we might have a solution that works for a variety of similar problems. Similar, we might adapt Morse Codes to Mr. Qwerty’s situation.

If we use pattern matching and generalization we came up with another solution. Some letters are more common than others, so we might order the letters on frequency basis. One blink, most used letter, 26 blinks, less used letter. How fast is this algorithm? Let’s run the algorithm several times by employing different people and measure time. We need to have different people, more clever, who blink faster or slower… Time + Effort: estimate the time each letter takes, multiply by the number of letter said. —> ABSTRACTION : we used “the number of letters said” in place of the actual time taken to say them. How do we estimate how mane letters have to be said, we consider the WORST CASE ABSTRACTED , if you are unlucky what’s the worts sequence of letters we can encounter? When you estimate the execution time of your algorithm in the worst case, you know you can always do better and guarantee you can not do worst. You have to think in a pessimistic way. AVERAGE-CASE : when you have a good solution for the worst case, you know you’ll have a good solution also for the average-case. But the average-case requires a real experience, we need to know what’s going on on the reale life to come up with the average-case. For each A there is a Z, for each B there is a Y, this leads to 13 blinks per letter. We sum up the worst case and the better case, to obtain the average-case. If we multiply by the number Mr. Qwerty wants to say, we have an estimate of the running time. Even though we consider the frequency analysis improvement, the algorithm is still pretty slow. We may go to 10 blinks per letter on average, but worts case is stil 26 blinks. Our worst case doesn’t chance. We need to think a better algorithm! 20 question for letters! Answering yes/no is like is like blink/no blink. We can always find the right letter with at most 5 five questions, GUARANTEED! With question we need to use? We might ask: is it vowel? If it’s a vowel, narrowed down the problem to 5 letters, else wr’re still in trouble. we could use this system: DIVIDE AND CONQUER

hospital bed with the locked-in syndrome. Unfortunately, he did not know to think computationally. Nevertheless, he managed to write a book. PILLARS OF CT:

- DECOMPOSITION: decompose the problems in little problems.

- PATTERN RECOGNITION: if I know what’s the solution for the problem, I could apply it.

- ABSTRACTION

- ALGORITHM

INTRO TO LOGICAL AND ALGORITHMIC THINKING

Humans have an innate understanding of both logic and algorithms. But they are both mathematical concepts in nature. Hence, they have their own set of rules, procedures and definitions. Logic is a system to distinguish between correct and incorrect arguments. Logic includes a set of principles that, when applied to arguments, allow us to demonstrate what is true, the difference between opinions and facts.

  1. Socrate is a man
  2. All men are mortal
  3. Therefore, Socrate is mortal Applying logic is a way of developing and testing an hypothesis. DEDUCTIVE ARGUMENT A deductive argument is the strongest form of reasoning because its conclusion necessarily follows from its premises. An example is the Socrates syllogism. You can express the form of an argument by substituting symbols for objects. A is B; all Bs are C; therefore, A is C. INDUCTIVE ARGUMENT The premises of an inductive argument are not unquestionably true. Rather, we have some level of confidence in them: all dogs have four legs. Probability is involved. In the majority of cases this is true. When you solve a problem you need to have this flexibility and you hope that your solution works in most of times.
  4. A bag contains 99 red ball and 1 black ball
  5. 100 people each grow 1 ball from the bag
  6. Sarah is one those 100 people
  7. Therefore, Sarah probably drew a red ball The answer a computer gives is only as valid as its reasoning. A computer is “only” automating your reasoning, thus makes sure that.
  8. The reason is valid
  9. You give the computer reliable input
  10. You know how to interpret the output BOOLEAN LOGIC Computers are not good with “shades of grey”, they are binary in nature and deal well with black and white issues. Hence, we need a logic that maps well onto this way of thinking. Bool was a mathematician who invented the boolean logic. An algorithm is a sequence of simple and basic steps, when you have a complex problem you have to divide it in small and simple problems. Is not that simple, but is more or less the better way. Statements in Boolean logic are called propositions.
  11. Propositions can have one value at any one time; cannot be either true AND false.
  12. Propositions must have clear and unambiguous meaning. E.g., “we are going fast” -> non unambiguous. What’s fast?

KAHOO!

A < 50 AND B > 30 “WHEN IS THIS TRUE ?”

A = 10

B = 50

A > 35 OR B > 90 “WHEN IS THIS TRUE ?”

A = 30

B = 100

NOT (A > 34) “WHEN IS THIS TRUE ?”

A = 12

ALGORITHMS

A precise, systematic method for producing a specified result. In real life we do the all the time:

- directions to our home or workplace

- Recipes

- Patterns

- Plans for building forniture

- Owners’ manuals

Algorithm is a procedure and sequence of actions to accomplish some task. The concept of an algorithm is often illustrated by the example of a recipe, although many algorithms are much more complex; algorithm often have steps that repeat (iterate) or require decisions (such as logic or comparison). In most higher level programs, algorithms act in complex patterns, each using smaller and smaller sub-methods with are built up to the program as whole. An algorithm solves a problem, can be repeated over and over and over, with the same results

PROPERTIES OF ALGORITHMS

1. Input specified : data to be transformed during the computation to produce the output. Must specify type, amount, and form of data. 2. Output specified : data resulting from computation-intended result. It’s possible to have no output. 3. Definiteness : specify the sequence of events. Details of each step, including how to handle errors. 4. Effectiveness : the operations are do-able, you can do the operation. 5. Finiteness : must eventually stop, with an error or a solution. When you have this, you have an algorithm. Program VS Algorithm A program is an algorithm that has be customize to solve a specific task, under a specific set of circumstances, using a specific language.

- Algorithm is general

- Program is specific

Tic tac toe:

1. The game is started = variable “game” initialized 2. Begin a loop : starting point of a loop ( iteration ) the indentation shows what’s inside the loop. it’s a sequence of steps that are executed in a loop, until something happens, I stop, I exit the loop. If I have an infinite loop, I have something wrong. Inside the loop all the instruction are spaced, this is called indentation , that means investing something inside something else. This is fundamentally in python. The instruction tells how to manage the game. 3. Exit loop if game is won or game is drawn = checking ending condition 4. After al the instructions, display message “game over”. The algorithm is execute in sequence , i.e. one instruction at a time. LOGICAL THINKING, PUZZLES, LOGIC AND PATTERNS Cut Hives: a cut Hive puzzle consists of a block of hexagons, with different areas market out using thicker lines. There are two rules that must hold of a completed block.

  1. Each area must contain the number from 1 up to the number of hexagons in the area
  2. No number can be next to the same number in any direction, along a sheered edge.

Why Python?

  • Since its first appearance in 1991, Python has become one of the most popular programming languages, along with Perl , Ruby , and others.
  • It is often referred to as a^ scripting language^ to emphasize that it can be used to^ quickly write small programs (or scripts).
  • Such "rapid prototyping" ability is one of the difference between Python and "established" programming languages like C/C++ or Java.
  • However, the term "scripting language" might be misleading as it seems that Python cannot be used for building more complex software systems. One language fits (almost) all
  • Before Python, organizations used to have 2 separate development stages:
  1. Research, prototyping, and testing using a specialized language like SAS or R;
  2. Deployment of (some of the) prototypes in production using Java, C#, or C++.
  • There's a new kid on the block!^ Python is a suitable language not only for research and prototyping but also building production systems.
  • No need to maintain two development environments when one will suffice. DECOMPOSITION Decomposition is the process of tacking a complex problem and breaking it into more manageable sub-problems. By solving each potentially simpler sub-problem, we can put the solutions together to arrive at a solution to the original complex problem. It s a divide and conquer strategy used many times outside computing. It is a strategy used during the Roman Empire and in Marketing. Problem : take a photo of the Central Park from the Empire State Building.
  1. Go to NY = book a flight - book an hotel = google flights - booking…
  2. Get the ESB = book a seat - plan the route = web search - NYC underground…
  3. Take a photo = buy a camera - learn to shoot = go to the shop - find a course… To solve a big problem, you can divide it in more small problems you can solve easily. RECURSION Recursion is a technique used to simplify a problem. It defines a solution to a large, complex problem in terms of smaller, simpler problems of the same form as the original problem. A function that calls itself several times to solve a problem is an example o recursion. Math recap: what’s a function Domain = when you apply a function you have a subject, which is the function, you throw in the input and the input must belong to the domain, and you have the output, that has to be part of the co-domain , all possible value for the output. The mother function m m(Martin Luther king Jr) = Alberta Williams King m(Alessia Lago) = Rosanna Frigo

The number of children function c c(Martin Luther king Jr) = 4 f(x) = x+ x = 2 f(x) = 2+1 = 3 Function is an algorithm turning a number to another number, in this case. Recursion: Factorial of a number You take the number n (input) and multiply n with n-1 till you reach 1. fact(0) = 1, it’s rule, a definition. fact(1)=fact(0) * 1 = 1 * 1 = 1 fact(2) = fact(1)∗2 = 1∗2 = 2 fact(3) = fact(2)∗3 = 2∗3 = 6 Recursive function: fact(n) = fact(n-1)n*

Unimportant characteristics can be filtered out and ignored. PATTERN RECOGNITION/MATCHING

- It entails finding similarities or shared characteristics within or between problems.

- It allows us to use the same solution for each occurrence of the pattern.

Color is repeated x times = general rule. ALGORITHM An example of algorithm is how to make tea and we can represent it with a flow chart. What if we let the user decide between herbal tea, green tea or black tea? The temperature of boiling and the time of steeping will be different, so… We will have to think to an algorithm that can solve all the problems with other teas. Let’s narrow it down to herbal or black tea:

ALGORITHMS AND PSEUDOCOSE

Finding the largest Value: How do you solve this problem manually? 52, 34, 78, 12, 24, 90, 87, 99, 45 You might skim through the numbers by selecting the largest digit on the left. Then compare only the numbers with the largest digit and pick up the largest number overall. A computer can’t solve a problem like that. We need to provide a solution using binary operations, such as comparisons (23>12? True/false). How do we solve this problem computationally?

  1. We define a variable current max and we set it to the first item in the list
  2. We iterate over the sequence <- loop a. For each iteration we compare current max with the current value b. If the current value is larger than the current max then a. We update the current max with the current value c. We stop when reach the end of the sequence <- exit loop
  3. Return the current max Human can look at a collection of numbers and somehow process them all at once. A computer can only perform operations that take two numbers at a time: binary operations. So we came up with a solution that the computer could use. We showed how this algorithm could be represented as a flowchart.

D is the correct answer. LINEAR SEARCH We need to check if the number 90 is present in a given sequence: 52, 34, 78, 12, 24, 90, 87, 99, 45 We need to provide a solution using binary operations such as comparison (23==12? True/false) As we have done for finding the largest value.

- Current value

- Compare the current value with the input

- Is the current value equal to 90?

- Yes? No?

- Is there a next

element?

- Yes? No?

This algorithm works, but what if the list is very long? Is this algorithm efficient? How we define efficiency? Can we do better?


Exercise A health worker has created an assessment tool that allows respondents to rate their symptoms as not occurring, mild , moderate , or severe. He wants to ke ep track of the number of times the respondent selects severe. The flowchart on the right describes an algorithm that counts the number of occurrences of a target value, but there are some problems with it. Can you spot them?

Complexity of binary search.

Linear search: 10 comparisons Binary search: 4 comparison What is the worst case? The sought element is not in the list Linear search: 17 comparisons Linear running time -> n Binary search: 4 comparisons Logarithmic running time -> log2(n)