comp-think / 2021-2022

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

Lecture "Programming languages", exercise 3 #13

Open essepuntato opened 2 years ago

essepuntato commented 2 years 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.

ghasempouri1984 commented 2 years ago

I edited my code as you commented @essepuntato > I think the previous version requires 3 tests because of using OR and this edition requires 4 tests as you clarified at class>

def contains_word(first_word, second_word, bib_entry):
    container = 0
    if (first_word in bib_entry) :
        container = container+1
    if (second_word in bib_entry):
        container = container+1
    return container

def test(first_word, second_word, bib_entry, contained):
    container = contains_word(first_word, second_word, bib_entry)
    if container == contained:
        return True
    else:
        return False

print(test("British Columbia", "1961",
                         "L'Avventura (1960). Michelangelo Antonioni. Cino Del Duca", 0))
print(test("Adventure", "Antonioni",
                         "L'Avventura (1960). Michelangelo Antonioni. Cino Del Duca", 1))
print(test("Antonioni", "Adventure",
                         "L'Avventura (1960). Michelangelo Antonioni. Cino Del Duca", 1))
print(test("Michelangelo", "Del Duca",
                         "L'Avventura (1960). Michelangelo Antonioni. Cino Del Duca", 2))

the previous version:

def contains_word(first_word, second_word, bib_entry):
    container = 0
    if (first_word in bib_entry) or (second_word in bib_entry):
        container = 1
    if (first_word in bib_entry) and (second_word in bib_entry):
        container = 2
    return container

def test(first_word, second_word, bib_entry, contained):
    container = contains_word(first_word, second_word, bib_entry)
    if container == contained:
        return True
    else:
        return False

print(test("British Columbia", "1961",
                         "L'Avventura (1960). Michelangelo Antonioni. Cino Del Duca", 0))
print(test("Adventure", "Antonioni",
                         "L'Avventura (1960). Michelangelo Antonioni. Cino Del Duca", 1))
print(test("Michelangelo", "Del Duca",
                         "L'Avventura (1960). Michelangelo Antonioni. Cino Del Duca", 2))
ManueleVeggi commented 2 years ago

I attach my solution. At first I have to consider the structure of "Template Listing 11", defined as

def test_<algorithm>(<algorithm input params>, expected): result = <algorithm>(<algorithm input params>)
if result == expected: 
return True else: 
return False 
def <algorithm>(<algorithm input params>): return 
print(test_<algorithm>(<algorithm input params 1>, <expected_1>)) print(test_<algorithm>(<algorithm input params 2>, <expected_2>)) 

and the graphical representation of the algorithm in the flowchart:

Schermata 2021-10-23 alle 07 56 39

Then, I emulate the behaviour of the algorithm as following: Case A

first_word = Humfrey
second_word = Venice
bib_entry = Humfrey, Peter. Painting in Renaissance Venice. Yale University Press, 1995.
output = 0 + 1 + 1 = 2

Case B

first_word = Humfrey
second_word = Florence
bib_entry = Humfrey, Peter. Painting in Renaissance Venice. Yale University Press, 1995.
output = 0 + 1 + 0 = 1

Case C

first_word = Vasari
second_word = Venice
bib_entry = Humfrey, Peter. Painting in Renaissance Venice. Yale University Press, 1995.
output = 0 + 0 + 1

Case D

first_word = Vasari
second_word = Florence
bib_entry = Humfrey, Peter. Painting in Renaissance Venice. Yale University Press, 1995.
output = 0 + 0 + 0 = 0

I can now write my code starting from the template:

# 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):
    result = 0
    if first_word in bib_entry:
        result = result + 1
    if second_word in bib_entry: 
        result = result + 1
    return result

Lastly, I verify the correctness of my script with these four sets of inputs:

print(test_contains_word("Humfrey", "Venice", "Humfrey, Peter. Painting in Renaissance Venice. Yale University Press, 1995.", 2))
print(test_contains_word("Humfrey", "Florence", "Humfrey, Peter. Painting in Renaissance Venice. Yale University Press, 1995.", 1))
print(test_contains_word("Vasari", "Venice", "Humfrey, Peter. Painting in Renaissance Venice. Yale University Press, 1995.", 1))
print(test_contains_word("Vasari", "Florence", "Humfrey, Peter. Painting in Renaissance Venice. Yale University Press, 1995.", 0))

Running this script with a Python interpreter (Visual Code) gives the following results:

The default interactive shell is now zsh.
To update your account to use zsh, please run `chsh -s /bin/zsh`.
For more details, please visit https://support.apple.com/kb/HT208050.
MacBook-Pro-di-Manuele:Course manuele$  /usr/bin/env /usr/local/bin/python3 /Users/manuele/.vscode/extensions/ms-python.python-2021.10.1336267007/pythonFiles/lib/python/debugpy/launcher 58367 -- "/Users/manuele/Desktop/CTP/Course/Lecture 4 Exercise Py Code" 
True
True
True
True
MacBook-Pro-di-Manuele:Course manuele$ 

which should confirm the correctness of the code.

11051620 commented 2 years ago

Screenshot 2021-10-23 at 14 42 47

tommasobattisti commented 2 years ago

I wrote the algorithm and its related test as follows:

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 = result + 1

    if second_word in bib_entry:
        result = result + 1

    return result

print(test_contains_word("Writing", "Process", "Stephen D. Hudson (ed.), The First Writing: Script Invention as History and Process, Cambridge, Cambridge University Press, 2004.", 2))
print(test_contains_word("History", "2008", "Stephen D. Hudson (ed.), The First Writing: Script Invention as History and Process, Cambridge, Cambridge University Press, 2004.", 1))
print(test_contains_word("Book", "Human", "Stephen D. Hudson (ed.), The First Writing: Script Invention as History and Process, Cambridge, Cambridge University Press, 2004.", 0)) 

Results:

/usr/local/bin/python3 /Users/tommasobattisti/Desk
The default interactive shell is now zsh.
To update your account to use zsh, please run `chsh -s /bin/zsh`.
For more details, please visit https://support.apple.com/kb/HT208050.
MBP-di-Tommaso:~ tommasobattisti$ /usr/local/bin/python3 /Users/tommasobattisti/Desktop/x=2.py
True
True
True
MBP-di-Tommaso:~ tommasobattisti$
OrsolaMBorrini commented 2 years ago
#Function
def words_in_entry(first_word, second_word, bibliographic_entry):
    value = 0
    if first_word in bibliographic_entry:
        value += 1
    if second_word in bibliographic_entry:
        value += 1
    return value

#Test function
def test_function(first_word, second_word, bibliographic_entry, expected):
    result = words_in_entry(first_word, second_word, bibliographic_entry)
    if expected == result:
        return True
    else:
        return False

#Example1
print("Example 1")
print(words_in_entry("Results","studies","Results of previous studies"))
print(test_function("Results","studies","Results of previous studies",2))
#Example2
print("\nExample 2")
print(words_in_entry("HTML","studies","Results of previous studies"))
print(test_function("HTML","studies","Results of previous studies",1))
#Example3
print("\nExample 3")
print(words_in_entry("HTML","Python","Results of previous studies"))
print(test_function("HTML","Python","Results of previous studies",0))

Results:

Example 1
2
True

Example 2
1
True

Example 3
0
True
angstigone commented 2 years ago
Schermata 2021-10-23 alle 17 47 51 Schermata 2021-10-23 alle 18 25 59 Schermata 2021-10-23 alle 18 27 55
CarmenSantaniello commented 2 years ago
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

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

print(test_contains_word("Myth", "Literature", "W. Soyinka, Myth, Literature and African World, Cambridge University Press, Cambridge 1976", 2))
print(test_contains_word("Cambridge", "Theater", "W. Soyinka, Myth, Literature and African World, Cambridge University Press, Cambridge 1976", 1))
print(test_contains_word("1988", "Wole", "W. Soyinka, Myth, Literature and African World, Cambridge University Press, Cambridge 1976", 0))

True
True
True
giorgimariachiara commented 2 years ago

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

elizastuglik commented 2 years ago

def contains_word (first_word, second_word, bib_entry) :

  result=0 

  contains_first_word= first_word in bib_entry
  result= result + 1

  contains_second_word= second_word in bib_entry
  result= result+1 

  return result 
federicabonifazi commented 2 years 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) :
    contains_first_word = first_word in bib_entry
    contains_second_word = second_word in bib_entry
    result = 0

    if contains_first_word:
        result = result + 1
    if contains_second_word:
        result = result + 1
        return result
    else:
        return result

print(test_contains_word("Edmunds","London", "Edmunds L., Oedipus, London-New York, Routledge, 2006", 2 ))
print(test_contains_word("letteratura", "Grecia", "Bettini M., Guidorizzi G., Il mito di Edipo: immagini e racconti dalla Grecia a oggi, Torino, G. Einaudi, 2004", 1))
print(test_contains_word("mito", "Guidorizzi", "Berman D.W., Myth, literature, and the creation of the topography of Thebes, Cambridge, Cambridge University Press, 2015", 0))

True
True
True
olgagolgan commented 2 years ago
def test_container(first_word, second_word, bib_entry, expected):
    result = container(first_word, second_word, bib_entry)
    if result == expected:
        return True
    else:
        return False

def container (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_container("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", 2))
print(test_container("Peroni", "xml", "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_container("Rossi", "json", "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))
RebeccaJillianBeattie commented 2 years ago

Picture3 (2)

sotarega commented 2 years 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_first_word = first_word in bib_entry
    contains_second_word = second_word in bib_entry

    if contains_first_word and contains_second_word:
        return 2
    elif contains_first_word or contains_second_word:
        return 1
    else:
        return 0

#Three test runs
print(test_contains_word("odd", "even", "On the number of primes less than a given magnitude", 0))
print(test_contains_word("algebra", "linear", "Linear algebra and matrix theory", 1)) # Linear != linear
print(test_contains_word("algebra", "Linear", "Linear algebra and matrix theory", 2))

True
True
True
ManuSrivastava1 commented 2 years ago

The following code is a representation of the flowchart described in figure 4 in the chapter entitled Algorithms.

def test_<algorithm>(<word1>, <word2>, <bib_entry>, <expected>):
    result = <algorithm>(<word1>, <word2>, <bib_entry>)
    if expected == result:
        return True
    else:
        return False

def <algorithm> (<word1>,<word2>,<bib_entry>):
    <result_value> = 0  
    if <word1> in <bib_entry> :
        <result_value> = <result_value> + 1
        if word2 in bib_entry :
            <result_value> = <result_value> + 1
            print(<result_value>)
            return(<result_value>)
    else :
        if <word2> in <bib_entry> :
             <result_value> = <result_value> + 1
             print(<result_value>)
             return(<result_value>)
        else :
            print(<result_value>)
            return(<result_value>)

test_<algorithm>('what','can','hello there didnt see you',0)
test_<algorithm>('hello','can','hello there didnt see you',1)
test_<algorithm>('what','there','hello there didnt see you',2)
test_<algorithm>('what','there','hello there didnt see you',1)

To see the algorithm in action, follow the link below The algorithm in action

essepuntato commented 2 years ago

Hi all,

Just a few notes:

MaddaGh commented 2 years ago

test

chloeppd commented 2 years ago
Screenshot 2021-10-27 at 3 41 49 PM
AmeliaLamargese commented 2 years ago
def test_is_in_bib(first, second, bib, expected):
    result = is_in_bib(first, second, bib)
    if result == expected:
        return True
    else:
        return False

def is_in_bib(first, second, bib):
    result = 0
    if first in bib:
        result += 1
    if second in bib:
        result += 1
    return result

print(test_is_in_bib('a', 'b', 'abc', 2))
print(test_is_in_bib('a', 'b', 'dbc', 1))   
print(test_is_in_bib('a', 'b', 'adc', 1))
print(test_is_in_bib('a', 'b', 'dec', 0)) 
martasoricetti commented 2 years ago

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(test_contains_words("Tagliaferri", "code", "Tagliaferri, L. (2018). How to code in Python. ISBN: 978-0999773017", 2)) print(test_contains_words("Tagliaferri", "xml", "Tagliaferri, L. (2018). How to code in Python. ISBN: 978-0999773017", 1)) print(test_contains_words("Peroni", "code", "Tagliaferri, L. (2018). How to code in Python. ISBN: 978-0999773017", 1)) print(test_contains_words("Peroni", "xml", "Tagliaferri, L. (2018). How to code in Python. ISBN: 978-0999773017", 0))

Immagine 2021-10-27 181653

sanyuezoe commented 2 years ago
# TEST
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
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("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))
Postitisnt commented 2 years ago
def word_in_biben(w1, w2, bib_en):
    result = 0
    bib_en = bib_en.lower()
    w1 = w1.lower()
    w2 = w2.lower()

    if w1 in bib_en and w2 in bib_en:
        result += 2
    elif w1 in bib_en or w2 in bib_en:
        result += 1
    else:
        return result
    return result

def test(w1, w2, bib_en, test_number):
    result = word_in_biben(w1, w2, bib_en)
    if result == test_number:
        print("It works, the result is ", result)
        return True
    else:
        print("There is something wrong...")
        return False

print("Test 1", test("Hi", "Hello", "Just a test of my function", 0))
print("Test 2", test("my", "Test", "Just a test of my function", 2))
print("Test 3", test("just", "several", "Just a test of my function", 1))
print("Test 4", test("Round", "function", "Just a test of my function", 1))
teragramgius commented 2 years ago

I follow the TDD template, playing on the boolean values to check if the software that will be behaves correctly. That is:

def test_<algorithm>(<algorithm input params>, EXPECTED):
    result = <algorithm>(<algorithm input params>)
    if result == EXPECTED:
        return True
    else:
        return False

print(test_<algorithm>(<algorithm input params 1>, <EXPECTED_1>))
print(test_<algorithm>(<algorithm input params 2>, <EXPECTED_2>))

Following this template, I'll go as it follows.

# TDD
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
# I implement the algorithm
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("Harper", "Lees", "Lee, Harper. To Kill a Mockingbird. Warner Books, 1982", 1))
print(test_contains_word("Medina", "Latino", "Medina, Pablo. "The Secret." Cool Salsa: Bilingual Poems on Growing Up Latino in the United States, Holt, 1994.", 2))
print(test_contains_word("Lerner", "Road", "Lerner, K. Lee, et al., editors. Alternative Energy. 2nd ed., vol. 1, Gale, 2012.", 1))
print(test_contains_word("Easter", "Grandmother", "Chase, Richard. "Old Christmas Eve." Grandfather Tales, edited by H. L. Pick, Viking, 1973.", 0))
sarabecchi commented 2 years ago
def word_research(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
        else:
            return result
    elif word_2 in bib_entry:
        result = result + 1
        return result
    else:
        return result

def test_word_research(word_1, word_2, bib_entry, expected):
    result = word_research(word_1, word_2, bib_entry)
    if expected == result:
        return True
    else:
        return False

print(test_word_research('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", 2))

print(test_word_research("dovahkiin", "nerevar", "Dovahkiin Dovahkiin naal ok zin los vahriin", 0))
print(test_word_research("Dovahkiin", "nerevar", "Dovahkiin Dovahkiin naal ok zin los vahriin", 1))
print(test_word_research("Dovahkiin", "Nerevar", "Dovahkiin beats Nerevar", 2))