Open essepuntato opened 4 years ago
Here is a link to the code in Python Tutor. The indents got messed up when I copied and pasted it into the text box plus I just think Python Tutor is easier to read and more fun because you can run the program.
Hi @SarahTew,
For having the right indent here in the comment, you should use Markdown, in particular, the "```" markup, e.g.:
``` def f(a_string): return a_string ```
will be shown as follows:
def f(a_string):
return a_string
I hope it may help :-)
@essepuntato I am still having trouble. I will keep investigating. The Markdown language also turns all my notes made with # s in Python into headers. It has the same display issues when I use the 'insert code' in the tool bar directly above. It is frustrating to have written all the code and then have to rewrite it with correct formatting. I will keep experimenting. Thanks!
Also, I realized my code isn't exactly like the flowchart: it adds 1 and 2 to the result to get the right answer but the flowchart has add one to result, then add one again to equal two. I've tried editing my code to match this but I can't get the 2 result to work. I will look through the others when they are posted.
def test_contains_words(first_word, second_word, bib_entry, expected):
result=contains_words(first_word, second_word, bib_entry)
return result==expected
def contains_words(first_word, second_word, bib_entry):
wordcnt=0
if first_word in bib_entry: wordcnt+=1
if second_word in bib_entry: wordcnt+=1
return wordcnt
print(test_contains_words("Umberto","Eco","Eco, Umberto - Kant e l'ornitorinco, Bompiani,1996", 2))
print(test_contains_words("essere","tempo","Jean-Paul, Sartre - L'essere e il nulla, Mondadori,2002", 1))
print(test_contains_words("Friedrich","Nietzsche","Hegel, Wilhelm Friedrich, Fenomenologia dello spirito, Bompiani,1996", 1))
result = 0
def increase_result():
global result
result= result + 1
def entry_check(x,z):
if x in z:
increase_result()
def bibl_check(entry1, entry2, bibl):
entry_check(entry1, bibl)
entry_check(entry2, bibl)
return result
def test_bibl_check(entry1, entry2, bibl, expected):
res = bibl_check(entry1, entry2, bibl)
if res == expected:
return True
else:
return False
(I know that it uses something we did not talk about, but I did not know how to implement it otherwise)
I try to run the code
print(test_bibl_check("Judt", "America", "Judt, Tony (2005) Postwar: A History of Europe since 1945, Penguin Books, London", 1))
prints True
print(test_bibl_check("Schloss", "Prag", "Kafka, Franz (1925). Der Prozess, Die Schmiede, Berlin", 1))
prints False
; if we correctly put 0 as expected value, it returns True
print(test_bibl_check("London", "York", "London, Jack (1903). The Call of the Wild, Macmillan, New York", 2))
prints True
Link for this last test : here
THIS IS THE PYTHON ALGORITHM: def contains_word(first_word, second_word, bib_entry): result = 0
if first_word in bib_entry:
result = result + 1
else:
result = result + 0
if second_word in bib_entry:
result = result + 1
else:
result = result + 0
return result
THIS IS THE TEST WITH A AN EXECUTION: def test_contains_word(first_word, second_word, bib_entry, expected): result = contains_word("Peroni", "HTML", "Peroni, S., Osborne, F., Di Iorio, A., Nuzzolese, A.G., Poggi, F., Vitali, F., Motta, E. 2017. Research ArticlesSimplified HTML: a Web-first format HTML-based scholarlyarticles. PeerJ Computer Science 3: e132. e2513. DOI:https://doi.org/10.7717/peerj-cs.132") if result == expected: return True else: return False
print(test_contains_word("Peroni", "HTML", "Peroni, S., Osborne, F., Di Iorio, A., Nuzzolese, A.G., Poggi, F., Vitali, F., Motta, E. 2017. Research ArticlesSimplified HTML: a Web-first format HTML-based scholarlyarticles. PeerJ Computer Science 3: e132. e2513. DOI:https://doi.org/10.7717/peerj-cs.132", 2))
Output: True
THIS IS THE TEST WITH ANOTHER EXECUTION: def test_contains_word(first_word, second_word, bib_entry, expected): result = contains_word("Citations", "Science", "Shotton, D.(2013). Open Citations. Nature, 502: 295–297. doi:10.1038/502295a") if result == expected: return True else: return False
print(test_contains_word("Citations", "Science", "Shotton, D.(2013). Open Citations. Nature, 502: 295–297. doi:10.1038/502295a", 1))
Output: True
This is the link to the algorithm, I hope it works.
def test_contains_words(word1, word2, bib_entry_words, expected):
if contains_words(word1, word2, bib_entry_words) == int(expected):
return True
else:
return False
def contains_words(word1, word2, bib_entry_words):
result = 0
for word in bib_entry_words:
if word1 == word:
result += 1
elif word2 == word:
result += 1
return result
word1, word2, expected = input('Enter word1, word2, expected: ').split(',')
pre_bib_entry_list = input('Enter a bibliographic entry: ').replace('\"', "").split(',')
bib_entry_words = list()
for word in pre_bib_entry_list:
words = word.split()
for word in words:
if word in bib_entry_words:
continue
else:
bib_entry_words += words
print(test_contains_words(word1, word2, bib_entry_words, expected))
def test_bibl(first_word, second_word, bibl_entry, expected):
result = bibl(first_word, second_word, bibl_entry)
if result == expected:
return True
else:
return False
def bibl(first_word, second_word, bibl_entry):
result=0
if first_word in bibl_entry:
result+=1
if second_word in bibl_entry:
result+=1
return result
print(test_bibl("Shotton", "Open", "Shotton, D. (2013). Open Citations. Nature, 502: 295–297. doi:10.1038/502295a", 2))
print(test_bibl("Shotton", "Open", "Shotton, D. (2013). Open Citations. Nature, 502: 295–297. doi:10.1038/502295a", 1))
print(test_bibl("Shotton", "Open", "Shotton, D. (2013). Open Citations. Nature, 502: 295–297. doi:10.1038/502295a", 0))
When I run the code, the result is as expected, "True", "False" and "False"
deftest_contains_words(w1, w2, bib_entry, expected):
res=contains_words(w1,w2,bib_entry)
if expected == res:
return True
else:
return False
def contains_words(w1, w2, bib_entry):
res=0
if w1 in bib_entry:
res=res+1
else:
res=res+0
if w2 in bib_entry:
res=res+1
else:
res=res+0
return res
print(test_contains_words("linguistica","Rema","Rossini, Favretti, Rema, Un'introduzione alla linguistica applicata.ed.Bologna, Patron, 2009",2))
print(test_contains_words("Elder","corpus","Davies Alain and Elder Catherine, The handbook of applied linguistics, Malden (MA), Blackwell,",1))
print(test_contains_words("2009","italian","D. Jurafsky and J.H. Martin (2008). Speech and Language Processing, 2nd ed., Prentice Hall.",0))
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
x=0
if contains_first_word:
x=x+1
if contains_second_word:
x=x+1
return x
print(contains_word("Deleuze", "Differenza", "Gilles Deleuze, Differenza e ripetizione, Cortina Editore"))
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
print(test_contains_word("Deleuze", "Differenza", "Gilles Deleuze, Differenza e ripetizione, Cortina Editore", 2)) print(test_contains_word("Deleuze", "Differenza", "Gilles Deleuze, Differenza e ripetizione, Cortina Editore", 1))
What follows is my proposed solution:
def test_arewordsinentry(word_1, word_2, bib_entry, expected):
result = are_words_in_entry(word_1, word_2, bib_entry)
if result == expected:
return True
else:
return False
def are_words_in_entry(word_1, word_2, bib_entry):
result = 0
if word_1 in bib_entry:
result = result + 1
else:
result = result + 0
if word_2 in bib_entry:
result = result + 1
else:
result = result + 0
return result
print("Testing are_words_in_entry function: all results should be True:")
print(test_arewordsinentry('a', 'b', 'abcdef', 2))
print(test_arewordsinentry('a', 'b', 'bcdefg', 1))
print(test_arewordsinentry('a', 'b', 'cdefgh', 0))
All the tests work, so I guess the function itself is implemented well
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(contains_word("Connie","Annie","Connie J. A. Beck and Bruce D. Sales, Family Mediation: Facts, Myths, and Future Prospects (Washington: APA, 2001), 99-100"))
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
print(test_contains_word("Jewell", "Mike","Jewelle Taylor Gibbs and Larke Nahme Huang, eds., Children of Color: Psychological Interventions With Minority Youth", 1))
One possible solution:
def test_findwords (word1, word2, bibentry, expected):
result = findwords (word1, word2, bibentry)
if result == expected:
return True
else:
return False
def findwords (word1, word2, bibentry):
result = 0
if word1 in bibentry:
result = result + 1
if word2 in bibentry:
result = result + 1
return result
else:
return 0
print(test_findwords("Animal", "Penguin", "Orwell, G. Animal Farm. Penguin Books, 1945", 2))
print(test_findwords("Person", "Penguin", "Orwell, G. Animal Farm. Penguin Books, 1945", 2))
print(test_findwords("Person", "Penguin", "Orwell, G. Animal Farm. Penguin Books, 1945", 1))
print(test_findwords("Animal", "Penguin", "Orwell, G. Animal Farm. Penguin Books, 1945", 0))
The test returns the expected Boolean values "True", "False"," True", "False".
def test_contains_words(first_word, second_word, bib_entry, expected):
result = contains_words(first_word, second_word, bib_entry)
if result == expected:
return True
else:
return False
def contains_words(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(contains_words("East", "Steinbeck", "John Steinbeck, East of Eden, The Viking press, 1952"))
print(test_contains_words("East", "Steinbeck", "John Steinbeck, East of Eden, The Viking press, 1952", 2))
print(test_contains_words("USA", "Steinbeck", "John Steinbeck, East of Eden, The Viking press, 1952", 1))
print(test_contains_words("USA", "novel", "John Steinbeck, East of Eden, The Viking press, 1952", 0))
The results of the three prints are respectively: True, True, True.
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
contains_first_word = first_word in bib_entry
contains_second_word = second_word in bib_entry
if contains_first_word:
result += 1
if contains_second_word:
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 contains_first_word = first_word in bib_entry contains_second_word = second_word in bib_entry if contains_first_word: result += 1 if contains_second_word: result += 1 return result
print(test_contains_word("English", "Literature", "Oxford University Press,Ed.7,2009", 2)) print(test_contains_word("English", "Literature", "Oxford University Press,Ed.7,2009", 1)) print(test_contains_word("English", "Literature", "Oxford University Press,Ed.7,2009", 0))
#test
def test_contains_word(word_1, word_2, bib_entry, expected):
result = contains_word(word_1, word_2, bib_entry)
if result == expected:
return True
else:
return False
#start developing my algorithm
def contains_word(word_1, word_2, bib_entry):
#run test for 1st time withour setting a value in return
#test returns all the prints as False
#set the result value as 0
#set result as return value
#run the test that will
#test returns the first 3 prints as False and the 4th as true
result= 0
#set first if condition that adds 1 to the initial result if word_1 is in bib_entry
#run the test that will
#test returns 1st and 3rd prints as False and 2nd and 4th as true
if word_1 in bib_entry:
result = result+1
#set second if condition that adds 1 to the initial result if word_1 is in bib_entry
#run the test
#test returns all prints as True
if word_2 in bib_entry:
result = result+1
return result
# 4 test runs
print(test_contains_word("Ranum", "Problem", "Miller, B. N., Ranum, D. L. (2011). Problem Solving with Algorithms and Data Structures usingPython", 2))
print(test_contains_word("Ranum", "Computer", "Miller, B. N., Ranum, D. L. (2011). Problem Solving with Algorithms and Data Structures usingPython", 1))
print(test_contains_word("Exercise", "Problem", "Miller, B. N., Ranum, D. L. (2011). Problem Solving with Algorithms and Data Structures usingPython", 1))
print(test_contains_word("Exercise", "Computer", "Miller, B. N., Ranum, D. L. (2011). Problem Solving with Algorithms and Data Structures usingPython", 0))
Here is the link to the algorithm. I have run the test and it gave me True for each entry, so I am assuming that it works.
#tests
def test_contains_word(first_word, second_word, bib_entry, expected):
count = contains_word(first_word, second_word, bib_entry)
if count == expected:
return True
else:
return False
#code algorithm
def contains_word(first_word, second_word, bib_entry):
count = 0
if first_word in bib_entry:
count = count + 1
if second_word in bib_entry:
count = count + 1
return count
#four test runs
print(test_contains_word("Fubini", "Settecento", "Fubini, E., L'estetica musicale dall'antichità al Settecento, 2002, Einaudi", 2))
print(test_contains_word("Fubini", "pittura", "Fubini, E., L'estetica musicale dall'antichità al Settecento, 2002, Einaudi", 1))
print(test_contains_word("Novecento", "estetica", "Fubini, E., L'estetica musicale dall'antichità al Settecento, 2002, Einaudi", 1))
print(test_contains_word("Vinay", "Rinascimento", "Fubini, E., L'estetica musicale dall'antichità al Settecento, 2002, Einaudi", 0))
def test_enrica(string1, string2, string3, expected):
result = enrica(string1, string2, string3)
if result == expected:
return True
else:
return False
def enrica(string1, string2, string3):
resultvalue=0
check1=string1 in string3
check2=string2 in string3
if check1 :
resultvalue=resultvalue+1
else :
resultvalue=resultvalue+0
if check2 :
resultvalue=resultvalue+1
else :
resultvalue=resultvalue+0
return resultvalue
print(test_enrica('a', 'b', 'cb', 1))
print(test_enrica('a', 'b', 'ab', 2))
print(test_enrica('a', 'b', 'cd', 0))
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.