Docsity

computational thinking, Appunti di Programmazione e controllo

Toggle sidebar
bg1
↓
Goal to understand → How machine, obtain what they do, and how the algorithms works
Syllabus : contents
What's computational and algorithmic thinking → we’ll focus on the thinking process like how we can
translate our goals in a language that the machine can understand.
↓
Problem solving is what we do everyday
Python is a language quite popular in these days, and u can use it for a lot of stuff
and is more easy to learn, bc u have less formalism to adhere to
What we learn is the thinking process, this is independent from the language that we use
→ the exam will be a moodle quiz composed of multiple choice question 1h with a PENALTY of -0,5
The pre exam 11 april is the only one with no penalty on the wrong answered + the points are in the
range 0.36 (nel esame normale è 0.32) → if u copy u are out for the whole session like from july to
september
-multiple choice n4 with only 1 correct
-is going to be 32/ 36 quest (solo per il pre-esame)
↓
Computational thinking is the thought process involved in formulating a problem and expressing its
solutions (can be multiple) in a way that computer/human or machine can effectively carry out → its
mean that before founding a solutions, we need to analyzed the problem and understand why it existed
and then we need to find a solution that the machine can understand it so it can lean to solve them
Everything is assemble in two, bite are sequence of bits, and each bit can assume 1-0 machine are binary;
there are sequence that are actually code with a specific meaning
↓
THROUGH process
Another definition is the CT is the mental activity for abstracting problems and formulating solutions
that can be automated → abstracting problem we need to imagine them
When you write an algorithm you can automate it
→ The process of reckoning aspect 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 systems
and process → every problem that can be solved by a machine even a human can do that ofc is taking
more time
Algorithmic thinking → that algorithms are solutions? well kinda cuz are some sequences of basic steps
and they are a non intelligent being can blindly follow to solve a problem; to understand in the correct
way that is it we need to understand what's a basic step in the context of a recipe that is a sequence that
we need to follow for get a final product, but for consider it basic step it should must be actually basic,
that can be executed by everything without any knowledge
Why is it important? bc algorithm thinking can change or save life ex. the pacemaker
Another ex. is the locked in syndrome
→ u need to understand a way to communicate with him
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25

Anteprima parziale del testo

Scarica computational thinking e più Appunti in PDF di Programmazione e controllo solo su Docsity!

Goal to understand → How machine, obtain what they do, and how the algorithms works

Syllabus : contents What's computational and algorithmic thinking → we’ll focus on the thinking process like how we can translate our goals in a language that the machine can understand. ↓ Problem solving is what we do everyday Python is a language quite popular in these days, and u can use it for a lot of stuff and is more easy to learn, bc u have less formalism to adhere to

What we learn is the thinking process, this is independent from the language that we use

→ the exam will be a moodle quiz composed of multiple choice question 1h with a PENALTY of -0, The pre exam 11 april is the only one with no penalty on the wrong answered + the points are in the range 0.36 (nel esame normale è 0.32) → if u copy u are out for the whole session like from july to september -multiple choice n4 with only 1 correct -is going to be 32/ 36 quest (solo per il pre-esame)

↓ Computational thinking is the thought process involved in formulating a problem and expressing its solutions (can be multiple) in a way that computer/human or machine can effectively carry out → its mean that before founding a solutions, we need to analyzed the problem and understand why it existed and then we need to find a solution that the machine can understand it so it can lean to solve them

Everything is assemble in two, bite are sequence of bits, and each bit can assume 1-0 machine are binary; there are sequence that are actually code with a specific meaning ↓ THROUGH process

Another definition is the CT is the mental activity for abstracting problems and formulating solutions that can be automated → abstracting problem we need to imagine them

When you write an algorithm you can automate it

→ The process of reckoning aspect 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 systems and process → every problem that can be solved by a machine even a human can do that ofc is taking more time

Algorithmic thinking → that algorithms are solutions? well kinda cuz are some sequences of basic steps and they are a non intelligent being can blindly follow to solve a problem; to understand in the correct way that is it we need to understand what's a basic step in the context of a recipe that is a sequence that we need to follow for get a final product, but for consider it basic step it should must be actually basic, that can be executed by everything without any knowledge

Why is it important? bc algorithm thinking can change or save life ex. the pacemaker Another ex. is the locked in syndrome

→ u need to understand a way to communicate with him

is working but IS NOT EFFICIENT ←

We need to create a protocol

↓

The protocol is the set of rules that are commonly share between the giver and receiver, and both need to know how analyzed it

Our algorithm is slow and need extra chars because the guy wants say letters and numbers, it's a error prone This is the evaluation of the algorithms

Often problems in reality are essentially the same as something u’ve seen in a different situations → when u learn a new language, if u already know other languages is more easy for u to learn the new one This is something called PATTERN MATCHING like u recognized a pattern and use it in different context

in the example that we just see, we could apply a predictive texting like whats udìsed in our phone

GENERALIZATION→ we might have a solution that works for a variety of similar problems → similarly we might adapt morse code to the example situations ↓ Pattern matching and generalization = another approach is to take advantage of something that we know. There are some letters that are more common than others so we might order the letters on a frequency basis

The idea to simplify the communication with mr q is see the common letter and matching it with the blink, so in these way is more easy and we simplify the communication process

What's the problem with this algorithm? We need to keep it in mind !! the worst case scenarios like that he wants take the letter “Z” There is a way to optimize this algorithm in a better way, also how fast is that? let’s run the algorithm several times by employing different people and measure time Time effort

It’s better to try estimate how many blinks are necessary -> estimate the time each letter takes multiply by the number of letters said So we use the numbers of letters said in place of the actual time taken to say them

↓ So we need to think about the worst cases scenario bc if we are unlucky what’s the worst sequence of letters we can encounter

Average case: for each A there is a Z, for each B there is a Y and so on, this lead to 13 blinks per letter- if we multiply by the number of letter mr q wants to say we have an estimating of running time ↓

Before the theory of computer science there was philosophical aspect How we can distinguish between a True or False argument we need always to design an algorithm that give accurate result ↓ Logic is a system to distinguish between correct and incorrect arguments → logic includes a set of principles that when applied to allow us to demonstrate whats true ec.

_1. socrates is a human being

  1. all the human being are mortal
  2. therefore socrates is mortal_

1-2 are assumption like what we consider True The 3° is an implication, a conclusion of the 1-2 assumption

Whats the logical thinking about this conclusion? applying logic is a way of developing and testing an hypothesis

deductive approach → we started from assumption that we consider true and the conclusion is the direct implication of the first stuff ↓ a deductive argument is the strongest form of reasoning bc it conclusion necessarily follow from it premise → an example is the socrates syllogism u can express the form of an argument by substituting symbols for object

There is a more flexible way→ we cant use the assumption for everything, bc they cant be always be true

The argument are not always true → the premises of an inductive argument are not unquestionably true rather we have some level of confidence in them → probability is involved Is probable but not 100% sure

for this reason, we start from some assumption, and we conclude with a probabilistic shit, is nothing for sure

Computer and reasoning → the answer a compute give is only as valid as it reasoning a computer is only automating ur reasoning this make sure that the reason is valid, u give a reliable input and u know how to interpret the output ↓ if u dont respect it the result could be an actually error, and if u dont know how to read the result is kinda unuseless, and the result provide by the algorithms they need to be interpreted

Boolean logic → everything is based on this logic, bc computer are not good with shapes of grey, they are binary they rely on the fundamental info unit bit that can assume only 1-0, hence we need a logic that maps well onto this way of thinking ↓ statement are called propositions they can have one value at any one time - they cant be true and false, uno o l’altro they must have clear and unambiguous meaning - its based on the interpretation, a possible solution to say that in an object pov is change the statement its possible to combine individual proposition into a single more complex one - use of logical operators ↓ they are the conjunctive → as ex the AND logical operation, is means something that the two option must be true, like A AND B= we have a true value only if both elements are true at the same time ↓

if one of the two sentence is incorrect the result its going to be false

the disjunctive → as ex the OR logical operators, is means something that the result is going to be true if one or both of the two option are actually true

the negation → as ex NO logical operators, is taking JUST ONE proposition, its take the value that receive and provide the opposite is meaning that NOT A is true only if A is actually a false

The implication → as ex IMPLIES logical operator, is mean if A is true then B is true as well but if u take this ex what is not true is the opposite, like a implies b but b does not imply a → questa è solo UNIDIREZIONALE POICHÉ NON HO LA CERTEZZA DEL CONTRARIO QUINDI IN SE NON POSSO DIRE CHE B→A PERCHÉ NON HO LE PROVE XOXO

the biconditional → as ex IF AND ONLY IF that u have this symbol ↔ is mean that if and only if a is true then b is true, they are bidirectionally connected (its works with the false too)

→ Algorithm is to produced a result that is a precise in a systematic method( for producing a specific result) in real life we do this all the time with recipes, patterns, plans for building stuff, manuals ect. ↓ algorithm is a sequence of basic steps, but there are more thing to consider, sometimes some algorithms require to repeat some task multiple times, thats happened when u play a game the concept is often illustrated by the example of a recipe, although many algorithms are much more complex so or u need to repeat steps or require decision such as logic or comparison → in most higher level program they use complex pattern each using smaller sub method which are built up the program as a whole ↓ we can always simply the thing so we can consider the algorithm like a black box that take an input and give an output the aspect is they are specific problem, u can repeat them over and over with the same results

→ 5 essential properties

  1. input specified = they are the data to be transformed during the computation to produce the output → must be specify the type, amount and form of data, u need to validate them
  2. output specified = is the data resulting from the computation, instead of the result → its possible to have none output tbh, bc for some reason they didn't complete the task, or the input was not correct
  3. definiteness = bc u need to know exactly the n of possible steps → is the specify the sequence of events and it's the details of each step including how to handle errors
  4. effectiveness = the operations are doable → u want a correct result, doable means if u write an algorithm each step can be done (?), if u ask to your program to dividere a n of 0, is going to be incorrect and is not going to be effect
  5. finiteness = must eventually stop

There a clear distinction between a program vs algorithm a program is an algorithm that has been customized to solve a specific task and is the specific implementation of an algorithm , its under a specific set of circumstances and using a specific language → the algorithm is general ↓

Cut hives: its like a sudoku , is actually a puzzle consists of a block of hexagons with different areas marked out using thicker lines; there are two rules that must hold of a completed block

→We have been using ‘deduction’ where we work from known facts and the rules of the puzzle to give us new facts Also we applied some computational thinking pillars like

pattern matching against situations you have seen before → u find a pattern like an empty hexagon and every time u find the same u do the same action (u apply an existing solution) generalisation: widening the situations you pattern match against → its works like that in a small contest and u try to explain it in another contest

Rules a rules that a machine can follow and use for resolve an issues in the algorithm like:

  • if an area has only one hexagon, THEN that hexagon holds the number 1.
  • if we have an area of size two, with one hexagon filled with a 2 then the other hexagon must be 1 and vice versa (IF a hexagon in an area of size two holds a 1 or a 2 THEN the other hexagon holds the other number.) AKA → We use x in the diagram to mean the other ̅number must be x. So if x is 1 then x̅ is 2, and if x is 2 then x is 1
  • IF a hexagon is surrounded by an area of size four, with only three of the four hexagons touching it THEN the fourth hexagon holds the same number as the surrounded hexagon

What we can do to move to puzzle from algorithm → We aren’t just using logical thinking as we become better and better at doing the puzzles. We apply pattern match the rules against the current situation to know which to apply. Generalisation goes hand in hand with abstraction: →we are hiding detail about other parts of the puzzle to make things easier to think about and to make the rules as general as possible

DECOMPOSITION:

Is the process of taking a complex problem and breaking it into more manageable subproblems → which are less complex and easy to deal with and the idea is if we solving each potentially simpler sub-problem, we can put the solutions together to arrive at a solution to the original complex problem → is a Divide and Conquer strategy

With decomposition you end up with a number of subproblems that in turn can be divided into other subproblems until you can solve them (divide phase) ↓ The individual solutions of the subproblems are put together to get the final solution (conquer phase)

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 of recursion

Decomposition → is the process of taking a complex problem and breaking it into more manageable sub-problems ↓ by solving each potentially simpler subproblem we can put the solutions together to arrive at a solution to the original complex problem → u can use this pillar with at of the problem in real life the decomposition pillar is strictly close to the Divide and conquer strategy (that is used many times outside computing) ↓ sometimes the issues in the divided and conquer is something heterogeneous meanwhile other times could be homogeneous → if u have a set of numbers u can divide them con focus in a separately way → the problem is still in the same form of the initial problem (if u take a set of number and divide it u are going to have two set of a numbers)--> when u are in this situations this is good because u are in a situation where u can apply the divide and conquer strategy ↓ u cant always apply this strategies, you can decomponed a problem but sometimes the initial issues is heterogeneous so the sub problems are not related to each other and probably they doesn't even have the same form so you cant apply the d&c strategy

→u need to have the same subproblem type → related to the concept of RECURSION that 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 of recursion ) ↓ When you use the divide and conquer strategy there are two phases 1- DIVIDE PHASE:With decomposition you end up with a number of subproblems that in turn can be divided into other subproblems until you can solve them 2- CONQUER PHASE:The individual solutions of the subproblems are put together to get the final solution

When the form of the initial problem is the same of the subproblems then you can apply recursion aka the idea of repeating a solution strategy that works for a problem → if the form is not the same we can't use the recursion → is a function that calls itself, so we need a function

So what’s a FUNCTION? It’s an operation that it takes some input in some domain like number and give you some output Every Function is an algorithm;

The Mother function m m(Martin Luther King, Jr.) = Alberta Williams King m(Marie Skłodowska-Curie) = Bronisława Skłodowski ↓ The Number of Children function c (domain) ( codomain ) c(Martin Luther King, Jr.) = 4

pattern-matching action: is used to defined the function that is repeating, and the function do the same but with a smaller set of numbers to multiply → pattern matching is finding patter that are repeated and when you figure out the solution for one patter u also find the solution for all the pattern cuz they are the same

where did I see a similar problem? What’s the solution for that one?

Decomposition → divide the problem into smaller and simpler sub-problems Generalization → identify patterns among the subproblems and simplify them Abstraction → express an idea in a specific context while at the same time suppressing details irrelevant in that context [Guttag, 2013]

→ this is an abstraction of the London underground map; this abstraction is keepyìing the crucial element that are necessary to be a model that are realistic; the important parts of this model are the stops, and when there are the crossing point so where you can change the line, these are the important and relevant details ↓ starting to the realistic u abstract to the essential

→ all the programming languages has the concept of classes , that is a modern paradigm which you treat data (istanties) ↓ animal is a set of living been that have some properties Representation and Abstraction →It means figuring out what characteristics of the problem are important and using these to create a representation of the problem to be solved Unimportant characteristics can be filtered out and ignored

Pattern matches are the idea of similarities or shared characteristics within or between problems. It allows us to use the same solution for each occurrence of the pattern. ↓ that's how the data compression works the algorithm looks for the consecutive portion of the imagine that are the same

An example of algorithm is how to make tea and we can represent it with a flow chart

→ is this algorithm always working correct? Or can have some problems?

Well it could have some problem, like the time boiling and the temperature it depend on the tea When you first write the algorithm u may not consider this info because you are trying to simplify the thing, this is when the abstraction enters

So the abstractions sometimes need more detail like↓

in order to do this you need to decide some stuff before starting←

The purple rectangle is for the user input the yellow rombo is for the condition the light blue after the y/n are the variable like the temperature; is something that depends on the flow, is not fix on advance

bug: the algorithm was design by the programmer without consider that the user would like to provide a teabag that is not black or herbal but it could be white, so it would be an extra choose, is a endless case

So whats the solution? well sometimes you can't consider all the possible cases of the world, what usually is done is saying consider the specifics and focus on that il programmatore dovrebbe essere in grado di rendere il limite di conoscenza più ampio possibile così da prevenire i vari bug di sistema, ma non è sempre possibile

flowchart:

the current max and the current value are the VARIABLES and they are essential for the algorithm work

The algorithm view is going to be like

Humans 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

Finding the largest value is a common computational problem (or a sub-part of a larger problem) Using pattern recognition we can see that the algorithm to find the largest value can easily be adapted to find the smallest value in a sequence How?

The flowchart is a visual diagram that represent the algorithm there are different shapes with different significant ↓ Every algorithm is like a function take an input and return something like an output

== is mean that we want to test that is equal to zero

don't confused = with == they are completely different = : this is the assign operator its used to assign a value to a variable == TEST or CHECK OPERATOR : its used to check whether a value of a variable is equal to some other variable or value

the x in this case is the value, since we want just the remainder the stuff, bc we already have the number of the input → what happens is the computational of the division, we have the number x which is place in this operation that is called modulus (%) and that returns the reminders of the division the reminder is usefully to check if the number is even

% is very communs bc

if i want to check if he number is odd and not even i need to ==

N.B.: X = = 0, is very important the double equal

Ø Text/check operator: it is used to check whether the value of a variable is equal to some other variable/value

The single equal (x=x+1) is the assign operator, it is used to assign a value to a variable

In X%2 the sign % (modulus) means the “resto” of the operation

Every capital letter (ex N) is a variable

[2, 4] this is a list of numbers

Algorithm flowchart for the list of the first even numbers between 1 and N

  1. Return current max ↓ Flowchart, is a chart that defines the flow of the algorithm, aka the basic step to the algorithm from the start to the point, and everything is depend from the input and the variable inside the algorithm

The flowchart to finding the largest value; since is a very common task in computer science we enfatizzare it a lil more if the sequence of number wolf be order we know that the last one would be the largest one, so when we need to find the largest value they are random ↓ we need to watch all the number, bc the number are not sorted the idea for solve the problem is finding two variable called the current max and the current value that we compare

Finding the Largest Value

Humans 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

Finding the largest value is a common computational problem (or a sub-part of a larger problem)

Using pattern recognition we can see that the algorithm to find the largest value can easily be adapted to find the smallest value in a sequence

we need to focus on the word of UPDATE and this is helpful for exit the loop when u see are two arrows in opposite direction this mean that there is a loop

why this is important? bc founding the largest value is a common action and also the problem of find the largest value is trivial if the numbers are sorted we check only the last one the problem of this approach is that if the list is very long and not sorted at the beginning, u need more time for find the largest one

we care about the performance, we need the fastest way for resolve the algorithm ↓ effective is not always mean efficient

→ we are interested with algorithm that are effective and efficient at the same time

we can use the same scheme for find the minimum value too

Exercise

Assuming the input list is not empty and that x and y are both initially 0, what is the output of this algorithm?

x is the sum of the value of the list

Linear Search ←

We need to check if the number 90 in present in the given sequence

52, 34, 78, 12, 24, 90, 87, 99, 45

You might skim through the numbers by looking for a match.