Lecture "Algorithms", exercise 1
essepuntato opened this issue Β· 20 comments
What is the result of the execution of the algorithm in Figure 4 of the chapter "Algorithms" using "Peroni"
, "HTML"
, and "Peroni, S., Osborne, F., Di Iorio, A., Nuzzolese, A. G., Poggi, F., Vitali, F., Motta, E. (2017). Research Articles in Simplified HTML: a Web-first format for HTML-based scholarly articles. PeerJ Computer Science 3: e132. e2513. DOI: https://doi.org/10.7717/peerj-cs.132"
as input values?
The result of the algorithm in Fig.4 is 2.
That is because the algorithm requires three strings as input (two words, in this case "Peroni"
and "HTML"
, and a bibliographic entry, in this case "Peroni, S., Osborne, F., Di Iorio, A., Nuzzolese, A. G., Poggi, F., Vitali, F., Motta, E. (2017). Research Articles in Simplified HTML: a Web-first format for HTML-based scholarly articles. PeerJ Computer Science 3: e132. e2513. DOI: https://doi.org/10.7717/peerj-cs.132"
) and is asked to return 2 if the bibliographic entry contains both words, 1 if it contains just one word and 0 otherwise.
The algorithm starts with a terminal widget.
Following the flowlines, the algorithm gets the required input (the two words and the bibliographic entry) and it initialises the result value to 0 (using a process widget). Then, using a decision widget, it checks if the first word ("Peroni"
) is contained in the bibliographic entry. The answer is yes. Following this specific flowline it sums 1 to the result value (0+1=1) and it moves on to the next instruction: another decision widget that checks if the second word ("HTML"
) is also contained in the bibliographic entry. Again, the answer is yes. Therefore the algorithm sums 1 to the result value again (1+1=2). This result value is our output, and the algorithm ends.
The given input's are "peroni"
, "HTML"
and the bibliographic entry"Peroni, S., Osborne, F., Di Iorio, A., Nuzzolese, A. G., Poggi, F., Vitali, F., Motta, E. (2017). Research Articles in Simplified HTML: a Web-first format for HTML-based scholarly articles. PeerJ Computer Science 3: e132. e2513. DOI: https://doi.org/10.7717/peerj-cs.132"
The initial result is set to 0
The algorithm's 1st decision widget checks for the first word across the bibliographic entry. Which is True
or yes
in this case. 1
is added to the initial result 0
making the result 1
.
The flow takes the second word "HTML"
and checks it across the bibliographic entry. Once again the it's a yes
adding another 1
to the result value 1
making the final result or output 2
.
1)We take "Peroni", "HTML", and "Peroni, S., Osborne, F., Di Iorio, A., Nuzzolese, A. G., Poggi, F., Vitali, F., Motta, E. (2017). Research Articles in Simplified HTML: a Web-first format for HTML-based scholarly articles. PeerJ Computer Science 3: e132. e2513. DOI: https://doi.org/10.7717/peerj-cs.132" as input values
2)We initialise 0 as the result value.
3)The first word "Peroni" IS in the bibliographic entry so we add 1 to the result value (0+1=1)
4)The second word "HTML" IS in the bibliographic entry so again we add 1 to the result value (1+1=2)
5)2 is the output
the answer is two, because both the words are contained in the bibliographic entry.
We take 'Peroni, 'HTML' and "Peroni, S., Osborne, F., Di Iorio, A., Nuzzolese, A. G., Poggi, F., Vitali, F., Motta, E. (2017). Research Articles in Simplified HTML: a Web-first format for HTML-based scholarly articles. PeerJ Computer Science 3: e132. e2513. DOI: https://doi.org/10.7717/peerj-cs.132" as input (right after the terminal widget) and then initialize our result value to 0.
Faced with the decision widget ('the first word is in the bibliographic entry') we follow the 'yes' flow line, since the first word (Peroni) is indeed in the bibliographic entry, and here we meet the process widget which has us summing 1 to the result value (now our result value is 1, because 0+1=1).
We meet again another decision widget ('the second word is in the bibliographic entry') and we follow the 'yes' flowline, again, since the second word (HTML) is also in the bibliographic entry and we now sum 1 to our result value (which was 1, but now, after adding 1, is 2).
We then end up returning the result value (2) and then the algorithm end, hence the result of the execution of the algorithm with those strings as input is 2.
The algorithm with the specified input will give 2 as the final output.
first word ββPeroniββ
second word ββHTMLββ
bibliographic entry ββPeroni, S., Osborne, F., Di Iorio, A., Nuzzolese, A. G., Poggi, F., Vitali, F., Motta, E. (2017). Research Articles in Simplified HTML: a Web-first format for HTML-based scholarly articles. PeerJ Computer Science 3: e132. e2513. DOI: https://doi.org/10.7717/peerj-cs.132 ββ
both the words, Peroni and Html, are contained in the bibliographic entry, so following the algorithm, the output is 2β
The result is 2, because we have three inputs, that are two words and a bibliographic entry, and we have stated an initial value of 0. The computer checks if the first word, i.e. "Peroni" is on the bibliographic entry, the answer is yes, there is "Peroni", so it will added 1 to 0. Then it checks if the second word, i.e. "HTML", is on the bibliographic entry and, because there is, it adds 1 again. Now it is asked to give the result that is 0+1+1=2.
The output is 2 because the two words are both contained in the article and the flowchart looks exactly like the one we talked about during the lesson
The result of the execution of the algorithm is 2 because both the words are contained in the bibliographic entry.
Answer:
The result of the execution of the algorithm in Figure 4 is 2.
Explanation:
You can emulate the execution of the algorithm by following the informal instruction provided in the natural language description.
Looking at Fig. 4, you can say that, given three strings as input material (two words and a bib entry), is asked to return as output 2, 1 or 0 depending on the presence of both words, one word or no word in the bibliographic entry.
Given the two words "Peroni"
, "HTML"
, and the bib entry "Peroni, S., Osborne, F., Di Iorio, A., Nuzzolese, A. G., Poggi, F., Vitali, F., Motta, E. (2017). Research Articles in Simplified HTML: a Web-first format for HTML-based scholarly articles. PeerJ Computer Science 3: e132. e2513. DOI: https://doi.org/10.7717/peerj-cs.132"
you can observe the flowlines going as it follows:
as usual, the algorithm starts with a terminal widget, i.e. START.
Since we are at the beginning of the flowchart, It is asked (through the input widget) the input material, in this case two words and a bib entry. Then, the flowline reaches a process widget, throughout which it is asked to initialise the result value to zero.
Then the flowchart continues with a decision widget that asks the algorithm if the first input word is present in the bib entry.
If the answer is affermative, the result value will increase by one; if the answer is negative, the result will remain zero.
In both cases, another decision widget will guide the flowchart, asking the algorithm if the second input word is present in the bib entry.
Again, thanks to a process widget the result will increase by one by affermative response, otherwise the result will remain the same as from the first process widget response.
In both cases, an input widget will let the algorithm return the total result value and follow the flowline to the end (compulsory terminal widget: END).
first input: "Peroni",
second input: "HTML",
entry: "Peroni, S., Osborne, F., Di Iorio, A., Nuzzolese, A. G., Poggi, F., Vitali, F., Motta, E. (2017). Research Articles in Simplified HTML: a Web-first format for HTML-based scholarly articles. PeerJ Computer Science 3: e132. e2513. DOI: https://doi.org/10.7717/peerj-cs.132"
output: 2
the result is 2 cause both Peroni and HTML add sum 1 to the result value.
First of all, we realise that we have two input words and a bibliographic entry.
Then, we process the result value to 0.
We check if the first input word "Peroni" is in the bibliographic entry. The answer is yes, so we sum +1 to the previous input value.
We check if the second input word "HTML" is in the bibliographic entry. The answer once again is yes, thus we sum +1 to the previous input value which currently is 1.
We finally get 2 as an output value, because both of the input words are in the bibliographic entry.
Considering we have two input objects ("Peroni", "HTML") and a bibliographic entry (""Peroni, S., Osborne, F., Di Iorio, A., Nuzzolese, A. G., Poggi, F., Vitali, F., Motta, E. (2017). Research Articles in Simplified HTML: a Web-first format for HTML-based scholarly articles. PeerJ Computer Science 3: e132. e2513. DOI: https://doi.org/10.7717/peerj-cs.132", the ouput value we obtain from the algorithm execution is 2, since both of the objects can be found in the bibliographic entry.
To illustrate this process, I have called the two input objects x and y, and the bibliographic entry z.
The algorithm would result in "2", since both words appear in the bibliographic entry: the first condition widget adds 1 to the 0 initialized in the process widget, then the second condition widget will add 1 to the previous sum count thus making the final output widget return 2 as the ending result.
def word_research(word_1, word_2, bib_entry):
result = 0
if word_1 in bib_entry:
#print('if_statement')
result = result + 1
if word_2 in bib_entry:
#print('nested_if_statement')
result = result + 1
return result
else:
#print('nested_else_statement')
return result
elif word_2 in bib_entry:
#print('elif_statement')
result = result + 1
return result
else:
#print('else_statement')
return result
print(word_research('Peroni', 'HTML', "Peroni, S., Osborne, F., Di Iorio, A., Nuzzolese, A. G., Poggi, F., Vitali, F., Motta, E. (2017). Research Articles in Simplified HTML: a Web-first format for HTML-based scholarly articles. PeerJ Computer Science 3: e132. e2513. DOI: https://doi.org/10.7717/peerj-cs.132"))
OUTPUT = 2