diff --git a/labs/01/A01656188_relation_analyser.py b/labs/01/A01656188_relation_analyser.py new file mode 100644 index 0000000..55b819b --- /dev/null +++ b/labs/01/A01656188_relation_analyser.py @@ -0,0 +1,101 @@ +import ast +import graphviz # https://graphviz.readthedocs.io/en/stable/index.html + +#-----------------------------REFLEXIVA-------------------------------------- +def buscando_reflexion(relacion): + elementos=set() + for par in relacion: + elementos.add(par[0]) + elementos.add(par[1]) + + for elemento in elementos: + if (elemento,elemento) not in relacion: + return "no reflexiva" + return "reflexiva" + +#-----------------------------SIMETRICA-------------------------------------- + +def buscando_simetria(relacion): + for par in relacion: + parReverso=(par[1],par[0]) + if parReverso not in relacion: + return "no simetrica" + return "simetrica" + +#-----------------------------TRANSITIVA-------------------------------------- + +def buscando_transicion(relacion): + for primerPar in relacion: + for segundoPar in relacion: + if primerPar[1]==segundoPar[0] and (primerPar[0], segundoPar[1]) not in relacion: + return "no transitiva" + return "transitiva" + +def analyze(val): + """ + Here goes your code to do the analysis + 1. Reflexive: aRa for all a in X, + 2. Symmetric: aRb implies bRa for all a,b in X + 3. Transitive: aRb and bRc imply aRc for all a,b,c in X, + """ + Reflexive = buscando_reflexion(val) + Symmetric = buscando_simetria(val) + Transitive = buscando_transicion(val) + + return Reflexive, Symmetric, Transitive + +def plot(val): + """ + Here goes your code to generate graph.log and plot the graph + """ + + # Create the graph + dot= graphviz.Digraph('G', comment='graph.log') + for i in val: + dot.edge(str(i[0]), str(i[1])) + + print(dot.source) + # Render the graph + dot.view() + + +def get_set(val): + """ + How to put the input + Example: + {(0,0),(0,1),(0,3),(1,0),(1,1),(2,2),(3,0),(3,3)} + { (0,0), (1,1), (1,0) } + """ + result = None + + try: + # Use literal_eval to convert the string into a set + result = ast.literal_eval(val) + + # Check if result is a set + if not isinstance(result, set): + raise ValueError("Input string does not represent a set") + + except ValueError as e: + print(f"Error: Invalid input. {e}") + raise e + except Exception as e: + print("Error: Something went wrong.") + raise e + + return result + +def main(): + print("Hello World analyzing input!") + val = input("Enter your set: ") + val=get_set(val) + print(val) + Reflexive,Symmetric,Transitive = analyze(val) + print(f"\ + 1. Reflexive: {Reflexive} \ + 2. Symmetric: {Symmetric} \ + 3. Transitive: {Transitive}") + plot(val) + +if __name__ == "__main__": + main() diff --git a/labs/01/relation_analyser.py b/labs/01/relation_analyser.py deleted file mode 100644 index 6fd8b91..0000000 --- a/labs/01/relation_analyser.py +++ /dev/null @@ -1,36 +0,0 @@ -import graphviz # https://graphviz.readthedocs.io/en/stable/index.html - -def analyze(val): - """ - Here goes your code to do the analysis - 1. Reflexive: aRa for all a in X, - 2. Symmetric: aRb implies bRa for all a,b in X - 3. Transitive: aRb and bRc imply aRc for all a,b,c in X, - """ - Reflexive = False - Symmetric = False - Transitive = False - - return Reflexive,Symmetric,Transitive - -def plot(): - """ - Here goes your code to do the plot of the set - """ - g = graphviz.Digraph('G', filename='hello.gv') - g.edge('Hello', 'World') - g.view() - -def main(): - print("Hello World analyzing input!") - val = input("Enter your set: ") - print(val) - Reflexive,Symmetric,Transitive = analyze(val) - print(f"\ - 1. Reflexive: {Reflexive} \ - 2. Symmetric: {Symmetric} \ - 3. Transitive: {Transitive}") - plot() - -if __name__ == "__main__": - main() diff --git a/labs/03/analyzer.l b/labs/03/analyzer.l new file mode 100644 index 0000000..921ac3a --- /dev/null +++ b/labs/03/analyzer.l @@ -0,0 +1,28 @@ +%{ +#include "y.tab.h" +%} + +%{ +int spacing = 1; +%} + +%% +a | +the { return ARTICLE; } + +boy | +girl | +flower { return NOUN; } + +touches | +likes | +sees { return VERB; } + +with { return PREP; } + +[ \t]+ /* ignore whitespace */ +^[ \t]*\n /* ignore blank lines */ + +\n { spacing++; return '\n'; } + +%% diff --git a/labs/03/analyzer.y b/labs/03/analyzer.y new file mode 100644 index 0000000..7d3a04e --- /dev/null +++ b/labs/03/analyzer.y @@ -0,0 +1,54 @@ +%{ +#include +#include +#include + +int yylex(); +void yyerror(const char *s); +extern FILE *yyin; +%} + +%token ARTICLE NOUN VERB PREP + +%% + +start: SENTENCES '\n' { printf("PASS\n"); } + + +SENTENCES: /* empty */| SENTENCES SENTENCE '\n'; + +SENTENCE: NOUN_PHRASE VERB_PHRASE { printf("PASS\n"); } + | NOUN_PHRASE { printf("PASS\n"); } + | VERB_PHRASE { printf("PASS\n"); } + +NOUN_PHRASE: ARTICLE NOUN| ARTICLE NOUN PREP_PHRASE; + +VERB_PHRASE: VERB| VERB NOUN_PHRASE; + +PREP_PHRASE: PREP NOUN_PHRASE; + + +%% + +void yyerror(const char *s){ + printf("FAIL\n"); +} + +int main(int argc, char **argv){ + + FILE *fd; + + if (argc == 2) + { + if (!(fd = fopen(argv[1], "r"))) + { + perror("Error: "); + return (-1); + } + yyin=fd; + yyparse(); + fclose(fd); + } + + return 0; +} diff --git a/labs/03/example.txt b/labs/03/example.txt new file mode 100644 index 0000000..a37efe5 --- /dev/null +++ b/labs/03/example.txt @@ -0,0 +1,5 @@ +a boy sees +the boy sees a flower +a girl with a flower likes the boy +a flower sees a flower +a sees boy flower diff --git a/labs/03/lab03.png b/labs/03/lab03.png new file mode 100644 index 0000000..c87763a Binary files /dev/null and b/labs/03/lab03.png differ