From 9969b1e5293e48c9ec3313f35b0cfdca320ec82a Mon Sep 17 00:00:00 2001 From: angieriosc <111139966+angieriosc@users.noreply.github.com> Date: Mon, 8 Jan 2024 19:07:51 -0600 Subject: [PATCH 1/3] Update relation_analyser.py --- labs/01/relation_analyser.py | 89 ++++++++++++++++++++++++------------ 1 file changed, 60 insertions(+), 29 deletions(-) diff --git a/labs/01/relation_analyser.py b/labs/01/relation_analyser.py index 6fd8b91..19de687 100644 --- a/labs/01/relation_analyser.py +++ b/labs/01/relation_analyser.py @@ -1,36 +1,67 @@ -import graphviz # https://graphviz.readthedocs.io/en/stable/index.html +#Tuve problemas instalando graphviz por lo que decidi generar un archivo de texto para que lo pueda copiar y pegar en Graphviz online 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() + Reflexivo = False + Simetrico = False + Transitivo = False + + for pair in val: + a, b = pair + + # condicional para ver si es reflexivo + if a == b or (a, a) in val: + Reflexivo = True + + # condicional para ver si es simétrico + if (b, a) in val: + Simetrico = True + + # condicional para ver si es transitivo + for pair_1 in val: + a, b = pair_1 + for pair_2 in val: + c, d = pair_2 + if b == c and (a, d) not in val: + Transitivo = False + break + + return Reflexivo, Simetrico, Transitivo + +def text_graph(val): + with open('graph.txt', 'w') as file: + file.write('Digraph {\n') + + for pair in val: + file.write(f'\t{pair[0]} -> {pair[1]} ;\n') + + file.write('}\n') 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() + print("Set:") + val_str = input("Ingresa tu set en el formato { (a,b), (c,d), (e,f) ... }: ") + val = eval(val_str) # Convierte string a una tupla + + Reflexivo, Simetrico, Transitivo = analyze(val) + + if Reflexivo: + print("R es reflexivo.") + else: + print("R no es reflexivo.") + + if Simetrico: + print("R es simetrico.") + else: + print("R no es simetrico.") + + if Transitivo: + print("R es transitivo.") + else: + print("R no es transitivo.") + + if Reflexivo and Simetrico and Transitivo: + print("R tiene una relación equivalente") + + text_graph(val) if __name__ == "__main__": main() From 55c3f43cbfb1c47e13ac0447c6c17ce8aa96c4f9 Mon Sep 17 00:00:00 2001 From: angieriosc <111139966+angieriosc@users.noreply.github.com> Date: Mon, 8 Jan 2024 20:03:19 -0600 Subject: [PATCH 2/3] Update relation_analyser.py --- labs/01/relation_analyser.py | 25 +++---------------------- 1 file changed, 3 insertions(+), 22 deletions(-) diff --git a/labs/01/relation_analyser.py b/labs/01/relation_analyser.py index 19de687..48e1a3f 100644 --- a/labs/01/relation_analyser.py +++ b/labs/01/relation_analyser.py @@ -1,29 +1,10 @@ #Tuve problemas instalando graphviz por lo que decidi generar un archivo de texto para que lo pueda copiar y pegar en Graphviz online def analyze(val): - Reflexivo = False - Simetrico = False - Transitivo = False - for pair in val: - a, b = pair - - # condicional para ver si es reflexivo - if a == b or (a, a) in val: - Reflexivo = True - - # condicional para ver si es simétrico - if (b, a) in val: - Simetrico = True - - # condicional para ver si es transitivo - for pair_1 in val: - a, b = pair_1 - for pair_2 in val: - c, d = pair_2 - if b == c and (a, d) not in val: - Transitivo = False - break + Reflexivo = all((a, a) in val for a, _ in val) + Simetrico = all((b, a) in val for a, b in val) + Transitivo = all((a, c) in val for a, b1 in val for b2, c in val if b1 == b2) return Reflexivo, Simetrico, Transitivo From 591d46f6632ad17b148ce5f799e0c34bcf8cec31 Mon Sep 17 00:00:00 2001 From: angieriosc <111139966+angieriosc@users.noreply.github.com> Date: Mon, 29 Jan 2024 19:51:52 -0600 Subject: [PATCH 3/3] Add files via upload --- labs/03/grammar.l | 20 ++++++++++++++ labs/03/grammar.y | 67 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 labs/03/grammar.l create mode 100644 labs/03/grammar.y diff --git a/labs/03/grammar.l b/labs/03/grammar.l new file mode 100644 index 0000000..a06174e --- /dev/null +++ b/labs/03/grammar.l @@ -0,0 +1,20 @@ +%{ +#include "y.tab.h" +%} + +%% + +"a" { return ARTICLE; } +"the" { return ARTICLE; } +"boy"|"girl"|"flower" { return NOUN; } +"touches"|"likes"|"sees" { return VERB; } +"with" { return PREP; } +\n { return EOL; } +. { /* ignore anything else */ } + +%% + +int yywrap() { + return 1; +} + diff --git a/labs/03/grammar.y b/labs/03/grammar.y new file mode 100644 index 0000000..9148005 --- /dev/null +++ b/labs/03/grammar.y @@ -0,0 +1,67 @@ +%{ +#include + +int yylex(); +void yyerror(const char *s); + +#define PASS 1 +#define FAIL 0 + +extern FILE* yyin; + +%} + +%token ARTICLE NOUN VERB PREP EOL + +%% + +sentence_list: sentence EOL { if ($1 == PASS) printf("PASS\n"); else printf("FAIL\n"); } + | sentence_list sentence EOL { if ($2 == PASS) printf("PASS\n"); else printf("FAIL\n"); } + ; + +sentence: noun_phrase verb_phrase { $$ = $1 && $2; } + ; + +noun_phrase: cmplx_noun { $$ = $1; } + | cmplx_noun prep_phrase { $$ = $1 && $2; } + ; + +verb_phrase: cmplx_verb { $$ = $1; } + | cmplx_verb prep_phrase { $$ = $1 && $2; } + ; + +prep_phrase: PREP cmplx_noun { $$ = $2; } + ; + +cmplx_noun: ARTICLE NOUN { $$ = PASS; } + ; + +cmplx_verb: VERB { $$ = PASS; } + | VERB noun_phrase { $$ = $2; } + ; + +%% + +void yyerror(const char *s) { + fprintf(stderr, "%s\n", s); +} + +int main(int argc, char *argv[]) { + if (argc != 2) { + printf("Usage: %s \n", argv[0]); + return 1; + } + + FILE *input_file = fopen(argv[1], "r"); + if (!input_file) { + perror("Error opening file"); + return 1; + } + + yyin = input_file; + yyparse(); + fclose(input_file); + + return 0; +} +