comp-think / 2018-2019

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. 2018/2019).
Other
30 stars 8 forks source link

Lecture "Programming languages", exercise 3 #11

Open essepuntato opened 5 years ago

essepuntato commented 5 years ago

Following the template in Listing 11, write in Python the algorithm proposed originally in Figure 4 of the lecture notes entitled "Algorithms" as a flowchart, and accompany such code with the related test function and some executions with different input values.

delfimpandiani commented 5 years ago
def test_find_words(first_word, second_word, bib_entry, expected):
    result = find_words(first_word, second_word, bib_entry)
    if result == expected:
        return True
    else:
        return False

def find_words(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
        else:
            return result
    elif second_word in bib_entry:
        result += 1
        return result
    else:
            return result

print(test_find_words("poema", "storni", "los poemas de Alfonsina Storni", 2)
print(test_find_words("delfina", "sol", "delfina sol martinez pandiani", 2)
print(test_find_words("complexity", "trick", "A few complex tricks to simplify", 1)
giuliapl commented 5 years ago

I tried but I am not sure.

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):
    if first_word in bib_entry and second_word in bib_entry:
        return 2
    else:
        (first_word in bib_entry or second_word in bib_entry)
        return 1
    if first_word not in bib_entry and second_word not in bib_entry:
        return 0

print(test_contains_words("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_contains_words("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", 1))
print(test_contains_words("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", 0))
simayguzel commented 5 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): result = 0

if first_word in bib_entry and second_word in bib_entry:
    return result + 2
elif first_word in bib_entry or second_word in bib_entry:
    return result + 1
else:
    return result

print(contains_word("Problem", "Solving", "Miller, B. N., Ranum, D. L. (2011). Problem Solving with Algorithms and Data Structures using Python") output = 2 print(contains_word("Development", "Programming", " Beck, K. (2003). Test-Driven Development by Example. Addison Wesley") output = 1 `

federicabologna commented 5 years ago

def test_words(first_word, second_word, bib_entry, expected_result): result = words(first_word, second_word, bib_entry)

if result == expected_result: return True else: return False

def words(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 += 1 if contains_second_word: result += 1 return result else: return result if contains_second_word: result +=1 return result else: return result

print(test_words("move", "sole", "amor che move il sole", 2)) print(test_words("mani", "sera", "queste tue mani a difesa di te", 1)) print(test_words("anima", "bicicletta", "ho sceso dandoti", 0))

EleonoraPeruch commented 5 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) : result_value = 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 : return result_value + 2 elif contains_first_word or contains_second_word : return result_value + 1 else: return result_value

print (test_contains_word ("a", "b", "abcd", 2)) print (test_contains_word ("a", "b", "acde", 1)) print (test_contains_word ("a", "b", "cdef", 0))

mangiafrangette commented 5 years ago
def test_words_in_bib(word1, word2, bibentry, expected):
    result = words_in_bib(word1, word2, bibentry)

    if expected == result:
        return True
    else: 
        return False

def words_in_bib(word1, word2, bibentry):
    result = 0

    if word1 in bibentry:
        result = result + 1
    if word2 in bibentry:
        result = result + 1

    return result

print(test_words_in_bib("Pink", "Floyd", "Pink Floyd were an English rock band", 2))
print(test_words_in_bib("Pink", "Monkeys", "Arctic Monkeys are an English rock band", 1))
print(test_words_in_bib("bau", "Stelle", "Baustelle is an indie rock band from Siena, Italy", 2))

First and second test calls return True, the last one is False (the words are not found in bibentry due to case sensitiveness)

beccadelbens commented 5 years ago

def test_contains_word (first_word, second_word, bib_entry, expected_result) 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

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

print (test_contains_word ("cura", "opera", "L'opera d'arte nell'epoca della sua riproducibilità tecnica (2012)/ W. Benjamin; a cura di F. Desideri"), 2) print (test_contains_word ("cura", "opera", "Minima culinaria (2015)/ Z. Ciuffoletti; a cura di C. Costagli"), 1) print (test_contains_word ("cura", "opera", "Il piccolo principe (1981)/ A. de Saint-Exupéry"), 0)

hizclick commented 5 years ago
    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 and contains_second_word:
        result += 2
        return result
    elif contains_first_word or contains_second_word:
        result +=1
        return result
    else:
        return result

print(test_contains_word("h", "i", "hiii", 2))
print(test_contains_word("h", "k", "hiii", 1))
print(test_contains_word("l", "m", "hiii", 0))
leticiasandra commented 5 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_value = 0

   if contains_first in bib_entry:
      result_value= +1
      return result
   elif contains_second_word:
         result_value = +1
         return result
  else:
        return result_value

  print(test_contains_word("food", "craft", "eat food", 1)) print(test_contains_word("craft", "art", "craft chair", 1)) print(test_contains_word("food", "craft", "enjoy living", 0))

ilsamoano commented 5 years ago
#Following the template in ​Listing 11,​ write in Python the algorithm proposed originally ​in
#Figure 4 of the lecture notes entitled "Algorithms" as a flowchart, and accompany such code 
#with the related test function and some executions with different input values

def test_thereader(string1, string2, string3, expected):
    result = thereader(string1, string2, string3)

    if expected == result: 
        return True
    else:
        return False

def thereader(string1, string2, string3):
    result = 0

    if string1 and string2 in string3:
       result = result + 2

    elif string1 or string2 in string3:
            result = result + 1

    else:
       result = result

    return result

print(test_thereader("a", "b", "abcd", 2))
print(test_thereader("a", "d", "abce", 1))
print(test_thereader("c", "e", "abcd", 1))

True True True

friendlynihilist commented 5 years ago
def test_match_words (first_word, second_word, bib_entry, expected):
    result = match_words (first_word, second_word, bib_entry)
    if result == expected:
        return True
    else:
        return False

def match_words (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_match_words("heart", "gold", "I've been a miner for a heart of gold", 2))
print(test_match_words("Martin", "Heidegger", "I'm sitting here reading Heidegger and hoping this water will boil", 1))
print(test_match_words("Pavese", "1945", "Hai viso di pietra scolpita, sangue di terra dura, sei venuta dal mare", 0))
SeverinJB commented 5 years ago
def test_flowchart(term_1, term_2, bib_entry, expected): 
    result = flowchart(term_1, term_2, bib_entry)
    if result == expected:
        return True 
    else:
        return False

def flowchart(term_1, term_2, bib_entry): 
    result_value = 0
    if term_1 in bib_entry: 
        result_value = result_value + 1
    if term_2 in bib_entry:
        result_value = result_value + 1
    return result_value

print(test_flowchart("ab","cd","abcd",2)) 
print(test_flowchart("ab","cd","abef",1))
print(test_flowchart("ab","cd","xycd",1)) 
print(test_flowchart("ab","cd","xyz",0))
MattiaSpadoni commented 5 years ago

I had some problems with the correct syntax of summing something to the variable. But I checked it and now it works.

P.S. I think that we don't need a "If... else" statement here, we don't have an alternative set of instruction if the first if is false, the only thing that we have to do is going on to the next one.

def test(first_word, second_word, bib_entry, expected): result = istheretheword (first_word, second_word, bib_entry)

    if expected == result:

        return True

    else:

        return False

def istheretheword (first_word, second_word, bib_entry):
    x = first_word in bib_entry
    y = second_word in bib_entry
    result = 0

    if x:
       result += 1
    if y:
       result += 1

           return result

  print(test("a", "b", "abcd", 2))

  print(test("a", "b", "acde", 1))
  print(test("a", "b", "cdef", 0))
lisasiurina commented 5 years ago

def test_find_words(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_words(first_word, second_word, bib_entry): result = 0 If first_word in bib_entry: result +=1 return result

If second_word in bib_entry: result +=1 return result

else: return result

elif second_word in bib_entry:

result += 1 return result else: return result

print(test_words("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))

True, output 2

print(test_words)(“Mesmerizing”, “Miracle”, “Mesmerizing Ghost Doctor”))

True, output 1

DavideApolloni commented 5 years ago

screenshot at nov 21 15-13-43

dersuchendee commented 5 years ago

def test_(<word1, word2, bib-entry>, expected): result = (<word1, word2, bib-entry>) if result == expected: return True else: return False

def (<word1, word2, bib-entry>): If word1 in bib_entry: return 1 If word 2 in bib_entry: return 1 If word1 and word2 in bib_entry: return 2 else return 0

print(test_("Ada", "of", "Ada Lovelace was the daughter", 1)) print(test_("Ada", "Lovelace", "Ada Lovelace was the daughter", 2)) print(test_("cat", "dog", "Ada Lovelace was the daughter", 0))

tceron commented 5 years ago

def test_containing_word(firstword, secondword, bibentry, expected): result = test_containing_word(firstword, secondword, bibentry)

if result == expected: return True else: return False

def test_containing_word (firstword, secondword, bibentry): return = 0

if firstword and secondword: result 2 return result if firstword or secondword: result 1 return result else: return result

print(test_containing_word("linguistics", "Alan", "Davies, Alan, and Catherine Elder, eds. The handbook of applied linguistics. John Wiley & Sons, 2008.", 2)) print(test_containing_word("linguistics", "Alan", "Church, Kenneth W., and Robert L. Mercer. "Introduction to the special issue on computational linguistics using large corpora." Computational linguistics 19.1 (1993): 1-24.", 1)) print(test_containing_word("linguistics", "Alan", "Chowdhury, Gobinda G. "Natural language processing." Annual review of information science and technology 37.1 (2003): 51-89.", 0))

MilenaCorbellini commented 5 years ago

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 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(contains_word("Shakespeare", "Sogno", "Sogno di una notte di mezz'estate, Shakespeare")) print(contains_word("Shakespeare", "la", "la locandiera, Goldoni")) print(contains_word("Sakespeare", "Giulietta", "Capuleti e Montecchi, Bellini"))

essepuntato commented 5 years ago

Hi all,

Thanks for your comment. The idea of the exercise is just to convert literally the various widgets in the flowchart as proper constructs in Python. In particular – since several of you have done that – you should not use more than one result statements, because in the flowchart only one output widget is actually used.

The solution of the exercise is shown as follows - and it is also available as Python file in the GitHub repository:

# 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  # process: initialize the result value to 0

    if first_word in bib_entry:  # decision: the first word is in the bibliographic entry
        result = result + 1  # process: sum 1 to the result value

    if second_word in bib_entry:  # decision: the second word is in the bibliographic entry
        result = result + 1  # process: sum 1 to the result value

    return result  # input/output: return the result value

# Three different test runs
print(test_contains_word("a", "b", "abcd", 2))
print(test_contains_word("a", "b", "acde", 1))
print(test_contains_word("a", "b", "cdef", 0))