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).
18 stars 9 forks source link

Lecture "Algorithms", exercise 2 #6

Open essepuntato opened 3 years ago

essepuntato commented 3 years ago

Write the flowchart of an algorithm that takes in input two objects and returns the string “yes” whether the two objects are the same; otherwise, it returns the string “no”.

OrsolaMBorrini commented 3 years ago

AlgorithmEx2 I don't know if this basic flowchart will be fine as an answer, I hope so! Maybe a practical example could be the comparison between two strings. The decision widget can check if the two strings share the same alphanumeric characters in the same positions and if the two strings have the same length. Another example could be the comparison between two files, if they are the same type or not (PDF, RTF, JPG...), but I don't think a shared type can exactly define two objects as "the same".

ManueleVeggi commented 3 years ago

I think the algorithm can be represented through this flowchart:

Schermata 2021-10-16 alle 09 49 33
martasoricetti commented 3 years ago

image

AmeliaLamargese commented 3 years ago

ex 2

rahak commented 3 years ago

yes:no

AnastasiyaSopyryaeva commented 3 years ago

task2

Pablo751 commented 3 years ago

image

olgagolgan commented 3 years ago

image

angstigone commented 3 years ago

exercise 2

victorchaix commented 3 years ago

Answer Ex 2 Lec 3 CT

giorgimariachiara commented 3 years ago

IMG_20211017_194234

ljutach commented 3 years ago

Screenshot from 2021-10-17 19-57-59

elizastuglik commented 3 years ago

WhatsApp Image 2021-10-17 at 19 42 14

chloeppd commented 3 years ago

flowchart ex 2 (2)

ManuSrivastava1 commented 3 years ago

string comparision

lelax commented 3 years ago

FlowChart_2 drawio (1)

sanyuezoe commented 3 years ago

exercise2

teragramgius commented 3 years ago

algorithm

SHOUT SHOOT

PREMISE

I wrote this in a couple of hours, not continuously but taking some breaks. I've been a little slow and I hope to be faster in the future. I remember some math from high school, so I'm sure that there will be another mathematical form for what I've written in the algorithm. I don't want to seem picky, I want to make the conversation start 😁 Let me know what you think

ASSUMPTION

According to the Oxford Dictionary, a letter is a written or printed sign representing a sound used in speech (https://www.oxfordlearnersdictionaries.com/definition/english/letter_1) .

So, for this example, we'll have in mind as input the words "SHOOT" and "SHOUT".

LET'S START

It is asked to input two words, and after that, to name them F(X) and G(Y) where X and Y indicate respectively the number of signs in the two words. If you want to take the example of SHOOT and SHOUT, we'll assume that SHOOT will be F(5) and SHOUT will be G(5).

After that, it is informed that, if the two numbers X and Y are the same, we have a chance that the algorithm will give as output "yes" (i.e. it would mean that the two words had the same sign number).

Next passage consists in naming every letter of the two words in numbers, respectively starting from 1 for A and so on. It doesn't matter if the signs are in the uppercase or lowercase format.

Then it is asked to sum the first and the last number in G(X) and check if is the same as the sum of first and last number in F(Y).

If yes, it means that the two words in the input start and end with the same sign. This is another step that give the algorithm a chance that it will give as output "yes".

A word has to have at least two signs. So in the next passage the flowline leads to a decision widget to see if the word constists in two signs of more.

If the signs are more, the flowline conducts the algorithm to a process widget, through which the algorithm will be able to know how many signs in the words are left, apart from the first and the last sign. (See the photo below)

So, if α= X-2=Y-2, where 2 is to exclude the first and last signs of the two words given as input, then (X-α) = (Y-α) will indicate the remaining letters in the two words, both F(X) and G(Y).

At this point, what the algorithm have to check is whether the two words are the same or not.

So the algorithm will go through a process widget: every remaining sign in the two input words, transformed in number before, will get an indicative name: x₂ x₃ for signs in G(X) ... and y₂ y₃ for signs in F(Y)... The numbers in subscript start from 2, since the first and last sign are excluded from this reasoning.

If the sum between the x₂ + x₃ ... is equal to the sum between y₂ + y₃ ... , then the agorithm will get as output "yes". Otherwise, the two words will not be equal and the algorithm will return "no".

CarmenSantaniello commented 3 years ago

Diagramma senza titolo drawio

ghasempouri1984 commented 3 years ago

print yes

francescabudel commented 3 years ago

g

essepuntato commented 3 years ago

Hi all,

There are a few suggestions I list here that may be useful to improve your diagram and, in general, for even the future diagrams you will have to define:

  1. Read carefully the text of the algorithm to implement. If the text mentions "two objects", you cannot assume they are strings.
  2. Remember to use the correct widgets for introducing the correct instructions. For instance, when you need to return an output, you have to use an input/output widget always.
  3. The input is the first thing to do. An input/output widget must be used immediately after the initial terminal widget to declare which kind of input such an algorithm expects.
  4. You are not alone. Remember to ask some colleagues to check your algorithm by executing it on an input - this would allow you to see if what you wrote is comprehensible by another computer.

A note for @teragramgius: what happens if I run the algorithm specifying as input "shoot" and "thoos"?

teragramgius commented 3 years ago

All right, you got me🤣 Thank you🤝

sarabecchi commented 3 years ago

20211019_153759


def alg(obj_1, obj_2):
    if obj_1 == obj_2:
        return "yes"
    else:
        return "no"

print(alg("skyrim", "oblivion"))
print(alg("skyrim", "Skyrim"))
print(alg("skyrim", "skyrim"))

def test_alg(obj_1, obj_2, expected):
    result = alg(obj_1, obj_2)
    if result == expected:
        return True
    else:
        return False

print(test_alg("skyrim", "oblivion", "no"))
print(test_alg("skyrim", "Skyrim", "no"))
print(test_alg("skyrim", "skyrim", "yes"))

OUTPUT:

no
no
yes
True
True
True
Marethyu6 commented 3 years ago

Hope that's fine Untitled Diagram drawio

mirna-regolo commented 3 years ago

exercise 2-flowchart