comp-think / 2022-2023

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. 2022/2023).
17 stars 5 forks source link

Lecture "Programming languages", exercise 3 #12

Open essepuntato opened 1 year ago

essepuntato commented 1 year 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.

vattelalberto commented 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))
matteo-guenci commented 1 year ago
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

mjavadf commented 1 year ago
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))
delete4ever commented 1 year ago
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))
AmirAliyan74 commented 1 year ago
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))
n1kg0r commented 1 year ago
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))
irematmar commented 1 year ago

image

NicoleLiggeri commented 1 year ago

bibalgorithm (3)

ranacoskun commented 1 year ago
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))
SalvatoreDiMarzo commented 1 year ago

Bib entry program

giorgiacrosilla commented 1 year ago
ex3
alka2696 commented 1 year ago
Screenshot 2022-10-25 at 5 48 21 PM
falaimo99 commented 1 year ago
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))
lucia1299 commented 1 year ago

image

evanarnoldi commented 1 year ago

# 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))
eugeniavd commented 1 year ago

image

EricaAndreose commented 1 year ago
3ex
ChiaraParravicini commented 1 year 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 = 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))
essepuntato commented 1 year ago

Hi all,

A few comments:

corrado877 commented 1 year ago

Algorithm exercise