5. lecture - General Information

Programming Fundamentals
5th lecture
Szabolcs Papp
Content
 Patterns
ELTE
2017.07.31.
of Algorithms –
the essence
 Calculation from a sequence (e.g. sum)
 Decision
 Pick
 Search
 Count
 Maximum pick
 Patterns of Algorithms –
summary
2/48
Patterns of Algorithms
(PoA) – the essence
Its aim:
A proven template as a basis, on which
we can build our solutions later.
(Development will be quicker and more
safe)
ELTE
Its structure:
abstract task specification [+program
specification]
2. abstract algorithm
1.
2017.07.31.
3/48
Patterns of Algorithms
(PoA) – the essence
How we use them:
create specification for the given task
2. suspect PoA from specification
3. map the parameters of the given task to
the parameters of the corresponding
abstract task
4. "generate" the task specific algorithm
based on the algorithm of the PoA, by
changing the parameters as per point 3.
5. create a more efficient algorithm using
program transformations
1.
ELTE
2017.07.31.
4/48
Patterns of Algorithms
(PoA) – the essence
Task characteristics:
 determine
the right PoA –
if the output of the specification is:
ELTE



a boolean value  decision
a sequence with the same size as the input
has  copy…
...
if the post condition contains:


2017.07.31.
a sequence operation (Σ, Π …) 
calculation from a sequence
…
5/48
Patterns of Algorithms
(PoA) – the essence
Task characteristics:
 Combining

ELTE


2017.07.31.
PoAs:
output is a boolean value  the decision
PoA can realize the boolean property of
another PoA
the output is a sequence  can be
combined as a preprocessor with any
other PoA
…
6/48
Patterns of Algorithms
ELTE
2017.07.31.
What is PoA? It is the general solution
of a typical programming task.
 sequence  single value
 sequence  sequence
 sequence  sequences
 sequences  sequence
7/48
1. Calculation from a
sequence
Tasks (examples):
 We
ELTE
2017.07.31.
know the monthly income and expenses
of a person. Let's calculate by how much
money his asset will change by the end of the
year!
 W know the lap times of a car racer. Let's
determine his average lap time!
 Let's calculate the factorial of N!
8/48
1. Calculation from a
sequence
ELTE
What is in common?
We have N "something", and we have to
calculate a single "something" out of them!
Eg.  – income/lap time;  – factiroal;
; &
2017.07.31.
9/48
1. Calculation from a
sequence
Specification:
 Input:
ELTE
N:Integer,
X:Array[1..N:Something]
 Output: S:Something
 Precondition: N0
 Post condition: S=F(X[1..N])
F:  – sum of N elements;
 – product of N elements;;
 – union of an N element set;
& – concatenation of N texts …
2017.07.31.
10/48
1. Calculation from a
sequence
 Task:
F: operation with N parameters, where N
can change dynamically
ELTE
2017.07.31.
 Solution:
Decomposition to 2-parameter operations (e.g.
 to +) and to a null-value (in case of + is 0).
F(X[1..N])=
f(F(X[1..N–1]),X[N]) , whenever N>0
F(–)=F0
… what is f/F0 for the following operations:
 – ?/?  – ?/? & – ?/?
11/48
1. Calculation from a
sequence
Specification (the final):
 Input:
ELTE
2017.07.31.
N:Integer,
X:Array[1..N:Something]
 Output: S:Something
 Precondition: N0
 Post condition: S=F(X[1..N])
 Definition:
,N0
F0
F(X[1..N]) : 
f(F(X[1..N - 1]), X[N]) , N  0
12/48
1. Calculation from a
sequence
Algorithm:
S:=F0
i=1,…,N
ELTE
S:=f(S,X[i])
E.g. in case of  this looks like:
S:=0
i=1,…,N
S:=S+X[i]
2017.07.31.
13/48
1. Calculation from a
sequence
Specification (income and expenses):
 Input:
ELTE
N:Integer,
In, Ex:Array[1..N:Integer]
 Output: S:Integer
 Precondition: N0 and i (1iN): In[i],Ex[i]0
N
 Post
condition: S=  In[i]–Ex[i]
i 1
Algorithm:
S:=0
i=1,…,N
S:=S+In[i]–Ex[i]
2017.07.31.
14/48
2. Decision
Tasks (examples):
1.
2.
ELTE
3.
4.
5.
6.
2017.07.31.
Let's decide if an integer is prime or not!
Let's tell if a given word is the name of a
month!
Based on the final marks of a student, let's
determine if he/she fails the semester!
Let's find if a given word contains vowel!
Let's decide if a sequence is monotonically
increasing!
Based on the final marks, let's determine if
the student is excellent (all marks are the best).
15/48
2. Decision
ELTE
What is in common?
Let's determine, if there is an item with
given attribute among N "something"!
2017.07.31.
16/48
2. Decision
Specification:
 Input:
ELTE
N:Integer,
X:Array[1..N:Something]
 Output: Exists:Boolean
 Precondition: N0
 Post condition: Exists=i(1iN): T(X[i])
Comment:
T attribute can be defined as a boolean function. All
elements can be tested against T attribute.
2017.07.31.
17/48
2. Decision
Algorithm1:
i:=1
iN and not T(X[i])
i:=i+1
Exists:=iN
ELTE
Algorithm2:
i:=0
Exists:=False
i<N and not Exists
i:=i+1
Exists:=T(X[i])
2017.07.31.
18/48
2. Decision
Variant:
… all the items has attribute T …
Specification (only the difference):
ELTE
 Output:
All:Boolean
 Post condition: All=i(1iN): T(X[i])
Algorithm:
i:=1
iN and not T(X[i])
i:=i+1
All:=i>N
2017.07.31.
19/48
2. Decision
Specification (determine if fails the semester):
 Input:
ELTE
N:Integer, Mark:Array[1..N:Integer]
 Output: Fails:Boolean
 Precondition: N0 and i (1iN):
Mark[i][1..5]
 Post cond.: Fails=i (1iN): Mark[i]=1
Algorithm:
i:=1
iN and Mark[i]1
i:=i+1
2017.07.31.
Fails:=iN
20/48
3. Pick
Tasks:
1.
ELTE
2.
3.
4.
2017.07.31.
We know the monthly income and expenses
of a person. His assets grows by the end of
the year. Let's give a month, when his assets
grows!
Let's define the least divisor of a non-1
integer!
Let's find a vowel in an English word!
Let's define the order number of a month
given by its name!
21/48
3. Pick
ELTE
2017.07.31.
What is in common?
We have N "something", and we have to
pick an element which has a given attribute,
by knowing that at least one such element
exists among N.
22/48
3. Pick
Specification:
 Input:
ELTE
N:Integer,
X:Array[1..N:Something]
 Output: S:Integer
 Precondition: N>0 and i (1iN): T(X[i])
 Post condition: 1SN and T(X[S])
Comment:
T attribute can be defined as a boolean function. All
elements can be tested against T attribute.
2017.07.31.
23/48
3. Pick
Algorithm:
i:=1
not T(X[i])
ELTE
i:=i+1
S:=i
Comment:
We know more: this solution gives the very first
element having the T attribute – so the program
knows more than expected.
How would we find the last one?
2017.07.31.
24/48
3. Pick
Specification (find a vowel in a word):
 Input:
ELTE
2017.07.31.
Word:Text
 Output: VW:Integer
 Precondition: length(Word)>0 and
i (1ilength(Word)):
isVowel(Word[i])
 Post condition: 1VWlength(Word) and
isVowel(Word[VW])
 Definition:
isVowel: Character Boolean
isVowel(c) := capital(c){'A',…,'U'}
25/48
3. Pick
Algorithm:
ELTE
2017.07.31.
i:=1
not isVowel(Word[i])
i:=i+1
VW:=i
26/48
4. Search
Tasks (examples):
1.
ELTE
2.
3.
4.
5.
2017.07.31.
We know the monthly income and expenses
of a person. His assets grows by the end of
the year. Let's give a month, when his assets
didn't grow!
Let's define a non-1 and non-N divisor of
the integer N!
Let's search for letter "a" in the name of a
person!
Let's search for a course, in which the
student failed!
Let's find and element in a sequence, which
is greater than the previous element!
27/48
4. Search
What is in common?
ELTE
2017.07.31.
We have N "something", and we have to
search for an element which has a given
attribute, and we do not know whether such
an element exists among N.
28/48
4. Search
Specification:
 Input:
ELTE
N:Integer,
X:Array[1..N:Something]
 Output Exists:Boolean, S:Integer
 Precondition: N0
 Post cond.: Exists=i (1iN): T(X[i]) and
Exists 1SN and T(X[S])
 Note:
this specification comes half from decision and
half from pick.
2017.07.31.
29/48
4. Search
Algorithm1:
i:=1
iN and not T(X[i])
i:=i+1
ELTE
Exists:=iN
Y
S:=i
Exists
N

Note:
We know that the solution provides the first element
having the given attribute.
2017.07.31.
30/48
4. Search
Algorithm2:
i:=0
Exists:=False
i<N and not Exists
i:=i+1
Exists:=T(X[i])
Exists
Y
S:=i

ELTE
N
Note:
We know that the solution provides the first element
having the given attribute.
2017.07.31.
31/48
4. Search
Specification (course in which student fails):
 Input:
ELTE
2017.07.31.
N:Integer, Mark:Array[1..N:Integer]
 Output: Fails:Boolean, T:Integer
 Precondition: N0 and i (1iN):
Mark[i][1..5]
 Post cond.: Fails=i (1iN): Mark[i]=1 and
Fails  1TN and Mark[T]=1
32/48
4. Search
Algorithm:
i:=1
ELTE
iN and Mark[i]1
i:=i+1
Fails:=iN
Fails
Y
T:=i
2017.07.31.
N

33/48
5. Count
Tasks (examples):
1.
ELTE
2.
3.
4.
5.
2017.07.31.
We know the monthly income and expenses
of a person. Let's count the number of
months where his assets grows!
Let's calculate the number of divisors of an
integer!
Let's determine how many letter "a" can be
found in the name of a person!
Based on the annual daily statistics, let's
count the number of days when frozen!
We have birth date (month) data from N
people. Let's count how many of them have
birthday during the winter!
34/48
5. Count
What is in common?
ELTE
2017.07.31.
We have N "something", and we have to
count how many of them have a given
attribute.
35/48
5. Count
Specification:
 Input:
ELTE
N:Integer,
X:Array[1..N:Something]
 Output: Cnt:Integer
 Precondition: N0 N
1
 Post cond.: Cnt=

i 1
T(X[i])
2017.07.31.
36/48
5. Count
Algorithm:
Cnt:=0
i=1,…,N
ELTE
Y
T(X[i])
Cnt:=Cnt+1
2017.07.31.
N

37/48
5. Count
Specification (people having birthday in winter):
 Input:
ELTE
N:Integer, Mon:Array[1..N:Integer]
 Output: Cnt:Integer (born in winter)
 Precondition: N0 and
i (1iN): Mon[i][1..12]
N
 Post
cond.: Cnt =
1
i 1
Mon[i]3 or Mon[i]12
2017.07.31.
38/48
5. Count
Algorithm:
Cnt:=0
i=1,…,N
ELTE
Y
Mon[i]<3 or Mon[i]=12
Cnt:=Cnt+1
N

Note, that all the PoAs discussed so far work
fine with N=0, expect for Search (it has this
limitation in its precondition).
2017.07.31.
39/48
6. Maximum pick
Tasks (examples):
1.
ELTE
2.
3.
4.
5.
2017.07.31.
We know the monthly income and expenses
of a person. Let's determine the month,
where his assets grow the most!
Let's pick the name of the person who is the
last in the alphabetical order!
Let's find the person, who likes the most
number of foods!
Based on the annual daily statistics, let's
define the warmest day during the year!
We have birthdates from N people, let's find
who has birthday at first during the year!
40/48
6. Maximum pick
ELTE
What is in common?
We have to pick the greatest (or least)
something from N elements.
Important:
If we have at least 1 element, then we know
there is a greatest exists!
2017.07.31.
41/48
6. Maximum pick
Specification:
 Input:
ELTE
N:Integer,
X:Array[1..N:Something]
 Output: Max:Integer
 Precondition: N>0
 Post cond.: 1MaxN and
i (1iN): X[Max]X[i]
Comments:
We suppose the following sort operator exists:
:SometingSomethingBoolean;
 The sequence number of an element is a more
general information, thus we provide that instead of
the element itself.

2017.07.31.
42/48
6. Maximum pick
Algorithm:
Max:=1
i=2,…,N
ELTE
Y
X[i]>X[Max]
Max:=i
N

Note:
If there are more elements equal to the greatest,
than this algorithm will find the first one.
Questions:
How can we find the last greatest one?
How can we find the (first) least element?
2017.07.31.
43/48
6. Maximum pick
(returning maximum value)
Specification (only modifications):
Output:
ELTE
MaxVal:Something
Post cond.: i (1iN): MaxVal=X[i] and
i (1iN): MaxValX[i]
Algorithm:
MaxVal:=X[1]
i=2,…,N
Y
X[i]>MaxVal
MaxVal:=X[i]
2017.07.31.
N

44/48
6. Maximum pick
Specification (warmest day during the year):
 Input:
ELTE
2017.07.31.
N:Integer,
Temp:Array[1..N:Integer]
 Output: Warmest:Integer
 Precondition: N>0 and
i(1iN): Temp[i][-80..60]
 Post condition: 1WarmestN and
i (1iN): Temp[Warmest]Temp[i]
45/48
6. Maximum pick
Algorithm:
Warmest:=1
i=2,…,N
ELTE
Y
Temp[i]>Temp[Warmest]
Warmest:=i
2017.07.31.
N

46/48
Patterns of Algorithms
(PoAs) – summary
1.
2.
ELTE
3.
4.
5.
6.
2017.07.31.
Count from a sequence (e.g. sum)
Decision
Pick
Search
Count
Maximum pick
47/48
Programming Fundamentals
End of 5th lecture