comp-think / 2023-2024

The GitHub repository containing all the material related to the Computational Thinking and Programming course of the Digital Humanities and Digital Knowledge degree at the University of Bologna (a.a. 2023/2024).
14 stars 0 forks source link

Lecture "Programming languages", exercise 3 #12

Open essepuntato opened 8 months ago

essepuntato commented 8 months ago

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.

qwindici commented 8 months ago
def is_in_bibliography(word_1, word_2, bib_entry):
   counter = 0 
   if word_1 in bib_entry:
      counter += 1
   if word_2 in bib_entry:
      counter += 1
   return counter

print(is_in_bibliography('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

print(is_in_bibliography('Peroni', 'word', '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 = 1

print(is_in_bibliography('barboncino', 'word', '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 = 0

valetedd commented 8 months ago

pytests_ex3


# test function for the algorithm

def test_is_in_entry(first_word, second_word, bib_entry, expected):
    result = is_in_entry(first_word, second_word, bib_entry)
    if result == expected:
        return True
    else:
        return False

# function

def is_in_entry(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 

# tests

print(test_is_in_entry("murder", "horror", "Tagliaferri, L. (2018). How To Code in Python. ISBN: 978-0999773017", 0))
print(test_is_in_entry("Code", "ananas", "Tagliaferri, L. (2018). How To Code in Python. ISBN: 978-0999773017", 1))
print(test_is_in_entry("Python", "ISBN", "Tagliaferri, L. (2018). How To Code in Python. ISBN: 978-0999773017", 2))    
lucreziapograri commented 8 months ago
# Consider three different strings as input:
# two words and a bibliographic entry of a published paper. 
# The algorithm must return 
# the number 2 if the bibliographic entry contains both words; 
# the number 1 if the bibliographic entry contains only one word; 
# the number 0 otherwise.

# Test case for the algorithm
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

# Code of the algorithm
def contains_word(first_word, second_word, bib_entry):  # input/output: input two words and a bibliographic entry
    result = 0  # process1: initialize the result value to 0

    if first_word in bib_entry:  # decision1: the first word is in the bibliographic entry (?)
        result = result + 1  # if decision1 returns yes ==> process2: sum 1 to the result value

    if second_word in bib_entry:  # if decision1 returns no ==> decision2: the second word is in the bibliographic entry (?)
        result = result + 1  # process3: sum 1 to the result value

    return result  # input/output: return the result value

# Three different test runs considering particular input data as emulated in chapter 'Algorithms'
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))
frammenti commented 8 months ago
# TEST FUNCTION
# The output integer of the contains_word function is compared with the given expected value
# to return a boolean value
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

# FUNCTION
# Returns 0 if no word is present in bib_entry, 1 if only one, 2 if both
def contains_word(first_word, second_word, bib_entry):
    result = 0 # set counter to 0
    if first_word in bib_entry:
        result += 1 # short for result = result + 1
    if second_word in bib_entry:
        result += 1
    return result

# Tuples of bibliographic entries to be accessed via *args for test
bib1 = ("Conquistata", "Superiore",
        "Residori, M. (2004). L'idea del poema. Studi sulla Gerusalemme Conquistata di Torquato Tasso, Pisa, Scuola Normale Superiore.")
bib2 = ("Divine", "Comedy",
        "Barolini, T. (1992). The Undivine Comedy. Detheologizing Dante, New Jersey, Princeton University Press, pp. 48-73.")
bib3 = ("riconoscimento", "gratitudine",
        "Kremers, D. (1966). Rinaldo und Odysseus. Zur Frage der Diesseitserkenntnis bei Luigi Pulci und Dante Alighieri, Heidelberg, Winter.")

# Three different test runs
print(test_contains_word(*bib1, 2))
print(test_contains_word(*bib2, 1))
print(test_contains_word(*bib3, 0))

Algorithm Test in Python Tutor

image

Liber-R commented 8 months ago

Screenshot 2023-10-21 143739

Chiaramartina commented 8 months ago

def test_bibliography (word_one, word_two, bib_entry, expected): result=bibliography (word_one, word_two, bib_entry) if expected==result: return True else: return False

def bibliography (word_one,word_two,bib_entry): result= 0 if word_one in bib_entry: result = result +1 if word_two in bib_entry: result = result +1 return result

print(test_bibliography( "Shotton", "Open", "Shotton, D. (2013). Open Citations. Nature, 502: 295–297. doi:10.1038/502295a", 2)) print(test_bibliography( "Citations", "Science", "Shotton, D. (2013). Open Citations. Nature, 502: 295–297. doi:10.1038/502295a", 1)) print(test_bibliography( "References", "1983", "Shotton, D. (2013). Open Citations. Nature, 502: 295–297. doi:10.1038/502295a", 0))

alicepiazzi commented 8 months ago

Consider three different strings as input:

two words and a bibliographic entry

The algorithm must return the following output:

number 2 if the bibliographic entry contains both words;

number 1 if the bibliographic entry contains only one word;

number 0 otherwise.

test function for the algorithm

def test_contains_word(first_word, second_word, bibliographic_entry, expected) result= test_contains_word(first word, second_word; bibliographica_entry) if expected == result return True else: return False

function

def test_contains_word(first_word, second_word, bibliographical_entry) result = 0 if first_word is in bibliographical_entry result = +1 if second_word is in bibliographical_entry result = +1 return result

Test returns

bib1 = ("Conquistata", "Superiore", "Residori, M. (2004). L'idea del poema. Studi sulla Gerusalemme Conquistata di Torquato Tasso, Pisa, Scuola Normale Superiore.") result = 2 bib2 = ("Divine", "Comedy", "Barolini, T. (1992). The Undivine Comedy. Detheologizing Dante, New Jersey, Princeton University Press, pp. 48-73.") result = 1 bib3 = ("riconoscimento", "gratitudine", "Kremers, D. (1966). Rinaldo und Odysseus. Zur Frage der Diesseitserkenntnis bei Luigi Pulci und Dante Alighieri, Heidelberg, Winter.") result = 0

ThIheb commented 8 months ago

Ex3

Also ran the following 3 tests and they also returned the expected results : print(test_check_bib("D", "2013", "Shotton, D. (2013). Open Citations. Nature, 502: 295–297. doi:10.1038/502295a", 2)) print(test_check_bib("Shotton", "Dream", "Shotton, D. (2013). Open Citations. Nature, 502: 295–297. doi:10.1038/502295a", 1)) print(test_check_bib("Close", "1984", "Shotton, D. (2013). Open Citations. Nature, 502: 295–297. doi:10.1038/502295a", 0))

AnnaNicoletti commented 8 months ago

image

rufferbaraldi commented 8 months ago

python

matildepassafaro commented 8 months ago
programming_languages_exercise_3
MariaFrancesca6 commented 8 months ago

Test function works well 'cause the return values are always False:

image

Function executed with different inputs works well 'cause the return values are always True:

image

valentinabertelli commented 8 months 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_value = 0 if first_word in bib_entry: result_value = result_value + 1 if second_word in bib_entry: result_value = result_value + 1 return result_value

print(test_contains_word("Virginia", "stanza", "Virginia Woolf, Una stanza tutta per sé, Milano, Feltrinelli, 2013", 2)) print(test_contains_word("Virginia", "Roma", "Virginia Woolf, Una stanza tutta per sé, Milano, Feltrinelli, 2013", 1)) print(test_contains_word("Einaudi", "stanza", "Virginia Woolf, Una stanza tutta per sé, Milano, Feltrinelli, 2013", 1)) print(test_contains_word("Einaudi", "Roma", "Virginia Woolf, Una stanza tutta per sé, Milano, Feltrinelli, 2013", 0))

vattelalberto commented 8 months ago

def test_check_bib(word1, word2, bib_entry, expected):
    result = check_bib(word1, word2, bib_entry)
    if result == expected:
        return True
    else:
        return False

def check_bib(word1, word2, bib_entry):
    word1_in_bib = word1 in bib_entry
    word2_in_bib = word2 in bib_entry
    result_value = 0
    if word1_in_bib:
        result_value = result_value + 1
    if word2_in_bib:
        result_value = result_value + 1
    return result_value

print(test_check_bib("a", "b", "abcd", 2))
print(test_check_bib("a", "b", "acde", 1))
print(test_check_bib("a", "b", "bcde", 1))
print(test_check_bib("a", "b", "wxyz", 0))
enricabruno commented 8 months ago

TEST FUNCTION

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

FUNCTION

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

TESTS

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))

saramadonia commented 8 months ago

myfirst

simocasaz commented 8 months ago

Here is the code to fail the first tests:

# Test case for the algorithm
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

# Code of the algorithm
def contains_word(first_word, second_word, bib_entry):
    return

# Three different tests run
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))

The printed values for the initial failed tests:

False
False
False

Here is the code with the complete function:

# Test case for the algorithm
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

# Code of the algorithm
def contains_word(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

# Three different tests run
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))

Here the printed values for the passed tests:

True
True
True
parkful commented 8 months 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): contains_no_word = 0 contains_first_word = first_word in bib_entry contains_second_word = second_word in bib_entry if contains_first_word and contains_second_word: contains_no_word = contains_no_word + 2 elif contains_first_word or contains_second_word: contains_no_word = contains_no_word + 1 return contains_no_word

print(test_contains_word("started", "Walt", "The way to get started is to quit talking and begin doing. -Walt Disney", 2)) print(test_contains_word("Osborne", "Wolowitz", "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", 1)) print(test_contains_word("Sheldon", "Leonard", "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", 0))

#12
CarlaMenegat commented 7 months ago

test code

def test_contain_word(first_word, second_word, bibliography, expected): result = contain_word(first_word, second_word, bibliography) if result == expected: return True else: return False

algorithm code

def contain_word(first_word, second_word, bibliography): result = 0 if first_word in bibliography: result = result + 1 if second_word in bibliography: result = result +1 return result

print(test_contain_word("Lara", "Chão", "Lara, Silvia Hunold ; Fachin, Phablo R. M. . Guerra contra Palmares. O manuscrito de 1678. 1. ed. São Paulo: Chão, 2021.", 2)) print(test_contain_word("Palmares", "São Paulo", "Lara, Silvia Hunold ; Fachin, Phablo R. M. . Guerra contra Palmares. O manuscrito de 1678. 1. ed. São Paulo: Chão, 2021.", 2)) print(test_contain_word("Silvia", "Fachin", "Lara, Silvia Hunold ; Fachin, Phablo R. M. . Guerra contra Palmares. O manuscrito de 1678. 1. ed. São Paulo: Chão, 2021.", 2))

image