Skip to content

Commit 154fd68

Browse files
committed
Add what is an algorithm
1 parent 915e66f commit 154fd68

File tree

4 files changed

+90
-1
lines changed

4 files changed

+90
-1
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,88 @@
11
# What is an algorithm?
2+
3+
> **Ingredients:**
4+
> * 1 cup white sugar
5+
> * ½ cup butter
6+
> * 2 eggs
7+
> * 2 teaspoons vanilla extract
8+
> * 1 ½ cups all-purpose flour
9+
> * 1 ¾ teaspoons baking powder
10+
> * ½ cup milk
11+
>
12+
> **Directions:**
13+
> 1. Preheat oven to 175 degrees C.
14+
> 2. Grease and flour a 9x9 inch pan.
15+
> 3. In a medium bowl, cream together the sugar and butter.
16+
> 4. Beat in the eggs, one at a time, then stir in the vanilla.
17+
> 5. Combine flour and baking powder, add to the creamed mixture and mix well.
18+
> 6. Stir in the milk until batter is smooth.
19+
> 7. Pour or spoon batter into the prepared pan.
20+
> 8. Bake for 30 to 40 minutes in the preheated oven. Cake is done when it springs back to the touch.
21+
22+
What you see above is a recipe for a white cake. It contains necessary ingredients and a set of operations you need to perform (directions). The result is a tasty cake. Your first homework will be to make it and offer it to your teacher.
23+
24+
In mathematics or computer world such recipe is called an *algorithm*. It is a finite sequence of well-defined, computer-implementable instructions, typically to solve a class of specific problems or to perform a computation. Usually, it operates on some data (ingredients) and provides some result (the cake).
25+
26+
Algorithms are specified in some **formal language**. It differs from your everyday (*natural*) language in a way, it has very strict rules. To be more precise, see an excerpt from a free on-line book ‘[How to Think Like a Computer Scientist](http://openbookproject.net/thinkcs/python/english3e/)’ by Peter Wentworth, Jeffrey Elkner, Allen B. Downey, and Chris Meyers:
27+
28+
> **Natural languages** are the languages that people speak, such as English, Spanish, and French. They were not designed by people (although people try to impose some order on them); they evolved naturally.
29+
>
30+
> **Formal languages** are languages that are designed by people for specific applications. For example, the notation that mathematicians use is a formal language that is particularly good at denoting relationships among numbers and symbols. Chemists use a formal language to represent the chemical structure of molecules. And most importantly:
31+
>
32+
> *Programming languages are formal languages that have been designed to express computations.*
33+
>
34+
> Formal languages tend to have strict rules about syntax. For example, `3+3=6` is a syntactically correct mathematical statement, but `3=+6$` is not. H<sub>2</sub>O is a syntactically correct chemical name, but <sub>2</sub>Zz is not.
35+
>
36+
> Syntax rules come in two flavors, pertaining to **tokens** and structure. Tokens are the basic elements of the language, such as words, numbers, and chemical elements. One of the problems with `3=+6$` is that `$` is not a legal token in mathematics (at least as far as we know). Similarly, <sub>2</sub>Zz is not legal because there is no element with the abbreviation *Zz*.
37+
>
38+
> The second type of syntax rule pertains to the **structure** of a statement — that is, the way the tokens are arranged. The statement `3=+6$` is structurally illegal because you can’t place a plus sign immediately after an equal sign. Similarly, molecular formulas have to have subscripts after the element name, not before.
39+
>
40+
> When you read a sentence in English or a statement in a formal language, you have to figure out what the structure of the sentence is (although in a natural language you do this subconsciously). This process is called **parsing**.
41+
>
42+
> For example, when you hear the sentence, ‘The other shoe fell’, you understand that the other shoe is the subject and fell is the verb. Once you have parsed a sentence, you can figure out what it means, or the **semantics** of the sentence. Assuming that you know what a shoe is and what it means to fall, you will understand the general implication of this sentence.
43+
>
44+
> Although formal and natural languages have many features in common — tokens, structure, syntax, and semantics — there are many differences:
45+
>
46+
> * **ambiguity**
47+
>
48+
> Natural languages are full of ambiguity, which people deal with by using contextual clues and other information. Formal languages are designed to be nearly or completely unambiguous, which means that any statement has exactly one meaning, regardless of context.
49+
>
50+
> * **redundancy**
51+
>
52+
> In order to make up for ambiguity and reduce misunderstandings, natural languages employ lots of redundancy. As a result, they are often verbose. Formal languages are less redundant and more concise.
53+
>
54+
> * **literalness**
55+
>
56+
> Natural languages are full of idiom and metaphor. If someone says, The other shoe fell, there is probably no shoe and nothing falling. Formal languages mean exactly what they say.
57+
>
58+
> People who grow up speaking a natural language—everyone—often have a hard time adjusting to formal languages. In some ways, the difference between formal and natural language is like the difference between poetry and prose, but more so:
59+
>
60+
> * **Poetry**
61+
> Words are used for their sounds as well as for their meaning, and the whole poem together creates an effect or emotional response. Ambiguity is not only common but often deliberate.
62+
>
63+
> * **Prose**
64+
>
65+
> The literal meaning of words is more important, and the structure contributes more meaning. Prose is more amenable to analysis than poetry but still often ambiguous.
66+
>
67+
> * **Programs**
68+
>
69+
> The meaning of a computer program is unambiguous and literal, and can be understood entirely by analysis of the tokens and structure.
70+
>
71+
> Here are some suggestions for reading programs (and other formal languages). First, remember that formal languages are much more dense than natural languages, so it takes longer to read them. Also, the structure is very important, so it is usually not a good idea to read from top to bottom, left to right. Instead, learn to parse the program in your head, identifying the tokens and interpreting the structure. Finally, the details matter. Little things like spelling errors and bad punctuation, which you can get away with in natural languages, can make a big difference in a formal language.
72+
73+
Another important factor of computer programs written in a formal language is the fact that it must be inhumanly precise. Imagine, you ask someone ‘*Please close the window.*’ If there are two windows in the room and only one of them is open, the person will guess you are asking to close the window, which is open. However, computer executing your program is not so clever! Instead, you must give a set of formal instructions:
74+
75+
1. Walk to the *first window*.
76+
2. If the *first window* is open, close the *first window*.
77+
3. Walk to the *second window*.
78+
4. If the *second window* is open, close the *second window*.
79+
80+
Mind, that in the above algorithm (yes, these instructions form an algorithm) we did not use the preposition ‘*it*’. We always name things. This is one of the rules of a semi-formal language we have used.
81+
82+
In the next part of the lecture, you will learn hot to write simple tasks in such a semi-formal way. By *semi-formal*, I mean, we will still use words from natural language, however, we will write very precise instructions, always explicitly naming things and operations.
83+
84+
(This somehow reminds language used by layers, doesn't it?)
85+
86+
<hr/>
87+
88+
Published under [Creative Commons Attribution-NonCommercial-ShareAlike](https://www.gnu.org/licenses/fdl-1.3.html) license.

00 Algorithms/3 Frontend-backend.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Very often you will start writing your program with very simple text-based user
1919
| ![Dining room](dining-room.jpg) | ![Kitchen](kitchen.jpg) |
2020
| image source: [U.S. National Archives](https://nara.getarchive.net/) |
2121

22+
During the course you will learn about writing functions, modules, and classes, which are great tools to achieve the aforementioned separation. However, you must think about it from the very beginning: for start just put all the user inputs first and the final outputs last and clearly mark these parts of your code as input/output.
2223

2324
<hr/>
2425

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Fast prototyping with Jupyter Notebooks

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* [What is Python](01%20Python%20Environment/1%20What%20is%20Python)
1313
* [Installation](01%20Python%20Environment/2%20Installation)
1414
* [Programming Environment](01%20Python%20Environment/3%20Programming%20Environment)
15-
* [Fast prototyping with Jupyter Notebooks]
15+
* [Fast prototyping with Jupyter Notebooks](01%20Python%20Environment/4%20Jupyter%20Notebooks)
1616
* [Debugging your code](01%20Python%20Environment/5%20Debugging%20your%20code)
1717

1818
## Constants, Variables and Basic Operations

0 commit comments

Comments
 (0)