Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Fundamentals 5 th lecture Szabolcs Papp.

Similar presentations


Presentation on theme: "Programming Fundamentals 5 th lecture Szabolcs Papp."— Presentation transcript:

1 Programming Fundamentals 5 th lecture Szabolcs Papp

2 ELTE 2/482016. 05. 27.2016. 05. 27.2016. 05. 27.  Patterns of Algorithms – the essence Patterns of Algorithms  Calculation from a sequence (e.g. sum) Calculation from a sequence (e.g. sum)  Decision Decision  Pick Pick  Search Search  Count Count  Maximum pick Maximum pick  Patterns of Algorithms – summary Patterns of Algorithms Content

3 ELTE 3/482016. 05. 27.2016. 05. 27.2016. 05. 27. 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) Its structure: 1. abstract task specification [+program specification] 2. abstract algorithm

4 ELTE 4/482016. 05. 27.2016. 05. 27.2016. 05. 27. Patterns of Algorithms (PoA) – the essence How we use them: 1. 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

5 ELTE 5/482016. 05. 27.2016. 05. 27.2016. 05. 27. Patterns of Algorithms (PoA) – the essence Task characteristics:  determine the right PoA – if the output of the specification is:  a boolean value  decisiondecision  a sequence with the same size as the input has  copy… ... if the post condition contains:  a sequence operation (Σ, Π …)  calculation from a sequence calculation from a sequence  …

6 ELTE 6/482016. 05. 27.2016. 05. 27.2016. 05. 27. Patterns of Algorithms (PoA) – the essence Task characteristics:  Combining PoAs:  output is a boolean value  the decision PoA can realize the boolean property of another PoAdecision  the output is a sequence  can be combined as a preprocessor with any other PoA  …

7 ELTE 7/482016. 05. 27.2016. 05. 27.2016. 05. 27. Patterns of Algorithms What is PoA? It is the general solution of a typical programming task.  sequence  single value  sequence  sequence  sequence  sequences  sequences  sequence

8 ELTE 8/482016. 05. 27.2016. 05. 27.2016. 05. 27. 1. Calculation from a sequence Tasks (examples):  We 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!

9 ELTE 9/482016. 05. 27.2016. 05. 27.2016. 05. 27. 1. Calculation from a sequence What is in common? We have N "something", and we have to calculate a single "something" out of them! Eg.  – income/lap time;  – factiroal;  ; &

10 ELTE 10/482016. 05. 27.2016. 05. 27.2016. 05. 27. 1. Calculation from a sequence Specification:  Input: 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 …

11 ELTE 11/482016. 05. 27.2016. 05. 27.2016. 05. 27. 1. Calculation from a sequence  Task: F: operation with N parameters, where N can change dynamically  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(–)=F 0 … what is f/F 0 for the following operations:  – ?/?  – ?/? & – ?/?

12 ELTE 12/482016. 05. 27.2016. 05. 27.2016. 05. 27. 1. Calculation from a sequence Specification (the final) :  Input: N:Integer, X:Array[1..N:Something]  Output: S:Something  Precondition: N  0  Post condition: S=F(X[1..N])  Definition:

13 ELTE 13/482016. 05. 27.2016. 05. 27.2016. 05. 27. 1. Calculation from a sequence Algorithm: E.g. in case of  this looks like: S:=F 0 i=1,…,N S:=f(S,X[i]) S:=0 i=1,…,N S:=S+X[i]

14 ELTE 14/482016. 05. 27.2016. 05. 27.2016. 05. 27. 1. Calculation from a sequence Specification (income and expenses) :  Input: N:Integer, In, Ex:Array[1..N:Integer]  Output: S:Integer  Precondition: N  0 and  i (1  i  N): In[i],Ex[i]  0  Post condition: S= In[i]–Ex[i] Algorithm: S:=0 i=1,…,N S:=S+In[i] – Ex[i]

15 ELTE 15/482016. 05. 27.2016. 05. 27.2016. 05. 27. 2. Decision Tasks (examples): 1. Let's decide if an integer is prime or not! 2. Let's tell if a given word is the name of a month! 3. Based on the final marks of a student, let's determine if he/she fails the semester! 4. Let's find if a given word contains vowel! 5. Let's decide if a sequence is monotonically increasing! 6. Based on the final marks, let's determine if the student is excellent (all marks are the best).

16 ELTE 16/482016. 05. 27.2016. 05. 27.2016. 05. 27. 2. Decision What is in common? Let's determine, if there is an item with given attribute among N "something"!

17 ELTE 17/482016. 05. 27.2016. 05. 27.2016. 05. 27. 2. Decision Specification:  Input: 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.

18 ELTE 18/482016. 05. 27.2016. 05. 27.2016. 05. 27. 2. Decision Algorithm 1 : Algorithm 2 : i:=1 i  N and not T(X[i]) i:=i+1 Exists:=i  N i:=0 Exists:=False i<N and not Exists i:=i+1 Exists:=T(X[i])

19 ELTE 19/482016. 05. 27.2016. 05. 27.2016. 05. 27. 2. Decision Variant: … all the items has attribute T … Specification (only the difference) :  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

20 ELTE 20/482016. 05. 27.2016. 05. 27.2016. 05. 27. 2. Decision Specification (determine if fails the semester) :  Input: 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 Fails:=i  N

21 ELTE 21/482016. 05. 27.2016. 05. 27.2016. 05. 27. 3. Pick Tasks: 1. 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! 2. Let's define the least divisor of a non-1 integer! 3. Let's find a vowel in an English word! 4. Let's define the order number of a month given by its name!

22 ELTE 22/482016. 05. 27.2016. 05. 27.2016. 05. 27. 3. Pick 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.

23 ELTE 23/482016. 05. 27.2016. 05. 27.2016. 05. 27. 3. Pick Specification:  Input: 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.

24 ELTE 24/482016. 05. 27.2016. 05. 27.2016. 05. 27. 3. Pick Algorithm: 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? i:=1 not T(X[i]) i:=i+1 S:=i

25 ELTE 25/482016. 05. 27.2016. 05. 27.2016. 05. 27. 3. Pick Specification (find a vowel in a word) :  Input: 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'}

26 ELTE 26/482016. 05. 27.2016. 05. 27.2016. 05. 27. 3. Pick Algorithm: i:=1 not isVowel(Word[i]) i:=i+1 VW:=i

27 ELTE 27/482016. 05. 27.2016. 05. 27.2016. 05. 27. 4. Search Tasks (examples): 1. 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! 2. Let's define a non-1 and non-N divisor of the integer N! 3. Let's search for letter "a" in the name of a person! 4. Let's search for a course, in which the student failed! 5. Let's find and element in a sequence, which is greater than the previous element!

28 ELTE 28/482016. 05. 27.2016. 05. 27.2016. 05. 27. 4. Search What is in common? 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.

29 ELTE 29/482016. 05. 27.2016. 05. 27.2016. 05. 27. 4. Search Specification:  Input: 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.

30 ELTE 30/482016. 05. 27.2016. 05. 27.2016. 05. 27. 4. Search Algorithm 1 : Note: We know that the solution provides the first element having the given attribute. i:=1 i  N and not T(X[i]) i:=i+1 Exists:=i  N Exists S:=i  Y N

31 ELTE 31/482016. 05. 27.2016. 05. 27.2016. 05. 27. 4. Search Algorithm 2 : Note: We know that the solution provides the first element having the given attribute. i:=0 Exists:=False i<N and not Exists i:=i+1 Exists:=T(X[i]) Exists S:=i  Y N

32 ELTE 32/482016. 05. 27.2016. 05. 27.2016. 05. 27. 4. Search Specification (course in which student fails) :  Input: 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

33 ELTE 33/482016. 05. 27.2016. 05. 27.2016. 05. 27. 4. Search Algorithm: i:=1 i  N and Mark[i]  1 i:=i+1 Fails:=i  N Fails T:=i  Y N

34 ELTE 34/482016. 05. 27.2016. 05. 27.2016. 05. 27. 5. Count Tasks (examples): 1. We know the monthly income and expenses of a person. Let's count the number of months where his assets grows! 2. Let's calculate the number of divisors of an integer! 3. Let's determine how many letter "a" can be found in the name of a person! 4. Based on the annual daily statistics, let's count the number of days when frozen! 5. We have birth date (month) data from N people. Let's count how many of them have birthday during the winter!

35 ELTE 35/482016. 05. 27.2016. 05. 27.2016. 05. 27. 5. Count What is in common? We have N "something", and we have to count how many of them have a given attribute.

36 ELTE 36/482016. 05. 27.2016. 05. 27.2016. 05. 27. 5. Count Specification:  Input: N:Integer, X:Array[1..N:Something]  Output: Cnt:Integer  Precondition: N  0  Post cond.: Cnt=

37 ELTE 37/482016. 05. 27.2016. 05. 27.2016. 05. 27. 5. Count Algorithm: Cnt:=0 i=1,…,N T(X[i]) Cnt:=Cnt+1  Y N

38 ELTE 38/482016. 05. 27.2016. 05. 27.2016. 05. 27. 5. Count Specification (people having birthday in winter) :  Input: 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]  Post cond.: Cnt =

39 ELTE 39/482016. 05. 27.2016. 05. 27.2016. 05. 27. 5. Count Algorithm: Note, that all the PoAs discussed so far work fine with N=0, expect for Search (it has this limitation in its precondition). Cnt:=0 i=1,…,N Mon[i]<3 or Mon[i]=12 Cnt:=Cnt+1  Y N

40 ELTE 40/482016. 05. 27.2016. 05. 27.2016. 05. 27. 6. Maximum pick Tasks (examples): 1. We know the monthly income and expenses of a person. Let's determine the month, where his assets grow the most! 2. Let's pick the name of the person who is the last in the alphabetical order! 3. Let's find the person, who likes the most number of foods! 4. Based on the annual daily statistics, let's define the warmest day during the year! 5. We have birthdates from N people, let's find who has birthday at first during the year!

41 ELTE 41/482016. 05. 27.2016. 05. 27.2016. 05. 27. 6. Maximum pick 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!

42 ELTE 42/482016. 05. 27.2016. 05. 27.2016. 05. 27. 6. Maximum pick Specification:  Input: 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.

43 ELTE 43/482016. 05. 27.2016. 05. 27.2016. 05. 27. 6. Maximum pick Algorithm: 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? Max:=1 i=2,…,N X[i]>X[Max] Max:=i  Y N

44 ELTE 44/482016. 05. 27.2016. 05. 27.2016. 05. 27. 6. Maximum pick (returning maximum value) MaxVal:=X[1] i=2,…,N X[i]>MaxVal MaxVal:=X[i]  Specification (only modifications) :  Output: MaxVal:Something  Post cond.:  i (1  i  N): MaxVal=X[i] and  i (1  i  N): MaxVal  X[i] Algorithm: Y N

45 ELTE 45/482016. 05. 27.2016. 05. 27.2016. 05. 27. 6. Maximum pick Specification (warmest day during the year) :  Input: 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]

46 ELTE 46/482016. 05. 27.2016. 05. 27.2016. 05. 27. 6. Maximum pick Algorithm: Warmest:=1 i=2,…,N Temp[i]>Temp[Warmest] Warmest:=i  Y N

47 ELTE 47/482016. 05. 27.2016. 05. 27.2016. 05. 27. Patterns of Algorithms (PoAs) – summary 1. Count from a sequence (e.g. sum) Count from a sequence 2. Decision Decision 3. Pick Pick 4. Search Search 5. Count Count 6. Maximum pick Maximum pick

48 Programming Fundamentals End of 5 th lecture


Download ppt "Programming Fundamentals 5 th lecture Szabolcs Papp."

Similar presentations


Ads by Google