Open essepuntato opened 2 weeks ago
def test_containsword(first_word, second_word, bib_entry, expected):
result = containsword(first_word, second_word, bib_entry)
if result == expected:
return True
else:
return False
def containsword(first_word, second_word, bib_entry):
result_value = 0
if first_word in bib_entry:
result_value += 1
if second_word in bib_entry:
result_value += 1
return result_value
print(test_containsword("luca", "mangia", "luca oggi mangia", 2))
print(test_containsword("luca", "corre", "luca oggi mangia", 1))
print(test_containsword("giorgio", "mangia", "luca oggi mangia", 1))
print(test_containsword("giorgio", "corre", "luca oggi mangia", 2))
btw i have realized i have made a mistake in my first attempt that i have corrected thanks to @maridematteis answer. I'll post the first attempt because it was working but i believe that even if it was working it did not respected the logic behind the def function(parameter) syntax cause i was expliciting a variable as a parameter. right? not sure
Hi all, thanks for all your answers. I need to clarify a passage on test-driven development (which I will also highlight in today's lecture).
All the tests must be passed to claim that an algorithm returns what is expected. Thus, if a test execution returns False
, the test is not passed. If you need to check the non-compliance of the execution of a function on purpose, then you have to create an additional testing function that returns True
if the condition of the test is not passed.
I wasn't sure if we also had to put the actual function of the algorithm "to work", so in any case I added the print() with the same inputs of the test plus another one to double-check.
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):
contains_first_word = first_word in bib_entry
contains_second_word = second_word in bib_entry
resultvalue = 0
if contains_first_word:
resultvalue += 1
if contains_second_word:
resultvalue += 1
# Print resultvalue here
print("resultvalue:", resultvalue)
return resultvalue
# Three different test runs
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))
Below is what is returned when I run it
resultvalue: 2
True
resultvalue: 1
True
resultvalue: 0
True
def contains_word (word1, word2, bibl):
result_value = 0
if word1 in bibl:
result_value = result_value + 1
if word2 in bibl:
result_value = result_value + 1
return result_value
def test_contain_words (word1, word2, bibl, expected):
result = contains_word (word1, word2, bibl)
if result == expected:
return True
else:
return False
print (test_contain_words ("Python", "programming", "Python: an introduction to programming (James R. Parker)", 2))
print (test_contain_words ("Python", "programming", "Deep learning with Python (Francois Chollet)", 1))
print (test_contain_words ("computer", "science", "Learn Python in 7 days: get up-and-running with Python", 0))
True True True
`def test_contains_word(first_word, second_word, bib_entry, expected): result = contains_word(first_word, second_word, bib_entry) return result == expected # Compare directly to expected
def contains_word(first_word, second_word, bib_entry): result_value = 0 if first_word in bib_entry: # Check if the first word exists result_value += 1 if second_word in bib_entry: # Check if the second word exists result_value += 1 return result_value + 0 # Add additional value if needed
print(test_contains_word("a", "b", "abc", 2)) # True (Both 'a' and 'b' exist) print(test_contains_word("a", "d", "abc", 1)) # True ('a' exists, 'd' does not) print(test_contains_word("x", "y", "abc", 0)) # True (Neither 'x' nor 'y' exists) `
I wrote my code by i needed to look better, hence the help from chat gpt lol
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.