comp-think / 2024-2025

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. 2024/2025).
12 stars 0 forks source link

Lecture "Programming languages", exercise 3 #14

Open essepuntato opened 2 weeks ago

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

maridematteis commented 1 week ago
Screenshot 2024-11-12 alle 11 17 24
KikaYang commented 1 week ago
TEST-1
martinaucch commented 1 week ago
Screenshot 2024-11-12 alle 17 33 07
def test_containsword(first_word, second_word, bib_entry, expected):
    result = containsword(first_word, second_word, bib_entry)
    if result == expected: 
        return True
    else:
        return False

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

print(test_containsword("luca", "mangia", "luca oggi mangia", 2))
print(test_containsword("luca", "corre", "luca oggi mangia", 1))
print(test_containsword("giorgio", "mangia", "luca oggi mangia", 1))
print(test_containsword("giorgio", "corre", "luca oggi mangia", 2))

btw i have realized i have made a mistake in my first attempt that i have corrected thanks to @maridematteis answer. I'll post the first attempt because it was working but i believe that even if it was working it did not respected the logic behind the def function(parameter) syntax cause i was expliciting a variable as a parameter. right? not sure

Screenshot 2024-11-12 alle 17 36 34
ValkyrieCain9 commented 1 week ago
Screenshot 2024-11-12 at 19 30 39
polinakhrm commented 1 week ago
Screenshot 2024-11-13 at 00 09 50
essepuntato commented 1 week ago

Hi all, thanks for all your answers. I need to clarify a passage on test-driven development (which I will also highlight in today's lecture).

All the tests must be passed to claim that an algorithm returns what is expected. Thus, if a test execution returns False, the test is not passed. If you need to check the non-compliance of the execution of a function on purpose, then you have to create an additional testing function that returns True if the condition of the test is not passed.

arinee01 commented 1 week ago
Снимок экрана 2024-11-13 в 17 47 10
martinamrc commented 1 week ago

Immagine 13-11-24 - 18 10 Immagine 13-11-24 - 18 18 I wasn't sure if we also had to put the actual function of the algorithm "to work", so in any case I added the print() with the same inputs of the test plus another one to double-check.

yutong316 commented 1 week ago

image

ERendall commented 1 week ago
Screenshot 2024-11-14 at 16 24 19
shiho1000 commented 1 week 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
    resultvalue = 0

    if contains_first_word:
        resultvalue += 1
    if contains_second_word:
        resultvalue += 1
    # Print resultvalue here
    print("resultvalue:", resultvalue)  
    return resultvalue      

# Three different test runs
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))

Below is what is returned when I run it

resultvalue: 2
True
resultvalue: 1
True
resultvalue: 0
True
theair-hub commented 1 week ago

image

romashovar commented 4 days ago
def contains_word (word1, word2, bibl):

    result_value = 0

    if word1 in bibl:
        result_value = result_value + 1

    if word2 in bibl:
        result_value = result_value + 1

    return result_value

def test_contain_words (word1, word2, bibl, expected):

    result = contains_word (word1, word2, bibl)

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

print (test_contain_words ("Python", "programming", "Python: an introduction to programming (James R. Parker)", 2))
print (test_contain_words ("Python", "programming", "Deep learning with Python (Francois Chollet)", 1))
print (test_contain_words ("computer", "science", "Learn Python in 7 days: get up-and-running with Python", 0))

True True True

justyna09549 commented 2 days ago
Zrzut ekranu 2024-11-23 o 4 21 43 PM
simplycyrus99 commented 1 day ago

`def test_contains_word(first_word, second_word, bib_entry, expected): result = contains_word(first_word, second_word, bib_entry) return result == expected # Compare directly to expected

def contains_word(first_word, second_word, bib_entry): result_value = 0 if first_word in bib_entry: # Check if the first word exists result_value += 1 if second_word in bib_entry: # Check if the second word exists result_value += 1 return result_value + 0 # Add additional value if needed

Test cases

print(test_contains_word("a", "b", "abc", 2)) # True (Both 'a' and 'b' exist) print(test_contains_word("a", "d", "abc", 1)) # True ('a' exists, 'd' does not) print(test_contains_word("x", "y", "abc", 0)) # True (Neither 'x' nor 'y' exists) `

I wrote my code by i needed to look better, hence the help from chat gpt lol