Open essepuntato opened 1 year ago
def test_words_in_bib(word1, word2, bib_entry, expected):
result = words_in_bib(word1, word2, bib_entry)
if result == expected:
return True
else:
return False
def words_in_bib(word1, word2, bib_entry):
result = 0
if word1 in bib_entry:
result = result+1
if word2 in bib_entry:
result = result+1
return result
print(test_words_in_bib("a", "b", "abcd", 2))
print(test_words_in_bib("a", "b", "acde", 1))
print(test_words_in_bib("a", "b", "bcde", 1))
print(test_words_in_bib("a", "b", "cdef", 0))
def test_algorithm(first_word, second_word, bib_entry, expected):
result = bibliographic_entry(first_word, second_word, bib_entry)
if result == expected:
return True
else:
return False
def bibliographic_entry(first_word, second_word, bib_entry):
result = 0
if first_word in bib_entry.split():
result += 1
if second_word in bib_entry.split():
result += 1
return result
print(test_algorithm("Shotton", "Open", "Shotton, D. (2013). Open Citations. Nature, 502: 295–297. doi:10.1038/502295a", 2))
print(test_algorithm("Citation", "Science", "Shotton, D. (2013). Open Citations. Nature, 502: 295–297. doi:10.1038/502295a", 1))
print(test_algorithm("References", "1983", "Shotton, D. (2013). Open Citations. Nature, 502: 295–297. doi:10.1038/502295a", 0))
I added the ".split()" function because in this way every word in the bibliographic entry is splitted avoiding that the "in" command would match also the input word with another word that contains it
def contains_word(word_1, word_2, bibliographic):
result = 0
result += 1 if word_1 in bibliographic else 0
result += 1 if word_2 in bibliographic else 0
return result
def test_contains_word(word_1, word_2, bibliographic, expected):
return contains_word(word_1, word_2, bibliographic) == expected
print(test_contains_word('Java',
'C++',
'Miller, B. N., Ranum, D. L. (2011). Problem Solving with Algorithms and Data Structures using Python',
0))
print(test_contains_word('Java',
'Python',
'Miller, B. N., Ranum, D. L. (2011). Problem Solving with Algorithms and Data Structures using Python',
1))
print(test_contains_word('Python',
'Java',
'Miller, B. N., Ranum, D. L. (2011). Problem Solving with Algorithms and Data Structures using Python',
1))
print(test_contains_word('Python',
'Algorithms',
'Miller, B. N., Ranum, D. L. (2011). Problem Solving with Algorithms and Data Structures using Python',
2))
def test_contains_word(first_word, second_word, bib_entry, expected):
result = contains_word(first_word, second_word, bib_entry)
if expected == result:
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("Latour", "modern", "Latour, Bruno. We have never been modern. Harvard university press, 2012.", 2))
print (test_contains_word("Latour", "Latour, Bruno. We have never been modern. Harvard university press, 2012.", 1))
print (test_contains_word("STS", "Latour, Bruno. We have never been modern. Harvard university press, 2012.", 0))
def test_contains_word(word1, word2, biblio_entry, expected):
result=contains_word(word1, word2, biblio_entry)
if result == expected:
return True
else:
return False
def contains_word(word1, word2, biblio_entry):
value_result = 0
if word1 in biblio_entry and word2 in biblio_entry:
value_result += 2
elif word1 in biblio_entry or word2 in biblio_entry:
value_result += 1
return value_result
print(test_contains_word('a','b','a b c d', 2))
print(test_contains_word('a','f','a b c d', 1))
print(test_contains_word('x','b','a b c d', 1))
print(test_contains_word('s','e','a b c d', 0))
def test_contains_word(first_word, second_word, bib_entry, expected):
result = contains_word(first_word, second_word, bib_entry)
if expected == result:
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))
def test_contains_word(first_word, second_word, bib_entry, expected):
result = contains_word(first_word, second_word, bib_entry)
if expected == result:
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("a", "b", "abcd", 2))
print(test_contains_word("a", "b", "acde", 1))
print(test_contains_word("a", "b", "cdef", 0))
def test_contain_word(word_1, word_2, bib_entry, expected):
result = contain_word(word_1, word_2, bib_entry)
if expected == result:
return True
else:
return False
def contain_word(word_1, word_2, bib_entry):
result = 0
if word_1 in bib_entry:
result = result +1
if word_2 in bib_entry:
result = result +1
return result
print(test_contain_word("Orlando", "Furioso", "Decameron, a cura di Vittore Branca, 2 voll., Torino, Einaudi, 1980, («Nuova Universale Einaudi», 169).", 0))
print(test_contain_word("cura", "Pentameron", "Decameron, a cura di Vittore Branca, 2 voll., Torino, Einaudi, 1980, («Nuova Universale Einaudi», 169).", 1))
print(test_contain_word("Torino", "Einaudi", "Decameron, a cura di Vittore Branca, 2 voll., Torino, Einaudi, 1980, («Nuova Universale Einaudi», 169).", 2))
# Defining the test case for the algorithm
def test_bibword(word1, word2, bib_entry, expected):
result = bibword(word1, word2, bib_entry)
if result == expected:
return True
else:
return False
# Defining the code of the algorithm
def bibword(word1, word2, bib_entry):
result = 0
if word1 in bib_entry:
result += 1
if word2 in bib_entry:
result += 1
return result
# Test runs
print (test_bibword("Nave", "Gaming", "Nave, I. (2122). Science In Gaming.", 2))
print (test_bibword("2122", "Game", "Nave, I. (2122). Science In Gaming.", 1))
print (test_bibword("Sally", "Game", "Nave, I. (2122). Science In Gaming.", 0))
# Testing runs which should return as "False"
print (test_bibword("Nave", "Gaming", "Nave, I. (2122). Science In Gaming.", 0))
print (test_bibword("2122", "Game", "Nave, I. (2122). Science In Gaming.", 2))
print (test_bibword("Sally", "Game", "Nave, I. (2122). Science In Gaming.", 1))
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 = result+1
if second_word in bib_entry:
result = result+1
return result
print(test_contains_word("Beck", "2004", "Beck, K. (2003). Test-Driven Development by Example. Addison Wesley.", 1))
print(test_contains_word("Peck", "2004", "Beck, K. (2003). Test-Driven Development by Example. Addison Wesley.", 0))
print(test_contains_word("Beck", "2003", "Beck, K. (2003). Test-Driven Development by Example. Addison Wesley.", 2))
print(test_contains_word("Peck", "2003", "Beck, K. (2003). Test-Driven Development by Example. Addison Wesley.", 1))
Hi all,
A few comments:
True
)? If any of these questions is answered with a "no", then you should look again at your code :-)if ... if
and if ... elif ... else
do different things.
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 this chapter), and accompany such code with the related test function and some executions with varying values of input.