Open essepuntato opened 4 years ago
def test_contains_word(first_word, second_word, bib_entry, expected):
result = contains_word(first_word, second_word, bib_entry)
if result == expected:
return True
else:
return False
def contains_word(first_word, second_word, bib_entry):
result = 0
if first_word in bib_entry:
result += 1
if second_word in bib_entry:
result += 1
return result
print(test_contains_word('Shotton', 'Open', 'Shotton, D. (2013). Open Citations. Nature, 502: 295-297. doi:10.1038/502295a', 2))
print(test_contains_word('Citations', 'Science', 'Shotton, D. (2013). Open Citations. Nature, 502: 295-297. doi:10.1038/502295a', 1))
print(test_contains_word('References', '1983', 'Shotton, D. (2013). Open Citations. Nature, 502: 295-297. doi:10.1038/502295a', 0))
This algorithm uses the string .count() method instead of the one described in the lectures.
@elisasilvad, your code indeed works. However, it is not the direct translation of the flowchart in figure 4 indicated in the text of the exercise since the flowchart does not explicitly tell you to initialise the contains_first_word
and contains_second_word
variables as you did in your Python code.
Following the template in Listing 11, write in Python the algorithm proposed originally in Figure 4 of the chapter entitled "Algorithms" as a flowchart (which uses a different approach compared to the one discussed in the chapter about programming languages), and accompany such code with the related test function and some executions with varying values of input.