From fdf5f485d20da38dc441b1a351005d1bc01594d7 Mon Sep 17 00:00:00 2001 From: Pedro Lopes Date: Wed, 8 May 2024 13:51:41 -0300 Subject: [PATCH] add: tp02 solutions --- .../TP02Q01 - Classe em Java}/Character.java | 0 .../tp02/TP02Q01 - Classe em Java/pedro.out | 27 + .../tp02/TP02Q01 - Classe em Java/pub.in | 28 + .../tp02/TP02Q02 - Classe em C/Character | Bin 0 -> 27800 bytes .../TP02Q02 - Classe em C}/Character.c | 0 .../tp02/TP02Q02 - Classe em C/pedro.out | 27 + tps/fonte/tp02/TP02Q02 - Classe em C/pub.in | 28 + .../753045_sequencial.txt | 1 + .../Arq.java | 206 +++++ .../Character.java | 511 ++++++++++++ .../pedro.out | 19 + .../pub.in | 48 ++ .../753045_binaria.txt | 1 + .../TP02Q04 - Pesquisa Binaria em C/Character | Bin 0 -> 27896 bytes .../Character.c | 737 +++++++++++++++++ .../TP02Q04 - Pesquisa Binaria em C/pedro.out | 19 + .../TP02Q04 - Pesquisa Binaria em C/pub.in | 48 ++ .../753045_selecao.txt | 1 + .../Arq.java | 206 +++++ .../Character.java | 510 ++++++++++++ .../pedro.out | 27 + .../pub.in | 28 + .../753045_selecaoRecursiva.txt | 1 + .../Character | Bin 0 -> 27848 bytes .../Character.c | 696 ++++++++++++++++ .../pedro.out | 27 + .../pub.in | 28 + .../753045_insercao.txt | 1 + .../Arq.java | 206 +++++ .../Character.java | 513 ++++++++++++ .../pedro.out | 27 + .../pub.in | 28 + .../753045_shellsort.txt | 1 + .../tp02/TP02Q08 - Shellsort em C/Character | Bin 0 -> 27848 bytes .../tp02/TP02Q08 - Shellsort em C/Character.c | 709 ++++++++++++++++ .../tp02/TP02Q08 - Shellsort em C/pedro.out | 27 + .../tp02/TP02Q08 - Shellsort em C/pub.in | 28 + .../753045_heapsort.txt | 1 + .../tp02/TP02Q09 - Heapsort em Java/Arq.java | 206 +++++ .../TP02Q09 - Heapsort em Java/Character.java | 538 ++++++++++++ .../tp02/TP02Q09 - Heapsort em Java/pedro.out | 27 + .../tp02/TP02Q09 - Heapsort em Java/pub.in | 28 + .../753045_quicksort.txt | 1 + .../tp02/TP02Q10 - Quicksort em C/Character | Bin 0 -> 27848 bytes .../tp02/TP02Q10 - Quicksort em C/Character.c | 706 ++++++++++++++++ .../tp02/TP02Q10 - Quicksort em C/pedro.out | 27 + .../tp02/TP02Q10 - Quicksort em C/pub.in | 28 + .../753045_countingsort.txt | 1 + .../TP02Q11 - Counting Sort em Java/Arq.java | 206 +++++ .../Character.java | 565 +++++++++++++ .../TP02Q11 - Counting Sort em Java/pedro.out | 27 + .../TP02Q11 - Counting Sort em Java/pub.in | 28 + .../TP02Q12 - Bolha em C/753045_bolha.txt | 1 + tps/fonte/tp02/TP02Q12 - Bolha em C/Character | Bin 0 -> 27848 bytes .../tp02/TP02Q12 - Bolha em C/Character.c | 706 ++++++++++++++++ tps/fonte/tp02/TP02Q12 - Bolha em C/pedro.out | 27 + tps/fonte/tp02/TP02Q12 - Bolha em C/pub.in | 28 + .../753045_mergesort.txt | 1 + .../tp02/TP02Q13 - Mergesort em Java/Arq.java | 206 +++++ .../Character.java | 575 +++++++++++++ .../TP02Q13 - Mergesort em Java/pedro.out | 27 + .../tp02/TP02Q13 - Mergesort em Java/pub.in | 28 + .../753045_radixsort.txt | 1 + .../tp02/TP02Q14 - Radixsort em C/Character | Bin 0 -> 28072 bytes .../tp02/TP02Q14 - Radixsort em C/Character.c | 765 ++++++++++++++++++ .../tp02/TP02Q14 - Radixsort em C/pedro.out | 27 + .../tp02/TP02Q14 - Radixsort em C/pub.in | 28 + .../753045_selecaoParcial.txt | 1 + .../Arq.java | 206 +++++ .../Character.java | 526 ++++++++++++ .../pedro.out | 10 + .../pub.in | 28 + .../753045_insercaoParcial.txt | 1 + .../Character | Bin 0 -> 31944 bytes .../Character.c | 719 ++++++++++++++++ .../pedro.out | 10 + .../pub.in | 28 + .../753045_heapsortParcial.txt | 1 + .../TP02Q17 - Heapsort PARCIAL em C/Character | Bin 0 -> 31976 bytes .../Character.c | 741 +++++++++++++++++ .../TP02Q17 - Heapsort PARCIAL em C/pedro.out | 10 + .../TP02Q17 - Heapsort PARCIAL em C/pub.in | 28 + .../753045_quicksortParcial.txt | 1 + .../Arq.java | 206 +++++ .../Character.java | 529 ++++++++++++ .../pedro.out | 10 + .../pub.in | 28 + 87 files changed, 12656 insertions(+) rename tps/fonte/{ => tp02/TP02Q01 - Classe em Java}/Character.java (100%) create mode 100644 tps/fonte/tp02/TP02Q01 - Classe em Java/pedro.out create mode 100644 tps/fonte/tp02/TP02Q01 - Classe em Java/pub.in create mode 100644 tps/fonte/tp02/TP02Q02 - Classe em C/Character rename tps/fonte/{ => tp02/TP02Q02 - Classe em C}/Character.c (100%) create mode 100644 tps/fonte/tp02/TP02Q02 - Classe em C/pedro.out create mode 100644 tps/fonte/tp02/TP02Q02 - Classe em C/pub.in create mode 100644 tps/fonte/tp02/TP02Q03 - Pesquisa Sequencial em Java/753045_sequencial.txt create mode 100644 tps/fonte/tp02/TP02Q03 - Pesquisa Sequencial em Java/Arq.java create mode 100644 tps/fonte/tp02/TP02Q03 - Pesquisa Sequencial em Java/Character.java create mode 100644 tps/fonte/tp02/TP02Q03 - Pesquisa Sequencial em Java/pedro.out create mode 100644 tps/fonte/tp02/TP02Q03 - Pesquisa Sequencial em Java/pub.in create mode 100644 tps/fonte/tp02/TP02Q04 - Pesquisa Binaria em C/753045_binaria.txt create mode 100644 tps/fonte/tp02/TP02Q04 - Pesquisa Binaria em C/Character create mode 100644 tps/fonte/tp02/TP02Q04 - Pesquisa Binaria em C/Character.c create mode 100644 tps/fonte/tp02/TP02Q04 - Pesquisa Binaria em C/pedro.out create mode 100644 tps/fonte/tp02/TP02Q04 - Pesquisa Binaria em C/pub.in create mode 100644 tps/fonte/tp02/TP02Q05 - Ordenação por Seleção em Java/753045_selecao.txt create mode 100644 tps/fonte/tp02/TP02Q05 - Ordenação por Seleção em Java/Arq.java create mode 100644 tps/fonte/tp02/TP02Q05 - Ordenação por Seleção em Java/Character.java create mode 100644 tps/fonte/tp02/TP02Q05 - Ordenação por Seleção em Java/pedro.out create mode 100644 tps/fonte/tp02/TP02Q05 - Ordenação por Seleção em Java/pub.in create mode 100644 tps/fonte/tp02/TP02Q06 - Ordenação por Seleção Recursiva em C/753045_selecaoRecursiva.txt create mode 100644 tps/fonte/tp02/TP02Q06 - Ordenação por Seleção Recursiva em C/Character create mode 100644 tps/fonte/tp02/TP02Q06 - Ordenação por Seleção Recursiva em C/Character.c create mode 100644 tps/fonte/tp02/TP02Q06 - Ordenação por Seleção Recursiva em C/pedro.out create mode 100644 tps/fonte/tp02/TP02Q06 - Ordenação por Seleção Recursiva em C/pub.in create mode 100644 tps/fonte/tp02/TP02Q07 - Ordenação por Inserção em Java/753045_insercao.txt create mode 100644 tps/fonte/tp02/TP02Q07 - Ordenação por Inserção em Java/Arq.java create mode 100644 tps/fonte/tp02/TP02Q07 - Ordenação por Inserção em Java/Character.java create mode 100644 tps/fonte/tp02/TP02Q07 - Ordenação por Inserção em Java/pedro.out create mode 100644 tps/fonte/tp02/TP02Q07 - Ordenação por Inserção em Java/pub.in create mode 100644 tps/fonte/tp02/TP02Q08 - Shellsort em C/753045_shellsort.txt create mode 100644 tps/fonte/tp02/TP02Q08 - Shellsort em C/Character create mode 100644 tps/fonte/tp02/TP02Q08 - Shellsort em C/Character.c create mode 100644 tps/fonte/tp02/TP02Q08 - Shellsort em C/pedro.out create mode 100644 tps/fonte/tp02/TP02Q08 - Shellsort em C/pub.in create mode 100644 tps/fonte/tp02/TP02Q09 - Heapsort em Java/753045_heapsort.txt create mode 100644 tps/fonte/tp02/TP02Q09 - Heapsort em Java/Arq.java create mode 100644 tps/fonte/tp02/TP02Q09 - Heapsort em Java/Character.java create mode 100644 tps/fonte/tp02/TP02Q09 - Heapsort em Java/pedro.out create mode 100644 tps/fonte/tp02/TP02Q09 - Heapsort em Java/pub.in create mode 100644 tps/fonte/tp02/TP02Q10 - Quicksort em C/753045_quicksort.txt create mode 100644 tps/fonte/tp02/TP02Q10 - Quicksort em C/Character create mode 100644 tps/fonte/tp02/TP02Q10 - Quicksort em C/Character.c create mode 100644 tps/fonte/tp02/TP02Q10 - Quicksort em C/pedro.out create mode 100644 tps/fonte/tp02/TP02Q10 - Quicksort em C/pub.in create mode 100644 tps/fonte/tp02/TP02Q11 - Counting Sort em Java/753045_countingsort.txt create mode 100644 tps/fonte/tp02/TP02Q11 - Counting Sort em Java/Arq.java create mode 100644 tps/fonte/tp02/TP02Q11 - Counting Sort em Java/Character.java create mode 100644 tps/fonte/tp02/TP02Q11 - Counting Sort em Java/pedro.out create mode 100644 tps/fonte/tp02/TP02Q11 - Counting Sort em Java/pub.in create mode 100644 tps/fonte/tp02/TP02Q12 - Bolha em C/753045_bolha.txt create mode 100644 tps/fonte/tp02/TP02Q12 - Bolha em C/Character create mode 100644 tps/fonte/tp02/TP02Q12 - Bolha em C/Character.c create mode 100644 tps/fonte/tp02/TP02Q12 - Bolha em C/pedro.out create mode 100644 tps/fonte/tp02/TP02Q12 - Bolha em C/pub.in create mode 100644 tps/fonte/tp02/TP02Q13 - Mergesort em Java/753045_mergesort.txt create mode 100644 tps/fonte/tp02/TP02Q13 - Mergesort em Java/Arq.java create mode 100644 tps/fonte/tp02/TP02Q13 - Mergesort em Java/Character.java create mode 100644 tps/fonte/tp02/TP02Q13 - Mergesort em Java/pedro.out create mode 100644 tps/fonte/tp02/TP02Q13 - Mergesort em Java/pub.in create mode 100644 tps/fonte/tp02/TP02Q14 - Radixsort em C/753045_radixsort.txt create mode 100644 tps/fonte/tp02/TP02Q14 - Radixsort em C/Character create mode 100644 tps/fonte/tp02/TP02Q14 - Radixsort em C/Character.c create mode 100644 tps/fonte/tp02/TP02Q14 - Radixsort em C/pedro.out create mode 100644 tps/fonte/tp02/TP02Q14 - Radixsort em C/pub.in create mode 100644 tps/fonte/tp02/TP02Q15 - Ordenação PARCIAL por Seleção em Java/753045_selecaoParcial.txt create mode 100644 tps/fonte/tp02/TP02Q15 - Ordenação PARCIAL por Seleção em Java/Arq.java create mode 100644 tps/fonte/tp02/TP02Q15 - Ordenação PARCIAL por Seleção em Java/Character.java create mode 100644 tps/fonte/tp02/TP02Q15 - Ordenação PARCIAL por Seleção em Java/pedro.out create mode 100644 tps/fonte/tp02/TP02Q15 - Ordenação PARCIAL por Seleção em Java/pub.in create mode 100644 tps/fonte/tp02/TP02Q16 - Ordenação PARCIAL por Inserção em C/753045_insercaoParcial.txt create mode 100644 tps/fonte/tp02/TP02Q16 - Ordenação PARCIAL por Inserção em C/Character create mode 100644 tps/fonte/tp02/TP02Q16 - Ordenação PARCIAL por Inserção em C/Character.c create mode 100644 tps/fonte/tp02/TP02Q16 - Ordenação PARCIAL por Inserção em C/pedro.out create mode 100644 tps/fonte/tp02/TP02Q16 - Ordenação PARCIAL por Inserção em C/pub.in create mode 100644 tps/fonte/tp02/TP02Q17 - Heapsort PARCIAL em C/753045_heapsortParcial.txt create mode 100644 tps/fonte/tp02/TP02Q17 - Heapsort PARCIAL em C/Character create mode 100644 tps/fonte/tp02/TP02Q17 - Heapsort PARCIAL em C/Character.c create mode 100644 tps/fonte/tp02/TP02Q17 - Heapsort PARCIAL em C/pedro.out create mode 100644 tps/fonte/tp02/TP02Q17 - Heapsort PARCIAL em C/pub.in create mode 100644 tps/fonte/tp02/TP02Q18 - Quicksort PARCIAL em Java/753045_quicksortParcial.txt create mode 100644 tps/fonte/tp02/TP02Q18 - Quicksort PARCIAL em Java/Arq.java create mode 100644 tps/fonte/tp02/TP02Q18 - Quicksort PARCIAL em Java/Character.java create mode 100644 tps/fonte/tp02/TP02Q18 - Quicksort PARCIAL em Java/pedro.out create mode 100644 tps/fonte/tp02/TP02Q18 - Quicksort PARCIAL em Java/pub.in diff --git a/tps/fonte/Character.java b/tps/fonte/tp02/TP02Q01 - Classe em Java/Character.java similarity index 100% rename from tps/fonte/Character.java rename to tps/fonte/tp02/TP02Q01 - Classe em Java/Character.java diff --git a/tps/fonte/tp02/TP02Q01 - Classe em Java/pedro.out b/tps/fonte/tp02/TP02Q01 - Classe em Java/pedro.out new file mode 100644 index 0000000..3a30b52 --- /dev/null +++ b/tps/fonte/tp02/TP02Q01 - Classe em Java/pedro.out @@ -0,0 +1,27 @@ +[9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8 ## Harry Potter ## {The Boy Who Lived, The Chosen One, Undesirable No. 1, Potty} ## Gryffindor ## half-blood ## human ## stag ## false ## true ## Daniel Radcliffe ## true ## 31-07-1980 ## 1980 ## green ## male ## black ## true] +[1413e1b3-2903-4a47-a2d5-e8abc5ce8014 ## Seamus Finnigan ## {O Flaherty, Seamus Finnegan} ## Gryffindor ## half-blood ## human ## N/A ## false ## true ## Devon Murray ## true ## 31-12-1926 ## 1960 ## amber ## male ## sandy ## true] +[ca3827f0-375a-4891-aaa5-f5e8a5bad225 ## Minerva McGonagall ## {} ## Gryffindor ## half-blood ## human ## tabby cat ## true ## false ## Dame Maggie Smith ## true ## 04-10-1925 ## 1925 ## grey ## female ## black ## true] +[36bfefd0-e0bb-4d11-be98-d1ef6117a77a ## Rubeus Hagrid ## {Professor Hagrid, Hagger} ## Gryffindor ## half-blood ## half-giant ## N/A ## true ## false ## Robbie Coltrane ## true ## 06-12-1928 ## 1928 ## black ## male ## black ## true] +[20354d7a-e4fe-47af-8ff6-187bca92f3f9 ## Pandora Lovegood ## {} ## Ravenclaw ## quarter-veela ## human ## N/A ## false ## false ## N/A ## false ## 31-12-1926 ## 1959 ## dark ## female ## white ## true] +[57fe29d4-312a-4711-bd9a-c320253d9176 ## Victoire Weasley ## {} ## Slytherin ## pure-blood ## human ## N/A ## false ## true ## N/A ## true ## 31-12-1926 ## 1926 ## grey ## female ## blonde ## true] +[b415c867-1cff-455e-b194-748662ac2cca ## Albus Dumbledore ## {Professor Dumbledore} ## Gryffindor ## half-blood ## human ## Phoenix ## true ## false ## Richard Harris ## false ## 27-03-1960 ## 1881 ## blue ## male ## silver ## true] +[5a4c95db-947d-4914-a631-41e8d466328e ## Kendra Dumbledore ## {} ## Slytherin ## muggleborn ## human ## N/A ## false ## false ## N/A ## false ## 31-12-1926 ## 1925 ## dark ## female ## black ## true] +[861c4cde-2f0f-4796-8d8f-9492e74b2573 ## Luna Lovegood ## {Loony Lovegood} ## Ravenclaw ## pure-blood ## human ## hare ## false ## true ## Evanna Lynch ## true ## 13-02-1981 ## 1981 ## silver ## female ## blonde ## true] +[2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f ## Sirius Black ## {Padfoot, Snuffles} ## Gryffindor ## pure-blood ## human ## hare ## false ## false ## Gary Oldman ## false ## 03-11-1959 ## 1959 ## grey ## male ## black ## true] +[41cd0bbe-a943-431b-9bde-bb2cad3491a1 ## Lily Potter ## {} ## Gryffindor ## muggleborn ## human ## N/A ## false ## false ## Geraldine Somerville ## false ## 30-01-1960 ## 1960 ## green ## female ## blond ## true] +[2a0615de-8aa4-41e7-9504-dd875f5f3f01 ## George Weasley ## {} ## Gryffindor ## pure-blood ## human ## N/A ## false ## true ## Oliver Phelps ## true ## 13-02-1981 ## 1954 ## amber ## male ## red ## true] +[11b5ca88-64ad-41a4-9f36-317b66c290af ## Nearly Headless Nick ## {Sir Nicholas de Mimsy-Porpington, Sir Nicholas, Nick} ## Gryffindor ## pure-blood ## ghost ## N/A ## false ## false ## John Cleese ## false ## 27-03-1960 ## 1926 ## amber ## male ## sandy ## true] +[eaea5eb3-48a3-41c6-9ea5-c695299bab16 ## Lisa Turpin ## {} ## Ravenclaw ## pure-blood ## human ## N/A ## false ## true ## N/A ## true ## 27-03-1960 ## 1951 ## amber ## female ## sandy ## true] +[0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3 ## Dobby ## {} ## Slytherin ## pure-blood ## house-elf ## N/A ## false ## false ## Toby Jones ## false ## 06-02-1950 ## 1950 ## green ## male ## blond ## false] +[b0620914-858d-46fc-8e6d-033c565e138b ## Mrs Norris ## {} ## Slytherin ## pure-blood ## cat ## N/A ## true ## false ## Maxime, Alanis and Tommy the cats ## true ## 31-12-1926 ## 1954 ## yellow ## female ## brown ## false] +[6b59be3f-e527-422d-959d-79fcdb3b24eb ## Hedwig ## {} ## Hufflepuff ## muggle ## owl ## N/A ## false ## false ## Gizmo ## false ## 07-04-1979 ## 1954 ## amber ## female ## brown ## false] +[fed624df-56d9-495e-9ad4-ea77000957e8 ## Petunia Dursley ## {} ## Slytherin ## muggle ## human ## N/A ## false ## false ## Fiona Shaw ## true ## 31-12-1926 ## 1954 ## yellow ## female ## blonde ## false] +[d58e7249-19d1-40bd-a43f-1da0497fe8aa ## Dolores Umbridge ## {} ## Slytherin ## half-blood ## human ## persian cat ## true ## false ## Imelda Staunton ## true ## 31-12-1926 ## 1926 ## brown ## female ## grey ## true] +[3a0fe4df-2e40-4541-8d7f-13586f0b9294 ## Tom Riddle ## {Tom Riddle Senior} ## Ravenclaw ## muggle ## human ## N/A ## false ## false ## N/A ## false ## 27-03-1960 ## 1980 ## brown ## male ## dark ## false] +[6afb1960-febd-418d-b604-e50c1b59459b ## Bellatrix Lestrange ## {Bella} ## Slytherin ## pure-blood ## human ## N/A ## false ## false ## Helena Bonham Carter ## false ## 06-02-1950 ## 1951 ## brown ## female ## black ## true] +[efa802c8-ae18-4ae1-a524-49df21d05939 ## Lord Voldemort ## {Tom Riddle, Tom Marvolo Riddle, You-Know-Who, He Who Must Not Be Named, the Dark Lord, the Heir of Slytherin} ## Slytherin ## half-blood ## human ## N/A ## false ## false ## Ralph Fiennes ## false ## 31-12-1926 ## 1926 ## Scarlet ## male ## bald ## true] +[933787c2-51e3-4eac-8a85-ab332cac0456 ## Miranda Gaushawk ## {} ## Gryffindor ## muggle ## human ## N/A ## false ## false ## N/A ## false ## 27-03-1960 ## 1960 ## blue ## female ## silver ## true] +[94c993f6-a443-4408-b908-6e78e223e8ac ## Aberforth Dumbledore ## {} ## Gryffindor ## half-blood ## human ## goat ## false ## false ## Jim McManus ## true ## 10-03-1960 ## 1960 ## blue ## male ## grey ## true] +[00434cd3-fcc7-44c7-8f98-7368415b4206 ## Miriam Strout ## {} ## Gryffindor ## half-blood ## human ## N/A ## false ## false ## N/A ## true ## 27-03-1960 ## 1954 ## blue ## female ## silver ## true] +[7614cf6e-689e-47ac-a976-b1e9997637e9 ## Dudley Dursley ## {} ## Slytherin ## muggle ## human ## N/A ## false ## false ## Harry Melling ## true ## 23-06-1980 ## 1980 ## blue ## male ## blond ## false] +[d59691a4-f830-4eb0-a819-a0fb00b7e80f ## Garrick Ollivander ## {} ## Hufflepuff ## muggle ## human ## N/A ## false ## false ## John Hurt ## true ## 04-10-1925 ## 1954 ## pale, silvery ## male ## brown ## true] diff --git a/tps/fonte/tp02/TP02Q01 - Classe em Java/pub.in b/tps/fonte/tp02/TP02Q01 - Classe em Java/pub.in new file mode 100644 index 0000000..a782d8c --- /dev/null +++ b/tps/fonte/tp02/TP02Q01 - Classe em Java/pub.in @@ -0,0 +1,28 @@ +9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8 +1413e1b3-2903-4a47-a2d5-e8abc5ce8014 +ca3827f0-375a-4891-aaa5-f5e8a5bad225 +36bfefd0-e0bb-4d11-be98-d1ef6117a77a +20354d7a-e4fe-47af-8ff6-187bca92f3f9 +57fe29d4-312a-4711-bd9a-c320253d9176 +b415c867-1cff-455e-b194-748662ac2cca +5a4c95db-947d-4914-a631-41e8d466328e +861c4cde-2f0f-4796-8d8f-9492e74b2573 +2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f +41cd0bbe-a943-431b-9bde-bb2cad3491a1 +2a0615de-8aa4-41e7-9504-dd875f5f3f01 +11b5ca88-64ad-41a4-9f36-317b66c290af +eaea5eb3-48a3-41c6-9ea5-c695299bab16 +0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3 +b0620914-858d-46fc-8e6d-033c565e138b +6b59be3f-e527-422d-959d-79fcdb3b24eb +fed624df-56d9-495e-9ad4-ea77000957e8 +d58e7249-19d1-40bd-a43f-1da0497fe8aa +3a0fe4df-2e40-4541-8d7f-13586f0b9294 +6afb1960-febd-418d-b604-e50c1b59459b +efa802c8-ae18-4ae1-a524-49df21d05939 +933787c2-51e3-4eac-8a85-ab332cac0456 +94c993f6-a443-4408-b908-6e78e223e8ac +00434cd3-fcc7-44c7-8f98-7368415b4206 +7614cf6e-689e-47ac-a976-b1e9997637e9 +d59691a4-f830-4eb0-a819-a0fb00b7e80f +FIM \ No newline at end of file diff --git a/tps/fonte/tp02/TP02Q02 - Classe em C/Character b/tps/fonte/tp02/TP02Q02 - Classe em C/Character new file mode 100644 index 0000000000000000000000000000000000000000..28a250d947dfddfaffc0143d0d420fab115bb6fa GIT binary patch literal 27800 zcmeHQ4|G)3nZL;-5dI}m0i#eE5h|#JKn0 z&t_LwZJdAo?CSDa)m62@hFJ{@=FggceQv!kcP=mYcmzUy>XzlDZ0bEfiIyHb_3}Tz zni?JR)XJNBKfUx7iko5}f22bf>C5amy$n3bMY$eCiZR0IdfEF@jYuWF?*V*q0@0i# z_zS@83G^xdM1B?~!B-~1i<00}h(!J~li=S>f;*Gop(OY_!0q_NUta-`D4q#P@JUJV zo08y_N${yj@IVsWlLQ}^1gCMCs9a}(+wqCNwgRA-23_?5e_7oY=5447u=+ZGRc)Yx zRru<>wXDwT_xt?J<*KUpl`UN8sxR}@Rxs$-l0jWCP)|(2cMmJ`R9E}Th^vNOMOn44 z-pgtXu*?%+o`A246v`_7Y?C*DLFR?$vIdW(YU3H(AOKa+YVknlm>H{8sz*Xa^ zfYi{lhMrv;EJmy-qs@PTT^?Nr})d#%(RmDrHeYM_Io{iO% z(WV+-t#s)UstFZoywZrJyY-(Lvl+73_)Y=#BUq|==+dexG@Xt8LSn(ot1y$NvS(!j zsQFsWuWs3x{c=85xLx9%Zegf!#h-KuoaPjoZ`9Re!bd9*G-AT3Z>g)_gy$#_bijnG zxr)R&Zn`rz5+9mV)Ma;z6ya*_BV>k~?m}P9@xH5TlHc7Xe5?ttG~qOtsjJQ{Qj~9&0zn(xf+1Y> z2|^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E#4)*YG2SVUwxcdv%bh% zyD3n~>ioX4GH*Q^HW2VvZ43s)D~~JviOpQdK=W<77q3CCUm5UJRG1=#Xl0$ZtP1@m zRvdgAUC!!*8&L`P%9>TxJ8R=gr}={Trafj+@Acq?%mrIe3xnReoFpjJk&!ch!LvmV zof!2dUbJk=2jMzuQFQ8_)I>FP7Atc=0Y^2ygs=4kyvxyzO~o@Mtu+%YN54n>)>k~c z%IkfuN>6P$zPFG0M<4}N{y=3R{Npvyws#+In#avSqt~Cl%N2Ng|-)(Iv1Rn}Z z)Yt#!XluRqT0?M@VSH2ZOfZxRsy(%Mm$q_2r7ZCT{Jz?t*&9&JQ{|Wa&gI&O4kdp7x+_=~hH8gT)@=1z2n+behlS;L4QC8hu@E)x|De%Dmp=kc2>7?@Eq{;qLL zO^Wx9TOf2Ayl313#WJQE8Pu^&W7DFUxQvzxvkokx`EJ?7xTv#20mUqfg~!tr&evoL?YhYZc(FGx?2F3YgO?QHNn&C(i6S|Jr)2J zy_&pRAkzS{yuaO&Rm=h*%Jyfu^;>EJo{gYrPEIRT3a_2_`s-LOel_FG-BcUQ-59LG z|HV*ME+I$3vRMJoCdOrzp886bTfU_h{say9g=8~+zfNoqH-;Cmmxu z#_LJhtMm2M!13s0*sJqVMb-BLP~lg6{oMtW&fr!2>Ks+k>!qEFU+F1&59D;FuXuId zs;GP{}e|=Z~rJT zQla|g7ao<(RBqoTIJo_FNX+(jX|E{%4V7U{&zj;=bVr=MIyY8S#j9K>UePDw?A3F! zqF@qD!5Ns{eNsO;5wt@?kBdSv-kF_fKZ$9C9~in3Sd^Cxcyh--!*#$DMf`W_@^ zd*+s^C4zQHRuN^V=*JM4?K|8;vqRdeIOFXV{}&|4mx{l~EhHzTeVaTd6DVVLm5}sv zP|80q4p;W_7}}7ME?s77^j#t1_kNjwj{L%x)*(8JQRP allCharacters = new ArrayList(); + + // -------------------------- // + + // Attributes + private UUID id; + private String name; + private ArrayList alternateNames; + private String house; + private String ancestry; + private String species; + private String patronus; + private boolean hogwartsStaff; + private boolean hogwartsStudent; + private String actorName; + private boolean alive; + private Date birthDate; + private int yearOfBirth; + private String eyeColour; + private String gender; + private String hairColour; + private boolean wizard; + + // -------------------------- // + + // Empty constructor + public Character() { + + this.id = UUID.randomUUID(); + this.name = ""; + this.alternateNames = new ArrayList(); + this.house = ""; + this.ancestry = ""; + this.species = ""; + this.patronus = ""; + this.hogwartsStaff = false; + this.hogwartsStudent = false; + this.actorName = ""; + this.alive = false; + this.birthDate = null; + this.yearOfBirth = 0; + this.eyeColour = ""; + this.gender = ""; + this.hairColour = ""; + this.wizard = false; + } + + // Constructor + public Character(UUID id, String name, ArrayList alternateNames, String house, String ancestry, String species, String patronus, boolean hogwartsStaff, boolean hogwartsStudent, String actorName, boolean alive, Date birthDate, int yearOfBirth, String eyeColour, String gender, String hairColour, boolean wizard) { + + this.id = id; + this.name = name; + this.alternateNames = alternateNames; + this.house = house; + this.ancestry = ancestry; + this.species = species; + this.patronus = patronus; + this.hogwartsStaff = hogwartsStaff; + this.hogwartsStudent = hogwartsStudent; + this.actorName = actorName; + this.alive = alive; + this.birthDate = birthDate; + this.yearOfBirth = yearOfBirth; + this.eyeColour = eyeColour; + this.gender = gender; + this.hairColour = hairColour; + this.wizard = wizard; + } + + // -------------------------- // + + // Gets + public UUID getId() { return this.id; } + public String getName() { return this.name; } + public String getHouse() { return this.house; } + public String getAncestry() { return this.ancestry; } + public String getSpecies() { return this.species; } + public String getPatronus() { return this.patronus; } + public boolean getHogwartsStaff() { return this.hogwartsStaff; } + public boolean getHogwartsStudent() { return this.hogwartsStudent; } + public String getActorName() { return this.actorName; } + public boolean getAlive() { return this.alive; } + public Date getBirthDate() { return this.birthDate; } + public int getYearOfBirth() { return this.yearOfBirth; } + public String getEyeColour() { return this.eyeColour; } + public String getGender() { return this.gender; } + public String getHairColour() { return this.hairColour; } + public boolean getWizard() { return this.wizard; } + + public String getAlternateNames() { + + // Construct string e.g. {item1, item2, item3} + String alternateNames = "{"; + + for(int i = 0; i < this.alternateNames.size(); i++) { + + alternateNames += this.alternateNames.get(i); + + if(i < this.alternateNames.size() - 1) alternateNames += ", "; + } + + alternateNames += "}"; + + return alternateNames; + } + + // Sets + public void setId(UUID id) { this.id = id; } + public void setName(String name) { this.name = name; } + public void setAlternateNames(ArrayList alternateNames) { this.alternateNames = alternateNames; } + public void setHouse(String house) { this.house = house; } + public void setAncestry(String ancestry) { this.ancestry = ancestry; } + public void setSpecies(String species) { this.species = species; } + public void setPatronus(String patronus) { this.patronus = patronus; } + public void setHogwartsStaff(boolean hogwartsStaff) { this.hogwartsStaff = hogwartsStaff; } + public void setHogwartsStudent(boolean hogwartsStudent) { this.hogwartsStudent = hogwartsStudent; } + public void setActorNane(String actorName) { this.actorName = actorName; } + public void setAlive(boolean alive) { this.alive = alive; } + public void setBirthDate(Date birthDate) { this.birthDate = birthDate; } + public void setYearOfBirth(int yearOfBirth) { this.yearOfBirth = yearOfBirth; } + public void setEyeColour(String eyeColour) { this.eyeColour = eyeColour; } + public void setGender(String gender) { this.gender = gender; } + public void setHairColour(String hairColour) { this.hairColour = hairColour; } + public void setWizard(boolean wizard) { this.wizard = wizard; } + + // -------------------------- // + + // Clone + public Character clone() { return new Character(this.id, this.name, this.alternateNames, this.house, this.ancestry, this.species, this.patronus, this.hogwartsStaff, this.hogwartsStudent, this.actorName, this.alive, this.birthDate, this.yearOfBirth, this.eyeColour, this.gender, this.hairColour, this.wizard); } + + // -------------------------- // + + // Print + public void print() { + + System.out.println("[" + + this.getId() + " ## " + + this.getName() + " ## " + + this.getAlternateNames() + " ## " + + (this.getHouse() == "" ? "N/A" : this.getHouse()) + " ## " + + (this.getAncestry() == "" ? "N/A" : this.getAncestry()) + " ## " + + (this.getSpecies() == "" ? "N/A" : this.getSpecies()) + " ## " + + (this.getPatronus() == "" ? "N/A" : this.getPatronus()) + " ## " + + (this.getHogwartsStaff() ? "true" : "false") + " ## " + + (this.getHogwartsStudent() ? "true" : "false") + " ## " + + (this.getActorName() == "" ? "N/A" : this.getActorName()) + " ## " + + (this.getAlive() ? "true" : "false") + " ## " + + (this.getBirthDate() == null ? "N/A" : ddf.format(this.getBirthDate())) + " ## " + + (this.getYearOfBirth() == 0 ? "N/A" : this.getYearOfBirth()) + " ## " + + (this.getEyeColour() == "" ? "N/A" : this.getEyeColour()) + " ## " + + (this.getGender() == "" ? "N/A" : this.getGender()) + " ## " + + (this.getHairColour() == "" ? "N/A" : this.getHairColour()) + " ## " + + (this.getWizard() ? "true" : "false") + "]"); + } + + // -------------------------- // + + public static ArrayList extractNames(String input) { + + ArrayList names = new ArrayList<>(); + + // Clean input + String cleanedInput = input.substring(1, input.length() - 1); + + // Check if input contains double quotes + if (cleanedInput.contains("\"\"")) { + + String[] parts = cleanedInput.split("\", "); + + for (String part : parts) names.add(part.replace("\"", "").replace("'", "")); + } + else { + + // Split input by comma and space + String[] parts = cleanedInput.split(", "); + + for (String part : parts) names.add(part.replace("'", "")); + } + return names; + } + + // -------------------------- // + + // Read + public void read(String line) { + + // ---------------------- // + + // Start position + int positionStart = 0; + int positionEnd = 0; + + // ---------------------- // + + // Set id + positionEnd = line.indexOf(";", positionStart); + this.setId(UUID.fromString(line.substring(positionStart, positionEnd))); + + // ---------------------- // + + // Set name + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setName(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set alternate names + positionStart = positionEnd + 1; + + if(line.charAt(positionStart) == '[') { // If first character is '[', has one or no names + + if(line.charAt(positionStart + 1) == ']') { + + positionEnd = positionStart + 2; + + this.setAlternateNames(new ArrayList()); + } + else { + + positionEnd = line.indexOf("];", positionStart); + + this.setAlternateNames(extractNames(line.substring(positionStart, positionEnd++))); + } + } + + // ---------------------- // + + // Set house + positionStart = positionEnd; + positionEnd = line.indexOf(";", ++positionStart); + + this.setHouse(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set ancestry + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setAncestry(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set species + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setSpecies(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set patronus + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setPatronus(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set hogwartsStaff + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setHogwartsStaff("VERDADEIRO".equals(line.substring(positionStart, positionEnd))); + + // ---------------------- // + + // Set hogwartsStudent + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setHogwartsStudent("VERDADEIRO".equals(line.substring(positionStart, positionEnd))); + + // ---------------------- // + + // Set actor name + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setActorNane(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set alive + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setAlive("VERDADEIRO".equals(line.substring(positionStart, positionEnd))); + + // ---------------------- // + + // Set date of birth + positionStart = positionEnd; + positionEnd = line.indexOf(";", ++positionStart); + + try { this.setBirthDate(ddf.parse(line.substring(positionStart, positionEnd))); } + catch(Exception e) { this.setBirthDate(null); } + + // ---------------------- // + + // Set year of birth + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + try { this.setYearOfBirth(Integer.parseInt(line.substring(positionStart, positionEnd))); } + catch(Exception e) { this.setYearOfBirth(0); } + + // ---------------------- // + + // Set eye colour + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setEyeColour(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set gender + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setGender(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set hair colour + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setHairColour(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set wizard + positionStart = positionEnd + 1; + + this.setWizard("VERDADEIRO".equals(line.substring(positionStart, line.length()))); + + // ---------------------- // + } + + // ---------------------------------------------------------------------------------------------------- // + + // Read all characters function + public static void startCharacters() { + + // Initialize variables + try { + + FileInputStream fstream = new FileInputStream(FILE_PATH); + BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); + + // ---------------------- // + + // Explode CSV file + String line = br.readLine(); + + while((line = br.readLine()) != null) { + + // Initialize character + Character character = new Character(); + + // Read line + character.read(line); + + // Add character to array + allCharacters.add(character); + } + + // Close CSV file + fstream.close(); + } + catch(IOException e) { e.printStackTrace(); } + } + + // ---------------------------------------------------------------------------------------------------- // + + // Search by id function + public static Character searchById(UUID id, ArrayList characters) { + + // Search for character + for(int i = 0; i < characters.size(); i++) { + + if(characters.get(i).getId().equals(id)) return characters.get(i); + } + return null; + } + + // ---------------------------------------------------------------------------------------------------- // + + public static void main(String[] args) { + + // ----------------------------------------------------------------- // + + // #1 - Start - Read all characters in CSV file + startCharacters(); + + // ----------------------------------------------------------------- // + + // #2 - Read input and print characters from pub.in id entries and add to mainCharacters array + + // Initialize mainCharacters array + ArrayList mainCharacters = new ArrayList(); + + // Initialize scanner + Scanner inScanner = new Scanner(System.in); + + // Read first line + String line = inScanner.nextLine(); + + // While line is not "FIM" + while(!line.equals("FIM")) { + + // Get id + UUID id = UUID.fromString(line); + + // Search for character + Character character = searchById(id, allCharacters); + + // Add character to mainCharacters array + if(character != null) mainCharacters.add(character); + + // Read line + line = inScanner.nextLine(); + } + + // ----------------------------------------------------------------- // + + // #3 - Read input and search for characters from pub.in name entries in mainCharacters array + + // Start benchmark + long startTime = System.currentTimeMillis(); + int comparisons = 0; + + // Read first line + line = inScanner.nextLine(); + + // While line is not "FIM" + while(!line.equals("FIM")) { + + Character foundCharacter = null; + + // Search for character + for(int i = 0; i < mainCharacters.size(); i++) { + + // Increment comparisons + comparisons++; + + // If character is found + if(mainCharacters.get(i).getName().equals(line)) { + + foundCharacter = mainCharacters.get(i); + break; + } + } + + if(foundCharacter != null) System.out.println("SIM"); + else System.out.println("NAO"); + + // ----------------- // + + // Read line + line = inScanner.nextLine(); + } + + // ----------------- // + + // Save benchmark in file + Arq.openWrite("753045_sequencial.txt"); + Arq.print("753045\t" + (System.currentTimeMillis() - startTime) + "ms\t" + comparisons); + Arq.close(); + + // ----------------------------------------------------------------- // + + // Close scanner + inScanner.close(); + } +} + +// ---------------------------------------------------------------------------------------------------- // \ No newline at end of file diff --git a/tps/fonte/tp02/TP02Q03 - Pesquisa Sequencial em Java/pedro.out b/tps/fonte/tp02/TP02Q03 - Pesquisa Sequencial em Java/pedro.out new file mode 100644 index 0000000..8ce90f3 --- /dev/null +++ b/tps/fonte/tp02/TP02Q03 - Pesquisa Sequencial em Java/pedro.out @@ -0,0 +1,19 @@ +SIM +NAO +NAO +NAO +SIM +NAO +NAO +NAO +SIM +NAO +NAO +NAO +SIM +NAO +NAO +NAO +NAO +NAO +SIM diff --git a/tps/fonte/tp02/TP02Q03 - Pesquisa Sequencial em Java/pub.in b/tps/fonte/tp02/TP02Q03 - Pesquisa Sequencial em Java/pub.in new file mode 100644 index 0000000..c88b918 --- /dev/null +++ b/tps/fonte/tp02/TP02Q03 - Pesquisa Sequencial em Java/pub.in @@ -0,0 +1,48 @@ +9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8 +1413e1b3-2903-4a47-a2d5-e8abc5ce8014 +ca3827f0-375a-4891-aaa5-f5e8a5bad225 +36bfefd0-e0bb-4d11-be98-d1ef6117a77a +20354d7a-e4fe-47af-8ff6-187bca92f3f9 +57fe29d4-312a-4711-bd9a-c320253d9176 +b415c867-1cff-455e-b194-748662ac2cca +5a4c95db-947d-4914-a631-41e8d466328e +861c4cde-2f0f-4796-8d8f-9492e74b2573 +2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f +41cd0bbe-a943-431b-9bde-bb2cad3491a1 +2a0615de-8aa4-41e7-9504-dd875f5f3f01 +11b5ca88-64ad-41a4-9f36-317b66c290af +eaea5eb3-48a3-41c6-9ea5-c695299bab16 +0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3 +b0620914-858d-46fc-8e6d-033c565e138b +6b59be3f-e527-422d-959d-79fcdb3b24eb +fed624df-56d9-495e-9ad4-ea77000957e8 +d58e7249-19d1-40bd-a43f-1da0497fe8aa +3a0fe4df-2e40-4541-8d7f-13586f0b9294 +6afb1960-febd-418d-b604-e50c1b59459b +efa802c8-ae18-4ae1-a524-49df21d05939 +933787c2-51e3-4eac-8a85-ab332cac0456 +94c993f6-a443-4408-b908-6e78e223e8ac +00434cd3-fcc7-44c7-8f98-7368415b4206 +7614cf6e-689e-47ac-a976-b1e9997637e9 +d59691a4-f830-4eb0-a819-a0fb00b7e80f +FIM +Lord Voldemort +Bathilda Bagshot +Terry Boot +Fat Friar +Seamus Finnigan +Ellie Cattermole +Stubby Boardman +Newt Scamander +Rubeus Hagrid +Gibbon +James Potter +Mr Mason +Tom Riddle +Madam Puddifoot +Ginny Weasley +Eileen Prince +Lucian Bole +Gilderoy Lockhart +Lord Voldemort +FIM diff --git a/tps/fonte/tp02/TP02Q04 - Pesquisa Binaria em C/753045_binaria.txt b/tps/fonte/tp02/TP02Q04 - Pesquisa Binaria em C/753045_binaria.txt new file mode 100644 index 0000000..35eb761 --- /dev/null +++ b/tps/fonte/tp02/TP02Q04 - Pesquisa Binaria em C/753045_binaria.txt @@ -0,0 +1 @@ +753045 0ms 517 \ No newline at end of file diff --git a/tps/fonte/tp02/TP02Q04 - Pesquisa Binaria em C/Character b/tps/fonte/tp02/TP02Q04 - Pesquisa Binaria em C/Character new file mode 100644 index 0000000000000000000000000000000000000000..1db1309af5ada7b837974cc5da71ee13815d1175 GIT binary patch literal 27896 zcmeHwe|S{Yng2~fAfRNT6`Lw8qm~+^gg}Kt0nG#kE^Y!52%>^BLoy-J{Bkk_!HPyh z++41s`F5d=eqo>3?fT7k-QCiv6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001 diff --git a/tps/fonte/tp02/TP02Q04 - Pesquisa Binaria em C/Character.c b/tps/fonte/tp02/TP02Q04 - Pesquisa Binaria em C/Character.c new file mode 100644 index 0000000..d1ccb07 --- /dev/null +++ b/tps/fonte/tp02/TP02Q04 - Pesquisa Binaria em C/Character.c @@ -0,0 +1,737 @@ +/** + * @path TP02Q02 - Classe em C/Character.c + * @description C file that implements the Character class with binary search algorithm + * @author Pedro Lopes - github.com/httpspedroh + * @version 1.0 + * @update 2024-04-09 + */ + +// ---------------------------------------------------------------------------------------------------- // + +// Includes +#include +#include +#include +#include +#include + +// ---------------------------------------------------------------------------------------------------- // + +// Constants +#define MAX_CHARACTERS 405 +#define FILE_PATH "/tmp/characters.csv" + +#define MAX_UUID_SIZE 37 +#define MAX_NAME_SIZE 30 +#define MAX_ALTERNATE_NAMES 10 +#define MAX_ALTERNATE_NAME_SIZE 35 +#define MAX_HOUSE_SIZE 15 +#define MAX_ANCESTRY_SIZE 15 +#define MAX_SPECIES_SIZE 20 +#define MAX_PATRONUS_SIZE 25 +#define MAX_ACTOR_NAME_SIZE 35 +#define MAX_EYE_COLOUR_SIZE 10 +#define MAX_GENDER_SIZE 10 +#define MAX_HAIR_COLOUR_SIZE 10 + +#define MAX_LINE_SIZE 300 + +// ---------------------------------------------------------------------------------------------------- // + +// Structs +typedef struct Date { + + int day; + int month; + int year; +} Date; + +typedef struct Character { + + char *id; + char *name; + char *alternateNames[MAX_ALTERNATE_NAMES]; + char *house; + char *ancestry; + char *species; + char *patronus; + bool hogwartsStaff; + bool hogwartsStudent; + char *actorName; + bool alive; + Date birthDate; + int yearOfBirth; + char *eyeColour; + char *gender; + char *hairColour; + bool wizard; +} Character; + +// ---------------------------------------------------------------------------------------------------- // + +// Global variables +Character characters[MAX_CHARACTERS]; +int charactersLength = 0; + +// ---------------------------------------------------------------------------------------------------- // + +// Functions +bool isEnd(char* line) { return line[0] == 'F' && line[1] == 'I' && line[2] == 'M'; } + +void substring(char *string, char *stringStart, int length) { + + strncpy(string, stringStart, length); + string[length] = '\0'; +} + +void proccess_attribute(char *attribute, char **substringStart, char **substringEnd, bool isFirstAttribute, bool isStringArray) { + + // Skip first comma + if(!isFirstAttribute) { + + if(*substringEnd != NULL) *substringStart = *substringEnd + 1; + else *substringStart = *substringEnd; + } + + if(!isStringArray) { + + if((*substringStart)[0] == '"') { + + *substringStart = *substringStart + 1; + *substringEnd = strchr(*substringStart, '"'); + } + else *substringEnd = strchr(*substringStart, ';'); + + // Get substring + if(*substringEnd) { + + substring(attribute, *substringStart, *substringEnd - *substringStart); + + if(*substringEnd[0] == '"') *substringEnd = *substringEnd + 1; + } + else strcpy(attribute, *substringStart); + + // Set default value if attribute is empty + if(strcmp(attribute, "") == 0 || attribute[0] == '\n' || attribute[0] == '\r' || attribute[0] == '\0') strcpy(attribute, "N/A"); + + // Clean \n from the end of the string + if(attribute[strlen(attribute) - 1] == '\n' || attribute[strlen(attribute) - 1] == '\r') attribute[strlen(attribute) - 1] = '\0'; + } + else { + + // Check if the first character is a [ + if((*substringStart)[0] == '[') { + + *substringStart = *substringStart + 1; + + if((*substringStart)[0] == ']') strcpy(attribute, ""); // Case: [] + else { + + char *tempConcat = (char *) calloc(MAX_LINE_SIZE, sizeof(char)); + + *substringStart = *substringStart - 1; + + while(1) { + + *substringStart = *substringStart + 1; + + if((*substringStart)[0] == ';') break; + else if((*substringStart)[0] == '\'') { // Case: "['example', 'example']" + + *substringStart = *substringStart + 1; + *substringEnd = strchr(*substringStart, '\''); + + // Get substring + if(*substringEnd) { + + // Create tmp name + char tmp[MAX_LINE_SIZE]; + substring(tmp, *substringStart, *substringEnd - *substringStart); + + // Concat tempConcat with tmp + strcat(tempConcat, tmp); + strcat(tempConcat, ", "); + + *substringStart = *substringEnd + 1; + } + } + } + + // Get substring + strcpy(attribute, tempConcat); + + // Clean "attribute" removing last 2 characters + attribute[strlen(attribute) - 2] = '\0'; + } + } + + *substringEnd = strchr(*substringStart, ';'); + } +} + +// ---------------------------------------------------------------------------------------------------- // + +// Methods implementations + +// Gets +char *character_getId(Character *character) { return character -> id; } +char *character_getName(Character *character) { return character -> name; } +char *character_getHouse(Character *character) { return character -> house; } +char *character_getAncestry(Character *character) { return character -> ancestry; } +char *character_getSpecies(Character *character) { return character -> species; } +char *character_getPatronus(Character *character) { return character -> patronus; } +bool character_getHogwartsStaff(Character *character) { return character -> hogwartsStaff; } +bool character_getHogwartsStudent(Character *character) { return character -> hogwartsStudent; } +char *character_getActorName(Character *character) { return character -> actorName; } +bool character_getAlive(Character *character) { return character -> alive; } +char *character_getEyeColour(Character *character) { return character -> eyeColour; } +char *character_getGender(Character *character) { return character -> gender; } +char *character_getHairColour(Character *character) { return character -> hairColour; } +bool character_getWizard(Character *character) { return character -> wizard; } + +char *character_getYearOfBirth(Character *character) { + + // "N/A" if yearOfBirth is -1 + char *yearOfBirth = (char *) calloc(15, sizeof(char)); + strcpy(yearOfBirth, "N/A"); + + if(character -> yearOfBirth != -1) sprintf(yearOfBirth, "%d", character -> yearOfBirth); + return yearOfBirth; +} + +char *character_getBirthDate(Character *character) { + + // "N/A" if day, month and year are -1 + char *birthDate = (char *) calloc(15, sizeof(char)); + strcpy(birthDate, "N/A"); + + if(character -> birthDate.day != -1 && character -> birthDate.month != -1 && character -> birthDate.year != -1) { + + sprintf(birthDate, "%02d-%02d-%04d", character -> birthDate.day, character -> birthDate.month, character -> birthDate.year); + } + + return birthDate; +} + +char *character_getAlternateNames(Character *character) { + + // Concatenate all alternate names with a comma + char *alternateNames = (char *) calloc(MAX_ALTERNATE_NAME_SIZE * MAX_ALTERNATE_NAMES, sizeof(char)); + + alternateNames[0] = '{'; + + for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) { + + if(strcmp(character -> alternateNames[i], "") != 0) { + + strcat(alternateNames, character -> alternateNames[i]); + + if(strcmp(character -> alternateNames[i + 1], "") != 0) strcat(alternateNames, ", "); + } + } + + strcat(alternateNames, "}"); + + return alternateNames; +} + +// Sets +void character_setId(Character *character, char *id) { strcpy(character -> id, id); } +void character_setName(Character *character, char *name) { strcpy(character -> name, name); } +void character_setHouse(Character *character, char *house) { strcpy(character -> house, house); } +void character_setAncestry(Character *character, char *ancestry) { strcpy(character -> ancestry, ancestry); } +void character_setSpecies(Character *character, char *species) { strcpy(character -> species, species); } +void character_setPatronus(Character *character, char *patronus) { strcpy(character -> patronus, patronus); } +void character_setHogwartsStaff(Character *character, bool hogwartsStaff) { character -> hogwartsStaff = hogwartsStaff; } +void character_setHogwartsStudent(Character *character, bool hogwartsStudent) { character -> hogwartsStudent = hogwartsStudent; } +void character_setActorName(Character *character, char *actorName) { strcpy(character -> actorName, actorName); } +void character_setAlive(Character *character, bool alive) { character -> alive = alive; } +void character_setYearOfBirth(Character *character, int yearOfBirth) { character -> yearOfBirth = yearOfBirth; } +void character_setEyeColour(Character *character, char *eyeColour) { strcpy(character -> eyeColour, eyeColour); } +void character_setGender(Character *character, char *gender) { strcpy(character -> gender, gender); } +void character_setHairColour(Character *character, char *hairColour) { strcpy(character -> hairColour, hairColour); } +void character_setWizard(Character *character, bool wizard) { character -> wizard = wizard; } + +void character_setBirthDate(Character *character, char *birthDate) { + + // Explode birthDate in format DD-MM-YYYY if in format DD-MM-YYYY + if(strlen(birthDate) >= 8 && strlen(birthDate) <= 10) { + + char *token = strtok(birthDate, "-"); + + character -> birthDate.day = atoi(token); + token = strtok(NULL, "-"); + character -> birthDate.month = atoi(token); + token = strtok(NULL, "-"); + character -> birthDate.year = atoi(token); + } +} + +void character_setAlternateNames(Character *character, char *alternateNames) { + + // Copy names to a temporary variable + char tempNames[MAX_ALTERNATE_NAME_SIZE * MAX_ALTERNATE_NAMES]; + strcpy(tempNames, alternateNames); + + // Separate names by comma + char *token = strtok(tempNames, ","); + int i = 0; + + // Copy names to the character + while (token != NULL && i < MAX_ALTERNATE_NAMES) { + + while (*token == ' ') token++; + + int len = strlen(token); + + while (len > 0 && token[len - 1] == ' ') { + + token[len - 1] = '\0'; + len--; + } + + strcpy(character -> alternateNames[i++], token); + token = strtok(NULL, ","); + } +} + +// Class +Character character_newBlank() { + + Character character; + + character.id = (char *) calloc(MAX_UUID_SIZE, sizeof(char)); + strcpy(character.id, ""); + + character.name = (char *) calloc(MAX_NAME_SIZE, sizeof(char)); + strcpy(character.name, ""); + + for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) { + + character.alternateNames[i] = (char *) calloc(MAX_ALTERNATE_NAME_SIZE, sizeof(char)); + strcpy(character.alternateNames[i], ""); + } + + character.house = (char *) calloc(MAX_HOUSE_SIZE, sizeof(char)); + strcpy(character.house, ""); + + character.ancestry = (char *) calloc(MAX_ANCESTRY_SIZE, sizeof(char)); + strcpy(character.ancestry, ""); + + character.species = (char *) calloc(MAX_SPECIES_SIZE, sizeof(char)); + strcpy(character.species, ""); + + character.patronus = (char *) calloc(MAX_PATRONUS_SIZE, sizeof(char)); + strcpy(character.patronus, ""); + + character.hogwartsStaff = false; + character.hogwartsStudent = false; + + character.actorName = (char *) calloc(MAX_ACTOR_NAME_SIZE, sizeof(char)); + strcpy(character.actorName, ""); + + character.alive = false; + + character.birthDate.day = -1; + character.birthDate.month = -1; + character.birthDate.year = -1; + + character.yearOfBirth = -1; + + character.eyeColour = (char *) calloc(MAX_EYE_COLOUR_SIZE, sizeof(char)); + strcpy(character.eyeColour, ""); + + character.gender = (char *) calloc(MAX_GENDER_SIZE, sizeof(char)); + strcpy(character.gender, ""); + + character.hairColour = (char *) calloc(MAX_HAIR_COLOUR_SIZE, sizeof(char)); + strcpy(character.hairColour, ""); + + character.wizard = false; + + return character; +} + +Character character_new(char *id, char *name, char *house, char *ancestry, char *species, char *patronus, bool hogwartsStaff, bool hogwartsStudent, char *actorName, bool alive, Date birthDate, int yearOfBirth, char *eyeColour, char *gender, char *hairColour, bool wizard) { + + Character character; + + character.id = (char *) calloc(MAX_UUID_SIZE, sizeof(char)); + strcpy(character.id, id); + + character.name = (char *) calloc(MAX_NAME_SIZE, sizeof(char)); + strcpy(character.name, name); + + for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) { + + character.alternateNames[i] = (char *) calloc(MAX_ALTERNATE_NAME_SIZE, sizeof(char)); + strcpy(character.alternateNames[i], ""); + } + + character.house = (char *) calloc(MAX_HOUSE_SIZE, sizeof(char)); + strcpy(character.house, house); + + character.ancestry = (char *) calloc(MAX_ANCESTRY_SIZE, sizeof(char)); + strcpy(character.ancestry, ancestry); + + character.species = (char *) calloc(MAX_SPECIES_SIZE, sizeof(char)); + strcpy(character.species, species); + + character.patronus = (char *) calloc(MAX_PATRONUS_SIZE, sizeof(char)); + strcpy(character.patronus, patronus); + + character.hogwartsStaff = hogwartsStaff; + character.hogwartsStudent = hogwartsStudent; + + character.actorName = (char *) calloc(MAX_ACTOR_NAME_SIZE, sizeof(char)); + strcpy(character.actorName, actorName); + + character.alive = alive; + character.birthDate = birthDate; + character.yearOfBirth = yearOfBirth; + + character.eyeColour = (char *) calloc(MAX_EYE_COLOUR_SIZE, sizeof(char)); + strcpy(character.eyeColour, eyeColour); + + character.gender = (char *) calloc(MAX_GENDER_SIZE, sizeof(char)); + strcpy(character.gender, gender); + + character.hairColour = (char *) calloc(MAX_HAIR_COLOUR_SIZE, sizeof(char)); + strcpy(character.hairColour, hairColour); + + character.wizard = wizard; + + return character; +} + +Character *character_clone(Character *character) { + + Character *clone = (Character *) malloc(sizeof(Character)); + + clone -> id = (char *) calloc(MAX_UUID_SIZE, sizeof(char)); + strcpy(clone -> id, character -> id); + + clone -> name = (char *) calloc(MAX_NAME_SIZE, sizeof(char)); + strcpy(clone -> name, character -> name); + + for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) { + + clone -> alternateNames[i] = (char *) calloc(MAX_ALTERNATE_NAME_SIZE, sizeof(char)); + strcpy(clone -> alternateNames[i], character -> alternateNames[i]); + } + + clone -> house = (char *) calloc(MAX_HOUSE_SIZE, sizeof(char)); + strcpy(clone -> house, character -> house); + + clone -> ancestry = (char *) calloc(MAX_ANCESTRY_SIZE, sizeof(char)); + strcpy(clone -> ancestry, character -> ancestry); + + clone -> species = (char *) calloc(MAX_SPECIES_SIZE, sizeof(char)); + strcpy(clone -> species, character -> species); + + clone -> patronus = (char *) calloc(MAX_PATRONUS_SIZE, sizeof(char)); + strcpy(clone -> patronus, character -> patronus); + + clone -> hogwartsStaff = character -> hogwartsStaff; + clone -> hogwartsStudent = character -> hogwartsStudent; + + clone -> actorName = (char *) calloc(MAX_ACTOR_NAME_SIZE, sizeof(char)); + strcpy(clone -> actorName, character -> actorName); + + clone -> alive = character -> alive; + clone -> birthDate = character -> birthDate; + clone -> yearOfBirth = character -> yearOfBirth; + + clone -> eyeColour = (char *) calloc(MAX_EYE_COLOUR_SIZE, sizeof(char)); + strcpy(clone -> eyeColour, character -> eyeColour); + + clone -> gender = (char *) calloc(MAX_GENDER_SIZE, sizeof(char)); + strcpy(clone -> gender, character -> gender); + + clone -> hairColour = (char *) calloc(MAX_HAIR_COLOUR_SIZE, sizeof(char)); + strcpy(clone -> hairColour, character -> hairColour); + + clone -> wizard = character -> wizard; + + return clone; +} + +void character_print(Character *character) { + + printf("[%s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s]\n", + + character_getId(character), + character_getName(character), + character_getAlternateNames(character), + character_getHouse(character), + character_getAncestry(character), + character_getSpecies(character), + character_getPatronus(character), + character_getHogwartsStaff(character) ? "true" : "false", + character_getHogwartsStudent(character) ? "true" : "false", + character_getActorName(character), + character_getAlive(character) ? "true" : "false", + character_getBirthDate(character), + character_getYearOfBirth(character), + character_getEyeColour(character), + character_getGender(character), + character_getHairColour(character), + character_getWizard(character) ? "true" : "false" + ); +} + +Character character_read(char *line) { + + Character character = character_newBlank(); + + char *substringStart = line; + char *substringEnd = NULL; + char attribute[MAX_LINE_SIZE]; + + // Get id + proccess_attribute(attribute, &substringStart, &substringEnd, true, false); + character_setId(&character, attribute); + + // Get name + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setName(&character, attribute); + + // Get alternate names + proccess_attribute(attribute, &substringStart, &substringEnd, false, true); + character_setAlternateNames(&character, attribute); + + // Get house + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setHouse(&character, attribute); + + // Get ancestry + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setAncestry(&character, attribute); + + // Get species + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setSpecies(&character, attribute); + + // Get patronus + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setPatronus(&character, attribute); + + // Get hogwarts staff + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setHogwartsStaff(&character, strcmp(attribute, "VERDADEIRO") == 0); + + // Get hogwarts student + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setHogwartsStudent(&character, strcmp(attribute, "VERDADEIRO") == 0); + + // Get actor name + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setActorName(&character, attribute); + + // Get alive + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setAlive(&character, strcmp(attribute, "VERDADEIRO") == 0); + + // Get birth date + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setBirthDate(&character, attribute); + + // Get year of birth + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setYearOfBirth(&character, atoi(attribute) == 0 ? -1 : atoi(attribute)); + + // Get eye colour + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setEyeColour(&character, attribute); + + // Get gender + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setGender(&character, attribute); + + // Get hair colour + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setHairColour(&character, attribute); + + // Get wizard + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + + // Clean \n from the end of the string + if(attribute[strlen(attribute) - 1] == '\n' || attribute[strlen(attribute) - 1] == '\r') attribute[strlen(attribute) - 1] = '\0'; + + character_setWizard(&character, strcmp(attribute, "VERDADEIRO") == 0); + return character; +} + +Character *character_searchById(char *id) { + + for(int i = 0; i < charactersLength; i++) { + + if(!strcmp(characters[i].id, id)) return &characters[i]; + } + return NULL; +} + +// General +void startCharacters() { + + // Open file + FILE *fp; + char *line = NULL; + size_t len = 0; + ssize_t read; + + fp = fopen(FILE_PATH, "r"); + + if(fp == NULL) { + + perror("x Error opening file"); + exit(EXIT_FAILURE); + } + + // Skip first line + getline(&line, &len, fp); + + // Read all lines + while((read = getline(&line, &len, fp)) != -1) { + + // Read character from line + Character character = character_read(line); + + characters[charactersLength++] = character; + + if(charactersLength >= MAX_CHARACTERS) { + + perror("x Max characters reached"); + exit(EXIT_FAILURE); + } + } + + // Close file and free memory + fclose(fp); + + if(line) free(line); +} + +// ---------------------------------------------------------------------------------------------------- // + +// Main +int main() { + + // ----------------------------------------------------------------- // + + // #1 - Start - Read all characters in CSV file + startCharacters(); + + // ----------------------------------------------------------------- // + + // #2 - Read input and print characters from pub.in id entries and add to mainCharacters array + + // Initialize mainCharacters array + Character mainCharacters[MAX_CHARACTERS]; + int m = 0; + + char id[MAX_UUID_SIZE]; + scanf(" %[^\n]s", id); + + while(true) { + + // Clean \n from the end of the string + if(id[strlen(id) - 1] == '\n' || id[strlen(id) - 1] == '\r') id[strlen(id) - 1] = '\0'; + + // ------------------------- // + + if(isEnd(id)) break; + else { + + Character *character = character_searchById(id); + + if(character) mainCharacters[m++] = *character; + + // ------------------------- // + + scanf(" %[^\n]s", id); + } + } + + // ----------------------------------------------------------------- // + + // #3 - Read input and search for characters from pub.in name entries in mainCharacters array + + // Start benchmark + clock_t startTime = clock(); + int comparisons = 0; + + // Order mainCharacters array by name asc + for(int i = 0; i < m; i++) { + + for(int j = i + 1; j < m; j++) { + + comparisons++; + + if(strcmp(character_getName(&mainCharacters[i]), character_getName(&mainCharacters[j])) > 0) { + + Character aux = mainCharacters[i]; + mainCharacters[i] = mainCharacters[j]; + mainCharacters[j] = aux; + } + } + } + + // ----------------- // + + // Read input + char name[MAX_NAME_SIZE]; + scanf(" %[^\n]s", name); + + while(true) { + + // Clean \n from the end of the string + if(id[strlen(name) - 1] == '\n' || name[strlen(name) - 1] == '\r') name[strlen(name) - 1] = '\0'; + + // ------------------------- // + + if(isEnd(name)) break; + else { + + // Search for character + Character *foundCharacter = NULL; + + // Binary search + int left = 0; + int right = m - 1; + + while(left <= right) { + + int mid = left + (right - left) / 2; + + comparisons += 2; + + if(strcmp(character_getName(&mainCharacters[mid]), name) == 0) { + + foundCharacter = &mainCharacters[mid]; + break; + } + else if(strcmp(character_getName(&mainCharacters[mid]), name) < 0) left = mid + 1; + else right = mid - 1; + } + + // Print result + printf("%s\n", foundCharacter == NULL ? "NAO" : "SIM"); + + // ------------------------- // + + scanf(" %[^\n]s", name); + } + } + + // ----------------- // + + // Save benchmark in file + FILE *fp = fopen("753045_binaria.txt", "w"); + fprintf(fp, "753045\t%.0fms\t%d", (double)(clock() - startTime) / CLOCKS_PER_SEC * 1000.0, comparisons); + fclose(fp); + + // ----------------- // + + return 0; +} \ No newline at end of file diff --git a/tps/fonte/tp02/TP02Q04 - Pesquisa Binaria em C/pedro.out b/tps/fonte/tp02/TP02Q04 - Pesquisa Binaria em C/pedro.out new file mode 100644 index 0000000..8ce90f3 --- /dev/null +++ b/tps/fonte/tp02/TP02Q04 - Pesquisa Binaria em C/pedro.out @@ -0,0 +1,19 @@ +SIM +NAO +NAO +NAO +SIM +NAO +NAO +NAO +SIM +NAO +NAO +NAO +SIM +NAO +NAO +NAO +NAO +NAO +SIM diff --git a/tps/fonte/tp02/TP02Q04 - Pesquisa Binaria em C/pub.in b/tps/fonte/tp02/TP02Q04 - Pesquisa Binaria em C/pub.in new file mode 100644 index 0000000..c88b918 --- /dev/null +++ b/tps/fonte/tp02/TP02Q04 - Pesquisa Binaria em C/pub.in @@ -0,0 +1,48 @@ +9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8 +1413e1b3-2903-4a47-a2d5-e8abc5ce8014 +ca3827f0-375a-4891-aaa5-f5e8a5bad225 +36bfefd0-e0bb-4d11-be98-d1ef6117a77a +20354d7a-e4fe-47af-8ff6-187bca92f3f9 +57fe29d4-312a-4711-bd9a-c320253d9176 +b415c867-1cff-455e-b194-748662ac2cca +5a4c95db-947d-4914-a631-41e8d466328e +861c4cde-2f0f-4796-8d8f-9492e74b2573 +2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f +41cd0bbe-a943-431b-9bde-bb2cad3491a1 +2a0615de-8aa4-41e7-9504-dd875f5f3f01 +11b5ca88-64ad-41a4-9f36-317b66c290af +eaea5eb3-48a3-41c6-9ea5-c695299bab16 +0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3 +b0620914-858d-46fc-8e6d-033c565e138b +6b59be3f-e527-422d-959d-79fcdb3b24eb +fed624df-56d9-495e-9ad4-ea77000957e8 +d58e7249-19d1-40bd-a43f-1da0497fe8aa +3a0fe4df-2e40-4541-8d7f-13586f0b9294 +6afb1960-febd-418d-b604-e50c1b59459b +efa802c8-ae18-4ae1-a524-49df21d05939 +933787c2-51e3-4eac-8a85-ab332cac0456 +94c993f6-a443-4408-b908-6e78e223e8ac +00434cd3-fcc7-44c7-8f98-7368415b4206 +7614cf6e-689e-47ac-a976-b1e9997637e9 +d59691a4-f830-4eb0-a819-a0fb00b7e80f +FIM +Lord Voldemort +Bathilda Bagshot +Terry Boot +Fat Friar +Seamus Finnigan +Ellie Cattermole +Stubby Boardman +Newt Scamander +Rubeus Hagrid +Gibbon +James Potter +Mr Mason +Tom Riddle +Madam Puddifoot +Ginny Weasley +Eileen Prince +Lucian Bole +Gilderoy Lockhart +Lord Voldemort +FIM diff --git a/tps/fonte/tp02/TP02Q05 - Ordenação por Seleção em Java/753045_selecao.txt b/tps/fonte/tp02/TP02Q05 - Ordenação por Seleção em Java/753045_selecao.txt new file mode 100644 index 0000000..3c4391b --- /dev/null +++ b/tps/fonte/tp02/TP02Q05 - Ordenação por Seleção em Java/753045_selecao.txt @@ -0,0 +1 @@ +753045 24ms 351 \ No newline at end of file diff --git a/tps/fonte/tp02/TP02Q05 - Ordenação por Seleção em Java/Arq.java b/tps/fonte/tp02/TP02Q05 - Ordenação por Seleção em Java/Arq.java new file mode 100644 index 0000000..9a632d1 --- /dev/null +++ b/tps/fonte/tp02/TP02Q05 - Ordenação por Seleção em Java/Arq.java @@ -0,0 +1,206 @@ +import java.io.*; +import java.util.Formatter; +import java.util.Scanner; +import java.io.File; +import java.nio.charset.*; + +public class Arq +{ + private static String nomeArquivo = ""; + private static String charsetArquivo = "ISO-8859-1"; + private static boolean write = false, read = false; + private static Formatter saida = null; + private static Scanner entrada = null; + + public static boolean openWrite(String nomeArq, String charset) { + boolean resp = false; + close(); + try{ + saida = new Formatter(nomeArq, charset); + nomeArquivo = nomeArq; + resp = write = true; + } catch (Exception e) {} + return resp; + } + + public static boolean openWrite(String nomeArq) { + return openWrite(nomeArq, charsetArquivo); + } + + public static boolean openWriteClose(String nomeArq, String charset, String conteudo) { + boolean resp = openWrite(nomeArq, charset); + if(resp == true){ + println(conteudo); + close(); + } + return resp; + } + + public static boolean openWriteClose(String nomeArq, String conteudo) { + return openWriteClose(nomeArq, charsetArquivo, conteudo); + } + + public static boolean openRead(String nomeArq) { + return openRead(nomeArq, charsetArquivo); + } + + public static boolean openRead(String nomeArq, String charset) { + boolean resp = false; + close(); + try{ + entrada = new Scanner(new File(nomeArq), charset); + nomeArquivo = nomeArq; + resp = read = true; + } catch (Exception e) {} + return resp; + } + + public static String openReadClose(String nomeArq){ + openRead(nomeArq); + String resp = readAll(); + close(); + return resp; + } + + public static void close() { + if(write == true){ + saida.close(); + } + if(read == true){ + entrada.close(); + } + write = read = false; + nomeArquivo = ""; + charsetArquivo = "ISO-8859-1"; + } + + public static long length(){ + long resp = -1; + if(read != write){ + File file = new File(nomeArquivo); + resp = file.length(); + } + return resp; + } + + public static void print(int x){ + if(write == true){ + saida.format( "%d", x); + } + } + + public static void print(double x){ + if(write == true){ + saida.format( "%f", x); + } + } + + public static void print(String x){ + if(write == true){ + saida.format( "%s", x); + } + } + + public static void print(boolean x){ + if(write == true){ + saida.format( "%s", ((x) ? "true" : "false")); + } + } + + public static void print(char x){ + if(write == true){ + saida.format( "%c", x); + } + } + + public static void println(int x){ + if(write == true){ + saida.format( "%d\n", x); + } + } + + public static void println(double x){ + if(write == true){ + saida.format( "%f\n", x); + } + } + + public static void println(String x){ + if(write == true){ + saida.format( "%s\n", x); + } + } + + public static void println(boolean x){ + if(write == true){ + saida.format( "%s\n", ((x) ? "true" : "false")); + } + } + + public static void println(char x){ + if(write == true){ + saida.format( "%c\n", x); + } + } + + public static int readInt(){ + int resp = -1; + try{ + resp = entrada.nextInt(); + } catch (Exception e) {} + return resp; + } + + public static char readChar(){ + char resp = ' '; + try{ + resp = (char)entrada.nextByte(); + } catch (Exception e) {} + return resp; + } + + public static double readDouble(){ + double resp = -1; + try{ + resp = Double.parseDouble(readString().replace(",",".")); + } catch (Exception e) {} + return resp; + } + + public static String readString(){ + String resp = ""; + try{ + resp = entrada.next(); + } catch (Exception e) { System.out.println(e.getMessage()); } + return resp; + } + + public static boolean readBoolean(){ + boolean resp = false; + try{ + resp = (entrada.next().equals("true")) ? true : false; + } catch (Exception e) {} + return resp; + } + + public static String readLine(){ + String resp = ""; + try{ + resp = entrada.nextLine(); + } catch (Exception e) { System.out.println(e.getMessage()); } + return resp; + } + + + public static boolean hasNext(){ + return entrada.hasNext(); + } + + public static String readAll(){ + String resp = ""; + while(hasNext()){ + resp += (readLine() + "\n"); + } + return resp; + } +} diff --git a/tps/fonte/tp02/TP02Q05 - Ordenação por Seleção em Java/Character.java b/tps/fonte/tp02/TP02Q05 - Ordenação por Seleção em Java/Character.java new file mode 100644 index 0000000..446236c --- /dev/null +++ b/tps/fonte/tp02/TP02Q05 - Ordenação por Seleção em Java/Character.java @@ -0,0 +1,510 @@ +/** + * @path TP02Q01 - Classe em Java/Characters.java + * @description Java class of all characters from Harry Potter's saga with selection sort algorithm + * @author Pedro Lopes - github.com/httpspedroh + * @version 1.0 + * @update 2024-04-09 + */ + +// ---------------------------------------------------------------------------------------------------- // + +// Imports +import java.util.Scanner; +import java.util.UUID; +import java.io.BufferedReader; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Date; +import java.text.SimpleDateFormat; + +// ---------------------------------------------------------------------------------------------------- // + +public class Character { + + static SimpleDateFormat ddf = new SimpleDateFormat("dd-MM-yyyy"); + + // Global variables + public static final String FILE_PATH = "/tmp/characters.csv"; + public static ArrayList allCharacters = new ArrayList(); + + // -------------------------- // + + // Attributes + private UUID id; + private String name; + private ArrayList alternateNames; + private String house; + private String ancestry; + private String species; + private String patronus; + private boolean hogwartsStaff; + private boolean hogwartsStudent; + private String actorName; + private boolean alive; + private Date birthDate; + private int yearOfBirth; + private String eyeColour; + private String gender; + private String hairColour; + private boolean wizard; + + // -------------------------- // + + // Empty constructor + public Character() { + + this.id = UUID.randomUUID(); + this.name = ""; + this.alternateNames = new ArrayList(); + this.house = ""; + this.ancestry = ""; + this.species = ""; + this.patronus = ""; + this.hogwartsStaff = false; + this.hogwartsStudent = false; + this.actorName = ""; + this.alive = false; + this.birthDate = null; + this.yearOfBirth = 0; + this.eyeColour = ""; + this.gender = ""; + this.hairColour = ""; + this.wizard = false; + } + + // Constructor + public Character(UUID id, String name, ArrayList alternateNames, String house, String ancestry, String species, String patronus, boolean hogwartsStaff, boolean hogwartsStudent, String actorName, boolean alive, Date birthDate, int yearOfBirth, String eyeColour, String gender, String hairColour, boolean wizard) { + + this.id = id; + this.name = name; + this.alternateNames = alternateNames; + this.house = house; + this.ancestry = ancestry; + this.species = species; + this.patronus = patronus; + this.hogwartsStaff = hogwartsStaff; + this.hogwartsStudent = hogwartsStudent; + this.actorName = actorName; + this.alive = alive; + this.birthDate = birthDate; + this.yearOfBirth = yearOfBirth; + this.eyeColour = eyeColour; + this.gender = gender; + this.hairColour = hairColour; + this.wizard = wizard; + } + + // -------------------------- // + + // Gets + public UUID getId() { return this.id; } + public String getName() { return this.name; } + public String getHouse() { return this.house; } + public String getAncestry() { return this.ancestry; } + public String getSpecies() { return this.species; } + public String getPatronus() { return this.patronus; } + public boolean getHogwartsStaff() { return this.hogwartsStaff; } + public boolean getHogwartsStudent() { return this.hogwartsStudent; } + public String getActorName() { return this.actorName; } + public boolean getAlive() { return this.alive; } + public Date getBirthDate() { return this.birthDate; } + public int getYearOfBirth() { return this.yearOfBirth; } + public String getEyeColour() { return this.eyeColour; } + public String getGender() { return this.gender; } + public String getHairColour() { return this.hairColour; } + public boolean getWizard() { return this.wizard; } + + public String getAlternateNames() { + + // Construct string e.g. {item1, item2, item3} + String alternateNames = "{"; + + for(int i = 0; i < this.alternateNames.size(); i++) { + + alternateNames += this.alternateNames.get(i); + + if(i < this.alternateNames.size() - 1) alternateNames += ", "; + } + + alternateNames += "}"; + + return alternateNames; + } + + // Sets + public void setId(UUID id) { this.id = id; } + public void setName(String name) { this.name = name; } + public void setAlternateNames(ArrayList alternateNames) { this.alternateNames = alternateNames; } + public void setHouse(String house) { this.house = house; } + public void setAncestry(String ancestry) { this.ancestry = ancestry; } + public void setSpecies(String species) { this.species = species; } + public void setPatronus(String patronus) { this.patronus = patronus; } + public void setHogwartsStaff(boolean hogwartsStaff) { this.hogwartsStaff = hogwartsStaff; } + public void setHogwartsStudent(boolean hogwartsStudent) { this.hogwartsStudent = hogwartsStudent; } + public void setActorNane(String actorName) { this.actorName = actorName; } + public void setAlive(boolean alive) { this.alive = alive; } + public void setBirthDate(Date birthDate) { this.birthDate = birthDate; } + public void setYearOfBirth(int yearOfBirth) { this.yearOfBirth = yearOfBirth; } + public void setEyeColour(String eyeColour) { this.eyeColour = eyeColour; } + public void setGender(String gender) { this.gender = gender; } + public void setHairColour(String hairColour) { this.hairColour = hairColour; } + public void setWizard(boolean wizard) { this.wizard = wizard; } + + // -------------------------- // + + // Clone + public Character clone() { return new Character(this.id, this.name, this.alternateNames, this.house, this.ancestry, this.species, this.patronus, this.hogwartsStaff, this.hogwartsStudent, this.actorName, this.alive, this.birthDate, this.yearOfBirth, this.eyeColour, this.gender, this.hairColour, this.wizard); } + + // -------------------------- // + + // Print + public void print() { + + System.out.println("[" + + this.getId() + " ## " + + this.getName() + " ## " + + this.getAlternateNames() + " ## " + + (this.getHouse() == "" ? "N/A" : this.getHouse()) + " ## " + + (this.getAncestry() == "" ? "N/A" : this.getAncestry()) + " ## " + + (this.getSpecies() == "" ? "N/A" : this.getSpecies()) + " ## " + + (this.getPatronus() == "" ? "N/A" : this.getPatronus()) + " ## " + + (this.getHogwartsStaff() ? "true" : "false") + " ## " + + (this.getHogwartsStudent() ? "true" : "false") + " ## " + + (this.getActorName() == "" ? "N/A" : this.getActorName()) + " ## " + + (this.getAlive() ? "true" : "false") + " ## " + + (this.getBirthDate() == null ? "N/A" : ddf.format(this.getBirthDate())) + " ## " + + (this.getYearOfBirth() == 0 ? "N/A" : this.getYearOfBirth()) + " ## " + + (this.getEyeColour() == "" ? "N/A" : this.getEyeColour()) + " ## " + + (this.getGender() == "" ? "N/A" : this.getGender()) + " ## " + + (this.getHairColour() == "" ? "N/A" : this.getHairColour()) + " ## " + + (this.getWizard() ? "true" : "false") + "]"); + } + + // -------------------------- // + + public static ArrayList extractNames(String input) { + + ArrayList names = new ArrayList<>(); + + // Clean input + String cleanedInput = input.substring(1, input.length() - 1); + + // Check if input contains double quotes + if (cleanedInput.contains("\"\"")) { + + String[] parts = cleanedInput.split("\", "); + + for (String part : parts) names.add(part.replace("\"", "").replace("'", "")); + } + else { + + // Split input by comma and space + String[] parts = cleanedInput.split(", "); + + for (String part : parts) names.add(part.replace("'", "")); + } + return names; + } + + // -------------------------- // + + // Read + public void read(String line) { + + // ---------------------- // + + // Start position + int positionStart = 0; + int positionEnd = 0; + + // ---------------------- // + + // Set id + positionEnd = line.indexOf(";", positionStart); + this.setId(UUID.fromString(line.substring(positionStart, positionEnd))); + + // ---------------------- // + + // Set name + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setName(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set alternate names + positionStart = positionEnd + 1; + + if(line.charAt(positionStart) == '[') { // If first character is '[', has one or no names + + if(line.charAt(positionStart + 1) == ']') { + + positionEnd = positionStart + 2; + + this.setAlternateNames(new ArrayList()); + } + else { + + positionEnd = line.indexOf("];", positionStart); + + this.setAlternateNames(extractNames(line.substring(positionStart, positionEnd++))); + } + } + + // ---------------------- // + + // Set house + positionStart = positionEnd; + positionEnd = line.indexOf(";", ++positionStart); + + this.setHouse(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set ancestry + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setAncestry(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set species + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setSpecies(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set patronus + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setPatronus(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set hogwartsStaff + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setHogwartsStaff("VERDADEIRO".equals(line.substring(positionStart, positionEnd))); + + // ---------------------- // + + // Set hogwartsStudent + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setHogwartsStudent("VERDADEIRO".equals(line.substring(positionStart, positionEnd))); + + // ---------------------- // + + // Set actor name + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setActorNane(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set alive + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setAlive("VERDADEIRO".equals(line.substring(positionStart, positionEnd))); + + // ---------------------- // + + // Set date of birth + positionStart = positionEnd; + positionEnd = line.indexOf(";", ++positionStart); + + try { this.setBirthDate(ddf.parse(line.substring(positionStart, positionEnd))); } + catch(Exception e) { this.setBirthDate(null); } + + // ---------------------- // + + // Set year of birth + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + try { this.setYearOfBirth(Integer.parseInt(line.substring(positionStart, positionEnd))); } + catch(Exception e) { this.setYearOfBirth(0); } + + // ---------------------- // + + // Set eye colour + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setEyeColour(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set gender + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setGender(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set hair colour + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setHairColour(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set wizard + positionStart = positionEnd + 1; + + this.setWizard("VERDADEIRO".equals(line.substring(positionStart, line.length()))); + + // ---------------------- // + } + + // ---------------------------------------------------------------------------------------------------- // + + // Read all characters function + public static void startCharacters() { + + // Initialize variables + try { + + FileInputStream fstream = new FileInputStream(FILE_PATH); + BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); + + // ---------------------- // + + // Explode CSV file + String line = br.readLine(); + + while((line = br.readLine()) != null) { + + // Initialize character + Character character = new Character(); + + // Read line + character.read(line); + + // Add character to array + allCharacters.add(character); + } + + // Close CSV file + fstream.close(); + } + catch(IOException e) { e.printStackTrace(); } + } + + // ---------------------------------------------------------------------------------------------------- // + + // Search by id function + public static Character searchById(UUID id, ArrayList characters) { + + // Search for character + for(int i = 0; i < characters.size(); i++) { + + if(characters.get(i).getId().equals(id)) return characters.get(i); + } + return null; + } + + // ---------------------------------------------------------------------------------------------------- // + + public static void main(String[] args) { + + // ----------------------------------------------------------------- // + + // #1 - Start - Read all characters in CSV file + startCharacters(); + + // ----------------------------------------------------------------- // + + // #2 - Read input and print characters from pub.in id entries and add to mainCharacters array + + // Initialize mainCharacters array + ArrayList mainCharacters = new ArrayList(); + + // Initialize scanner + Scanner inScanner = new Scanner(System.in); + + // Initialize character + Character character = new Character(); + + // Read first line + String line = inScanner.nextLine(); + + // While line is not "FIM" + while(!line.equals("FIM")) { + + // Get id + UUID id = UUID.fromString(line); + + // Search for character + character = searchById(id, allCharacters); + + // Add character to array + if(character != null) mainCharacters.add(character); + + // Read line + line = inScanner.nextLine(); + } + + // ----------------------------------------------------------------- // + + // #3 - Order mainCharacter array by key "name" using selection sort + + // Start benchmark + long startTime = System.currentTimeMillis(); + int comparisons = 0; + + // Selection sort + for(int i = 0; i < mainCharacters.size() - 1; i++) { + + // Initialize min + int min = i; + + // Search for min + for(int j = i + 1; j < mainCharacters.size(); j++) { + + // Compare + if(mainCharacters.get(j).getName().compareTo(mainCharacters.get(min).getName()) < 0) min = j; + + // Increment comparisons + comparisons++; + } + + // Swap + Character temp = mainCharacters.get(i); + mainCharacters.set(i, mainCharacters.get(min)); + mainCharacters.set(min, temp); + } + + // ----------------- // + + // Save benchmark in file + Arq.openWrite("753045_selecao.txt"); + Arq.print("753045\t" + (System.currentTimeMillis() - startTime) + "ms\t" + comparisons); + Arq.close(); + + // ----------------- // + + // Print mainCharacters array + for(int i = 0; i < mainCharacters.size(); i++) mainCharacters.get(i).print(); + + // ----------------------------------------------------------------- // + + // Close scanner + inScanner.close(); + } +} + +// ---------------------------------------------------------------------------------------------------- // \ No newline at end of file diff --git a/tps/fonte/tp02/TP02Q05 - Ordenação por Seleção em Java/pedro.out b/tps/fonte/tp02/TP02Q05 - Ordenação por Seleção em Java/pedro.out new file mode 100644 index 0000000..bf1a0c1 --- /dev/null +++ b/tps/fonte/tp02/TP02Q05 - Ordenação por Seleção em Java/pedro.out @@ -0,0 +1,27 @@ +[94c993f6-a443-4408-b908-6e78e223e8ac ## Aberforth Dumbledore ## {} ## Gryffindor ## half-blood ## human ## goat ## false ## false ## Jim McManus ## true ## 10-03-1960 ## 1960 ## blue ## male ## grey ## true] +[b415c867-1cff-455e-b194-748662ac2cca ## Albus Dumbledore ## {Professor Dumbledore} ## Gryffindor ## half-blood ## human ## Phoenix ## true ## false ## Richard Harris ## false ## 27-03-1960 ## 1881 ## blue ## male ## silver ## true] +[6afb1960-febd-418d-b604-e50c1b59459b ## Bellatrix Lestrange ## {Bella} ## Slytherin ## pure-blood ## human ## N/A ## false ## false ## Helena Bonham Carter ## false ## 06-02-1950 ## 1951 ## brown ## female ## black ## true] +[0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3 ## Dobby ## {} ## Slytherin ## pure-blood ## house-elf ## N/A ## false ## false ## Toby Jones ## false ## 06-02-1950 ## 1950 ## green ## male ## blond ## false] +[d58e7249-19d1-40bd-a43f-1da0497fe8aa ## Dolores Umbridge ## {} ## Slytherin ## half-blood ## human ## persian cat ## true ## false ## Imelda Staunton ## true ## 31-12-1926 ## 1926 ## brown ## female ## grey ## true] +[7614cf6e-689e-47ac-a976-b1e9997637e9 ## Dudley Dursley ## {} ## Slytherin ## muggle ## human ## N/A ## false ## false ## Harry Melling ## true ## 23-06-1980 ## 1980 ## blue ## male ## blond ## false] +[d59691a4-f830-4eb0-a819-a0fb00b7e80f ## Garrick Ollivander ## {} ## Hufflepuff ## muggle ## human ## N/A ## false ## false ## John Hurt ## true ## 04-10-1925 ## 1954 ## pale, silvery ## male ## brown ## true] +[2a0615de-8aa4-41e7-9504-dd875f5f3f01 ## George Weasley ## {} ## Gryffindor ## pure-blood ## human ## N/A ## false ## true ## Oliver Phelps ## true ## 13-02-1981 ## 1954 ## amber ## male ## red ## true] +[9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8 ## Harry Potter ## {The Boy Who Lived, The Chosen One, Undesirable No. 1, Potty} ## Gryffindor ## half-blood ## human ## stag ## false ## true ## Daniel Radcliffe ## true ## 31-07-1980 ## 1980 ## green ## male ## black ## true] +[6b59be3f-e527-422d-959d-79fcdb3b24eb ## Hedwig ## {} ## Hufflepuff ## muggle ## owl ## N/A ## false ## false ## Gizmo ## false ## 07-04-1979 ## 1954 ## amber ## female ## brown ## false] +[5a4c95db-947d-4914-a631-41e8d466328e ## Kendra Dumbledore ## {} ## Slytherin ## muggleborn ## human ## N/A ## false ## false ## N/A ## false ## 31-12-1926 ## 1925 ## dark ## female ## black ## true] +[41cd0bbe-a943-431b-9bde-bb2cad3491a1 ## Lily Potter ## {} ## Gryffindor ## muggleborn ## human ## N/A ## false ## false ## Geraldine Somerville ## false ## 30-01-1960 ## 1960 ## green ## female ## blond ## true] +[eaea5eb3-48a3-41c6-9ea5-c695299bab16 ## Lisa Turpin ## {} ## Ravenclaw ## pure-blood ## human ## N/A ## false ## true ## N/A ## true ## 27-03-1960 ## 1951 ## amber ## female ## sandy ## true] +[efa802c8-ae18-4ae1-a524-49df21d05939 ## Lord Voldemort ## {Tom Riddle, Tom Marvolo Riddle, You-Know-Who, He Who Must Not Be Named, the Dark Lord, the Heir of Slytherin} ## Slytherin ## half-blood ## human ## N/A ## false ## false ## Ralph Fiennes ## false ## 31-12-1926 ## 1926 ## Scarlet ## male ## bald ## true] +[861c4cde-2f0f-4796-8d8f-9492e74b2573 ## Luna Lovegood ## {Loony Lovegood} ## Ravenclaw ## pure-blood ## human ## hare ## false ## true ## Evanna Lynch ## true ## 13-02-1981 ## 1981 ## silver ## female ## blonde ## true] +[ca3827f0-375a-4891-aaa5-f5e8a5bad225 ## Minerva McGonagall ## {} ## Gryffindor ## half-blood ## human ## tabby cat ## true ## false ## Dame Maggie Smith ## true ## 04-10-1925 ## 1925 ## grey ## female ## black ## true] +[933787c2-51e3-4eac-8a85-ab332cac0456 ## Miranda Gaushawk ## {} ## Gryffindor ## muggle ## human ## N/A ## false ## false ## N/A ## false ## 27-03-1960 ## 1960 ## blue ## female ## silver ## true] +[00434cd3-fcc7-44c7-8f98-7368415b4206 ## Miriam Strout ## {} ## Gryffindor ## half-blood ## human ## N/A ## false ## false ## N/A ## true ## 27-03-1960 ## 1954 ## blue ## female ## silver ## true] +[b0620914-858d-46fc-8e6d-033c565e138b ## Mrs Norris ## {} ## Slytherin ## pure-blood ## cat ## N/A ## true ## false ## Maxime, Alanis and Tommy the cats ## true ## 31-12-1926 ## 1954 ## yellow ## female ## brown ## false] +[11b5ca88-64ad-41a4-9f36-317b66c290af ## Nearly Headless Nick ## {Sir Nicholas de Mimsy-Porpington, Sir Nicholas, Nick} ## Gryffindor ## pure-blood ## ghost ## N/A ## false ## false ## John Cleese ## false ## 27-03-1960 ## 1926 ## amber ## male ## sandy ## true] +[20354d7a-e4fe-47af-8ff6-187bca92f3f9 ## Pandora Lovegood ## {} ## Ravenclaw ## quarter-veela ## human ## N/A ## false ## false ## N/A ## false ## 31-12-1926 ## 1959 ## dark ## female ## white ## true] +[fed624df-56d9-495e-9ad4-ea77000957e8 ## Petunia Dursley ## {} ## Slytherin ## muggle ## human ## N/A ## false ## false ## Fiona Shaw ## true ## 31-12-1926 ## 1954 ## yellow ## female ## blonde ## false] +[36bfefd0-e0bb-4d11-be98-d1ef6117a77a ## Rubeus Hagrid ## {Professor Hagrid, Hagger} ## Gryffindor ## half-blood ## half-giant ## N/A ## true ## false ## Robbie Coltrane ## true ## 06-12-1928 ## 1928 ## black ## male ## black ## true] +[1413e1b3-2903-4a47-a2d5-e8abc5ce8014 ## Seamus Finnigan ## {O Flaherty, Seamus Finnegan} ## Gryffindor ## half-blood ## human ## N/A ## false ## true ## Devon Murray ## true ## 31-12-1926 ## 1960 ## amber ## male ## sandy ## true] +[2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f ## Sirius Black ## {Padfoot, Snuffles} ## Gryffindor ## pure-blood ## human ## hare ## false ## false ## Gary Oldman ## false ## 03-11-1959 ## 1959 ## grey ## male ## black ## true] +[3a0fe4df-2e40-4541-8d7f-13586f0b9294 ## Tom Riddle ## {Tom Riddle Senior} ## Ravenclaw ## muggle ## human ## N/A ## false ## false ## N/A ## false ## 27-03-1960 ## 1980 ## brown ## male ## dark ## false] +[57fe29d4-312a-4711-bd9a-c320253d9176 ## Victoire Weasley ## {} ## Slytherin ## pure-blood ## human ## N/A ## false ## true ## N/A ## true ## 31-12-1926 ## 1926 ## grey ## female ## blonde ## true] diff --git a/tps/fonte/tp02/TP02Q05 - Ordenação por Seleção em Java/pub.in b/tps/fonte/tp02/TP02Q05 - Ordenação por Seleção em Java/pub.in new file mode 100644 index 0000000..a782d8c --- /dev/null +++ b/tps/fonte/tp02/TP02Q05 - Ordenação por Seleção em Java/pub.in @@ -0,0 +1,28 @@ +9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8 +1413e1b3-2903-4a47-a2d5-e8abc5ce8014 +ca3827f0-375a-4891-aaa5-f5e8a5bad225 +36bfefd0-e0bb-4d11-be98-d1ef6117a77a +20354d7a-e4fe-47af-8ff6-187bca92f3f9 +57fe29d4-312a-4711-bd9a-c320253d9176 +b415c867-1cff-455e-b194-748662ac2cca +5a4c95db-947d-4914-a631-41e8d466328e +861c4cde-2f0f-4796-8d8f-9492e74b2573 +2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f +41cd0bbe-a943-431b-9bde-bb2cad3491a1 +2a0615de-8aa4-41e7-9504-dd875f5f3f01 +11b5ca88-64ad-41a4-9f36-317b66c290af +eaea5eb3-48a3-41c6-9ea5-c695299bab16 +0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3 +b0620914-858d-46fc-8e6d-033c565e138b +6b59be3f-e527-422d-959d-79fcdb3b24eb +fed624df-56d9-495e-9ad4-ea77000957e8 +d58e7249-19d1-40bd-a43f-1da0497fe8aa +3a0fe4df-2e40-4541-8d7f-13586f0b9294 +6afb1960-febd-418d-b604-e50c1b59459b +efa802c8-ae18-4ae1-a524-49df21d05939 +933787c2-51e3-4eac-8a85-ab332cac0456 +94c993f6-a443-4408-b908-6e78e223e8ac +00434cd3-fcc7-44c7-8f98-7368415b4206 +7614cf6e-689e-47ac-a976-b1e9997637e9 +d59691a4-f830-4eb0-a819-a0fb00b7e80f +FIM \ No newline at end of file diff --git a/tps/fonte/tp02/TP02Q06 - Ordenação por Seleção Recursiva em C/753045_selecaoRecursiva.txt b/tps/fonte/tp02/TP02Q06 - Ordenação por Seleção Recursiva em C/753045_selecaoRecursiva.txt new file mode 100644 index 0000000..dc20737 --- /dev/null +++ b/tps/fonte/tp02/TP02Q06 - Ordenação por Seleção Recursiva em C/753045_selecaoRecursiva.txt @@ -0,0 +1 @@ +753045 0ms 351 \ No newline at end of file diff --git a/tps/fonte/tp02/TP02Q06 - Ordenação por Seleção Recursiva em C/Character b/tps/fonte/tp02/TP02Q06 - Ordenação por Seleção Recursiva em C/Character new file mode 100644 index 0000000000000000000000000000000000000000..0569773fc7d6644ba0bd89d96744e607e5e63542 GIT binary patch literal 27848 zcmeHweR!1BnfFOTAW+D}7HX<=8MSEqAOva<+}EI_n()CYvw-p@BTRFIp;nf&v_T}(`IShybz_QeQ~7CXx1+j=mP@>tdm#+5p zD!+8~lCR3Ismfj~tgqX#M+LkU^u;kXl)~3S6eBZNC5Q?cg*HwzC zH-31}GX)nv+PwGF+z-FAd_kao<(H`3R0i@#I&_h~%8uz(;7Km;>rteHAdIe``G49V zQ>nkd1Nh@9M2phkr+_zEq=cskoxNqac9}uRjWOV3vaVhiwlc|XYIyvPqn|r zzq772;BVPjzPi4t(ZA8Rqn98V=*V#7(MFT*QSgy}jWq{2(z(!ZK&ncwVX!sYpxO!BwahR?L& zwKkmkroNiJGDZ11Gzi+}l?>rJCJ1Tw(zh&+S|Tyu?PbE{vG?yD8~zEY!r#3%oaQcl z^?7BA@};p(U;SR0BK(sY1U=-H4B=PGB!3_AN`~-!nY{dcxdtxRz~vgaTmzSDz^VbG zV>HJIW&h%CA&jm_ATu^(bi9~z!ji{|e+^M={#krG=9M5td@adGhGV$qzezkTAx4In zKSew(9Y!L|ze0Q#@rRf{PCP9kM*5ilN8)MeFw(>P_lc(^!$>>xe@i?~VTch|1|NmG#II1{;R~(l4+!b`6q~{CBjG%^N$fvOM?+7^A8bEOM;O+=39uTrPYXF z{`15;h#$KMVEzu`X-P0L%={MOX(=!=#Qb{VX$deAVg4@SY3d(2#QZAaY04kzW4>jb z5H&sE`C9b$TJvZ38|JT#jI0g)9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zj0@vRy4OX zRaN<0(XxR+OWlrOKpuHq+mhPMgA9zOoe$y|=12e_E*&*aN-5R>*{LJ z8r*?ez&mYG*XnLex?iIyh*$A(i&no62Q#nQje41I>T;Swq1LRN`Jm?nJw!2TQXaHy z%Lm1EHR9GOKBH~H9SPjzdP zr`Fe4jhFrLJW;q+buEF~QsjxFq3=fNtLqv)s408x(JdsF%oe@Wv(&lQ2?gN(v0C>s z`yDj;A51KQs|uZ-ZrKJyX;r_Cvoz8SYf z!^NXX))^nJouH9a0NC7z5V^aCD=4uT$$iOIfCQJRZVZ0!t$WDVVy|Fe)}8S|)bgDTa?njdL>~;C|eygTbKXmExYduXL zfQ;S&XkPEjYN{R+`7C_OTbG}F()AEN)F#?q@84<~fs8JlzqZ%+-w7GnuOcN}dY{*# z>W0almgdR!PXZ$qs$ahF=xnEIIj_Kp?QcS2w|6RgO}#3A?I6j$*q3DAt{x0E)#cUp zn%DGTlD&Rz*7P2|0j3=2{I$PtB-!izYE7qWMMbHu?bo-#&@Jlr6D3NjXi?Jr>vAhC z@jWE$_R0Sr&w(WSHSmv|MVp5YhKgWVdKC@ z+v|Pzv2yYjk2lH1d2RnI2&jnKUhnJb{|9MU-M_vKZKvC@7q+CL?e#wX>AL~qDv-qb z*7lmd1&Q5Wc$I2}q!GnxqU|*O0|a*ah*xSxl)Wxzvc2X%M1p+j@(+8ZWJuZfsQomF z3gOfVMK6L<{zXZ+wpZKLmXvhqvQsN|rHH@BRsKcl1u@M-v^S&gTaQ1~KXlpIP<<7s cjkJ84Tx^%lS3}g@Q`whPOO0ts21%^=zky{arT_o{ literal 0 HcmV?d00001 diff --git a/tps/fonte/tp02/TP02Q06 - Ordenação por Seleção Recursiva em C/Character.c b/tps/fonte/tp02/TP02Q06 - Ordenação por Seleção Recursiva em C/Character.c new file mode 100644 index 0000000..ebd6099 --- /dev/null +++ b/tps/fonte/tp02/TP02Q06 - Ordenação por Seleção Recursiva em C/Character.c @@ -0,0 +1,696 @@ +/** + * @path TP02Q02 - Classe em C/Character.c + * @description C file that implements the Character class with recursive selection sort algorithm + * @author Pedro Lopes - github.com/httpspedroh + * @version 1.0 + * @update 2024-04-09 + */ + +// ---------------------------------------------------------------------------------------------------- // + +// Includes +#include +#include +#include +#include +#include + +// ---------------------------------------------------------------------------------------------------- // + +// Constants +#define MAX_CHARACTERS 405 +#define FILE_PATH "/tmp/characters.csv" + +#define MAX_UUID_SIZE 37 +#define MAX_NAME_SIZE 30 +#define MAX_ALTERNATE_NAMES 10 +#define MAX_ALTERNATE_NAME_SIZE 35 +#define MAX_HOUSE_SIZE 15 +#define MAX_ANCESTRY_SIZE 15 +#define MAX_SPECIES_SIZE 20 +#define MAX_PATRONUS_SIZE 25 +#define MAX_ACTOR_NAME_SIZE 35 +#define MAX_EYE_COLOUR_SIZE 10 +#define MAX_GENDER_SIZE 10 +#define MAX_HAIR_COLOUR_SIZE 10 + +#define MAX_LINE_SIZE 300 + +// ---------------------------------------------------------------------------------------------------- // + +// Structs +typedef struct Date { + + int day; + int month; + int year; +} Date; + +typedef struct Character { + + char *id; + char *name; + char *alternateNames[MAX_ALTERNATE_NAMES]; + char *house; + char *ancestry; + char *species; + char *patronus; + bool hogwartsStaff; + bool hogwartsStudent; + char *actorName; + bool alive; + Date birthDate; + int yearOfBirth; + char *eyeColour; + char *gender; + char *hairColour; + bool wizard; +} Character; + +// ---------------------------------------------------------------------------------------------------- // + +// Global variables +Character characters[MAX_CHARACTERS]; +int charactersLength = 0; + +// ---------------------------------------------------------------------------------------------------- // + +// Functions +bool isEnd(char* line) { return line[0] == 'F' && line[1] == 'I' && line[2] == 'M'; } + +void substring(char *string, char *stringStart, int length) { + + strncpy(string, stringStart, length); + string[length] = '\0'; +} + +void proccess_attribute(char *attribute, char **substringStart, char **substringEnd, bool isFirstAttribute, bool isStringArray) { + + // Skip first comma + if(!isFirstAttribute) { + + if(*substringEnd != NULL) *substringStart = *substringEnd + 1; + else *substringStart = *substringEnd; + } + + if(!isStringArray) { + + if((*substringStart)[0] == '"') { + + *substringStart = *substringStart + 1; + *substringEnd = strchr(*substringStart, '"'); + } + else *substringEnd = strchr(*substringStart, ';'); + + // Get substring + if(*substringEnd) { + + substring(attribute, *substringStart, *substringEnd - *substringStart); + + if(*substringEnd[0] == '"') *substringEnd = *substringEnd + 1; + } + else strcpy(attribute, *substringStart); + + // Set default value if attribute is empty + if(strcmp(attribute, "") == 0 || attribute[0] == '\n' || attribute[0] == '\r' || attribute[0] == '\0') strcpy(attribute, "N/A"); + + // Clean \n from the end of the string + if(attribute[strlen(attribute) - 1] == '\n' || attribute[strlen(attribute) - 1] == '\r') attribute[strlen(attribute) - 1] = '\0'; + } + else { + + // Check if the first character is a [ + if((*substringStart)[0] == '[') { + + *substringStart = *substringStart + 1; + + if((*substringStart)[0] == ']') strcpy(attribute, ""); // Case: [] + else { + + char *tempConcat = (char *) calloc(MAX_LINE_SIZE, sizeof(char)); + + *substringStart = *substringStart - 1; + + while(1) { + + *substringStart = *substringStart + 1; + + if((*substringStart)[0] == ';') break; + else if((*substringStart)[0] == '\'') { // Case: "['example', 'example']" + + *substringStart = *substringStart + 1; + *substringEnd = strchr(*substringStart, '\''); + + // Get substring + if(*substringEnd) { + + // Create tmp name + char tmp[MAX_LINE_SIZE]; + substring(tmp, *substringStart, *substringEnd - *substringStart); + + // Concat tempConcat with tmp + strcat(tempConcat, tmp); + strcat(tempConcat, ", "); + + *substringStart = *substringEnd + 1; + } + } + } + + // Get substring + strcpy(attribute, tempConcat); + + // Clean "attribute" removing last 2 characters + attribute[strlen(attribute) - 2] = '\0'; + } + } + + *substringEnd = strchr(*substringStart, ';'); + } +} + +// ---------------------------------------------------------------------------------------------------- // + +// Methods implementations + +// Gets +char *character_getId(Character *character) { return character -> id; } +char *character_getName(Character *character) { return character -> name; } +char *character_getHouse(Character *character) { return character -> house; } +char *character_getAncestry(Character *character) { return character -> ancestry; } +char *character_getSpecies(Character *character) { return character -> species; } +char *character_getPatronus(Character *character) { return character -> patronus; } +bool character_getHogwartsStaff(Character *character) { return character -> hogwartsStaff; } +bool character_getHogwartsStudent(Character *character) { return character -> hogwartsStudent; } +char *character_getActorName(Character *character) { return character -> actorName; } +bool character_getAlive(Character *character) { return character -> alive; } +char *character_getEyeColour(Character *character) { return character -> eyeColour; } +char *character_getGender(Character *character) { return character -> gender; } +char *character_getHairColour(Character *character) { return character -> hairColour; } +bool character_getWizard(Character *character) { return character -> wizard; } + +char *character_getYearOfBirth(Character *character) { + + // "N/A" if yearOfBirth is -1 + char *yearOfBirth = (char *) calloc(15, sizeof(char)); + strcpy(yearOfBirth, "N/A"); + + if(character -> yearOfBirth != -1) sprintf(yearOfBirth, "%d", character -> yearOfBirth); + return yearOfBirth; +} + +char *character_getBirthDate(Character *character) { + + // "N/A" if day, month and year are -1 + char *birthDate = (char *) calloc(15, sizeof(char)); + strcpy(birthDate, "N/A"); + + if(character -> birthDate.day != -1 && character -> birthDate.month != -1 && character -> birthDate.year != -1) { + + sprintf(birthDate, "%02d-%02d-%04d", character -> birthDate.day, character -> birthDate.month, character -> birthDate.year); + } + + return birthDate; +} + +char *character_getAlternateNames(Character *character) { + + // Concatenate all alternate names with a comma + char *alternateNames = (char *) calloc(MAX_ALTERNATE_NAME_SIZE * MAX_ALTERNATE_NAMES, sizeof(char)); + + alternateNames[0] = '{'; + + for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) { + + if(strcmp(character -> alternateNames[i], "") != 0) { + + strcat(alternateNames, character -> alternateNames[i]); + + if(strcmp(character -> alternateNames[i + 1], "") != 0) strcat(alternateNames, ", "); + } + } + + strcat(alternateNames, "}"); + + return alternateNames; +} + +// Sets +void character_setId(Character *character, char *id) { strcpy(character -> id, id); } +void character_setName(Character *character, char *name) { strcpy(character -> name, name); } +void character_setHouse(Character *character, char *house) { strcpy(character -> house, house); } +void character_setAncestry(Character *character, char *ancestry) { strcpy(character -> ancestry, ancestry); } +void character_setSpecies(Character *character, char *species) { strcpy(character -> species, species); } +void character_setPatronus(Character *character, char *patronus) { strcpy(character -> patronus, patronus); } +void character_setHogwartsStaff(Character *character, bool hogwartsStaff) { character -> hogwartsStaff = hogwartsStaff; } +void character_setHogwartsStudent(Character *character, bool hogwartsStudent) { character -> hogwartsStudent = hogwartsStudent; } +void character_setActorName(Character *character, char *actorName) { strcpy(character -> actorName, actorName); } +void character_setAlive(Character *character, bool alive) { character -> alive = alive; } +void character_setYearOfBirth(Character *character, int yearOfBirth) { character -> yearOfBirth = yearOfBirth; } +void character_setEyeColour(Character *character, char *eyeColour) { strcpy(character -> eyeColour, eyeColour); } +void character_setGender(Character *character, char *gender) { strcpy(character -> gender, gender); } +void character_setHairColour(Character *character, char *hairColour) { strcpy(character -> hairColour, hairColour); } +void character_setWizard(Character *character, bool wizard) { character -> wizard = wizard; } + +void character_setBirthDate(Character *character, char *birthDate) { + + // Explode birthDate in format DD-MM-YYYY if in format DD-MM-YYYY + if(strlen(birthDate) >= 8 && strlen(birthDate) <= 10) { + + char *token = strtok(birthDate, "-"); + + character -> birthDate.day = atoi(token); + token = strtok(NULL, "-"); + character -> birthDate.month = atoi(token); + token = strtok(NULL, "-"); + character -> birthDate.year = atoi(token); + } +} + +void character_setAlternateNames(Character *character, char *alternateNames) { + + // Copy names to a temporary variable + char tempNames[MAX_ALTERNATE_NAME_SIZE * MAX_ALTERNATE_NAMES]; + strcpy(tempNames, alternateNames); + + // Separate names by comma + char *token = strtok(tempNames, ","); + int i = 0; + + // Copy names to the character + while (token != NULL && i < MAX_ALTERNATE_NAMES) { + + while (*token == ' ') token++; + + int len = strlen(token); + + while (len > 0 && token[len - 1] == ' ') { + + token[len - 1] = '\0'; + len--; + } + + strcpy(character -> alternateNames[i++], token); + token = strtok(NULL, ","); + } +} + +// Class +Character character_newBlank() { + + Character character; + + character.id = (char *) calloc(MAX_UUID_SIZE, sizeof(char)); + strcpy(character.id, ""); + + character.name = (char *) calloc(MAX_NAME_SIZE, sizeof(char)); + strcpy(character.name, ""); + + for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) { + + character.alternateNames[i] = (char *) calloc(MAX_ALTERNATE_NAME_SIZE, sizeof(char)); + strcpy(character.alternateNames[i], ""); + } + + character.house = (char *) calloc(MAX_HOUSE_SIZE, sizeof(char)); + strcpy(character.house, ""); + + character.ancestry = (char *) calloc(MAX_ANCESTRY_SIZE, sizeof(char)); + strcpy(character.ancestry, ""); + + character.species = (char *) calloc(MAX_SPECIES_SIZE, sizeof(char)); + strcpy(character.species, ""); + + character.patronus = (char *) calloc(MAX_PATRONUS_SIZE, sizeof(char)); + strcpy(character.patronus, ""); + + character.hogwartsStaff = false; + character.hogwartsStudent = false; + + character.actorName = (char *) calloc(MAX_ACTOR_NAME_SIZE, sizeof(char)); + strcpy(character.actorName, ""); + + character.alive = false; + + character.birthDate.day = -1; + character.birthDate.month = -1; + character.birthDate.year = -1; + + character.yearOfBirth = -1; + + character.eyeColour = (char *) calloc(MAX_EYE_COLOUR_SIZE, sizeof(char)); + strcpy(character.eyeColour, ""); + + character.gender = (char *) calloc(MAX_GENDER_SIZE, sizeof(char)); + strcpy(character.gender, ""); + + character.hairColour = (char *) calloc(MAX_HAIR_COLOUR_SIZE, sizeof(char)); + strcpy(character.hairColour, ""); + + character.wizard = false; + + return character; +} + +Character character_new(char *id, char *name, char *house, char *ancestry, char *species, char *patronus, bool hogwartsStaff, bool hogwartsStudent, char *actorName, bool alive, Date birthDate, int yearOfBirth, char *eyeColour, char *gender, char *hairColour, bool wizard) { + + Character character; + + character.id = (char *) calloc(MAX_UUID_SIZE, sizeof(char)); + strcpy(character.id, id); + + character.name = (char *) calloc(MAX_NAME_SIZE, sizeof(char)); + strcpy(character.name, name); + + for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) { + + character.alternateNames[i] = (char *) calloc(MAX_ALTERNATE_NAME_SIZE, sizeof(char)); + strcpy(character.alternateNames[i], ""); + } + + character.house = (char *) calloc(MAX_HOUSE_SIZE, sizeof(char)); + strcpy(character.house, house); + + character.ancestry = (char *) calloc(MAX_ANCESTRY_SIZE, sizeof(char)); + strcpy(character.ancestry, ancestry); + + character.species = (char *) calloc(MAX_SPECIES_SIZE, sizeof(char)); + strcpy(character.species, species); + + character.patronus = (char *) calloc(MAX_PATRONUS_SIZE, sizeof(char)); + strcpy(character.patronus, patronus); + + character.hogwartsStaff = hogwartsStaff; + character.hogwartsStudent = hogwartsStudent; + + character.actorName = (char *) calloc(MAX_ACTOR_NAME_SIZE, sizeof(char)); + strcpy(character.actorName, actorName); + + character.alive = alive; + character.birthDate = birthDate; + character.yearOfBirth = yearOfBirth; + + character.eyeColour = (char *) calloc(MAX_EYE_COLOUR_SIZE, sizeof(char)); + strcpy(character.eyeColour, eyeColour); + + character.gender = (char *) calloc(MAX_GENDER_SIZE, sizeof(char)); + strcpy(character.gender, gender); + + character.hairColour = (char *) calloc(MAX_HAIR_COLOUR_SIZE, sizeof(char)); + strcpy(character.hairColour, hairColour); + + character.wizard = wizard; + + return character; +} + +Character *character_clone(Character *character) { + + Character *clone = (Character *) malloc(sizeof(Character)); + + clone -> id = (char *) calloc(MAX_UUID_SIZE, sizeof(char)); + strcpy(clone -> id, character -> id); + + clone -> name = (char *) calloc(MAX_NAME_SIZE, sizeof(char)); + strcpy(clone -> name, character -> name); + + for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) { + + clone -> alternateNames[i] = (char *) calloc(MAX_ALTERNATE_NAME_SIZE, sizeof(char)); + strcpy(clone -> alternateNames[i], character -> alternateNames[i]); + } + + clone -> house = (char *) calloc(MAX_HOUSE_SIZE, sizeof(char)); + strcpy(clone -> house, character -> house); + + clone -> ancestry = (char *) calloc(MAX_ANCESTRY_SIZE, sizeof(char)); + strcpy(clone -> ancestry, character -> ancestry); + + clone -> species = (char *) calloc(MAX_SPECIES_SIZE, sizeof(char)); + strcpy(clone -> species, character -> species); + + clone -> patronus = (char *) calloc(MAX_PATRONUS_SIZE, sizeof(char)); + strcpy(clone -> patronus, character -> patronus); + + clone -> hogwartsStaff = character -> hogwartsStaff; + clone -> hogwartsStudent = character -> hogwartsStudent; + + clone -> actorName = (char *) calloc(MAX_ACTOR_NAME_SIZE, sizeof(char)); + strcpy(clone -> actorName, character -> actorName); + + clone -> alive = character -> alive; + clone -> birthDate = character -> birthDate; + clone -> yearOfBirth = character -> yearOfBirth; + + clone -> eyeColour = (char *) calloc(MAX_EYE_COLOUR_SIZE, sizeof(char)); + strcpy(clone -> eyeColour, character -> eyeColour); + + clone -> gender = (char *) calloc(MAX_GENDER_SIZE, sizeof(char)); + strcpy(clone -> gender, character -> gender); + + clone -> hairColour = (char *) calloc(MAX_HAIR_COLOUR_SIZE, sizeof(char)); + strcpy(clone -> hairColour, character -> hairColour); + + clone -> wizard = character -> wizard; + + return clone; +} + +void character_print(Character *character) { + + printf("[%s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s]\n", + + character_getId(character), + character_getName(character), + character_getAlternateNames(character), + character_getHouse(character), + character_getAncestry(character), + character_getSpecies(character), + character_getPatronus(character), + character_getHogwartsStaff(character) ? "true" : "false", + character_getHogwartsStudent(character) ? "true" : "false", + character_getActorName(character), + character_getAlive(character) ? "true" : "false", + character_getBirthDate(character), + character_getYearOfBirth(character), + character_getEyeColour(character), + character_getGender(character), + character_getHairColour(character), + character_getWizard(character) ? "true" : "false" + ); +} + +Character character_read(char *line) { + + Character character = character_newBlank(); + + char *substringStart = line; + char *substringEnd = NULL; + char attribute[MAX_LINE_SIZE]; + + // Get id + proccess_attribute(attribute, &substringStart, &substringEnd, true, false); + character_setId(&character, attribute); + + // Get name + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setName(&character, attribute); + + // Get alternate names + proccess_attribute(attribute, &substringStart, &substringEnd, false, true); + character_setAlternateNames(&character, attribute); + + // Get house + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setHouse(&character, attribute); + + // Get ancestry + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setAncestry(&character, attribute); + + // Get species + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setSpecies(&character, attribute); + + // Get patronus + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setPatronus(&character, attribute); + + // Get hogwarts staff + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setHogwartsStaff(&character, strcmp(attribute, "VERDADEIRO") == 0); + + // Get hogwarts student + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setHogwartsStudent(&character, strcmp(attribute, "VERDADEIRO") == 0); + + // Get actor name + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setActorName(&character, attribute); + + // Get alive + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setAlive(&character, strcmp(attribute, "VERDADEIRO") == 0); + + // Get birth date + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setBirthDate(&character, attribute); + + // Get year of birth + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setYearOfBirth(&character, atoi(attribute) == 0 ? -1 : atoi(attribute)); + + // Get eye colour + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setEyeColour(&character, attribute); + + // Get gender + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setGender(&character, attribute); + + // Get hair colour + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setHairColour(&character, attribute); + + // Get wizard + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + + // Clean \n from the end of the string + if(attribute[strlen(attribute) - 1] == '\n' || attribute[strlen(attribute) - 1] == '\r') attribute[strlen(attribute) - 1] = '\0'; + + character_setWizard(&character, strcmp(attribute, "VERDADEIRO") == 0); + return character; +} + +Character *character_searchById(char *id) { + + for(int i = 0; i < charactersLength; i++) { + + if(!strcmp(characters[i].id, id)) return &characters[i]; + } + return NULL; +} + +// General +void startCharacters() { + + // Open file + FILE *fp; + char *line = NULL; + size_t len = 0; + ssize_t read; + + fp = fopen(FILE_PATH, "r"); + + if(fp == NULL) { + + perror("x Error opening file"); + exit(EXIT_FAILURE); + } + + // Skip first line + getline(&line, &len, fp); + + // Read all lines + while((read = getline(&line, &len, fp)) != -1) { + + // Read character from line + Character character = character_read(line); + + characters[charactersLength++] = character; + + if(charactersLength >= MAX_CHARACTERS) { + + perror("x Max characters reached"); + exit(EXIT_FAILURE); + } + } + + // Close file and free memory + fclose(fp); + + if(line) free(line); +} + +// ---------------------------------------------------------------------------------------------------- // + +// Main +int main() { + + // ----------------------------------------------------------------- // + + // #1 - Start - Read all characters in CSV file + startCharacters(); + + // ----------------------------------------------------------------- // + + // #2 - Read input and print characters from pub.in id entries and add to mainCharacters array + + // Initialize mainCharacters array + Character mainCharacters[MAX_CHARACTERS]; + int m = 0; + + char id[MAX_UUID_SIZE]; + scanf(" %[^\n]s", id); + + while(true) { + + // Clean \n from the end of the string + if(id[strlen(id) - 1] == '\n' || id[strlen(id) - 1] == '\r') id[strlen(id) - 1] = '\0'; + + // ------------------------- // + + if(isEnd(id)) break; + else { + + Character *character = character_searchById(id); + + if(character) mainCharacters[m++] = *character; + + // ------------------------- // + + scanf(" %[^\n]s", id); + } + } + + // ----------------------------------------------------------------- // + + // #3 - Order mainCharacters array by key "name" using recursive selection sort + + // Start benchmark + clock_t startTime = clock(); + int comparisons = 0; + + // Recursive selection sort + for(int i = 0; i < m - 1; i++) { + + int min = i; + + for(int j = i + 1; j < m; j++) { + + comparisons++; + + if(strcmp(character_getName(&mainCharacters[j]), character_getName(&mainCharacters[min])) < 0) min = j; + } + + Character aux = mainCharacters[i]; + mainCharacters[i] = mainCharacters[min]; + mainCharacters[min] = aux; + } + + // ----------------- // + + // Save benchmark in file + FILE *fp = fopen("753045_selecaoRecursiva.txt", "w"); + fprintf(fp, "753045\t%.0fms\t%d", (double)(clock() - startTime) / CLOCKS_PER_SEC * 1000.0, comparisons); + fclose(fp); + + // ----------------- // + + // Print mainCharacters array + for(int i = 0; i < m; i++) character_print(&mainCharacters[i]); + + // ----------------- // + + return 0; +} \ No newline at end of file diff --git a/tps/fonte/tp02/TP02Q06 - Ordenação por Seleção Recursiva em C/pedro.out b/tps/fonte/tp02/TP02Q06 - Ordenação por Seleção Recursiva em C/pedro.out new file mode 100644 index 0000000..bf1a0c1 --- /dev/null +++ b/tps/fonte/tp02/TP02Q06 - Ordenação por Seleção Recursiva em C/pedro.out @@ -0,0 +1,27 @@ +[94c993f6-a443-4408-b908-6e78e223e8ac ## Aberforth Dumbledore ## {} ## Gryffindor ## half-blood ## human ## goat ## false ## false ## Jim McManus ## true ## 10-03-1960 ## 1960 ## blue ## male ## grey ## true] +[b415c867-1cff-455e-b194-748662ac2cca ## Albus Dumbledore ## {Professor Dumbledore} ## Gryffindor ## half-blood ## human ## Phoenix ## true ## false ## Richard Harris ## false ## 27-03-1960 ## 1881 ## blue ## male ## silver ## true] +[6afb1960-febd-418d-b604-e50c1b59459b ## Bellatrix Lestrange ## {Bella} ## Slytherin ## pure-blood ## human ## N/A ## false ## false ## Helena Bonham Carter ## false ## 06-02-1950 ## 1951 ## brown ## female ## black ## true] +[0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3 ## Dobby ## {} ## Slytherin ## pure-blood ## house-elf ## N/A ## false ## false ## Toby Jones ## false ## 06-02-1950 ## 1950 ## green ## male ## blond ## false] +[d58e7249-19d1-40bd-a43f-1da0497fe8aa ## Dolores Umbridge ## {} ## Slytherin ## half-blood ## human ## persian cat ## true ## false ## Imelda Staunton ## true ## 31-12-1926 ## 1926 ## brown ## female ## grey ## true] +[7614cf6e-689e-47ac-a976-b1e9997637e9 ## Dudley Dursley ## {} ## Slytherin ## muggle ## human ## N/A ## false ## false ## Harry Melling ## true ## 23-06-1980 ## 1980 ## blue ## male ## blond ## false] +[d59691a4-f830-4eb0-a819-a0fb00b7e80f ## Garrick Ollivander ## {} ## Hufflepuff ## muggle ## human ## N/A ## false ## false ## John Hurt ## true ## 04-10-1925 ## 1954 ## pale, silvery ## male ## brown ## true] +[2a0615de-8aa4-41e7-9504-dd875f5f3f01 ## George Weasley ## {} ## Gryffindor ## pure-blood ## human ## N/A ## false ## true ## Oliver Phelps ## true ## 13-02-1981 ## 1954 ## amber ## male ## red ## true] +[9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8 ## Harry Potter ## {The Boy Who Lived, The Chosen One, Undesirable No. 1, Potty} ## Gryffindor ## half-blood ## human ## stag ## false ## true ## Daniel Radcliffe ## true ## 31-07-1980 ## 1980 ## green ## male ## black ## true] +[6b59be3f-e527-422d-959d-79fcdb3b24eb ## Hedwig ## {} ## Hufflepuff ## muggle ## owl ## N/A ## false ## false ## Gizmo ## false ## 07-04-1979 ## 1954 ## amber ## female ## brown ## false] +[5a4c95db-947d-4914-a631-41e8d466328e ## Kendra Dumbledore ## {} ## Slytherin ## muggleborn ## human ## N/A ## false ## false ## N/A ## false ## 31-12-1926 ## 1925 ## dark ## female ## black ## true] +[41cd0bbe-a943-431b-9bde-bb2cad3491a1 ## Lily Potter ## {} ## Gryffindor ## muggleborn ## human ## N/A ## false ## false ## Geraldine Somerville ## false ## 30-01-1960 ## 1960 ## green ## female ## blond ## true] +[eaea5eb3-48a3-41c6-9ea5-c695299bab16 ## Lisa Turpin ## {} ## Ravenclaw ## pure-blood ## human ## N/A ## false ## true ## N/A ## true ## 27-03-1960 ## 1951 ## amber ## female ## sandy ## true] +[efa802c8-ae18-4ae1-a524-49df21d05939 ## Lord Voldemort ## {Tom Riddle, Tom Marvolo Riddle, You-Know-Who, He Who Must Not Be Named, the Dark Lord, the Heir of Slytherin} ## Slytherin ## half-blood ## human ## N/A ## false ## false ## Ralph Fiennes ## false ## 31-12-1926 ## 1926 ## Scarlet ## male ## bald ## true] +[861c4cde-2f0f-4796-8d8f-9492e74b2573 ## Luna Lovegood ## {Loony Lovegood} ## Ravenclaw ## pure-blood ## human ## hare ## false ## true ## Evanna Lynch ## true ## 13-02-1981 ## 1981 ## silver ## female ## blonde ## true] +[ca3827f0-375a-4891-aaa5-f5e8a5bad225 ## Minerva McGonagall ## {} ## Gryffindor ## half-blood ## human ## tabby cat ## true ## false ## Dame Maggie Smith ## true ## 04-10-1925 ## 1925 ## grey ## female ## black ## true] +[933787c2-51e3-4eac-8a85-ab332cac0456 ## Miranda Gaushawk ## {} ## Gryffindor ## muggle ## human ## N/A ## false ## false ## N/A ## false ## 27-03-1960 ## 1960 ## blue ## female ## silver ## true] +[00434cd3-fcc7-44c7-8f98-7368415b4206 ## Miriam Strout ## {} ## Gryffindor ## half-blood ## human ## N/A ## false ## false ## N/A ## true ## 27-03-1960 ## 1954 ## blue ## female ## silver ## true] +[b0620914-858d-46fc-8e6d-033c565e138b ## Mrs Norris ## {} ## Slytherin ## pure-blood ## cat ## N/A ## true ## false ## Maxime, Alanis and Tommy the cats ## true ## 31-12-1926 ## 1954 ## yellow ## female ## brown ## false] +[11b5ca88-64ad-41a4-9f36-317b66c290af ## Nearly Headless Nick ## {Sir Nicholas de Mimsy-Porpington, Sir Nicholas, Nick} ## Gryffindor ## pure-blood ## ghost ## N/A ## false ## false ## John Cleese ## false ## 27-03-1960 ## 1926 ## amber ## male ## sandy ## true] +[20354d7a-e4fe-47af-8ff6-187bca92f3f9 ## Pandora Lovegood ## {} ## Ravenclaw ## quarter-veela ## human ## N/A ## false ## false ## N/A ## false ## 31-12-1926 ## 1959 ## dark ## female ## white ## true] +[fed624df-56d9-495e-9ad4-ea77000957e8 ## Petunia Dursley ## {} ## Slytherin ## muggle ## human ## N/A ## false ## false ## Fiona Shaw ## true ## 31-12-1926 ## 1954 ## yellow ## female ## blonde ## false] +[36bfefd0-e0bb-4d11-be98-d1ef6117a77a ## Rubeus Hagrid ## {Professor Hagrid, Hagger} ## Gryffindor ## half-blood ## half-giant ## N/A ## true ## false ## Robbie Coltrane ## true ## 06-12-1928 ## 1928 ## black ## male ## black ## true] +[1413e1b3-2903-4a47-a2d5-e8abc5ce8014 ## Seamus Finnigan ## {O Flaherty, Seamus Finnegan} ## Gryffindor ## half-blood ## human ## N/A ## false ## true ## Devon Murray ## true ## 31-12-1926 ## 1960 ## amber ## male ## sandy ## true] +[2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f ## Sirius Black ## {Padfoot, Snuffles} ## Gryffindor ## pure-blood ## human ## hare ## false ## false ## Gary Oldman ## false ## 03-11-1959 ## 1959 ## grey ## male ## black ## true] +[3a0fe4df-2e40-4541-8d7f-13586f0b9294 ## Tom Riddle ## {Tom Riddle Senior} ## Ravenclaw ## muggle ## human ## N/A ## false ## false ## N/A ## false ## 27-03-1960 ## 1980 ## brown ## male ## dark ## false] +[57fe29d4-312a-4711-bd9a-c320253d9176 ## Victoire Weasley ## {} ## Slytherin ## pure-blood ## human ## N/A ## false ## true ## N/A ## true ## 31-12-1926 ## 1926 ## grey ## female ## blonde ## true] diff --git a/tps/fonte/tp02/TP02Q06 - Ordenação por Seleção Recursiva em C/pub.in b/tps/fonte/tp02/TP02Q06 - Ordenação por Seleção Recursiva em C/pub.in new file mode 100644 index 0000000..a782d8c --- /dev/null +++ b/tps/fonte/tp02/TP02Q06 - Ordenação por Seleção Recursiva em C/pub.in @@ -0,0 +1,28 @@ +9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8 +1413e1b3-2903-4a47-a2d5-e8abc5ce8014 +ca3827f0-375a-4891-aaa5-f5e8a5bad225 +36bfefd0-e0bb-4d11-be98-d1ef6117a77a +20354d7a-e4fe-47af-8ff6-187bca92f3f9 +57fe29d4-312a-4711-bd9a-c320253d9176 +b415c867-1cff-455e-b194-748662ac2cca +5a4c95db-947d-4914-a631-41e8d466328e +861c4cde-2f0f-4796-8d8f-9492e74b2573 +2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f +41cd0bbe-a943-431b-9bde-bb2cad3491a1 +2a0615de-8aa4-41e7-9504-dd875f5f3f01 +11b5ca88-64ad-41a4-9f36-317b66c290af +eaea5eb3-48a3-41c6-9ea5-c695299bab16 +0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3 +b0620914-858d-46fc-8e6d-033c565e138b +6b59be3f-e527-422d-959d-79fcdb3b24eb +fed624df-56d9-495e-9ad4-ea77000957e8 +d58e7249-19d1-40bd-a43f-1da0497fe8aa +3a0fe4df-2e40-4541-8d7f-13586f0b9294 +6afb1960-febd-418d-b604-e50c1b59459b +efa802c8-ae18-4ae1-a524-49df21d05939 +933787c2-51e3-4eac-8a85-ab332cac0456 +94c993f6-a443-4408-b908-6e78e223e8ac +00434cd3-fcc7-44c7-8f98-7368415b4206 +7614cf6e-689e-47ac-a976-b1e9997637e9 +d59691a4-f830-4eb0-a819-a0fb00b7e80f +FIM \ No newline at end of file diff --git a/tps/fonte/tp02/TP02Q07 - Ordenação por Inserção em Java/753045_insercao.txt b/tps/fonte/tp02/TP02Q07 - Ordenação por Inserção em Java/753045_insercao.txt new file mode 100644 index 0000000..6b29c0f --- /dev/null +++ b/tps/fonte/tp02/TP02Q07 - Ordenação por Inserção em Java/753045_insercao.txt @@ -0,0 +1 @@ +753045 22ms 167 \ No newline at end of file diff --git a/tps/fonte/tp02/TP02Q07 - Ordenação por Inserção em Java/Arq.java b/tps/fonte/tp02/TP02Q07 - Ordenação por Inserção em Java/Arq.java new file mode 100644 index 0000000..9a632d1 --- /dev/null +++ b/tps/fonte/tp02/TP02Q07 - Ordenação por Inserção em Java/Arq.java @@ -0,0 +1,206 @@ +import java.io.*; +import java.util.Formatter; +import java.util.Scanner; +import java.io.File; +import java.nio.charset.*; + +public class Arq +{ + private static String nomeArquivo = ""; + private static String charsetArquivo = "ISO-8859-1"; + private static boolean write = false, read = false; + private static Formatter saida = null; + private static Scanner entrada = null; + + public static boolean openWrite(String nomeArq, String charset) { + boolean resp = false; + close(); + try{ + saida = new Formatter(nomeArq, charset); + nomeArquivo = nomeArq; + resp = write = true; + } catch (Exception e) {} + return resp; + } + + public static boolean openWrite(String nomeArq) { + return openWrite(nomeArq, charsetArquivo); + } + + public static boolean openWriteClose(String nomeArq, String charset, String conteudo) { + boolean resp = openWrite(nomeArq, charset); + if(resp == true){ + println(conteudo); + close(); + } + return resp; + } + + public static boolean openWriteClose(String nomeArq, String conteudo) { + return openWriteClose(nomeArq, charsetArquivo, conteudo); + } + + public static boolean openRead(String nomeArq) { + return openRead(nomeArq, charsetArquivo); + } + + public static boolean openRead(String nomeArq, String charset) { + boolean resp = false; + close(); + try{ + entrada = new Scanner(new File(nomeArq), charset); + nomeArquivo = nomeArq; + resp = read = true; + } catch (Exception e) {} + return resp; + } + + public static String openReadClose(String nomeArq){ + openRead(nomeArq); + String resp = readAll(); + close(); + return resp; + } + + public static void close() { + if(write == true){ + saida.close(); + } + if(read == true){ + entrada.close(); + } + write = read = false; + nomeArquivo = ""; + charsetArquivo = "ISO-8859-1"; + } + + public static long length(){ + long resp = -1; + if(read != write){ + File file = new File(nomeArquivo); + resp = file.length(); + } + return resp; + } + + public static void print(int x){ + if(write == true){ + saida.format( "%d", x); + } + } + + public static void print(double x){ + if(write == true){ + saida.format( "%f", x); + } + } + + public static void print(String x){ + if(write == true){ + saida.format( "%s", x); + } + } + + public static void print(boolean x){ + if(write == true){ + saida.format( "%s", ((x) ? "true" : "false")); + } + } + + public static void print(char x){ + if(write == true){ + saida.format( "%c", x); + } + } + + public static void println(int x){ + if(write == true){ + saida.format( "%d\n", x); + } + } + + public static void println(double x){ + if(write == true){ + saida.format( "%f\n", x); + } + } + + public static void println(String x){ + if(write == true){ + saida.format( "%s\n", x); + } + } + + public static void println(boolean x){ + if(write == true){ + saida.format( "%s\n", ((x) ? "true" : "false")); + } + } + + public static void println(char x){ + if(write == true){ + saida.format( "%c\n", x); + } + } + + public static int readInt(){ + int resp = -1; + try{ + resp = entrada.nextInt(); + } catch (Exception e) {} + return resp; + } + + public static char readChar(){ + char resp = ' '; + try{ + resp = (char)entrada.nextByte(); + } catch (Exception e) {} + return resp; + } + + public static double readDouble(){ + double resp = -1; + try{ + resp = Double.parseDouble(readString().replace(",",".")); + } catch (Exception e) {} + return resp; + } + + public static String readString(){ + String resp = ""; + try{ + resp = entrada.next(); + } catch (Exception e) { System.out.println(e.getMessage()); } + return resp; + } + + public static boolean readBoolean(){ + boolean resp = false; + try{ + resp = (entrada.next().equals("true")) ? true : false; + } catch (Exception e) {} + return resp; + } + + public static String readLine(){ + String resp = ""; + try{ + resp = entrada.nextLine(); + } catch (Exception e) { System.out.println(e.getMessage()); } + return resp; + } + + + public static boolean hasNext(){ + return entrada.hasNext(); + } + + public static String readAll(){ + String resp = ""; + while(hasNext()){ + resp += (readLine() + "\n"); + } + return resp; + } +} diff --git a/tps/fonte/tp02/TP02Q07 - Ordenação por Inserção em Java/Character.java b/tps/fonte/tp02/TP02Q07 - Ordenação por Inserção em Java/Character.java new file mode 100644 index 0000000..cc7e35e --- /dev/null +++ b/tps/fonte/tp02/TP02Q07 - Ordenação por Inserção em Java/Character.java @@ -0,0 +1,513 @@ +/** + * @path TP02Q01 - Classe em Java/Characters.java + * @description Java class of all characters from Harry Potter's saga with insertion sort algorithm + * @author Pedro Lopes - github.com/httpspedroh + * @version 1.0 + * @update 2024-04-09 + */ + +// ---------------------------------------------------------------------------------------------------- // + +// Imports +import java.util.Scanner; +import java.util.UUID; +import java.io.BufferedReader; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Date; +import java.text.SimpleDateFormat; + +// ---------------------------------------------------------------------------------------------------- // + +public class Character { + + static SimpleDateFormat ddf = new SimpleDateFormat("dd-MM-yyyy"); + + // Global variables + public static final String FILE_PATH = "/tmp/characters.csv"; + public static ArrayList allCharacters = new ArrayList(); + + // -------------------------- // + + // Attributes + private UUID id; + private String name; + private ArrayList alternateNames; + private String house; + private String ancestry; + private String species; + private String patronus; + private boolean hogwartsStaff; + private boolean hogwartsStudent; + private String actorName; + private boolean alive; + private Date birthDate; + private int yearOfBirth; + private String eyeColour; + private String gender; + private String hairColour; + private boolean wizard; + + // -------------------------- // + + // Empty constructor + public Character() { + + this.id = UUID.randomUUID(); + this.name = ""; + this.alternateNames = new ArrayList(); + this.house = ""; + this.ancestry = ""; + this.species = ""; + this.patronus = ""; + this.hogwartsStaff = false; + this.hogwartsStudent = false; + this.actorName = ""; + this.alive = false; + this.birthDate = null; + this.yearOfBirth = 0; + this.eyeColour = ""; + this.gender = ""; + this.hairColour = ""; + this.wizard = false; + } + + // Constructor + public Character(UUID id, String name, ArrayList alternateNames, String house, String ancestry, String species, String patronus, boolean hogwartsStaff, boolean hogwartsStudent, String actorName, boolean alive, Date birthDate, int yearOfBirth, String eyeColour, String gender, String hairColour, boolean wizard) { + + this.id = id; + this.name = name; + this.alternateNames = alternateNames; + this.house = house; + this.ancestry = ancestry; + this.species = species; + this.patronus = patronus; + this.hogwartsStaff = hogwartsStaff; + this.hogwartsStudent = hogwartsStudent; + this.actorName = actorName; + this.alive = alive; + this.birthDate = birthDate; + this.yearOfBirth = yearOfBirth; + this.eyeColour = eyeColour; + this.gender = gender; + this.hairColour = hairColour; + this.wizard = wizard; + } + + // -------------------------- // + + // Gets + public UUID getId() { return this.id; } + public String getName() { return this.name; } + public String getHouse() { return this.house; } + public String getAncestry() { return this.ancestry; } + public String getSpecies() { return this.species; } + public String getPatronus() { return this.patronus; } + public boolean getHogwartsStaff() { return this.hogwartsStaff; } + public boolean getHogwartsStudent() { return this.hogwartsStudent; } + public String getActorName() { return this.actorName; } + public boolean getAlive() { return this.alive; } + public Date getBirthDate() { return this.birthDate; } + public int getYearOfBirth() { return this.yearOfBirth; } + public String getEyeColour() { return this.eyeColour; } + public String getGender() { return this.gender; } + public String getHairColour() { return this.hairColour; } + public boolean getWizard() { return this.wizard; } + + public String getAlternateNames() { + + // Construct string e.g. {item1, item2, item3} + String alternateNames = "{"; + + for(int i = 0; i < this.alternateNames.size(); i++) { + + alternateNames += this.alternateNames.get(i); + + if(i < this.alternateNames.size() - 1) alternateNames += ", "; + } + + alternateNames += "}"; + + return alternateNames; + } + + // Sets + public void setId(UUID id) { this.id = id; } + public void setName(String name) { this.name = name; } + public void setAlternateNames(ArrayList alternateNames) { this.alternateNames = alternateNames; } + public void setHouse(String house) { this.house = house; } + public void setAncestry(String ancestry) { this.ancestry = ancestry; } + public void setSpecies(String species) { this.species = species; } + public void setPatronus(String patronus) { this.patronus = patronus; } + public void setHogwartsStaff(boolean hogwartsStaff) { this.hogwartsStaff = hogwartsStaff; } + public void setHogwartsStudent(boolean hogwartsStudent) { this.hogwartsStudent = hogwartsStudent; } + public void setActorNane(String actorName) { this.actorName = actorName; } + public void setAlive(boolean alive) { this.alive = alive; } + public void setBirthDate(Date birthDate) { this.birthDate = birthDate; } + public void setYearOfBirth(int yearOfBirth) { this.yearOfBirth = yearOfBirth; } + public void setEyeColour(String eyeColour) { this.eyeColour = eyeColour; } + public void setGender(String gender) { this.gender = gender; } + public void setHairColour(String hairColour) { this.hairColour = hairColour; } + public void setWizard(boolean wizard) { this.wizard = wizard; } + + // -------------------------- // + + // Clone + public Character clone() { return new Character(this.id, this.name, this.alternateNames, this.house, this.ancestry, this.species, this.patronus, this.hogwartsStaff, this.hogwartsStudent, this.actorName, this.alive, this.birthDate, this.yearOfBirth, this.eyeColour, this.gender, this.hairColour, this.wizard); } + + // -------------------------- // + + // Print + public void print() { + + System.out.println("[" + + this.getId() + " ## " + + this.getName() + " ## " + + this.getAlternateNames() + " ## " + + (this.getHouse() == "" ? "N/A" : this.getHouse()) + " ## " + + (this.getAncestry() == "" ? "N/A" : this.getAncestry()) + " ## " + + (this.getSpecies() == "" ? "N/A" : this.getSpecies()) + " ## " + + (this.getPatronus() == "" ? "N/A" : this.getPatronus()) + " ## " + + (this.getHogwartsStaff() ? "true" : "false") + " ## " + + (this.getHogwartsStudent() ? "true" : "false") + " ## " + + (this.getActorName() == "" ? "N/A" : this.getActorName()) + " ## " + + (this.getAlive() ? "true" : "false") + " ## " + + (this.getBirthDate() == null ? "N/A" : ddf.format(this.getBirthDate())) + " ## " + + (this.getYearOfBirth() == 0 ? "N/A" : this.getYearOfBirth()) + " ## " + + (this.getEyeColour() == "" ? "N/A" : this.getEyeColour()) + " ## " + + (this.getGender() == "" ? "N/A" : this.getGender()) + " ## " + + (this.getHairColour() == "" ? "N/A" : this.getHairColour()) + " ## " + + (this.getWizard() ? "true" : "false") + "]"); + } + + // -------------------------- // + + public static ArrayList extractNames(String input) { + + ArrayList names = new ArrayList<>(); + + // Clean input + String cleanedInput = input.substring(1, input.length() - 1); + + // Check if input contains double quotes + if (cleanedInput.contains("\"\"")) { + + String[] parts = cleanedInput.split("\", "); + + for (String part : parts) names.add(part.replace("\"", "").replace("'", "")); + } + else { + + // Split input by comma and space + String[] parts = cleanedInput.split(", "); + + for (String part : parts) names.add(part.replace("'", "")); + } + return names; + } + + // -------------------------- // + + // Read + public void read(String line) { + + // ---------------------- // + + // Start position + int positionStart = 0; + int positionEnd = 0; + + // ---------------------- // + + // Set id + positionEnd = line.indexOf(";", positionStart); + this.setId(UUID.fromString(line.substring(positionStart, positionEnd))); + + // ---------------------- // + + // Set name + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setName(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set alternate names + positionStart = positionEnd + 1; + + if(line.charAt(positionStart) == '[') { // If first character is '[', has one or no names + + if(line.charAt(positionStart + 1) == ']') { + + positionEnd = positionStart + 2; + + this.setAlternateNames(new ArrayList()); + } + else { + + positionEnd = line.indexOf("];", positionStart); + + this.setAlternateNames(extractNames(line.substring(positionStart, positionEnd++))); + } + } + + // ---------------------- // + + // Set house + positionStart = positionEnd; + positionEnd = line.indexOf(";", ++positionStart); + + this.setHouse(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set ancestry + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setAncestry(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set species + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setSpecies(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set patronus + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setPatronus(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set hogwartsStaff + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setHogwartsStaff("VERDADEIRO".equals(line.substring(positionStart, positionEnd))); + + // ---------------------- // + + // Set hogwartsStudent + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setHogwartsStudent("VERDADEIRO".equals(line.substring(positionStart, positionEnd))); + + // ---------------------- // + + // Set actor name + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setActorNane(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set alive + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setAlive("VERDADEIRO".equals(line.substring(positionStart, positionEnd))); + + // ---------------------- // + + // Set date of birth + positionStart = positionEnd; + positionEnd = line.indexOf(";", ++positionStart); + + try { this.setBirthDate(ddf.parse(line.substring(positionStart, positionEnd))); } + catch(Exception e) { this.setBirthDate(null); } + + // ---------------------- // + + // Set year of birth + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + try { this.setYearOfBirth(Integer.parseInt(line.substring(positionStart, positionEnd))); } + catch(Exception e) { this.setYearOfBirth(0); } + + // ---------------------- // + + // Set eye colour + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setEyeColour(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set gender + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setGender(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set hair colour + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setHairColour(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set wizard + positionStart = positionEnd + 1; + + this.setWizard("VERDADEIRO".equals(line.substring(positionStart, line.length()))); + + // ---------------------- // + } + + // ---------------------------------------------------------------------------------------------------- // + + // Read all characters function + public static void startCharacters() { + + // Initialize variables + try { + + FileInputStream fstream = new FileInputStream(FILE_PATH); + BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); + + // ---------------------- // + + // Explode CSV file + String line = br.readLine(); + + while((line = br.readLine()) != null) { + + // Initialize character + Character character = new Character(); + + // Read line + character.read(line); + + // Add character to array + allCharacters.add(character); + } + + // Close CSV file + fstream.close(); + } + catch(IOException e) { e.printStackTrace(); } + } + + // ---------------------------------------------------------------------------------------------------- // + + // Search by id function + public static Character searchById(UUID id, ArrayList characters) { + + // Search for character + for(int i = 0; i < characters.size(); i++) { + + if(characters.get(i).getId().equals(id)) return characters.get(i); + } + return null; + } + + // ---------------------------------------------------------------------------------------------------- // + + public static void main(String[] args) { + + // ----------------------------------------------------------------- // + + // #1 - Start - Read all characters in CSV file + startCharacters(); + + // ----------------------------------------------------------------- // + + // #2 - Read input and print characters from pub.in id entries and add to mainCharacters array + + // Initialize mainCharacters array + ArrayList mainCharacters = new ArrayList(); + + // Initialize scanner + Scanner inScanner = new Scanner(System.in); + + // Initialize character + Character character = new Character(); + + // Read first line + String line = inScanner.nextLine(); + + // While line is not "FIM" + while(!line.equals("FIM")) { + + // Get id + UUID id = UUID.fromString(line); + + // Search for character + character = searchById(id, allCharacters); + + // Add character to mainCharacters array + if(character != null) mainCharacters.add(character); + + // Read line + line = inScanner.nextLine(); + } + + // ----------------------------------------------------------------- // + + // #3 - Order mainCharacters array by key "dateOfBirth" using insertion sort, in draw case, order by key "name" + + // Start benchmark + long startTime = System.currentTimeMillis(); + int comparisons = 0; + + // Insertion sort + for(int i = 1; i < mainCharacters.size(); i++) { + + Character current = mainCharacters.get(i); + int j = i - 1; + + // Compare based on "dateOfBirth" + while(j >= 0 && current.getBirthDate().compareTo(mainCharacters.get(j).getBirthDate()) < 0) { + + mainCharacters.set(j + 1, mainCharacters.get(j)); + j--; + comparisons++; + } + + // In case of a tie in "dateOfBirth" compare based on "name" + while(j >= 0 && current.getBirthDate().compareTo(mainCharacters.get(j).getBirthDate()) == 0 && current.getName().compareTo(mainCharacters.get(j).getName()) < 0) { + + mainCharacters.set(j + 1, mainCharacters.get(j)); + j--; + comparisons++; + } + + mainCharacters.set(j + 1, current); + } + + // ----------------- // + + // Save benchmark in file + Arq.openWrite("753045_insercao.txt"); + Arq.print("753045\t" + (System.currentTimeMillis() - startTime) + "ms\t" + comparisons); + Arq.close(); + + // ----------------- // + + // Print mainCharacters array + for(int i = 0; i < mainCharacters.size(); i++) mainCharacters.get(i).print(); + + // ----------------------------------------------------------------- // + + // Close scanner + inScanner.close(); + } +} + +// ---------------------------------------------------------------------------------------------------- // diff --git a/tps/fonte/tp02/TP02Q07 - Ordenação por Inserção em Java/pedro.out b/tps/fonte/tp02/TP02Q07 - Ordenação por Inserção em Java/pedro.out new file mode 100644 index 0000000..4a36f24 --- /dev/null +++ b/tps/fonte/tp02/TP02Q07 - Ordenação por Inserção em Java/pedro.out @@ -0,0 +1,27 @@ +[d59691a4-f830-4eb0-a819-a0fb00b7e80f ## Garrick Ollivander ## {} ## Hufflepuff ## muggle ## human ## N/A ## false ## false ## John Hurt ## true ## 04-10-1925 ## 1954 ## pale, silvery ## male ## brown ## true] +[ca3827f0-375a-4891-aaa5-f5e8a5bad225 ## Minerva McGonagall ## {} ## Gryffindor ## half-blood ## human ## tabby cat ## true ## false ## Dame Maggie Smith ## true ## 04-10-1925 ## 1925 ## grey ## female ## black ## true] +[d58e7249-19d1-40bd-a43f-1da0497fe8aa ## Dolores Umbridge ## {} ## Slytherin ## half-blood ## human ## persian cat ## true ## false ## Imelda Staunton ## true ## 31-12-1926 ## 1926 ## brown ## female ## grey ## true] +[5a4c95db-947d-4914-a631-41e8d466328e ## Kendra Dumbledore ## {} ## Slytherin ## muggleborn ## human ## N/A ## false ## false ## N/A ## false ## 31-12-1926 ## 1925 ## dark ## female ## black ## true] +[efa802c8-ae18-4ae1-a524-49df21d05939 ## Lord Voldemort ## {Tom Riddle, Tom Marvolo Riddle, You-Know-Who, He Who Must Not Be Named, the Dark Lord, the Heir of Slytherin} ## Slytherin ## half-blood ## human ## N/A ## false ## false ## Ralph Fiennes ## false ## 31-12-1926 ## 1926 ## Scarlet ## male ## bald ## true] +[b0620914-858d-46fc-8e6d-033c565e138b ## Mrs Norris ## {} ## Slytherin ## pure-blood ## cat ## N/A ## true ## false ## Maxime, Alanis and Tommy the cats ## true ## 31-12-1926 ## 1954 ## yellow ## female ## brown ## false] +[20354d7a-e4fe-47af-8ff6-187bca92f3f9 ## Pandora Lovegood ## {} ## Ravenclaw ## quarter-veela ## human ## N/A ## false ## false ## N/A ## false ## 31-12-1926 ## 1959 ## dark ## female ## white ## true] +[fed624df-56d9-495e-9ad4-ea77000957e8 ## Petunia Dursley ## {} ## Slytherin ## muggle ## human ## N/A ## false ## false ## Fiona Shaw ## true ## 31-12-1926 ## 1954 ## yellow ## female ## blonde ## false] +[1413e1b3-2903-4a47-a2d5-e8abc5ce8014 ## Seamus Finnigan ## {O Flaherty, Seamus Finnegan} ## Gryffindor ## half-blood ## human ## N/A ## false ## true ## Devon Murray ## true ## 31-12-1926 ## 1960 ## amber ## male ## sandy ## true] +[57fe29d4-312a-4711-bd9a-c320253d9176 ## Victoire Weasley ## {} ## Slytherin ## pure-blood ## human ## N/A ## false ## true ## N/A ## true ## 31-12-1926 ## 1926 ## grey ## female ## blonde ## true] +[36bfefd0-e0bb-4d11-be98-d1ef6117a77a ## Rubeus Hagrid ## {Professor Hagrid, Hagger} ## Gryffindor ## half-blood ## half-giant ## N/A ## true ## false ## Robbie Coltrane ## true ## 06-12-1928 ## 1928 ## black ## male ## black ## true] +[6afb1960-febd-418d-b604-e50c1b59459b ## Bellatrix Lestrange ## {Bella} ## Slytherin ## pure-blood ## human ## N/A ## false ## false ## Helena Bonham Carter ## false ## 06-02-1950 ## 1951 ## brown ## female ## black ## true] +[0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3 ## Dobby ## {} ## Slytherin ## pure-blood ## house-elf ## N/A ## false ## false ## Toby Jones ## false ## 06-02-1950 ## 1950 ## green ## male ## blond ## false] +[2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f ## Sirius Black ## {Padfoot, Snuffles} ## Gryffindor ## pure-blood ## human ## hare ## false ## false ## Gary Oldman ## false ## 03-11-1959 ## 1959 ## grey ## male ## black ## true] +[41cd0bbe-a943-431b-9bde-bb2cad3491a1 ## Lily Potter ## {} ## Gryffindor ## muggleborn ## human ## N/A ## false ## false ## Geraldine Somerville ## false ## 30-01-1960 ## 1960 ## green ## female ## blond ## true] +[94c993f6-a443-4408-b908-6e78e223e8ac ## Aberforth Dumbledore ## {} ## Gryffindor ## half-blood ## human ## goat ## false ## false ## Jim McManus ## true ## 10-03-1960 ## 1960 ## blue ## male ## grey ## true] +[b415c867-1cff-455e-b194-748662ac2cca ## Albus Dumbledore ## {Professor Dumbledore} ## Gryffindor ## half-blood ## human ## Phoenix ## true ## false ## Richard Harris ## false ## 27-03-1960 ## 1881 ## blue ## male ## silver ## true] +[eaea5eb3-48a3-41c6-9ea5-c695299bab16 ## Lisa Turpin ## {} ## Ravenclaw ## pure-blood ## human ## N/A ## false ## true ## N/A ## true ## 27-03-1960 ## 1951 ## amber ## female ## sandy ## true] +[933787c2-51e3-4eac-8a85-ab332cac0456 ## Miranda Gaushawk ## {} ## Gryffindor ## muggle ## human ## N/A ## false ## false ## N/A ## false ## 27-03-1960 ## 1960 ## blue ## female ## silver ## true] +[00434cd3-fcc7-44c7-8f98-7368415b4206 ## Miriam Strout ## {} ## Gryffindor ## half-blood ## human ## N/A ## false ## false ## N/A ## true ## 27-03-1960 ## 1954 ## blue ## female ## silver ## true] +[11b5ca88-64ad-41a4-9f36-317b66c290af ## Nearly Headless Nick ## {Sir Nicholas de Mimsy-Porpington, Sir Nicholas, Nick} ## Gryffindor ## pure-blood ## ghost ## N/A ## false ## false ## John Cleese ## false ## 27-03-1960 ## 1926 ## amber ## male ## sandy ## true] +[3a0fe4df-2e40-4541-8d7f-13586f0b9294 ## Tom Riddle ## {Tom Riddle Senior} ## Ravenclaw ## muggle ## human ## N/A ## false ## false ## N/A ## false ## 27-03-1960 ## 1980 ## brown ## male ## dark ## false] +[6b59be3f-e527-422d-959d-79fcdb3b24eb ## Hedwig ## {} ## Hufflepuff ## muggle ## owl ## N/A ## false ## false ## Gizmo ## false ## 07-04-1979 ## 1954 ## amber ## female ## brown ## false] +[7614cf6e-689e-47ac-a976-b1e9997637e9 ## Dudley Dursley ## {} ## Slytherin ## muggle ## human ## N/A ## false ## false ## Harry Melling ## true ## 23-06-1980 ## 1980 ## blue ## male ## blond ## false] +[9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8 ## Harry Potter ## {The Boy Who Lived, The Chosen One, Undesirable No. 1, Potty} ## Gryffindor ## half-blood ## human ## stag ## false ## true ## Daniel Radcliffe ## true ## 31-07-1980 ## 1980 ## green ## male ## black ## true] +[2a0615de-8aa4-41e7-9504-dd875f5f3f01 ## George Weasley ## {} ## Gryffindor ## pure-blood ## human ## N/A ## false ## true ## Oliver Phelps ## true ## 13-02-1981 ## 1954 ## amber ## male ## red ## true] +[861c4cde-2f0f-4796-8d8f-9492e74b2573 ## Luna Lovegood ## {Loony Lovegood} ## Ravenclaw ## pure-blood ## human ## hare ## false ## true ## Evanna Lynch ## true ## 13-02-1981 ## 1981 ## silver ## female ## blonde ## true] diff --git a/tps/fonte/tp02/TP02Q07 - Ordenação por Inserção em Java/pub.in b/tps/fonte/tp02/TP02Q07 - Ordenação por Inserção em Java/pub.in new file mode 100644 index 0000000..a782d8c --- /dev/null +++ b/tps/fonte/tp02/TP02Q07 - Ordenação por Inserção em Java/pub.in @@ -0,0 +1,28 @@ +9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8 +1413e1b3-2903-4a47-a2d5-e8abc5ce8014 +ca3827f0-375a-4891-aaa5-f5e8a5bad225 +36bfefd0-e0bb-4d11-be98-d1ef6117a77a +20354d7a-e4fe-47af-8ff6-187bca92f3f9 +57fe29d4-312a-4711-bd9a-c320253d9176 +b415c867-1cff-455e-b194-748662ac2cca +5a4c95db-947d-4914-a631-41e8d466328e +861c4cde-2f0f-4796-8d8f-9492e74b2573 +2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f +41cd0bbe-a943-431b-9bde-bb2cad3491a1 +2a0615de-8aa4-41e7-9504-dd875f5f3f01 +11b5ca88-64ad-41a4-9f36-317b66c290af +eaea5eb3-48a3-41c6-9ea5-c695299bab16 +0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3 +b0620914-858d-46fc-8e6d-033c565e138b +6b59be3f-e527-422d-959d-79fcdb3b24eb +fed624df-56d9-495e-9ad4-ea77000957e8 +d58e7249-19d1-40bd-a43f-1da0497fe8aa +3a0fe4df-2e40-4541-8d7f-13586f0b9294 +6afb1960-febd-418d-b604-e50c1b59459b +efa802c8-ae18-4ae1-a524-49df21d05939 +933787c2-51e3-4eac-8a85-ab332cac0456 +94c993f6-a443-4408-b908-6e78e223e8ac +00434cd3-fcc7-44c7-8f98-7368415b4206 +7614cf6e-689e-47ac-a976-b1e9997637e9 +d59691a4-f830-4eb0-a819-a0fb00b7e80f +FIM \ No newline at end of file diff --git a/tps/fonte/tp02/TP02Q08 - Shellsort em C/753045_shellsort.txt b/tps/fonte/tp02/TP02Q08 - Shellsort em C/753045_shellsort.txt new file mode 100644 index 0000000..9b0fa09 --- /dev/null +++ b/tps/fonte/tp02/TP02Q08 - Shellsort em C/753045_shellsort.txt @@ -0,0 +1 @@ +753045 0ms 87 \ No newline at end of file diff --git a/tps/fonte/tp02/TP02Q08 - Shellsort em C/Character b/tps/fonte/tp02/TP02Q08 - Shellsort em C/Character new file mode 100644 index 0000000000000000000000000000000000000000..d4eeca51d3a1d06c5121da7fd7bf41258d6badf3 GIT binary patch literal 27848 zcmeHw4|tT-neRzLARuI-g_PD(w>SCL^w4F%QRM5s6F=0Pg5&6t{r1I0$Zbx+!EthuD%}G?zwT ztMW@%FZrtcnyT!@?RB+V=HGdHUG?m`+J<23?A8VIXV1U0u%)qZF5mZ56omTJ%GH(P z_d_$5pWoeb^!%%{CKexh<(bm5NG6q=%0T`|h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)j0YP;H!k}h3#YV} literal 0 HcmV?d00001 diff --git a/tps/fonte/tp02/TP02Q08 - Shellsort em C/Character.c b/tps/fonte/tp02/TP02Q08 - Shellsort em C/Character.c new file mode 100644 index 0000000..a2140d5 --- /dev/null +++ b/tps/fonte/tp02/TP02Q08 - Shellsort em C/Character.c @@ -0,0 +1,709 @@ +/** + * @path TP02Q02 - Classe em C/Character.c + * @description C file that implements the Character class with shell sort algorithm + * @author Pedro Lopes - github.com/httpspedroh + * @version 1.0 + * @update 2024-04-09 + */ + +// ---------------------------------------------------------------------------------------------------- // + +// Includes +#include +#include +#include +#include +#include + +// ---------------------------------------------------------------------------------------------------- // + +// Constants +#define MAX_CHARACTERS 405 +#define FILE_PATH "/tmp/characters.csv" + +#define MAX_UUID_SIZE 37 +#define MAX_NAME_SIZE 30 +#define MAX_ALTERNATE_NAMES 10 +#define MAX_ALTERNATE_NAME_SIZE 35 +#define MAX_HOUSE_SIZE 15 +#define MAX_ANCESTRY_SIZE 15 +#define MAX_SPECIES_SIZE 20 +#define MAX_PATRONUS_SIZE 25 +#define MAX_ACTOR_NAME_SIZE 35 +#define MAX_EYE_COLOUR_SIZE 10 +#define MAX_GENDER_SIZE 10 +#define MAX_HAIR_COLOUR_SIZE 10 + +#define MAX_LINE_SIZE 300 + +// ---------------------------------------------------------------------------------------------------- // + +// Structs +typedef struct Date { + + int day; + int month; + int year; +} Date; + +typedef struct Character { + + char *id; + char *name; + char *alternateNames[MAX_ALTERNATE_NAMES]; + char *house; + char *ancestry; + char *species; + char *patronus; + bool hogwartsStaff; + bool hogwartsStudent; + char *actorName; + bool alive; + Date birthDate; + int yearOfBirth; + char *eyeColour; + char *gender; + char *hairColour; + bool wizard; +} Character; + +// ---------------------------------------------------------------------------------------------------- // + +// Global variables +Character characters[MAX_CHARACTERS]; +int charactersLength = 0; + +// ---------------------------------------------------------------------------------------------------- // + +// Functions +bool isEnd(char* line) { return line[0] == 'F' && line[1] == 'I' && line[2] == 'M'; } + +void substring(char *string, char *stringStart, int length) { + + strncpy(string, stringStart, length); + string[length] = '\0'; +} + +void proccess_attribute(char *attribute, char **substringStart, char **substringEnd, bool isFirstAttribute, bool isStringArray) { + + // Skip first comma + if(!isFirstAttribute) { + + if(*substringEnd != NULL) *substringStart = *substringEnd + 1; + else *substringStart = *substringEnd; + } + + if(!isStringArray) { + + if((*substringStart)[0] == '"') { + + *substringStart = *substringStart + 1; + *substringEnd = strchr(*substringStart, '"'); + } + else *substringEnd = strchr(*substringStart, ';'); + + // Get substring + if(*substringEnd) { + + substring(attribute, *substringStart, *substringEnd - *substringStart); + + if(*substringEnd[0] == '"') *substringEnd = *substringEnd + 1; + } + else strcpy(attribute, *substringStart); + + // Set default value if attribute is empty + if(strcmp(attribute, "") == 0 || attribute[0] == '\n' || attribute[0] == '\r' || attribute[0] == '\0') strcpy(attribute, "N/A"); + + // Clean \n from the end of the string + if(attribute[strlen(attribute) - 1] == '\n' || attribute[strlen(attribute) - 1] == '\r') attribute[strlen(attribute) - 1] = '\0'; + } + else { + + // Check if the first character is a [ + if((*substringStart)[0] == '[') { + + *substringStart = *substringStart + 1; + + if((*substringStart)[0] == ']') strcpy(attribute, ""); // Case: [] + else { + + char *tempConcat = (char *) calloc(MAX_LINE_SIZE, sizeof(char)); + + *substringStart = *substringStart - 1; + + while(1) { + + *substringStart = *substringStart + 1; + + if((*substringStart)[0] == ';') break; + else if((*substringStart)[0] == '\'') { // Case: "['example', 'example']" + + *substringStart = *substringStart + 1; + *substringEnd = strchr(*substringStart, '\''); + + // Get substring + if(*substringEnd) { + + // Create tmp name + char tmp[MAX_LINE_SIZE]; + substring(tmp, *substringStart, *substringEnd - *substringStart); + + // Concat tempConcat with tmp + strcat(tempConcat, tmp); + strcat(tempConcat, ", "); + + *substringStart = *substringEnd + 1; + } + } + } + + // Get substring + strcpy(attribute, tempConcat); + + // Clean "attribute" removing last 2 characters + attribute[strlen(attribute) - 2] = '\0'; + } + } + + *substringEnd = strchr(*substringStart, ';'); + } +} + +// ---------------------------------------------------------------------------------------------------- // + +// Methods implementations + +// Gets +char *character_getId(Character *character) { return character -> id; } +char *character_getName(Character *character) { return character -> name; } +char *character_getHouse(Character *character) { return character -> house; } +char *character_getAncestry(Character *character) { return character -> ancestry; } +char *character_getSpecies(Character *character) { return character -> species; } +char *character_getPatronus(Character *character) { return character -> patronus; } +bool character_getHogwartsStaff(Character *character) { return character -> hogwartsStaff; } +bool character_getHogwartsStudent(Character *character) { return character -> hogwartsStudent; } +char *character_getActorName(Character *character) { return character -> actorName; } +bool character_getAlive(Character *character) { return character -> alive; } +char *character_getEyeColour(Character *character) { return character -> eyeColour; } +char *character_getGender(Character *character) { return character -> gender; } +char *character_getHairColour(Character *character) { return character -> hairColour; } +bool character_getWizard(Character *character) { return character -> wizard; } + +char *character_getYearOfBirth(Character *character) { + + // "N/A" if yearOfBirth is -1 + char *yearOfBirth = (char *) calloc(15, sizeof(char)); + strcpy(yearOfBirth, "N/A"); + + if(character -> yearOfBirth != -1) sprintf(yearOfBirth, "%d", character -> yearOfBirth); + return yearOfBirth; +} + +char *character_getBirthDate(Character *character) { + + // "N/A" if day, month and year are -1 + char *birthDate = (char *) calloc(15, sizeof(char)); + strcpy(birthDate, "N/A"); + + if(character -> birthDate.day != -1 && character -> birthDate.month != -1 && character -> birthDate.year != -1) { + + sprintf(birthDate, "%02d-%02d-%04d", character -> birthDate.day, character -> birthDate.month, character -> birthDate.year); + } + + return birthDate; +} + +char *character_getAlternateNames(Character *character) { + + // Concatenate all alternate names with a comma + char *alternateNames = (char *) calloc(MAX_ALTERNATE_NAME_SIZE * MAX_ALTERNATE_NAMES, sizeof(char)); + + alternateNames[0] = '{'; + + for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) { + + if(strcmp(character -> alternateNames[i], "") != 0) { + + strcat(alternateNames, character -> alternateNames[i]); + + if(strcmp(character -> alternateNames[i + 1], "") != 0) strcat(alternateNames, ", "); + } + } + + strcat(alternateNames, "}"); + + return alternateNames; +} + +// Sets +void character_setId(Character *character, char *id) { strcpy(character -> id, id); } +void character_setName(Character *character, char *name) { strcpy(character -> name, name); } +void character_setHouse(Character *character, char *house) { strcpy(character -> house, house); } +void character_setAncestry(Character *character, char *ancestry) { strcpy(character -> ancestry, ancestry); } +void character_setSpecies(Character *character, char *species) { strcpy(character -> species, species); } +void character_setPatronus(Character *character, char *patronus) { strcpy(character -> patronus, patronus); } +void character_setHogwartsStaff(Character *character, bool hogwartsStaff) { character -> hogwartsStaff = hogwartsStaff; } +void character_setHogwartsStudent(Character *character, bool hogwartsStudent) { character -> hogwartsStudent = hogwartsStudent; } +void character_setActorName(Character *character, char *actorName) { strcpy(character -> actorName, actorName); } +void character_setAlive(Character *character, bool alive) { character -> alive = alive; } +void character_setYearOfBirth(Character *character, int yearOfBirth) { character -> yearOfBirth = yearOfBirth; } +void character_setEyeColour(Character *character, char *eyeColour) { strcpy(character -> eyeColour, eyeColour); } +void character_setGender(Character *character, char *gender) { strcpy(character -> gender, gender); } +void character_setHairColour(Character *character, char *hairColour) { strcpy(character -> hairColour, hairColour); } +void character_setWizard(Character *character, bool wizard) { character -> wizard = wizard; } + +void character_setBirthDate(Character *character, char *birthDate) { + + // Explode birthDate in format DD-MM-YYYY if in format DD-MM-YYYY + if(strlen(birthDate) >= 8 && strlen(birthDate) <= 10) { + + char *token = strtok(birthDate, "-"); + + character -> birthDate.day = atoi(token); + token = strtok(NULL, "-"); + character -> birthDate.month = atoi(token); + token = strtok(NULL, "-"); + character -> birthDate.year = atoi(token); + } +} + +void character_setAlternateNames(Character *character, char *alternateNames) { + + // Copy names to a temporary variable + char tempNames[MAX_ALTERNATE_NAME_SIZE * MAX_ALTERNATE_NAMES]; + strcpy(tempNames, alternateNames); + + // Separate names by comma + char *token = strtok(tempNames, ","); + int i = 0; + + // Copy names to the character + while (token != NULL && i < MAX_ALTERNATE_NAMES) { + + while (*token == ' ') token++; + + int len = strlen(token); + + while (len > 0 && token[len - 1] == ' ') { + + token[len - 1] = '\0'; + len--; + } + + strcpy(character -> alternateNames[i++], token); + token = strtok(NULL, ","); + } +} + +// Class +Character character_newBlank() { + + Character character; + + character.id = (char *) calloc(MAX_UUID_SIZE, sizeof(char)); + strcpy(character.id, ""); + + character.name = (char *) calloc(MAX_NAME_SIZE, sizeof(char)); + strcpy(character.name, ""); + + for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) { + + character.alternateNames[i] = (char *) calloc(MAX_ALTERNATE_NAME_SIZE, sizeof(char)); + strcpy(character.alternateNames[i], ""); + } + + character.house = (char *) calloc(MAX_HOUSE_SIZE, sizeof(char)); + strcpy(character.house, ""); + + character.ancestry = (char *) calloc(MAX_ANCESTRY_SIZE, sizeof(char)); + strcpy(character.ancestry, ""); + + character.species = (char *) calloc(MAX_SPECIES_SIZE, sizeof(char)); + strcpy(character.species, ""); + + character.patronus = (char *) calloc(MAX_PATRONUS_SIZE, sizeof(char)); + strcpy(character.patronus, ""); + + character.hogwartsStaff = false; + character.hogwartsStudent = false; + + character.actorName = (char *) calloc(MAX_ACTOR_NAME_SIZE, sizeof(char)); + strcpy(character.actorName, ""); + + character.alive = false; + + character.birthDate.day = -1; + character.birthDate.month = -1; + character.birthDate.year = -1; + + character.yearOfBirth = -1; + + character.eyeColour = (char *) calloc(MAX_EYE_COLOUR_SIZE, sizeof(char)); + strcpy(character.eyeColour, ""); + + character.gender = (char *) calloc(MAX_GENDER_SIZE, sizeof(char)); + strcpy(character.gender, ""); + + character.hairColour = (char *) calloc(MAX_HAIR_COLOUR_SIZE, sizeof(char)); + strcpy(character.hairColour, ""); + + character.wizard = false; + + return character; +} + +Character character_new(char *id, char *name, char *house, char *ancestry, char *species, char *patronus, bool hogwartsStaff, bool hogwartsStudent, char *actorName, bool alive, Date birthDate, int yearOfBirth, char *eyeColour, char *gender, char *hairColour, bool wizard) { + + Character character; + + character.id = (char *) calloc(MAX_UUID_SIZE, sizeof(char)); + strcpy(character.id, id); + + character.name = (char *) calloc(MAX_NAME_SIZE, sizeof(char)); + strcpy(character.name, name); + + for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) { + + character.alternateNames[i] = (char *) calloc(MAX_ALTERNATE_NAME_SIZE, sizeof(char)); + strcpy(character.alternateNames[i], ""); + } + + character.house = (char *) calloc(MAX_HOUSE_SIZE, sizeof(char)); + strcpy(character.house, house); + + character.ancestry = (char *) calloc(MAX_ANCESTRY_SIZE, sizeof(char)); + strcpy(character.ancestry, ancestry); + + character.species = (char *) calloc(MAX_SPECIES_SIZE, sizeof(char)); + strcpy(character.species, species); + + character.patronus = (char *) calloc(MAX_PATRONUS_SIZE, sizeof(char)); + strcpy(character.patronus, patronus); + + character.hogwartsStaff = hogwartsStaff; + character.hogwartsStudent = hogwartsStudent; + + character.actorName = (char *) calloc(MAX_ACTOR_NAME_SIZE, sizeof(char)); + strcpy(character.actorName, actorName); + + character.alive = alive; + character.birthDate = birthDate; + character.yearOfBirth = yearOfBirth; + + character.eyeColour = (char *) calloc(MAX_EYE_COLOUR_SIZE, sizeof(char)); + strcpy(character.eyeColour, eyeColour); + + character.gender = (char *) calloc(MAX_GENDER_SIZE, sizeof(char)); + strcpy(character.gender, gender); + + character.hairColour = (char *) calloc(MAX_HAIR_COLOUR_SIZE, sizeof(char)); + strcpy(character.hairColour, hairColour); + + character.wizard = wizard; + + return character; +} + +Character *character_clone(Character *character) { + + Character *clone = (Character *) malloc(sizeof(Character)); + + clone -> id = (char *) calloc(MAX_UUID_SIZE, sizeof(char)); + strcpy(clone -> id, character -> id); + + clone -> name = (char *) calloc(MAX_NAME_SIZE, sizeof(char)); + strcpy(clone -> name, character -> name); + + for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) { + + clone -> alternateNames[i] = (char *) calloc(MAX_ALTERNATE_NAME_SIZE, sizeof(char)); + strcpy(clone -> alternateNames[i], character -> alternateNames[i]); + } + + clone -> house = (char *) calloc(MAX_HOUSE_SIZE, sizeof(char)); + strcpy(clone -> house, character -> house); + + clone -> ancestry = (char *) calloc(MAX_ANCESTRY_SIZE, sizeof(char)); + strcpy(clone -> ancestry, character -> ancestry); + + clone -> species = (char *) calloc(MAX_SPECIES_SIZE, sizeof(char)); + strcpy(clone -> species, character -> species); + + clone -> patronus = (char *) calloc(MAX_PATRONUS_SIZE, sizeof(char)); + strcpy(clone -> patronus, character -> patronus); + + clone -> hogwartsStaff = character -> hogwartsStaff; + clone -> hogwartsStudent = character -> hogwartsStudent; + + clone -> actorName = (char *) calloc(MAX_ACTOR_NAME_SIZE, sizeof(char)); + strcpy(clone -> actorName, character -> actorName); + + clone -> alive = character -> alive; + clone -> birthDate = character -> birthDate; + clone -> yearOfBirth = character -> yearOfBirth; + + clone -> eyeColour = (char *) calloc(MAX_EYE_COLOUR_SIZE, sizeof(char)); + strcpy(clone -> eyeColour, character -> eyeColour); + + clone -> gender = (char *) calloc(MAX_GENDER_SIZE, sizeof(char)); + strcpy(clone -> gender, character -> gender); + + clone -> hairColour = (char *) calloc(MAX_HAIR_COLOUR_SIZE, sizeof(char)); + strcpy(clone -> hairColour, character -> hairColour); + + clone -> wizard = character -> wizard; + + return clone; +} + +void character_print(Character *character) { + + printf("[%s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s]\n", + + character_getId(character), + character_getName(character), + character_getAlternateNames(character), + character_getHouse(character), + character_getAncestry(character), + character_getSpecies(character), + character_getPatronus(character), + character_getHogwartsStaff(character) ? "true" : "false", + character_getHogwartsStudent(character) ? "true" : "false", + character_getActorName(character), + character_getAlive(character) ? "true" : "false", + character_getBirthDate(character), + character_getYearOfBirth(character), + character_getEyeColour(character), + character_getGender(character), + character_getHairColour(character), + character_getWizard(character) ? "true" : "false" + ); +} + +Character character_read(char *line) { + + Character character = character_newBlank(); + + char *substringStart = line; + char *substringEnd = NULL; + char attribute[MAX_LINE_SIZE]; + + // Get id + proccess_attribute(attribute, &substringStart, &substringEnd, true, false); + character_setId(&character, attribute); + + // Get name + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setName(&character, attribute); + + // Get alternate names + proccess_attribute(attribute, &substringStart, &substringEnd, false, true); + character_setAlternateNames(&character, attribute); + + // Get house + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setHouse(&character, attribute); + + // Get ancestry + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setAncestry(&character, attribute); + + // Get species + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setSpecies(&character, attribute); + + // Get patronus + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setPatronus(&character, attribute); + + // Get hogwarts staff + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setHogwartsStaff(&character, strcmp(attribute, "VERDADEIRO") == 0); + + // Get hogwarts student + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setHogwartsStudent(&character, strcmp(attribute, "VERDADEIRO") == 0); + + // Get actor name + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setActorName(&character, attribute); + + // Get alive + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setAlive(&character, strcmp(attribute, "VERDADEIRO") == 0); + + // Get birth date + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setBirthDate(&character, attribute); + + // Get year of birth + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setYearOfBirth(&character, atoi(attribute) == 0 ? -1 : atoi(attribute)); + + // Get eye colour + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setEyeColour(&character, attribute); + + // Get gender + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setGender(&character, attribute); + + // Get hair colour + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setHairColour(&character, attribute); + + // Get wizard + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + + // Clean \n from the end of the string + if(attribute[strlen(attribute) - 1] == '\n' || attribute[strlen(attribute) - 1] == '\r') attribute[strlen(attribute) - 1] = '\0'; + + character_setWizard(&character, strcmp(attribute, "VERDADEIRO") == 0); + return character; +} + +Character *character_searchById(char *id) { + + for(int i = 0; i < charactersLength; i++) { + + if(!strcmp(characters[i].id, id)) return &characters[i]; + } + return NULL; +} + +// General +void startCharacters() { + + // Open file + FILE *fp; + char *line = NULL; + size_t len = 0; + ssize_t read; + + fp = fopen(FILE_PATH, "r"); + + if(fp == NULL) { + + perror("x Error opening file"); + exit(EXIT_FAILURE); + } + + // Skip first line + getline(&line, &len, fp); + + // Read all lines + while((read = getline(&line, &len, fp)) != -1) { + + // Read character from line + Character character = character_read(line); + + characters[charactersLength++] = character; + + if(charactersLength >= MAX_CHARACTERS) { + + perror("x Max characters reached"); + exit(EXIT_FAILURE); + } + } + + // Close file and free memory + fclose(fp); + + if(line) free(line); +} + +// ---------------------------------------------------------------------------------------------------- // + +// Main +int main() { + + // ----------------------------------------------------------------- // + + // #1 - Start - Read all characters in CSV file + startCharacters(); + + // ----------------------------------------------------------------- // + + // #2 - Read input and print characters from pub.in id entries and add to mainCharacters array + + // Initialize mainCharacters array + Character mainCharacters[MAX_CHARACTERS]; + int m = 0; + + char id[MAX_UUID_SIZE]; + scanf(" %[^\n]s", id); + + while(true) { + + // Clean \n from the end of the string + if(id[strlen(id) - 1] == '\n' || id[strlen(id) - 1] == '\r') id[strlen(id) - 1] = '\0'; + + if(isEnd(id)) break; + else { + + Character *character = character_searchById(id); + + if(character) mainCharacters[m++] = *character; + + // ------------------------- // + + scanf(" %[^\n]s", id); + } + } + + // ----------------------------------------------------------------- // + + // #3 - Order mainCharacters array by key "eyeColour", in draw case, order by key "name" + + // Start benchmark + clock_t startTime = clock(); + int comparisons = 0; + + // ----------------- // + + // Shell sort + for(int gap = m / 2; gap > 0; gap /= 2) { + + for(int i = gap; i < m; i++) { + + Character temp = mainCharacters[i]; + int j; + + for(j = i; j >= gap; j -= gap) { + + if(strcmp(character_getEyeColour(&mainCharacters[j - gap]), character_getEyeColour(&temp)) > 0) { + + comparisons++; + mainCharacters[j] = mainCharacters[j - gap]; + } + else if(strcmp(character_getEyeColour(&mainCharacters[j - gap]), character_getEyeColour(&temp)) == 0) { + + // In case of a draw in eyeColour, compare by name + comparisons++; + + if(strcmp(character_getName(&mainCharacters[j - gap]), character_getName(&temp)) > 0) mainCharacters[j] = mainCharacters[j - gap]; + else break; + } + else break; + } + + mainCharacters[j] = temp; + } + } + + // ----------------- // + + // Save benchmark in file + FILE *fp = fopen("753045_shellsort.txt", "w"); + fprintf(fp, "753045\t%.0fms\t%d", (double)(clock() - startTime) / CLOCKS_PER_SEC * 1000.0, comparisons); + fclose(fp); + + // ----------------- // + + // Print mainCharacters array + for(int i = 0; i < m; i++) character_print(&mainCharacters[i]); + + // ----------------- // + + return 0; +} \ No newline at end of file diff --git a/tps/fonte/tp02/TP02Q08 - Shellsort em C/pedro.out b/tps/fonte/tp02/TP02Q08 - Shellsort em C/pedro.out new file mode 100644 index 0000000..33137ed --- /dev/null +++ b/tps/fonte/tp02/TP02Q08 - Shellsort em C/pedro.out @@ -0,0 +1,27 @@ +[efa802c8-ae18-4ae1-a524-49df21d05939 ## Lord Voldemort ## {Tom Riddle, Tom Marvolo Riddle, You-Know-Who, He Who Must Not Be Named, the Dark Lord, the Heir of Slytherin} ## Slytherin ## half-blood ## human ## N/A ## false ## false ## Ralph Fiennes ## false ## 31-12-1926 ## 1926 ## Scarlet ## male ## bald ## true] +[2a0615de-8aa4-41e7-9504-dd875f5f3f01 ## George Weasley ## {} ## Gryffindor ## pure-blood ## human ## N/A ## false ## true ## Oliver Phelps ## true ## 13-02-1981 ## 1954 ## amber ## male ## red ## true] +[6b59be3f-e527-422d-959d-79fcdb3b24eb ## Hedwig ## {} ## Hufflepuff ## muggle ## owl ## N/A ## false ## false ## Gizmo ## false ## 07-04-1979 ## 1954 ## amber ## female ## brown ## false] +[eaea5eb3-48a3-41c6-9ea5-c695299bab16 ## Lisa Turpin ## {} ## Ravenclaw ## pure-blood ## human ## N/A ## false ## true ## N/A ## true ## 27-03-1960 ## 1951 ## amber ## female ## sandy ## true] +[11b5ca88-64ad-41a4-9f36-317b66c290af ## Nearly Headless Nick ## {Sir Nicholas de Mimsy-Porpington, Sir Nicholas, Nick} ## Gryffindor ## pure-blood ## ghost ## N/A ## false ## false ## John Cleese ## false ## 27-03-1960 ## 1926 ## amber ## male ## sandy ## true] +[1413e1b3-2903-4a47-a2d5-e8abc5ce8014 ## Seamus Finnigan ## {O Flaherty, Seamus Finnegan} ## Gryffindor ## half-blood ## human ## N/A ## false ## true ## Devon Murray ## true ## 31-12-1926 ## 1960 ## amber ## male ## sandy ## true] +[36bfefd0-e0bb-4d11-be98-d1ef6117a77a ## Rubeus Hagrid ## {Professor Hagrid, Hagger} ## Gryffindor ## half-blood ## half-giant ## N/A ## true ## false ## Robbie Coltrane ## true ## 06-12-1928 ## 1928 ## black ## male ## black ## true] +[94c993f6-a443-4408-b908-6e78e223e8ac ## Aberforth Dumbledore ## {} ## Gryffindor ## half-blood ## human ## goat ## false ## false ## Jim McManus ## true ## 10-03-1960 ## 1960 ## blue ## male ## grey ## true] +[b415c867-1cff-455e-b194-748662ac2cca ## Albus Dumbledore ## {Professor Dumbledore} ## Gryffindor ## half-blood ## human ## Phoenix ## true ## false ## Richard Harris ## false ## 27-03-1960 ## 1881 ## blue ## male ## silver ## true] +[7614cf6e-689e-47ac-a976-b1e9997637e9 ## Dudley Dursley ## {} ## Slytherin ## muggle ## human ## N/A ## false ## false ## Harry Melling ## true ## 23-06-1980 ## 1980 ## blue ## male ## blond ## false] +[933787c2-51e3-4eac-8a85-ab332cac0456 ## Miranda Gaushawk ## {} ## Gryffindor ## muggle ## human ## N/A ## false ## false ## N/A ## false ## 27-03-1960 ## 1960 ## blue ## female ## silver ## true] +[00434cd3-fcc7-44c7-8f98-7368415b4206 ## Miriam Strout ## {} ## Gryffindor ## half-blood ## human ## N/A ## false ## false ## N/A ## true ## 27-03-1960 ## 1954 ## blue ## female ## silver ## true] +[6afb1960-febd-418d-b604-e50c1b59459b ## Bellatrix Lestrange ## {Bella} ## Slytherin ## pure-blood ## human ## N/A ## false ## false ## Helena Bonham Carter ## false ## 06-02-1950 ## 1951 ## brown ## female ## black ## true] +[d58e7249-19d1-40bd-a43f-1da0497fe8aa ## Dolores Umbridge ## {} ## Slytherin ## half-blood ## human ## persian cat ## true ## false ## Imelda Staunton ## true ## 31-12-1926 ## 1926 ## brown ## female ## grey ## true] +[3a0fe4df-2e40-4541-8d7f-13586f0b9294 ## Tom Riddle ## {Tom Riddle Senior} ## Ravenclaw ## muggle ## human ## N/A ## false ## false ## N/A ## false ## 27-03-1960 ## 1980 ## brown ## male ## dark ## false] +[5a4c95db-947d-4914-a631-41e8d466328e ## Kendra Dumbledore ## {} ## Slytherin ## muggleborn ## human ## N/A ## false ## false ## N/A ## false ## 31-12-1926 ## 1925 ## dark ## female ## black ## true] +[20354d7a-e4fe-47af-8ff6-187bca92f3f9 ## Pandora Lovegood ## {} ## Ravenclaw ## quarter-veela ## human ## N/A ## false ## false ## N/A ## false ## 31-12-1926 ## 1959 ## dark ## female ## white ## true] +[0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3 ## Dobby ## {} ## Slytherin ## pure-blood ## house-elf ## N/A ## false ## false ## Toby Jones ## false ## 06-02-1950 ## 1950 ## green ## male ## blond ## false] +[9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8 ## Harry Potter ## {The Boy Who Lived, The Chosen One, Undesirable No. 1, Potty} ## Gryffindor ## half-blood ## human ## stag ## false ## true ## Daniel Radcliffe ## true ## 31-07-1980 ## 1980 ## green ## male ## black ## true] +[41cd0bbe-a943-431b-9bde-bb2cad3491a1 ## Lily Potter ## {} ## Gryffindor ## muggleborn ## human ## N/A ## false ## false ## Geraldine Somerville ## false ## 30-01-1960 ## 1960 ## green ## female ## blond ## true] +[ca3827f0-375a-4891-aaa5-f5e8a5bad225 ## Minerva McGonagall ## {} ## Gryffindor ## half-blood ## human ## tabby cat ## true ## false ## Dame Maggie Smith ## true ## 04-10-1925 ## 1925 ## grey ## female ## black ## true] +[2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f ## Sirius Black ## {Padfoot, Snuffles} ## Gryffindor ## pure-blood ## human ## hare ## false ## false ## Gary Oldman ## false ## 03-11-1959 ## 1959 ## grey ## male ## black ## true] +[57fe29d4-312a-4711-bd9a-c320253d9176 ## Victoire Weasley ## {} ## Slytherin ## pure-blood ## human ## N/A ## false ## true ## N/A ## true ## 31-12-1926 ## 1926 ## grey ## female ## blonde ## true] +[d59691a4-f830-4eb0-a819-a0fb00b7e80f ## Garrick Ollivander ## {} ## Hufflepuff ## muggle ## human ## N/A ## false ## false ## John Hurt ## true ## 04-10-1925 ## 1954 ## pale, silvery ## male ## brown ## true] +[861c4cde-2f0f-4796-8d8f-9492e74b2573 ## Luna Lovegood ## {Loony Lovegood} ## Ravenclaw ## pure-blood ## human ## hare ## false ## true ## Evanna Lynch ## true ## 13-02-1981 ## 1981 ## silver ## female ## blonde ## true] +[b0620914-858d-46fc-8e6d-033c565e138b ## Mrs Norris ## {} ## Slytherin ## pure-blood ## cat ## N/A ## true ## false ## Maxime, Alanis and Tommy the cats ## true ## 31-12-1926 ## 1954 ## yellow ## female ## brown ## false] +[fed624df-56d9-495e-9ad4-ea77000957e8 ## Petunia Dursley ## {} ## Slytherin ## muggle ## human ## N/A ## false ## false ## Fiona Shaw ## true ## 31-12-1926 ## 1954 ## yellow ## female ## blonde ## false] diff --git a/tps/fonte/tp02/TP02Q08 - Shellsort em C/pub.in b/tps/fonte/tp02/TP02Q08 - Shellsort em C/pub.in new file mode 100644 index 0000000..a782d8c --- /dev/null +++ b/tps/fonte/tp02/TP02Q08 - Shellsort em C/pub.in @@ -0,0 +1,28 @@ +9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8 +1413e1b3-2903-4a47-a2d5-e8abc5ce8014 +ca3827f0-375a-4891-aaa5-f5e8a5bad225 +36bfefd0-e0bb-4d11-be98-d1ef6117a77a +20354d7a-e4fe-47af-8ff6-187bca92f3f9 +57fe29d4-312a-4711-bd9a-c320253d9176 +b415c867-1cff-455e-b194-748662ac2cca +5a4c95db-947d-4914-a631-41e8d466328e +861c4cde-2f0f-4796-8d8f-9492e74b2573 +2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f +41cd0bbe-a943-431b-9bde-bb2cad3491a1 +2a0615de-8aa4-41e7-9504-dd875f5f3f01 +11b5ca88-64ad-41a4-9f36-317b66c290af +eaea5eb3-48a3-41c6-9ea5-c695299bab16 +0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3 +b0620914-858d-46fc-8e6d-033c565e138b +6b59be3f-e527-422d-959d-79fcdb3b24eb +fed624df-56d9-495e-9ad4-ea77000957e8 +d58e7249-19d1-40bd-a43f-1da0497fe8aa +3a0fe4df-2e40-4541-8d7f-13586f0b9294 +6afb1960-febd-418d-b604-e50c1b59459b +efa802c8-ae18-4ae1-a524-49df21d05939 +933787c2-51e3-4eac-8a85-ab332cac0456 +94c993f6-a443-4408-b908-6e78e223e8ac +00434cd3-fcc7-44c7-8f98-7368415b4206 +7614cf6e-689e-47ac-a976-b1e9997637e9 +d59691a4-f830-4eb0-a819-a0fb00b7e80f +FIM \ No newline at end of file diff --git a/tps/fonte/tp02/TP02Q09 - Heapsort em Java/753045_heapsort.txt b/tps/fonte/tp02/TP02Q09 - Heapsort em Java/753045_heapsort.txt new file mode 100644 index 0000000..2605076 --- /dev/null +++ b/tps/fonte/tp02/TP02Q09 - Heapsort em Java/753045_heapsort.txt @@ -0,0 +1 @@ +753045 25ms 0 \ No newline at end of file diff --git a/tps/fonte/tp02/TP02Q09 - Heapsort em Java/Arq.java b/tps/fonte/tp02/TP02Q09 - Heapsort em Java/Arq.java new file mode 100644 index 0000000..9a632d1 --- /dev/null +++ b/tps/fonte/tp02/TP02Q09 - Heapsort em Java/Arq.java @@ -0,0 +1,206 @@ +import java.io.*; +import java.util.Formatter; +import java.util.Scanner; +import java.io.File; +import java.nio.charset.*; + +public class Arq +{ + private static String nomeArquivo = ""; + private static String charsetArquivo = "ISO-8859-1"; + private static boolean write = false, read = false; + private static Formatter saida = null; + private static Scanner entrada = null; + + public static boolean openWrite(String nomeArq, String charset) { + boolean resp = false; + close(); + try{ + saida = new Formatter(nomeArq, charset); + nomeArquivo = nomeArq; + resp = write = true; + } catch (Exception e) {} + return resp; + } + + public static boolean openWrite(String nomeArq) { + return openWrite(nomeArq, charsetArquivo); + } + + public static boolean openWriteClose(String nomeArq, String charset, String conteudo) { + boolean resp = openWrite(nomeArq, charset); + if(resp == true){ + println(conteudo); + close(); + } + return resp; + } + + public static boolean openWriteClose(String nomeArq, String conteudo) { + return openWriteClose(nomeArq, charsetArquivo, conteudo); + } + + public static boolean openRead(String nomeArq) { + return openRead(nomeArq, charsetArquivo); + } + + public static boolean openRead(String nomeArq, String charset) { + boolean resp = false; + close(); + try{ + entrada = new Scanner(new File(nomeArq), charset); + nomeArquivo = nomeArq; + resp = read = true; + } catch (Exception e) {} + return resp; + } + + public static String openReadClose(String nomeArq){ + openRead(nomeArq); + String resp = readAll(); + close(); + return resp; + } + + public static void close() { + if(write == true){ + saida.close(); + } + if(read == true){ + entrada.close(); + } + write = read = false; + nomeArquivo = ""; + charsetArquivo = "ISO-8859-1"; + } + + public static long length(){ + long resp = -1; + if(read != write){ + File file = new File(nomeArquivo); + resp = file.length(); + } + return resp; + } + + public static void print(int x){ + if(write == true){ + saida.format( "%d", x); + } + } + + public static void print(double x){ + if(write == true){ + saida.format( "%f", x); + } + } + + public static void print(String x){ + if(write == true){ + saida.format( "%s", x); + } + } + + public static void print(boolean x){ + if(write == true){ + saida.format( "%s", ((x) ? "true" : "false")); + } + } + + public static void print(char x){ + if(write == true){ + saida.format( "%c", x); + } + } + + public static void println(int x){ + if(write == true){ + saida.format( "%d\n", x); + } + } + + public static void println(double x){ + if(write == true){ + saida.format( "%f\n", x); + } + } + + public static void println(String x){ + if(write == true){ + saida.format( "%s\n", x); + } + } + + public static void println(boolean x){ + if(write == true){ + saida.format( "%s\n", ((x) ? "true" : "false")); + } + } + + public static void println(char x){ + if(write == true){ + saida.format( "%c\n", x); + } + } + + public static int readInt(){ + int resp = -1; + try{ + resp = entrada.nextInt(); + } catch (Exception e) {} + return resp; + } + + public static char readChar(){ + char resp = ' '; + try{ + resp = (char)entrada.nextByte(); + } catch (Exception e) {} + return resp; + } + + public static double readDouble(){ + double resp = -1; + try{ + resp = Double.parseDouble(readString().replace(",",".")); + } catch (Exception e) {} + return resp; + } + + public static String readString(){ + String resp = ""; + try{ + resp = entrada.next(); + } catch (Exception e) { System.out.println(e.getMessage()); } + return resp; + } + + public static boolean readBoolean(){ + boolean resp = false; + try{ + resp = (entrada.next().equals("true")) ? true : false; + } catch (Exception e) {} + return resp; + } + + public static String readLine(){ + String resp = ""; + try{ + resp = entrada.nextLine(); + } catch (Exception e) { System.out.println(e.getMessage()); } + return resp; + } + + + public static boolean hasNext(){ + return entrada.hasNext(); + } + + public static String readAll(){ + String resp = ""; + while(hasNext()){ + resp += (readLine() + "\n"); + } + return resp; + } +} diff --git a/tps/fonte/tp02/TP02Q09 - Heapsort em Java/Character.java b/tps/fonte/tp02/TP02Q09 - Heapsort em Java/Character.java new file mode 100644 index 0000000..b3e76b2 --- /dev/null +++ b/tps/fonte/tp02/TP02Q09 - Heapsort em Java/Character.java @@ -0,0 +1,538 @@ +/** + * @path TP02Q01 - Classe em Java/Characters.java + * @description Java class of all characters from Harry Potter's saga with heap sort algorithm + * @author Pedro Lopes - github.com/httpspedroh + * @version 1.0 + * @update 2024-04-09 + */ + +// ---------------------------------------------------------------------------------------------------- // + +// Imports +import java.util.Scanner; +import java.util.UUID; +import java.io.BufferedReader; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Date; +import java.text.SimpleDateFormat; + +// ---------------------------------------------------------------------------------------------------- // + +public class Character { + + static SimpleDateFormat ddf = new SimpleDateFormat("dd-MM-yyyy"); + + // Global variables + public static final String FILE_PATH = "/tmp/characters.csv"; + public static ArrayList allCharacters = new ArrayList(); + + // -------------------------- // + + // Attributes + private UUID id; + private String name; + private ArrayList alternateNames; + private String house; + private String ancestry; + private String species; + private String patronus; + private boolean hogwartsStaff; + private boolean hogwartsStudent; + private String actorName; + private boolean alive; + private Date birthDate; + private int yearOfBirth; + private String eyeColour; + private String gender; + private String hairColour; + private boolean wizard; + + // -------------------------- // + + // Empty constructor + public Character() { + + this.id = UUID.randomUUID(); + this.name = ""; + this.alternateNames = new ArrayList(); + this.house = ""; + this.ancestry = ""; + this.species = ""; + this.patronus = ""; + this.hogwartsStaff = false; + this.hogwartsStudent = false; + this.actorName = ""; + this.alive = false; + this.birthDate = null; + this.yearOfBirth = 0; + this.eyeColour = ""; + this.gender = ""; + this.hairColour = ""; + this.wizard = false; + } + + // Constructor + public Character(UUID id, String name, ArrayList alternateNames, String house, String ancestry, String species, String patronus, boolean hogwartsStaff, boolean hogwartsStudent, String actorName, boolean alive, Date birthDate, int yearOfBirth, String eyeColour, String gender, String hairColour, boolean wizard) { + + this.id = id; + this.name = name; + this.alternateNames = alternateNames; + this.house = house; + this.ancestry = ancestry; + this.species = species; + this.patronus = patronus; + this.hogwartsStaff = hogwartsStaff; + this.hogwartsStudent = hogwartsStudent; + this.actorName = actorName; + this.alive = alive; + this.birthDate = birthDate; + this.yearOfBirth = yearOfBirth; + this.eyeColour = eyeColour; + this.gender = gender; + this.hairColour = hairColour; + this.wizard = wizard; + } + + // -------------------------- // + + // Gets + public UUID getId() { return this.id; } + public String getName() { return this.name; } + public String getHouse() { return this.house; } + public String getAncestry() { return this.ancestry; } + public String getSpecies() { return this.species; } + public String getPatronus() { return this.patronus; } + public boolean getHogwartsStaff() { return this.hogwartsStaff; } + public boolean getHogwartsStudent() { return this.hogwartsStudent; } + public String getActorName() { return this.actorName; } + public boolean getAlive() { return this.alive; } + public Date getBirthDate() { return this.birthDate; } + public int getYearOfBirth() { return this.yearOfBirth; } + public String getEyeColour() { return this.eyeColour; } + public String getGender() { return this.gender; } + public String getHairColour() { return this.hairColour; } + public boolean getWizard() { return this.wizard; } + + public String getAlternateNames() { + + // Construct string e.g. {item1, item2, item3} + String alternateNames = "{"; + + for(int i = 0; i < this.alternateNames.size(); i++) { + + alternateNames += this.alternateNames.get(i); + + if(i < this.alternateNames.size() - 1) alternateNames += ", "; + } + + alternateNames += "}"; + + return alternateNames; + } + + // Sets + public void setId(UUID id) { this.id = id; } + public void setName(String name) { this.name = name; } + public void setAlternateNames(ArrayList alternateNames) { this.alternateNames = alternateNames; } + public void setHouse(String house) { this.house = house; } + public void setAncestry(String ancestry) { this.ancestry = ancestry; } + public void setSpecies(String species) { this.species = species; } + public void setPatronus(String patronus) { this.patronus = patronus; } + public void setHogwartsStaff(boolean hogwartsStaff) { this.hogwartsStaff = hogwartsStaff; } + public void setHogwartsStudent(boolean hogwartsStudent) { this.hogwartsStudent = hogwartsStudent; } + public void setActorNane(String actorName) { this.actorName = actorName; } + public void setAlive(boolean alive) { this.alive = alive; } + public void setBirthDate(Date birthDate) { this.birthDate = birthDate; } + public void setYearOfBirth(int yearOfBirth) { this.yearOfBirth = yearOfBirth; } + public void setEyeColour(String eyeColour) { this.eyeColour = eyeColour; } + public void setGender(String gender) { this.gender = gender; } + public void setHairColour(String hairColour) { this.hairColour = hairColour; } + public void setWizard(boolean wizard) { this.wizard = wizard; } + + // -------------------------- // + + // Clone + public Character clone() { return new Character(this.id, this.name, this.alternateNames, this.house, this.ancestry, this.species, this.patronus, this.hogwartsStaff, this.hogwartsStudent, this.actorName, this.alive, this.birthDate, this.yearOfBirth, this.eyeColour, this.gender, this.hairColour, this.wizard); } + + // -------------------------- // + + // Print + public void print() { + + System.out.println("[" + + this.getId() + " ## " + + this.getName() + " ## " + + this.getAlternateNames() + " ## " + + (this.getHouse() == "" ? "N/A" : this.getHouse()) + " ## " + + (this.getAncestry() == "" ? "N/A" : this.getAncestry()) + " ## " + + (this.getSpecies() == "" ? "N/A" : this.getSpecies()) + " ## " + + (this.getPatronus() == "" ? "N/A" : this.getPatronus()) + " ## " + + (this.getHogwartsStaff() ? "true" : "false") + " ## " + + (this.getHogwartsStudent() ? "true" : "false") + " ## " + + (this.getActorName() == "" ? "N/A" : this.getActorName()) + " ## " + + (this.getAlive() ? "true" : "false") + " ## " + + (this.getBirthDate() == null ? "N/A" : ddf.format(this.getBirthDate())) + " ## " + + (this.getYearOfBirth() == 0 ? "N/A" : this.getYearOfBirth()) + " ## " + + (this.getEyeColour() == "" ? "N/A" : this.getEyeColour()) + " ## " + + (this.getGender() == "" ? "N/A" : this.getGender()) + " ## " + + (this.getHairColour() == "" ? "N/A" : this.getHairColour()) + " ## " + + (this.getWizard() ? "true" : "false") + "]"); + } + + // -------------------------- // + + public static ArrayList extractNames(String input) { + + ArrayList names = new ArrayList<>(); + + // Clean input + String cleanedInput = input.substring(1, input.length() - 1); + + // Check if input contains double quotes + if (cleanedInput.contains("\"\"")) { + + String[] parts = cleanedInput.split("\", "); + + for (String part : parts) names.add(part.replace("\"", "").replace("'", "")); + } + else { + + // Split input by comma and space + String[] parts = cleanedInput.split(", "); + + for (String part : parts) names.add(part.replace("'", "")); + } + return names; + } + + // -------------------------- // + + // Read + public void read(String line) { + + // ---------------------- // + + // Start position + int positionStart = 0; + int positionEnd = 0; + + // ---------------------- // + + // Set id + positionEnd = line.indexOf(";", positionStart); + this.setId(UUID.fromString(line.substring(positionStart, positionEnd))); + + // ---------------------- // + + // Set name + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setName(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set alternate names + positionStart = positionEnd + 1; + + if(line.charAt(positionStart) == '[') { // If first character is '[', has one or no names + + if(line.charAt(positionStart + 1) == ']') { + + positionEnd = positionStart + 2; + + this.setAlternateNames(new ArrayList()); + } + else { + + positionEnd = line.indexOf("];", positionStart); + + this.setAlternateNames(extractNames(line.substring(positionStart, positionEnd++))); + } + } + + // ---------------------- // + + // Set house + positionStart = positionEnd; + positionEnd = line.indexOf(";", ++positionStart); + + this.setHouse(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set ancestry + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setAncestry(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set species + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setSpecies(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set patronus + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setPatronus(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set hogwartsStaff + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setHogwartsStaff("VERDADEIRO".equals(line.substring(positionStart, positionEnd))); + + // ---------------------- // + + // Set hogwartsStudent + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setHogwartsStudent("VERDADEIRO".equals(line.substring(positionStart, positionEnd))); + + // ---------------------- // + + // Set actor name + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setActorNane(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set alive + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setAlive("VERDADEIRO".equals(line.substring(positionStart, positionEnd))); + + // ---------------------- // + + // Set date of birth + positionStart = positionEnd; + positionEnd = line.indexOf(";", ++positionStart); + + try { this.setBirthDate(ddf.parse(line.substring(positionStart, positionEnd))); } + catch(Exception e) { this.setBirthDate(null); } + + // ---------------------- // + + // Set year of birth + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + try { this.setYearOfBirth(Integer.parseInt(line.substring(positionStart, positionEnd))); } + catch(Exception e) { this.setYearOfBirth(0); } + + // ---------------------- // + + // Set eye colour + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setEyeColour(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set gender + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setGender(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set hair colour + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setHairColour(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set wizard + positionStart = positionEnd + 1; + + this.setWizard("VERDADEIRO".equals(line.substring(positionStart, line.length()))); + + // ---------------------- // + } + + // -------------------------- // + + // Compare method + public int compareTo(Character other) { + + // Compare by hair colour + int hairComparison = this.hairColour.compareTo(other.hairColour); + + // If hair colour is the same, compare by name + if(hairComparison != 0) return hairComparison; + else return this.name.compareTo(other.name); + } + + // ---------------------------------------------------------------------------------------------------- // + + // Read all characters function + public static void startCharacters() { + + // Initialize variables + try { + + FileInputStream fstream = new FileInputStream(FILE_PATH); + BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); + + // ---------------------- // + + // Explode CSV file + String line = br.readLine(); + + while((line = br.readLine()) != null) { + + // Initialize character + Character character = new Character(); + + // Read line + character.read(line); + + // Add character to array + allCharacters.add(character); + } + + // Close CSV file + fstream.close(); + } + catch(IOException e) { e.printStackTrace(); } + } + + // ---------------------------------------------------------------------------------------------------- // + + // Search by id function + public static Character searchById(UUID id, ArrayList characters) { + + // Search for character + for(int i = 0; i < characters.size(); i++) { + + if(characters.get(i).getId().equals(id)) return characters.get(i); + } + return null; + } + + // Helper method to heapify + static void heapify(ArrayList characters, int n, int root, int comparisons) { + + int largest = root; + int left = 2 * root + 1; + int right = 2 * root + 2; + + comparisons++; + if(left < n && characters.get(left).compareTo(characters.get(largest)) > 0) largest = left; + + comparisons++; + if(right < n && characters.get(right).compareTo(characters.get(largest)) > 0) largest = right; + + if(largest != root) { + + Character swap = characters.get(root); + characters.set(root, characters.get(largest)); + characters.set(largest, swap); + + heapify(characters, n, largest, comparisons); + } + } + + // ---------------------------------------------------------------------------------------------------- // + + public static void main(String[] args) { + + // ----------------------------------------------------------------- // + + // #1 - Start - Read all characters in CSV file + startCharacters(); + + // ----------------------------------------------------------------- // + + // #2 - Read input and print characters from pub.in id entries and add to mainCharacters array + + // Initialize mainCharacters array + ArrayList mainCharacters = new ArrayList(); + + // Initialize scanner + Scanner inScanner = new Scanner(System.in); + + // Initialize character + Character character = new Character(); + + // Read first line + String line = inScanner.nextLine(); + + // While line is not "FIM" + while(!line.equals("FIM")) { + + // Get id + UUID id = UUID.fromString(line); + + // Search for character + character = searchById(id, allCharacters); + + // Print character + if(character != null) mainCharacters.add(character); + + // Read line + line = inScanner.nextLine(); + } + + // ----------------------------------------------------------------- // + + // #3 - Order mainCharacters array by key "hairColour" using insertion sort, in draw case, order by key "name" + + // Start benchmark + long startTime = System.currentTimeMillis(); + int comparisons = 0; + + // Heap sort + for(int i = mainCharacters.size() / 2 - 1; i >= 0; i--) heapify(mainCharacters, mainCharacters.size(), i, comparisons); + + for(int i = mainCharacters.size() - 1; i > 0; i--) { + + // Swap the root (maximum element) with the last character + Character temp = mainCharacters.get(0); + mainCharacters.set(0, mainCharacters.get(i)); + mainCharacters.set(i, temp); + + // Heapify the reduced heap + heapify(mainCharacters, i, 0, comparisons); + } + + // ----------------- // + + // Save benchmark in file + Arq.openWrite("753045_heapsort.txt"); + Arq.print("753045\t" + (System.currentTimeMillis() - startTime) + "ms\t" + comparisons); + Arq.close(); + + // ----------------- // + + // Print mainCharacters array + for(int i = 0; i < mainCharacters.size(); i++) mainCharacters.get(i).print(); + + // ----------------------------------------------------------------- // + + // Close scanner + inScanner.close(); + } +} + +// ---------------------------------------------------------------------------------------------------- // diff --git a/tps/fonte/tp02/TP02Q09 - Heapsort em Java/pedro.out b/tps/fonte/tp02/TP02Q09 - Heapsort em Java/pedro.out new file mode 100644 index 0000000..8110a9e --- /dev/null +++ b/tps/fonte/tp02/TP02Q09 - Heapsort em Java/pedro.out @@ -0,0 +1,27 @@ +[efa802c8-ae18-4ae1-a524-49df21d05939 ## Lord Voldemort ## {Tom Riddle, Tom Marvolo Riddle, You-Know-Who, He Who Must Not Be Named, the Dark Lord, the Heir of Slytherin} ## Slytherin ## half-blood ## human ## N/A ## false ## false ## Ralph Fiennes ## false ## 31-12-1926 ## 1926 ## Scarlet ## male ## bald ## true] +[6afb1960-febd-418d-b604-e50c1b59459b ## Bellatrix Lestrange ## {Bella} ## Slytherin ## pure-blood ## human ## N/A ## false ## false ## Helena Bonham Carter ## false ## 06-02-1950 ## 1951 ## brown ## female ## black ## true] +[9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8 ## Harry Potter ## {The Boy Who Lived, The Chosen One, Undesirable No. 1, Potty} ## Gryffindor ## half-blood ## human ## stag ## false ## true ## Daniel Radcliffe ## true ## 31-07-1980 ## 1980 ## green ## male ## black ## true] +[5a4c95db-947d-4914-a631-41e8d466328e ## Kendra Dumbledore ## {} ## Slytherin ## muggleborn ## human ## N/A ## false ## false ## N/A ## false ## 31-12-1926 ## 1925 ## dark ## female ## black ## true] +[ca3827f0-375a-4891-aaa5-f5e8a5bad225 ## Minerva McGonagall ## {} ## Gryffindor ## half-blood ## human ## tabby cat ## true ## false ## Dame Maggie Smith ## true ## 04-10-1925 ## 1925 ## grey ## female ## black ## true] +[36bfefd0-e0bb-4d11-be98-d1ef6117a77a ## Rubeus Hagrid ## {Professor Hagrid, Hagger} ## Gryffindor ## half-blood ## half-giant ## N/A ## true ## false ## Robbie Coltrane ## true ## 06-12-1928 ## 1928 ## black ## male ## black ## true] +[2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f ## Sirius Black ## {Padfoot, Snuffles} ## Gryffindor ## pure-blood ## human ## hare ## false ## false ## Gary Oldman ## false ## 03-11-1959 ## 1959 ## grey ## male ## black ## true] +[0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3 ## Dobby ## {} ## Slytherin ## pure-blood ## house-elf ## N/A ## false ## false ## Toby Jones ## false ## 06-02-1950 ## 1950 ## green ## male ## blond ## false] +[7614cf6e-689e-47ac-a976-b1e9997637e9 ## Dudley Dursley ## {} ## Slytherin ## muggle ## human ## N/A ## false ## false ## Harry Melling ## true ## 23-06-1980 ## 1980 ## blue ## male ## blond ## false] +[41cd0bbe-a943-431b-9bde-bb2cad3491a1 ## Lily Potter ## {} ## Gryffindor ## muggleborn ## human ## N/A ## false ## false ## Geraldine Somerville ## false ## 30-01-1960 ## 1960 ## green ## female ## blond ## true] +[861c4cde-2f0f-4796-8d8f-9492e74b2573 ## Luna Lovegood ## {Loony Lovegood} ## Ravenclaw ## pure-blood ## human ## hare ## false ## true ## Evanna Lynch ## true ## 13-02-1981 ## 1981 ## silver ## female ## blonde ## true] +[fed624df-56d9-495e-9ad4-ea77000957e8 ## Petunia Dursley ## {} ## Slytherin ## muggle ## human ## N/A ## false ## false ## Fiona Shaw ## true ## 31-12-1926 ## 1954 ## yellow ## female ## blonde ## false] +[57fe29d4-312a-4711-bd9a-c320253d9176 ## Victoire Weasley ## {} ## Slytherin ## pure-blood ## human ## N/A ## false ## true ## N/A ## true ## 31-12-1926 ## 1926 ## grey ## female ## blonde ## true] +[d59691a4-f830-4eb0-a819-a0fb00b7e80f ## Garrick Ollivander ## {} ## Hufflepuff ## muggle ## human ## N/A ## false ## false ## John Hurt ## true ## 04-10-1925 ## 1954 ## pale, silvery ## male ## brown ## true] +[6b59be3f-e527-422d-959d-79fcdb3b24eb ## Hedwig ## {} ## Hufflepuff ## muggle ## owl ## N/A ## false ## false ## Gizmo ## false ## 07-04-1979 ## 1954 ## amber ## female ## brown ## false] +[b0620914-858d-46fc-8e6d-033c565e138b ## Mrs Norris ## {} ## Slytherin ## pure-blood ## cat ## N/A ## true ## false ## Maxime, Alanis and Tommy the cats ## true ## 31-12-1926 ## 1954 ## yellow ## female ## brown ## false] +[3a0fe4df-2e40-4541-8d7f-13586f0b9294 ## Tom Riddle ## {Tom Riddle Senior} ## Ravenclaw ## muggle ## human ## N/A ## false ## false ## N/A ## false ## 27-03-1960 ## 1980 ## brown ## male ## dark ## false] +[94c993f6-a443-4408-b908-6e78e223e8ac ## Aberforth Dumbledore ## {} ## Gryffindor ## half-blood ## human ## goat ## false ## false ## Jim McManus ## true ## 10-03-1960 ## 1960 ## blue ## male ## grey ## true] +[d58e7249-19d1-40bd-a43f-1da0497fe8aa ## Dolores Umbridge ## {} ## Slytherin ## half-blood ## human ## persian cat ## true ## false ## Imelda Staunton ## true ## 31-12-1926 ## 1926 ## brown ## female ## grey ## true] +[2a0615de-8aa4-41e7-9504-dd875f5f3f01 ## George Weasley ## {} ## Gryffindor ## pure-blood ## human ## N/A ## false ## true ## Oliver Phelps ## true ## 13-02-1981 ## 1954 ## amber ## male ## red ## true] +[eaea5eb3-48a3-41c6-9ea5-c695299bab16 ## Lisa Turpin ## {} ## Ravenclaw ## pure-blood ## human ## N/A ## false ## true ## N/A ## true ## 27-03-1960 ## 1951 ## amber ## female ## sandy ## true] +[11b5ca88-64ad-41a4-9f36-317b66c290af ## Nearly Headless Nick ## {Sir Nicholas de Mimsy-Porpington, Sir Nicholas, Nick} ## Gryffindor ## pure-blood ## ghost ## N/A ## false ## false ## John Cleese ## false ## 27-03-1960 ## 1926 ## amber ## male ## sandy ## true] +[1413e1b3-2903-4a47-a2d5-e8abc5ce8014 ## Seamus Finnigan ## {O Flaherty, Seamus Finnegan} ## Gryffindor ## half-blood ## human ## N/A ## false ## true ## Devon Murray ## true ## 31-12-1926 ## 1960 ## amber ## male ## sandy ## true] +[b415c867-1cff-455e-b194-748662ac2cca ## Albus Dumbledore ## {Professor Dumbledore} ## Gryffindor ## half-blood ## human ## Phoenix ## true ## false ## Richard Harris ## false ## 27-03-1960 ## 1881 ## blue ## male ## silver ## true] +[933787c2-51e3-4eac-8a85-ab332cac0456 ## Miranda Gaushawk ## {} ## Gryffindor ## muggle ## human ## N/A ## false ## false ## N/A ## false ## 27-03-1960 ## 1960 ## blue ## female ## silver ## true] +[00434cd3-fcc7-44c7-8f98-7368415b4206 ## Miriam Strout ## {} ## Gryffindor ## half-blood ## human ## N/A ## false ## false ## N/A ## true ## 27-03-1960 ## 1954 ## blue ## female ## silver ## true] +[20354d7a-e4fe-47af-8ff6-187bca92f3f9 ## Pandora Lovegood ## {} ## Ravenclaw ## quarter-veela ## human ## N/A ## false ## false ## N/A ## false ## 31-12-1926 ## 1959 ## dark ## female ## white ## true] diff --git a/tps/fonte/tp02/TP02Q09 - Heapsort em Java/pub.in b/tps/fonte/tp02/TP02Q09 - Heapsort em Java/pub.in new file mode 100644 index 0000000..a782d8c --- /dev/null +++ b/tps/fonte/tp02/TP02Q09 - Heapsort em Java/pub.in @@ -0,0 +1,28 @@ +9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8 +1413e1b3-2903-4a47-a2d5-e8abc5ce8014 +ca3827f0-375a-4891-aaa5-f5e8a5bad225 +36bfefd0-e0bb-4d11-be98-d1ef6117a77a +20354d7a-e4fe-47af-8ff6-187bca92f3f9 +57fe29d4-312a-4711-bd9a-c320253d9176 +b415c867-1cff-455e-b194-748662ac2cca +5a4c95db-947d-4914-a631-41e8d466328e +861c4cde-2f0f-4796-8d8f-9492e74b2573 +2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f +41cd0bbe-a943-431b-9bde-bb2cad3491a1 +2a0615de-8aa4-41e7-9504-dd875f5f3f01 +11b5ca88-64ad-41a4-9f36-317b66c290af +eaea5eb3-48a3-41c6-9ea5-c695299bab16 +0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3 +b0620914-858d-46fc-8e6d-033c565e138b +6b59be3f-e527-422d-959d-79fcdb3b24eb +fed624df-56d9-495e-9ad4-ea77000957e8 +d58e7249-19d1-40bd-a43f-1da0497fe8aa +3a0fe4df-2e40-4541-8d7f-13586f0b9294 +6afb1960-febd-418d-b604-e50c1b59459b +efa802c8-ae18-4ae1-a524-49df21d05939 +933787c2-51e3-4eac-8a85-ab332cac0456 +94c993f6-a443-4408-b908-6e78e223e8ac +00434cd3-fcc7-44c7-8f98-7368415b4206 +7614cf6e-689e-47ac-a976-b1e9997637e9 +d59691a4-f830-4eb0-a819-a0fb00b7e80f +FIM \ No newline at end of file diff --git a/tps/fonte/tp02/TP02Q10 - Quicksort em C/753045_quicksort.txt b/tps/fonte/tp02/TP02Q10 - Quicksort em C/753045_quicksort.txt new file mode 100644 index 0000000..ff65d62 --- /dev/null +++ b/tps/fonte/tp02/TP02Q10 - Quicksort em C/753045_quicksort.txt @@ -0,0 +1 @@ +753045 0ms 521 \ No newline at end of file diff --git a/tps/fonte/tp02/TP02Q10 - Quicksort em C/Character b/tps/fonte/tp02/TP02Q10 - Quicksort em C/Character new file mode 100644 index 0000000000000000000000000000000000000000..f25da6ab5e4bd2f371ead198d19d0a1682958357 GIT binary patch literal 27848 zcmeHweSDPFo$pCPAW+D}7HX=rj9N6lBm^oHif9Hh@Ze1_3PI$>nIV}lsd;NMqrr*| z4Y7F~2D2^fO}+5BcJZ6|`7D%!Y3X1~ARIp>*Y zCNnAh>}U7>ae4U6{Lb(DdpYMh=XYM7^Gpt3b5|_S&dL({$`LmU;(9KTNSP8Cew6?z z6AQ&B@clXQSuqj(1jeMiOcF@t706Dec?!<~CA&T)Po*=Jo~F8lM9D7GH8NRBG!+Gs zC%arqDn6CIJxA(ks^wOGWm0b4nBrShewy0tsBU7F(k`|sg{u4RzD3=cDi^_IH>&JL zm7S(TN?lW3pHwIGc~<#NrR9*(r77vsRrT9oEv1WG%#NXZReqXkyW3$$<-E{KSNk0* zzfAQ~sLHRY%3jQAsNcNc#yJhO#SQgMq4whTg$s%o+*s1qTr!XEdpZh2F|}eLBRGT@st;L|eT)L+xx*T=w}_@rK40H{o3uC`!n zP0Lo{Z?6xEwwBiVreK|@Yi{v3i57orYjdmccIC$g$e^a7x#k|? zg3b4c8eaog;u>IB2U(k6G+JPdFDQJ$=6X`73ABnjmCqJ`5WUckENa?)p1S%bUqk)< ze%5Mi0gAp_<7o@}T7#ZOUp*YR`u&1H>aPpLii(v>mwD!ue9=nHE154mE7w$dYW=PL zE%j|df9smcWev?u{x!bM4V2NA#^xsF(j!&VDx#XQxi8aq>OU!yXpSY7V?Y-8AH!OY zQ((Brute_K?ej;Qfkk7i-{R4P2}Ns|JkD zGkHch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Qy-#B@1N@brzcfk^P0X28wWnxUhlh) zRFbzuyh$#>Yy00oKt;M1& literal 0 HcmV?d00001 diff --git a/tps/fonte/tp02/TP02Q10 - Quicksort em C/Character.c b/tps/fonte/tp02/TP02Q10 - Quicksort em C/Character.c new file mode 100644 index 0000000..0be4728 --- /dev/null +++ b/tps/fonte/tp02/TP02Q10 - Quicksort em C/Character.c @@ -0,0 +1,706 @@ +/** + * @path TP02Q02 - Classe em C/Character.c + * @description C file that implements the Character class with quicksort algorithm + * @author Pedro Lopes - github.com/httpspedroh + * @version 1.0 + * @update 2024-04-09 + */ + +// ---------------------------------------------------------------------------------------------------- // + +// Includes +#include +#include +#include +#include +#include + +// ---------------------------------------------------------------------------------------------------- // + +// Constants +#define MAX_CHARACTERS 405 +#define FILE_PATH "/tmp/characters.csv" + +#define MAX_UUID_SIZE 37 +#define MAX_NAME_SIZE 30 +#define MAX_ALTERNATE_NAMES 10 +#define MAX_ALTERNATE_NAME_SIZE 35 +#define MAX_HOUSE_SIZE 15 +#define MAX_ANCESTRY_SIZE 15 +#define MAX_SPECIES_SIZE 20 +#define MAX_PATRONUS_SIZE 25 +#define MAX_ACTOR_NAME_SIZE 35 +#define MAX_EYE_COLOUR_SIZE 10 +#define MAX_GENDER_SIZE 10 +#define MAX_HAIR_COLOUR_SIZE 10 + +#define MAX_LINE_SIZE 300 + +// ---------------------------------------------------------------------------------------------------- // + +// Structs +typedef struct Date { + + int day; + int month; + int year; +} Date; + +typedef struct Character { + + char *id; + char *name; + char *alternateNames[MAX_ALTERNATE_NAMES]; + char *house; + char *ancestry; + char *species; + char *patronus; + bool hogwartsStaff; + bool hogwartsStudent; + char *actorName; + bool alive; + Date birthDate; + int yearOfBirth; + char *eyeColour; + char *gender; + char *hairColour; + bool wizard; +} Character; + +// ---------------------------------------------------------------------------------------------------- // + +// Global variables +Character characters[MAX_CHARACTERS]; +int charactersLength = 0; + +// ---------------------------------------------------------------------------------------------------- // + +// Functions +bool isEnd(char* line) { return line[0] == 'F' && line[1] == 'I' && line[2] == 'M'; } + +void substring(char *string, char *stringStart, int length) { + + strncpy(string, stringStart, length); + string[length] = '\0'; +} + +void proccess_attribute(char *attribute, char **substringStart, char **substringEnd, bool isFirstAttribute, bool isStringArray) { + + // Skip first comma + if(!isFirstAttribute) { + + if(*substringEnd != NULL) *substringStart = *substringEnd + 1; + else *substringStart = *substringEnd; + } + + if(!isStringArray) { + + if((*substringStart)[0] == '"') { + + *substringStart = *substringStart + 1; + *substringEnd = strchr(*substringStart, '"'); + } + else *substringEnd = strchr(*substringStart, ';'); + + // Get substring + if(*substringEnd) { + + substring(attribute, *substringStart, *substringEnd - *substringStart); + + if(*substringEnd[0] == '"') *substringEnd = *substringEnd + 1; + } + else strcpy(attribute, *substringStart); + + // Set default value if attribute is empty + if(strcmp(attribute, "") == 0 || attribute[0] == '\n' || attribute[0] == '\r' || attribute[0] == '\0') strcpy(attribute, "N/A"); + + // Clean \n from the end of the string + if(attribute[strlen(attribute) - 1] == '\n' || attribute[strlen(attribute) - 1] == '\r') attribute[strlen(attribute) - 1] = '\0'; + } + else { + + // Check if the first character is a [ + if((*substringStart)[0] == '[') { + + *substringStart = *substringStart + 1; + + if((*substringStart)[0] == ']') strcpy(attribute, ""); // Case: [] + else { + + char *tempConcat = (char *) calloc(MAX_LINE_SIZE, sizeof(char)); + + *substringStart = *substringStart - 1; + + while(1) { + + *substringStart = *substringStart + 1; + + if((*substringStart)[0] == ';') break; + else if((*substringStart)[0] == '\'') { // Case: "['example', 'example']" + + *substringStart = *substringStart + 1; + *substringEnd = strchr(*substringStart, '\''); + + // Get substring + if(*substringEnd) { + + // Create tmp name + char tmp[MAX_LINE_SIZE]; + substring(tmp, *substringStart, *substringEnd - *substringStart); + + // Concat tempConcat with tmp + strcat(tempConcat, tmp); + strcat(tempConcat, ", "); + + *substringStart = *substringEnd + 1; + } + } + } + + // Get substring + strcpy(attribute, tempConcat); + + // Clean "attribute" removing last 2 characters + attribute[strlen(attribute) - 2] = '\0'; + } + } + + *substringEnd = strchr(*substringStart, ';'); + } +} + +// ---------------------------------------------------------------------------------------------------- // + +// Methods implementations + +// Gets +char *character_getId(Character *character) { return character -> id; } +char *character_getName(Character *character) { return character -> name; } +char *character_getHouse(Character *character) { return character -> house; } +char *character_getAncestry(Character *character) { return character -> ancestry; } +char *character_getSpecies(Character *character) { return character -> species; } +char *character_getPatronus(Character *character) { return character -> patronus; } +bool character_getHogwartsStaff(Character *character) { return character -> hogwartsStaff; } +bool character_getHogwartsStudent(Character *character) { return character -> hogwartsStudent; } +char *character_getActorName(Character *character) { return character -> actorName; } +bool character_getAlive(Character *character) { return character -> alive; } +char *character_getEyeColour(Character *character) { return character -> eyeColour; } +char *character_getGender(Character *character) { return character -> gender; } +char *character_getHairColour(Character *character) { return character -> hairColour; } +bool character_getWizard(Character *character) { return character -> wizard; } + +char *character_getYearOfBirth(Character *character) { + + // "N/A" if yearOfBirth is -1 + char *yearOfBirth = (char *) calloc(15, sizeof(char)); + strcpy(yearOfBirth, "N/A"); + + if(character -> yearOfBirth != -1) sprintf(yearOfBirth, "%d", character -> yearOfBirth); + return yearOfBirth; +} + +char *character_getBirthDate(Character *character) { + + // "N/A" if day, month and year are -1 + char *birthDate = (char *) calloc(15, sizeof(char)); + strcpy(birthDate, "N/A"); + + if(character -> birthDate.day != -1 && character -> birthDate.month != -1 && character -> birthDate.year != -1) { + + sprintf(birthDate, "%02d-%02d-%04d", character -> birthDate.day, character -> birthDate.month, character -> birthDate.year); + } + + return birthDate; +} + +char *character_getAlternateNames(Character *character) { + + // Concatenate all alternate names with a comma + char *alternateNames = (char *) calloc(MAX_ALTERNATE_NAME_SIZE * MAX_ALTERNATE_NAMES, sizeof(char)); + + alternateNames[0] = '{'; + + for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) { + + if(strcmp(character -> alternateNames[i], "") != 0) { + + strcat(alternateNames, character -> alternateNames[i]); + + if(strcmp(character -> alternateNames[i + 1], "") != 0) strcat(alternateNames, ", "); + } + } + + strcat(alternateNames, "}"); + + return alternateNames; +} + +// Sets +void character_setId(Character *character, char *id) { strcpy(character -> id, id); } +void character_setName(Character *character, char *name) { strcpy(character -> name, name); } +void character_setHouse(Character *character, char *house) { strcpy(character -> house, house); } +void character_setAncestry(Character *character, char *ancestry) { strcpy(character -> ancestry, ancestry); } +void character_setSpecies(Character *character, char *species) { strcpy(character -> species, species); } +void character_setPatronus(Character *character, char *patronus) { strcpy(character -> patronus, patronus); } +void character_setHogwartsStaff(Character *character, bool hogwartsStaff) { character -> hogwartsStaff = hogwartsStaff; } +void character_setHogwartsStudent(Character *character, bool hogwartsStudent) { character -> hogwartsStudent = hogwartsStudent; } +void character_setActorName(Character *character, char *actorName) { strcpy(character -> actorName, actorName); } +void character_setAlive(Character *character, bool alive) { character -> alive = alive; } +void character_setYearOfBirth(Character *character, int yearOfBirth) { character -> yearOfBirth = yearOfBirth; } +void character_setEyeColour(Character *character, char *eyeColour) { strcpy(character -> eyeColour, eyeColour); } +void character_setGender(Character *character, char *gender) { strcpy(character -> gender, gender); } +void character_setHairColour(Character *character, char *hairColour) { strcpy(character -> hairColour, hairColour); } +void character_setWizard(Character *character, bool wizard) { character -> wizard = wizard; } + +void character_setBirthDate(Character *character, char *birthDate) { + + // Explode birthDate in format DD-MM-YYYY if in format DD-MM-YYYY + if(strlen(birthDate) >= 8 && strlen(birthDate) <= 10) { + + char *token = strtok(birthDate, "-"); + + character -> birthDate.day = atoi(token); + token = strtok(NULL, "-"); + character -> birthDate.month = atoi(token); + token = strtok(NULL, "-"); + character -> birthDate.year = atoi(token); + } +} + +void character_setAlternateNames(Character *character, char *alternateNames) { + + // Copy names to a temporary variable + char tempNames[MAX_ALTERNATE_NAME_SIZE * MAX_ALTERNATE_NAMES]; + strcpy(tempNames, alternateNames); + + // Separate names by comma + char *token = strtok(tempNames, ","); + int i = 0; + + // Copy names to the character + while (token != NULL && i < MAX_ALTERNATE_NAMES) { + + while (*token == ' ') token++; + + int len = strlen(token); + + while (len > 0 && token[len - 1] == ' ') { + + token[len - 1] = '\0'; + len--; + } + + strcpy(character -> alternateNames[i++], token); + token = strtok(NULL, ","); + } +} + +// Class +Character character_newBlank() { + + Character character; + + character.id = (char *) calloc(MAX_UUID_SIZE, sizeof(char)); + strcpy(character.id, ""); + + character.name = (char *) calloc(MAX_NAME_SIZE, sizeof(char)); + strcpy(character.name, ""); + + for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) { + + character.alternateNames[i] = (char *) calloc(MAX_ALTERNATE_NAME_SIZE, sizeof(char)); + strcpy(character.alternateNames[i], ""); + } + + character.house = (char *) calloc(MAX_HOUSE_SIZE, sizeof(char)); + strcpy(character.house, ""); + + character.ancestry = (char *) calloc(MAX_ANCESTRY_SIZE, sizeof(char)); + strcpy(character.ancestry, ""); + + character.species = (char *) calloc(MAX_SPECIES_SIZE, sizeof(char)); + strcpy(character.species, ""); + + character.patronus = (char *) calloc(MAX_PATRONUS_SIZE, sizeof(char)); + strcpy(character.patronus, ""); + + character.hogwartsStaff = false; + character.hogwartsStudent = false; + + character.actorName = (char *) calloc(MAX_ACTOR_NAME_SIZE, sizeof(char)); + strcpy(character.actorName, ""); + + character.alive = false; + + character.birthDate.day = -1; + character.birthDate.month = -1; + character.birthDate.year = -1; + + character.yearOfBirth = -1; + + character.eyeColour = (char *) calloc(MAX_EYE_COLOUR_SIZE, sizeof(char)); + strcpy(character.eyeColour, ""); + + character.gender = (char *) calloc(MAX_GENDER_SIZE, sizeof(char)); + strcpy(character.gender, ""); + + character.hairColour = (char *) calloc(MAX_HAIR_COLOUR_SIZE, sizeof(char)); + strcpy(character.hairColour, ""); + + character.wizard = false; + + return character; +} + +Character character_new(char *id, char *name, char *house, char *ancestry, char *species, char *patronus, bool hogwartsStaff, bool hogwartsStudent, char *actorName, bool alive, Date birthDate, int yearOfBirth, char *eyeColour, char *gender, char *hairColour, bool wizard) { + + Character character; + + character.id = (char *) calloc(MAX_UUID_SIZE, sizeof(char)); + strcpy(character.id, id); + + character.name = (char *) calloc(MAX_NAME_SIZE, sizeof(char)); + strcpy(character.name, name); + + for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) { + + character.alternateNames[i] = (char *) calloc(MAX_ALTERNATE_NAME_SIZE, sizeof(char)); + strcpy(character.alternateNames[i], ""); + } + + character.house = (char *) calloc(MAX_HOUSE_SIZE, sizeof(char)); + strcpy(character.house, house); + + character.ancestry = (char *) calloc(MAX_ANCESTRY_SIZE, sizeof(char)); + strcpy(character.ancestry, ancestry); + + character.species = (char *) calloc(MAX_SPECIES_SIZE, sizeof(char)); + strcpy(character.species, species); + + character.patronus = (char *) calloc(MAX_PATRONUS_SIZE, sizeof(char)); + strcpy(character.patronus, patronus); + + character.hogwartsStaff = hogwartsStaff; + character.hogwartsStudent = hogwartsStudent; + + character.actorName = (char *) calloc(MAX_ACTOR_NAME_SIZE, sizeof(char)); + strcpy(character.actorName, actorName); + + character.alive = alive; + character.birthDate = birthDate; + character.yearOfBirth = yearOfBirth; + + character.eyeColour = (char *) calloc(MAX_EYE_COLOUR_SIZE, sizeof(char)); + strcpy(character.eyeColour, eyeColour); + + character.gender = (char *) calloc(MAX_GENDER_SIZE, sizeof(char)); + strcpy(character.gender, gender); + + character.hairColour = (char *) calloc(MAX_HAIR_COLOUR_SIZE, sizeof(char)); + strcpy(character.hairColour, hairColour); + + character.wizard = wizard; + + return character; +} + +Character *character_clone(Character *character) { + + Character *clone = (Character *) malloc(sizeof(Character)); + + clone -> id = (char *) calloc(MAX_UUID_SIZE, sizeof(char)); + strcpy(clone -> id, character -> id); + + clone -> name = (char *) calloc(MAX_NAME_SIZE, sizeof(char)); + strcpy(clone -> name, character -> name); + + for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) { + + clone -> alternateNames[i] = (char *) calloc(MAX_ALTERNATE_NAME_SIZE, sizeof(char)); + strcpy(clone -> alternateNames[i], character -> alternateNames[i]); + } + + clone -> house = (char *) calloc(MAX_HOUSE_SIZE, sizeof(char)); + strcpy(clone -> house, character -> house); + + clone -> ancestry = (char *) calloc(MAX_ANCESTRY_SIZE, sizeof(char)); + strcpy(clone -> ancestry, character -> ancestry); + + clone -> species = (char *) calloc(MAX_SPECIES_SIZE, sizeof(char)); + strcpy(clone -> species, character -> species); + + clone -> patronus = (char *) calloc(MAX_PATRONUS_SIZE, sizeof(char)); + strcpy(clone -> patronus, character -> patronus); + + clone -> hogwartsStaff = character -> hogwartsStaff; + clone -> hogwartsStudent = character -> hogwartsStudent; + + clone -> actorName = (char *) calloc(MAX_ACTOR_NAME_SIZE, sizeof(char)); + strcpy(clone -> actorName, character -> actorName); + + clone -> alive = character -> alive; + clone -> birthDate = character -> birthDate; + clone -> yearOfBirth = character -> yearOfBirth; + + clone -> eyeColour = (char *) calloc(MAX_EYE_COLOUR_SIZE, sizeof(char)); + strcpy(clone -> eyeColour, character -> eyeColour); + + clone -> gender = (char *) calloc(MAX_GENDER_SIZE, sizeof(char)); + strcpy(clone -> gender, character -> gender); + + clone -> hairColour = (char *) calloc(MAX_HAIR_COLOUR_SIZE, sizeof(char)); + strcpy(clone -> hairColour, character -> hairColour); + + clone -> wizard = character -> wizard; + + return clone; +} + +void character_print(Character *character) { + + printf("[%s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s]\n", + + character_getId(character), + character_getName(character), + character_getAlternateNames(character), + character_getHouse(character), + character_getAncestry(character), + character_getSpecies(character), + character_getPatronus(character), + character_getHogwartsStaff(character) ? "true" : "false", + character_getHogwartsStudent(character) ? "true" : "false", + character_getActorName(character), + character_getAlive(character) ? "true" : "false", + character_getBirthDate(character), + character_getYearOfBirth(character), + character_getEyeColour(character), + character_getGender(character), + character_getHairColour(character), + character_getWizard(character) ? "true" : "false" + ); +} + +Character character_read(char *line) { + + Character character = character_newBlank(); + + char *substringStart = line; + char *substringEnd = NULL; + char attribute[MAX_LINE_SIZE]; + + // Get id + proccess_attribute(attribute, &substringStart, &substringEnd, true, false); + character_setId(&character, attribute); + + // Get name + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setName(&character, attribute); + + // Get alternate names + proccess_attribute(attribute, &substringStart, &substringEnd, false, true); + character_setAlternateNames(&character, attribute); + + // Get house + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setHouse(&character, attribute); + + // Get ancestry + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setAncestry(&character, attribute); + + // Get species + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setSpecies(&character, attribute); + + // Get patronus + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setPatronus(&character, attribute); + + // Get hogwarts staff + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setHogwartsStaff(&character, strcmp(attribute, "VERDADEIRO") == 0); + + // Get hogwarts student + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setHogwartsStudent(&character, strcmp(attribute, "VERDADEIRO") == 0); + + // Get actor name + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setActorName(&character, attribute); + + // Get alive + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setAlive(&character, strcmp(attribute, "VERDADEIRO") == 0); + + // Get birth date + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setBirthDate(&character, attribute); + + // Get year of birth + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setYearOfBirth(&character, atoi(attribute) == 0 ? -1 : atoi(attribute)); + + // Get eye colour + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setEyeColour(&character, attribute); + + // Get gender + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setGender(&character, attribute); + + // Get hair colour + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setHairColour(&character, attribute); + + // Get wizard + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + + // Clean \n from the end of the string + if(attribute[strlen(attribute) - 1] == '\n' || attribute[strlen(attribute) - 1] == '\r') attribute[strlen(attribute) - 1] = '\0'; + + character_setWizard(&character, strcmp(attribute, "VERDADEIRO") == 0); + return character; +} + +Character *character_searchById(char *id) { + + for(int i = 0; i < charactersLength; i++) { + + if(!strcmp(characters[i].id, id)) return &characters[i]; + } + return NULL; +} + +// General +void startCharacters() { + + // Open file + FILE *fp; + char *line = NULL; + size_t len = 0; + ssize_t read; + + fp = fopen(FILE_PATH, "r"); + + if(fp == NULL) { + + perror("x Error opening file"); + exit(EXIT_FAILURE); + } + + // Skip first line + getline(&line, &len, fp); + + // Read all lines + while((read = getline(&line, &len, fp)) != -1) { + + // Read character from line + Character character = character_read(line); + + characters[charactersLength++] = character; + + if(charactersLength >= MAX_CHARACTERS) { + + perror("x Max characters reached"); + exit(EXIT_FAILURE); + } + } + + // Close file and free memory + fclose(fp); + + if(line) free(line); +} + +// ---------------------------------------------------------------------------------------------------- // + +// Main +int main() { + + // ----------------------------------------------------------------- // + + // #1 - Start - Read all characters in CSV file + startCharacters(); + + // ----------------------------------------------------------------- // + + // #2 - Read input and print characters from pub.in id entries and add to mainCharacters array + + // Initialize mainCharacters array + Character mainCharacters[MAX_CHARACTERS]; + int m = 0; + + char id[MAX_UUID_SIZE]; + scanf(" %[^\n]s", id); + + while(true) { + + // Clean \n from the end of the string + if(id[strlen(id) - 1] == '\n' || id[strlen(id) - 1] == '\r') id[strlen(id) - 1] = '\0'; + + if(isEnd(id)) break; + else { + + Character *character = character_searchById(id); + + if(character) mainCharacters[m++] = *character; + + // ------------------------- // + + scanf(" %[^\n]s", id); + } + } + + // ----------------------------------------------------------------- // + + // #3 - Order mainCharacters array by key "house", in draw case, order by key "name" + + // Start benchmark + clock_t startTime = clock(); + int comparisons = 0; + + // Quick sort + int i = 0, j = 0; + + for(i = 0; i < m; i++) { + + for(j = i + 1; j < m; j++) { + + comparisons++; + + if(strcmp(character_getHouse(&mainCharacters[i]), character_getHouse(&mainCharacters[j])) > 0) { + + Character temp = mainCharacters[i]; + mainCharacters[i] = mainCharacters[j]; + mainCharacters[j] = temp; + } + else if(strcmp(character_getHouse(&mainCharacters[i]), character_getHouse(&mainCharacters[j])) == 0) { + + comparisons++; + + if(strcmp(character_getName(&mainCharacters[i]), character_getName(&mainCharacters[j])) > 0) { + + Character temp = mainCharacters[i]; + mainCharacters[i] = mainCharacters[j]; + mainCharacters[j] = temp; + } + } + } + } + + // ----------------- // + + // Save benchmark in file + FILE *fp = fopen("753045_quicksort.txt", "w"); + fprintf(fp, "753045\t%.0fms\t%d", (double)(clock() - startTime) / CLOCKS_PER_SEC * 1000.0, comparisons); + fclose(fp); + + // ----------------- // + + // Print mainCharacters array + for(int i = 0; i < m; i++) character_print(&mainCharacters[i]); + + // ----------------- // + + return 0; +} \ No newline at end of file diff --git a/tps/fonte/tp02/TP02Q10 - Quicksort em C/pedro.out b/tps/fonte/tp02/TP02Q10 - Quicksort em C/pedro.out new file mode 100644 index 0000000..a81ca1f --- /dev/null +++ b/tps/fonte/tp02/TP02Q10 - Quicksort em C/pedro.out @@ -0,0 +1,27 @@ +[94c993f6-a443-4408-b908-6e78e223e8ac ## Aberforth Dumbledore ## {} ## Gryffindor ## half-blood ## human ## goat ## false ## false ## Jim McManus ## true ## 10-03-1960 ## 1960 ## blue ## male ## grey ## true] +[b415c867-1cff-455e-b194-748662ac2cca ## Albus Dumbledore ## {Professor Dumbledore} ## Gryffindor ## half-blood ## human ## Phoenix ## true ## false ## Richard Harris ## false ## 27-03-1960 ## 1881 ## blue ## male ## silver ## true] +[2a0615de-8aa4-41e7-9504-dd875f5f3f01 ## George Weasley ## {} ## Gryffindor ## pure-blood ## human ## N/A ## false ## true ## Oliver Phelps ## true ## 13-02-1981 ## 1954 ## amber ## male ## red ## true] +[9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8 ## Harry Potter ## {The Boy Who Lived, The Chosen One, Undesirable No. 1, Potty} ## Gryffindor ## half-blood ## human ## stag ## false ## true ## Daniel Radcliffe ## true ## 31-07-1980 ## 1980 ## green ## male ## black ## true] +[41cd0bbe-a943-431b-9bde-bb2cad3491a1 ## Lily Potter ## {} ## Gryffindor ## muggleborn ## human ## N/A ## false ## false ## Geraldine Somerville ## false ## 30-01-1960 ## 1960 ## green ## female ## blond ## true] +[ca3827f0-375a-4891-aaa5-f5e8a5bad225 ## Minerva McGonagall ## {} ## Gryffindor ## half-blood ## human ## tabby cat ## true ## false ## Dame Maggie Smith ## true ## 04-10-1925 ## 1925 ## grey ## female ## black ## true] +[933787c2-51e3-4eac-8a85-ab332cac0456 ## Miranda Gaushawk ## {} ## Gryffindor ## muggle ## human ## N/A ## false ## false ## N/A ## false ## 27-03-1960 ## 1960 ## blue ## female ## silver ## true] +[00434cd3-fcc7-44c7-8f98-7368415b4206 ## Miriam Strout ## {} ## Gryffindor ## half-blood ## human ## N/A ## false ## false ## N/A ## true ## 27-03-1960 ## 1954 ## blue ## female ## silver ## true] +[11b5ca88-64ad-41a4-9f36-317b66c290af ## Nearly Headless Nick ## {Sir Nicholas de Mimsy-Porpington, Sir Nicholas, Nick} ## Gryffindor ## pure-blood ## ghost ## N/A ## false ## false ## John Cleese ## false ## 27-03-1960 ## 1926 ## amber ## male ## sandy ## true] +[36bfefd0-e0bb-4d11-be98-d1ef6117a77a ## Rubeus Hagrid ## {Professor Hagrid, Hagger} ## Gryffindor ## half-blood ## half-giant ## N/A ## true ## false ## Robbie Coltrane ## true ## 06-12-1928 ## 1928 ## black ## male ## black ## true] +[1413e1b3-2903-4a47-a2d5-e8abc5ce8014 ## Seamus Finnigan ## {O Flaherty, Seamus Finnegan} ## Gryffindor ## half-blood ## human ## N/A ## false ## true ## Devon Murray ## true ## 31-12-1926 ## 1960 ## amber ## male ## sandy ## true] +[2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f ## Sirius Black ## {Padfoot, Snuffles} ## Gryffindor ## pure-blood ## human ## hare ## false ## false ## Gary Oldman ## false ## 03-11-1959 ## 1959 ## grey ## male ## black ## true] +[d59691a4-f830-4eb0-a819-a0fb00b7e80f ## Garrick Ollivander ## {} ## Hufflepuff ## muggle ## human ## N/A ## false ## false ## John Hurt ## true ## 04-10-1925 ## 1954 ## pale, silvery ## male ## brown ## true] +[6b59be3f-e527-422d-959d-79fcdb3b24eb ## Hedwig ## {} ## Hufflepuff ## muggle ## owl ## N/A ## false ## false ## Gizmo ## false ## 07-04-1979 ## 1954 ## amber ## female ## brown ## false] +[eaea5eb3-48a3-41c6-9ea5-c695299bab16 ## Lisa Turpin ## {} ## Ravenclaw ## pure-blood ## human ## N/A ## false ## true ## N/A ## true ## 27-03-1960 ## 1951 ## amber ## female ## sandy ## true] +[861c4cde-2f0f-4796-8d8f-9492e74b2573 ## Luna Lovegood ## {Loony Lovegood} ## Ravenclaw ## pure-blood ## human ## hare ## false ## true ## Evanna Lynch ## true ## 13-02-1981 ## 1981 ## silver ## female ## blonde ## true] +[20354d7a-e4fe-47af-8ff6-187bca92f3f9 ## Pandora Lovegood ## {} ## Ravenclaw ## quarter-veela ## human ## N/A ## false ## false ## N/A ## false ## 31-12-1926 ## 1959 ## dark ## female ## white ## true] +[3a0fe4df-2e40-4541-8d7f-13586f0b9294 ## Tom Riddle ## {Tom Riddle Senior} ## Ravenclaw ## muggle ## human ## N/A ## false ## false ## N/A ## false ## 27-03-1960 ## 1980 ## brown ## male ## dark ## false] +[6afb1960-febd-418d-b604-e50c1b59459b ## Bellatrix Lestrange ## {Bella} ## Slytherin ## pure-blood ## human ## N/A ## false ## false ## Helena Bonham Carter ## false ## 06-02-1950 ## 1951 ## brown ## female ## black ## true] +[0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3 ## Dobby ## {} ## Slytherin ## pure-blood ## house-elf ## N/A ## false ## false ## Toby Jones ## false ## 06-02-1950 ## 1950 ## green ## male ## blond ## false] +[d58e7249-19d1-40bd-a43f-1da0497fe8aa ## Dolores Umbridge ## {} ## Slytherin ## half-blood ## human ## persian cat ## true ## false ## Imelda Staunton ## true ## 31-12-1926 ## 1926 ## brown ## female ## grey ## true] +[7614cf6e-689e-47ac-a976-b1e9997637e9 ## Dudley Dursley ## {} ## Slytherin ## muggle ## human ## N/A ## false ## false ## Harry Melling ## true ## 23-06-1980 ## 1980 ## blue ## male ## blond ## false] +[5a4c95db-947d-4914-a631-41e8d466328e ## Kendra Dumbledore ## {} ## Slytherin ## muggleborn ## human ## N/A ## false ## false ## N/A ## false ## 31-12-1926 ## 1925 ## dark ## female ## black ## true] +[efa802c8-ae18-4ae1-a524-49df21d05939 ## Lord Voldemort ## {Tom Riddle, Tom Marvolo Riddle, You-Know-Who, He Who Must Not Be Named, the Dark Lord, the Heir of Slytherin} ## Slytherin ## half-blood ## human ## N/A ## false ## false ## Ralph Fiennes ## false ## 31-12-1926 ## 1926 ## Scarlet ## male ## bald ## true] +[b0620914-858d-46fc-8e6d-033c565e138b ## Mrs Norris ## {} ## Slytherin ## pure-blood ## cat ## N/A ## true ## false ## Maxime, Alanis and Tommy the cats ## true ## 31-12-1926 ## 1954 ## yellow ## female ## brown ## false] +[fed624df-56d9-495e-9ad4-ea77000957e8 ## Petunia Dursley ## {} ## Slytherin ## muggle ## human ## N/A ## false ## false ## Fiona Shaw ## true ## 31-12-1926 ## 1954 ## yellow ## female ## blonde ## false] +[57fe29d4-312a-4711-bd9a-c320253d9176 ## Victoire Weasley ## {} ## Slytherin ## pure-blood ## human ## N/A ## false ## true ## N/A ## true ## 31-12-1926 ## 1926 ## grey ## female ## blonde ## true] diff --git a/tps/fonte/tp02/TP02Q10 - Quicksort em C/pub.in b/tps/fonte/tp02/TP02Q10 - Quicksort em C/pub.in new file mode 100644 index 0000000..a782d8c --- /dev/null +++ b/tps/fonte/tp02/TP02Q10 - Quicksort em C/pub.in @@ -0,0 +1,28 @@ +9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8 +1413e1b3-2903-4a47-a2d5-e8abc5ce8014 +ca3827f0-375a-4891-aaa5-f5e8a5bad225 +36bfefd0-e0bb-4d11-be98-d1ef6117a77a +20354d7a-e4fe-47af-8ff6-187bca92f3f9 +57fe29d4-312a-4711-bd9a-c320253d9176 +b415c867-1cff-455e-b194-748662ac2cca +5a4c95db-947d-4914-a631-41e8d466328e +861c4cde-2f0f-4796-8d8f-9492e74b2573 +2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f +41cd0bbe-a943-431b-9bde-bb2cad3491a1 +2a0615de-8aa4-41e7-9504-dd875f5f3f01 +11b5ca88-64ad-41a4-9f36-317b66c290af +eaea5eb3-48a3-41c6-9ea5-c695299bab16 +0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3 +b0620914-858d-46fc-8e6d-033c565e138b +6b59be3f-e527-422d-959d-79fcdb3b24eb +fed624df-56d9-495e-9ad4-ea77000957e8 +d58e7249-19d1-40bd-a43f-1da0497fe8aa +3a0fe4df-2e40-4541-8d7f-13586f0b9294 +6afb1960-febd-418d-b604-e50c1b59459b +efa802c8-ae18-4ae1-a524-49df21d05939 +933787c2-51e3-4eac-8a85-ab332cac0456 +94c993f6-a443-4408-b908-6e78e223e8ac +00434cd3-fcc7-44c7-8f98-7368415b4206 +7614cf6e-689e-47ac-a976-b1e9997637e9 +d59691a4-f830-4eb0-a819-a0fb00b7e80f +FIM \ No newline at end of file diff --git a/tps/fonte/tp02/TP02Q11 - Counting Sort em Java/753045_countingsort.txt b/tps/fonte/tp02/TP02Q11 - Counting Sort em Java/753045_countingsort.txt new file mode 100644 index 0000000..4c266b2 --- /dev/null +++ b/tps/fonte/tp02/TP02Q11 - Counting Sort em Java/753045_countingsort.txt @@ -0,0 +1 @@ +753045 33ms 60 \ No newline at end of file diff --git a/tps/fonte/tp02/TP02Q11 - Counting Sort em Java/Arq.java b/tps/fonte/tp02/TP02Q11 - Counting Sort em Java/Arq.java new file mode 100644 index 0000000..9a632d1 --- /dev/null +++ b/tps/fonte/tp02/TP02Q11 - Counting Sort em Java/Arq.java @@ -0,0 +1,206 @@ +import java.io.*; +import java.util.Formatter; +import java.util.Scanner; +import java.io.File; +import java.nio.charset.*; + +public class Arq +{ + private static String nomeArquivo = ""; + private static String charsetArquivo = "ISO-8859-1"; + private static boolean write = false, read = false; + private static Formatter saida = null; + private static Scanner entrada = null; + + public static boolean openWrite(String nomeArq, String charset) { + boolean resp = false; + close(); + try{ + saida = new Formatter(nomeArq, charset); + nomeArquivo = nomeArq; + resp = write = true; + } catch (Exception e) {} + return resp; + } + + public static boolean openWrite(String nomeArq) { + return openWrite(nomeArq, charsetArquivo); + } + + public static boolean openWriteClose(String nomeArq, String charset, String conteudo) { + boolean resp = openWrite(nomeArq, charset); + if(resp == true){ + println(conteudo); + close(); + } + return resp; + } + + public static boolean openWriteClose(String nomeArq, String conteudo) { + return openWriteClose(nomeArq, charsetArquivo, conteudo); + } + + public static boolean openRead(String nomeArq) { + return openRead(nomeArq, charsetArquivo); + } + + public static boolean openRead(String nomeArq, String charset) { + boolean resp = false; + close(); + try{ + entrada = new Scanner(new File(nomeArq), charset); + nomeArquivo = nomeArq; + resp = read = true; + } catch (Exception e) {} + return resp; + } + + public static String openReadClose(String nomeArq){ + openRead(nomeArq); + String resp = readAll(); + close(); + return resp; + } + + public static void close() { + if(write == true){ + saida.close(); + } + if(read == true){ + entrada.close(); + } + write = read = false; + nomeArquivo = ""; + charsetArquivo = "ISO-8859-1"; + } + + public static long length(){ + long resp = -1; + if(read != write){ + File file = new File(nomeArquivo); + resp = file.length(); + } + return resp; + } + + public static void print(int x){ + if(write == true){ + saida.format( "%d", x); + } + } + + public static void print(double x){ + if(write == true){ + saida.format( "%f", x); + } + } + + public static void print(String x){ + if(write == true){ + saida.format( "%s", x); + } + } + + public static void print(boolean x){ + if(write == true){ + saida.format( "%s", ((x) ? "true" : "false")); + } + } + + public static void print(char x){ + if(write == true){ + saida.format( "%c", x); + } + } + + public static void println(int x){ + if(write == true){ + saida.format( "%d\n", x); + } + } + + public static void println(double x){ + if(write == true){ + saida.format( "%f\n", x); + } + } + + public static void println(String x){ + if(write == true){ + saida.format( "%s\n", x); + } + } + + public static void println(boolean x){ + if(write == true){ + saida.format( "%s\n", ((x) ? "true" : "false")); + } + } + + public static void println(char x){ + if(write == true){ + saida.format( "%c\n", x); + } + } + + public static int readInt(){ + int resp = -1; + try{ + resp = entrada.nextInt(); + } catch (Exception e) {} + return resp; + } + + public static char readChar(){ + char resp = ' '; + try{ + resp = (char)entrada.nextByte(); + } catch (Exception e) {} + return resp; + } + + public static double readDouble(){ + double resp = -1; + try{ + resp = Double.parseDouble(readString().replace(",",".")); + } catch (Exception e) {} + return resp; + } + + public static String readString(){ + String resp = ""; + try{ + resp = entrada.next(); + } catch (Exception e) { System.out.println(e.getMessage()); } + return resp; + } + + public static boolean readBoolean(){ + boolean resp = false; + try{ + resp = (entrada.next().equals("true")) ? true : false; + } catch (Exception e) {} + return resp; + } + + public static String readLine(){ + String resp = ""; + try{ + resp = entrada.nextLine(); + } catch (Exception e) { System.out.println(e.getMessage()); } + return resp; + } + + + public static boolean hasNext(){ + return entrada.hasNext(); + } + + public static String readAll(){ + String resp = ""; + while(hasNext()){ + resp += (readLine() + "\n"); + } + return resp; + } +} diff --git a/tps/fonte/tp02/TP02Q11 - Counting Sort em Java/Character.java b/tps/fonte/tp02/TP02Q11 - Counting Sort em Java/Character.java new file mode 100644 index 0000000..84ed581 --- /dev/null +++ b/tps/fonte/tp02/TP02Q11 - Counting Sort em Java/Character.java @@ -0,0 +1,565 @@ +/** + * @path TP02Q01 - Classe em Java/Characters.java + * @description Java class of all characters from Harry Potter's saga with counting sort algorithm + * @author Pedro Lopes - github.com/httpspedroh + * @version 1.0 + * @update 2024-04-09 + */ + +// ---------------------------------------------------------------------------------------------------- // + +// Imports +import java.util.Scanner; +import java.util.UUID; +import java.io.BufferedReader; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Date; +import java.text.SimpleDateFormat; + +// ---------------------------------------------------------------------------------------------------- // + +public class Character { + + static SimpleDateFormat ddf = new SimpleDateFormat("dd-MM-yyyy"); + + // Global variables + public static final String FILE_PATH = "/tmp/characters.csv"; + public static ArrayList allCharacters = new ArrayList(); + + // -------------------------- // + + // Attributes + private UUID id; + private String name; + private ArrayList alternateNames; + private String house; + private String ancestry; + private String species; + private String patronus; + private boolean hogwartsStaff; + private boolean hogwartsStudent; + private String actorName; + private boolean alive; + private Date birthDate; + private int yearOfBirth; + private String eyeColour; + private String gender; + private String hairColour; + private boolean wizard; + + // -------------------------- // + + // Empty constructor + public Character() { + + this.id = UUID.randomUUID(); + this.name = ""; + this.alternateNames = new ArrayList(); + this.house = ""; + this.ancestry = ""; + this.species = ""; + this.patronus = ""; + this.hogwartsStaff = false; + this.hogwartsStudent = false; + this.actorName = ""; + this.alive = false; + this.birthDate = null; + this.yearOfBirth = 0; + this.eyeColour = ""; + this.gender = ""; + this.hairColour = ""; + this.wizard = false; + } + + // Constructor + public Character(UUID id, String name, ArrayList alternateNames, String house, String ancestry, String species, String patronus, boolean hogwartsStaff, boolean hogwartsStudent, String actorName, boolean alive, Date birthDate, int yearOfBirth, String eyeColour, String gender, String hairColour, boolean wizard) { + + this.id = id; + this.name = name; + this.alternateNames = alternateNames; + this.house = house; + this.ancestry = ancestry; + this.species = species; + this.patronus = patronus; + this.hogwartsStaff = hogwartsStaff; + this.hogwartsStudent = hogwartsStudent; + this.actorName = actorName; + this.alive = alive; + this.birthDate = birthDate; + this.yearOfBirth = yearOfBirth; + this.eyeColour = eyeColour; + this.gender = gender; + this.hairColour = hairColour; + this.wizard = wizard; + } + + // -------------------------- // + + // Gets + public UUID getId() { return this.id; } + public String getName() { return this.name; } + public String getHouse() { return this.house; } + public String getAncestry() { return this.ancestry; } + public String getSpecies() { return this.species; } + public String getPatronus() { return this.patronus; } + public boolean getHogwartsStaff() { return this.hogwartsStaff; } + public boolean getHogwartsStudent() { return this.hogwartsStudent; } + public String getActorName() { return this.actorName; } + public boolean getAlive() { return this.alive; } + public Date getBirthDate() { return this.birthDate; } + public int getYearOfBirth() { return this.yearOfBirth; } + public String getEyeColour() { return this.eyeColour; } + public String getGender() { return this.gender; } + public String getHairColour() { return this.hairColour; } + public boolean getWizard() { return this.wizard; } + + public String getAlternateNames() { + + // Construct string e.g. {item1, item2, item3} + String alternateNames = "{"; + + for(int i = 0; i < this.alternateNames.size(); i++) { + + alternateNames += this.alternateNames.get(i); + + if(i < this.alternateNames.size() - 1) alternateNames += ", "; + } + + alternateNames += "}"; + + return alternateNames; + } + + // Sets + public void setId(UUID id) { this.id = id; } + public void setName(String name) { this.name = name; } + public void setAlternateNames(ArrayList alternateNames) { this.alternateNames = alternateNames; } + public void setHouse(String house) { this.house = house; } + public void setAncestry(String ancestry) { this.ancestry = ancestry; } + public void setSpecies(String species) { this.species = species; } + public void setPatronus(String patronus) { this.patronus = patronus; } + public void setHogwartsStaff(boolean hogwartsStaff) { this.hogwartsStaff = hogwartsStaff; } + public void setHogwartsStudent(boolean hogwartsStudent) { this.hogwartsStudent = hogwartsStudent; } + public void setActorNane(String actorName) { this.actorName = actorName; } + public void setAlive(boolean alive) { this.alive = alive; } + public void setBirthDate(Date birthDate) { this.birthDate = birthDate; } + public void setYearOfBirth(int yearOfBirth) { this.yearOfBirth = yearOfBirth; } + public void setEyeColour(String eyeColour) { this.eyeColour = eyeColour; } + public void setGender(String gender) { this.gender = gender; } + public void setHairColour(String hairColour) { this.hairColour = hairColour; } + public void setWizard(boolean wizard) { this.wizard = wizard; } + + // -------------------------- // + + // Clone + public Character clone() { return new Character(this.id, this.name, this.alternateNames, this.house, this.ancestry, this.species, this.patronus, this.hogwartsStaff, this.hogwartsStudent, this.actorName, this.alive, this.birthDate, this.yearOfBirth, this.eyeColour, this.gender, this.hairColour, this.wizard); } + + // -------------------------- // + + // Print + public void print() { + + System.out.println("[" + + this.getId() + " ## " + + this.getName() + " ## " + + this.getAlternateNames() + " ## " + + (this.getHouse() == "" ? "N/A" : this.getHouse()) + " ## " + + (this.getAncestry() == "" ? "N/A" : this.getAncestry()) + " ## " + + (this.getSpecies() == "" ? "N/A" : this.getSpecies()) + " ## " + + (this.getPatronus() == "" ? "N/A" : this.getPatronus()) + " ## " + + (this.getHogwartsStaff() ? "true" : "false") + " ## " + + (this.getHogwartsStudent() ? "true" : "false") + " ## " + + (this.getActorName() == "" ? "N/A" : this.getActorName()) + " ## " + + (this.getAlive() ? "true" : "false") + " ## " + + (this.getBirthDate() == null ? "N/A" : ddf.format(this.getBirthDate())) + " ## " + + (this.getYearOfBirth() == 0 ? "N/A" : this.getYearOfBirth()) + " ## " + + (this.getEyeColour() == "" ? "N/A" : this.getEyeColour()) + " ## " + + (this.getGender() == "" ? "N/A" : this.getGender()) + " ## " + + (this.getHairColour() == "" ? "N/A" : this.getHairColour()) + " ## " + + (this.getWizard() ? "true" : "false") + "]"); + } + + // -------------------------- // + + public static ArrayList extractNames(String input) { + + ArrayList names = new ArrayList<>(); + + // Clean input + String cleanedInput = input.substring(1, input.length() - 1); + + // Check if input contains double quotes + if (cleanedInput.contains("\"\"")) { + + String[] parts = cleanedInput.split("\", "); + + for (String part : parts) names.add(part.replace("\"", "").replace("'", "")); + } + else { + + // Split input by comma and space + String[] parts = cleanedInput.split(", "); + + for (String part : parts) names.add(part.replace("'", "")); + } + return names; + } + + // -------------------------- // + + // Read + public void read(String line) { + + // ---------------------- // + + // Start position + int positionStart = 0; + int positionEnd = 0; + + // ---------------------- // + + // Set id + positionEnd = line.indexOf(";", positionStart); + this.setId(UUID.fromString(line.substring(positionStart, positionEnd))); + + // ---------------------- // + + // Set name + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setName(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set alternate names + positionStart = positionEnd + 1; + + if(line.charAt(positionStart) == '[') { // If first character is '[', has one or no names + + if(line.charAt(positionStart + 1) == ']') { + + positionEnd = positionStart + 2; + + this.setAlternateNames(new ArrayList()); + } + else { + + positionEnd = line.indexOf("];", positionStart); + + this.setAlternateNames(extractNames(line.substring(positionStart, positionEnd++))); + } + } + + // ---------------------- // + + // Set house + positionStart = positionEnd; + positionEnd = line.indexOf(";", ++positionStart); + + this.setHouse(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set ancestry + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setAncestry(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set species + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setSpecies(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set patronus + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setPatronus(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set hogwartsStaff + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setHogwartsStaff("VERDADEIRO".equals(line.substring(positionStart, positionEnd))); + + // ---------------------- // + + // Set hogwartsStudent + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setHogwartsStudent("VERDADEIRO".equals(line.substring(positionStart, positionEnd))); + + // ---------------------- // + + // Set actor name + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setActorNane(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set alive + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setAlive("VERDADEIRO".equals(line.substring(positionStart, positionEnd))); + + // ---------------------- // + + // Set date of birth + positionStart = positionEnd; + positionEnd = line.indexOf(";", ++positionStart); + + try { this.setBirthDate(ddf.parse(line.substring(positionStart, positionEnd))); } + catch(Exception e) { this.setBirthDate(null); } + + // ---------------------- // + + // Set year of birth + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + try { this.setYearOfBirth(Integer.parseInt(line.substring(positionStart, positionEnd))); } + catch(Exception e) { this.setYearOfBirth(0); } + + // ---------------------- // + + // Set eye colour + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setEyeColour(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set gender + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setGender(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set hair colour + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setHairColour(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set wizard + positionStart = positionEnd + 1; + + this.setWizard("VERDADEIRO".equals(line.substring(positionStart, line.length()))); + + // ---------------------- // + } + + // -------------------------- // + + // Compare method + public int compareTo(Character other) { + + // Compare by yearOfBirth + int yearOfBirthComparison = Integer.compare(this.yearOfBirth, other.yearOfBirth); + + // If yearOfBirth is the same, compare by name + if(yearOfBirthComparison != 0) return yearOfBirthComparison; + else return this.name.compareTo(other.name); + } + + // ---------------------------------------------------------------------------------------------------- // + + // Read all characters function + public static void startCharacters() { + + // Initialize variables + try { + + FileInputStream fstream = new FileInputStream(FILE_PATH); + BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); + + // ---------------------- // + + // Explode CSV file + String line = br.readLine(); + + while((line = br.readLine()) != null) { + + // Initialize character + Character character = new Character(); + + // Read line + character.read(line); + + // Add character to array + allCharacters.add(character); + } + + // Close CSV file + fstream.close(); + } + catch(IOException e) { e.printStackTrace(); } + } + + // ---------------------------------------------------------------------------------------------------- // + + // Search by id function + public static Character searchById(UUID id, ArrayList characters) { + + // Search for character + for(int i = 0; i < characters.size(); i++) { + + if(characters.get(i).getId().equals(id)) return characters.get(i); + } + return null; + } + + // ---------------------------------------------------------------------------------------------------- // + + // Helper method to find the maximum yearOfBirth in the mainCharacters array + private static int getMaxHeight(ArrayList characters) { + + int max = Integer.MIN_VALUE; + + for(Character character : characters) { + + if(character.getYearOfBirth() > max) max = character.getYearOfBirth(); + } + return max; + } + + // ---------------------------------------------------------------------------------------------------- // + + public static void main(String[] args) { + + // ----------------------------------------------------------------- // + + // #1 - Start - Read all characters in CSV file + startCharacters(); + + // ----------------------------------------------------------------- // + + // #2 - Read input and print characters from pub.in id entries and add to mainCharacters array + + // Initialize mainCharacters array + ArrayList mainCharacters = new ArrayList(); + + // Initialize scanner + Scanner inScanner = new Scanner(System.in); + + // Initialize character + Character character = new Character(); + + // Read first line + String line = inScanner.nextLine(); + + // While line is not "FIM" + while(!line.equals("FIM")) { + + // Get id + UUID id = UUID.fromString(line); + + // Search for character + character = searchById(id, allCharacters); + + // Print character + if(character != null) mainCharacters.add(character); + + // Read line + line = inScanner.nextLine(); + } + + // ----------------------------------------------------------------- // + + // #3 - Order mainCharacters array by key "yearOfBirth" using counting sort, in draw case, order by key "name" + + // Start benchmark + long startTime = System.currentTimeMillis(); + int comparisons = 0; + + // ----------------- // + + // Counting sort + + // Create an array to store the counts + int maxHeight = getMaxHeight(mainCharacters); + int[] count = new int[maxHeight + 1]; + + // Initialize the count array + for(int i = 0; i < mainCharacters.size(); i++) count[mainCharacters.get(i).getYearOfBirth()]++; + + // Modify the count array to store the position of elements + for(int i = 1; i <= maxHeight; i++) count[i] += count[i - 1]; + + // Create a temporary array to store the sorted output + Character[] sortedCharacters = new Character[mainCharacters.size()]; + + // Build the sorted array + for(int i = mainCharacters.size() - 1; i >= 0; i--) { + + int yob = mainCharacters.get(i).getYearOfBirth(); + sortedCharacters[count[yob] - 1] = mainCharacters.get(i); + count[yob]--; + } + + // Copy the sorted array back to the mainCharacters array + for(int i = 0; i < mainCharacters.size(); i++) mainCharacters.set(i, sortedCharacters[i]); + + // Use insertion sort to order by key "yearOfBirth" and "name" in draw case + for(int i = 1; i < mainCharacters.size(); i++) { + + Character key = mainCharacters.get(i); + int j = i - 1; + + while(j >= 0 && mainCharacters.get(j).getYearOfBirth() == key.getYearOfBirth()) { + + // While and if comparisons + comparisons += 2; + + if(mainCharacters.get(j).getName().compareTo(key.getName()) > 0) { + + mainCharacters.set(j + 1, mainCharacters.get(j)); + j--; + } + else break; + } + + mainCharacters.set(j + 1, key); + } + + // ----------------- // + + // Save benchmark in file + Arq.openWrite("753045_countingsort.txt"); + Arq.print("753045\t" + (System.currentTimeMillis() - startTime) + "ms\t" + comparisons); + Arq.close(); + + // ----------------- // + + // Print mainCharacters array + for(int i = 0; i < mainCharacters.size(); i++) mainCharacters.get(i).print(); + + // ----------------------------------------------------------------- // + + // Close scanner + inScanner.close(); + } +} + +// ---------------------------------------------------------------------------------------------------- // \ No newline at end of file diff --git a/tps/fonte/tp02/TP02Q11 - Counting Sort em Java/pedro.out b/tps/fonte/tp02/TP02Q11 - Counting Sort em Java/pedro.out new file mode 100644 index 0000000..96d5268 --- /dev/null +++ b/tps/fonte/tp02/TP02Q11 - Counting Sort em Java/pedro.out @@ -0,0 +1,27 @@ +[b415c867-1cff-455e-b194-748662ac2cca ## Albus Dumbledore ## {Professor Dumbledore} ## Gryffindor ## half-blood ## human ## Phoenix ## true ## false ## Richard Harris ## false ## 27-03-1960 ## 1881 ## blue ## male ## silver ## true] +[5a4c95db-947d-4914-a631-41e8d466328e ## Kendra Dumbledore ## {} ## Slytherin ## muggleborn ## human ## N/A ## false ## false ## N/A ## false ## 31-12-1926 ## 1925 ## dark ## female ## black ## true] +[ca3827f0-375a-4891-aaa5-f5e8a5bad225 ## Minerva McGonagall ## {} ## Gryffindor ## half-blood ## human ## tabby cat ## true ## false ## Dame Maggie Smith ## true ## 04-10-1925 ## 1925 ## grey ## female ## black ## true] +[d58e7249-19d1-40bd-a43f-1da0497fe8aa ## Dolores Umbridge ## {} ## Slytherin ## half-blood ## human ## persian cat ## true ## false ## Imelda Staunton ## true ## 31-12-1926 ## 1926 ## brown ## female ## grey ## true] +[efa802c8-ae18-4ae1-a524-49df21d05939 ## Lord Voldemort ## {Tom Riddle, Tom Marvolo Riddle, You-Know-Who, He Who Must Not Be Named, the Dark Lord, the Heir of Slytherin} ## Slytherin ## half-blood ## human ## N/A ## false ## false ## Ralph Fiennes ## false ## 31-12-1926 ## 1926 ## Scarlet ## male ## bald ## true] +[11b5ca88-64ad-41a4-9f36-317b66c290af ## Nearly Headless Nick ## {Sir Nicholas de Mimsy-Porpington, Sir Nicholas, Nick} ## Gryffindor ## pure-blood ## ghost ## N/A ## false ## false ## John Cleese ## false ## 27-03-1960 ## 1926 ## amber ## male ## sandy ## true] +[57fe29d4-312a-4711-bd9a-c320253d9176 ## Victoire Weasley ## {} ## Slytherin ## pure-blood ## human ## N/A ## false ## true ## N/A ## true ## 31-12-1926 ## 1926 ## grey ## female ## blonde ## true] +[36bfefd0-e0bb-4d11-be98-d1ef6117a77a ## Rubeus Hagrid ## {Professor Hagrid, Hagger} ## Gryffindor ## half-blood ## half-giant ## N/A ## true ## false ## Robbie Coltrane ## true ## 06-12-1928 ## 1928 ## black ## male ## black ## true] +[0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3 ## Dobby ## {} ## Slytherin ## pure-blood ## house-elf ## N/A ## false ## false ## Toby Jones ## false ## 06-02-1950 ## 1950 ## green ## male ## blond ## false] +[6afb1960-febd-418d-b604-e50c1b59459b ## Bellatrix Lestrange ## {Bella} ## Slytherin ## pure-blood ## human ## N/A ## false ## false ## Helena Bonham Carter ## false ## 06-02-1950 ## 1951 ## brown ## female ## black ## true] +[eaea5eb3-48a3-41c6-9ea5-c695299bab16 ## Lisa Turpin ## {} ## Ravenclaw ## pure-blood ## human ## N/A ## false ## true ## N/A ## true ## 27-03-1960 ## 1951 ## amber ## female ## sandy ## true] +[d59691a4-f830-4eb0-a819-a0fb00b7e80f ## Garrick Ollivander ## {} ## Hufflepuff ## muggle ## human ## N/A ## false ## false ## John Hurt ## true ## 04-10-1925 ## 1954 ## pale, silvery ## male ## brown ## true] +[2a0615de-8aa4-41e7-9504-dd875f5f3f01 ## George Weasley ## {} ## Gryffindor ## pure-blood ## human ## N/A ## false ## true ## Oliver Phelps ## true ## 13-02-1981 ## 1954 ## amber ## male ## red ## true] +[6b59be3f-e527-422d-959d-79fcdb3b24eb ## Hedwig ## {} ## Hufflepuff ## muggle ## owl ## N/A ## false ## false ## Gizmo ## false ## 07-04-1979 ## 1954 ## amber ## female ## brown ## false] +[00434cd3-fcc7-44c7-8f98-7368415b4206 ## Miriam Strout ## {} ## Gryffindor ## half-blood ## human ## N/A ## false ## false ## N/A ## true ## 27-03-1960 ## 1954 ## blue ## female ## silver ## true] +[b0620914-858d-46fc-8e6d-033c565e138b ## Mrs Norris ## {} ## Slytherin ## pure-blood ## cat ## N/A ## true ## false ## Maxime, Alanis and Tommy the cats ## true ## 31-12-1926 ## 1954 ## yellow ## female ## brown ## false] +[fed624df-56d9-495e-9ad4-ea77000957e8 ## Petunia Dursley ## {} ## Slytherin ## muggle ## human ## N/A ## false ## false ## Fiona Shaw ## true ## 31-12-1926 ## 1954 ## yellow ## female ## blonde ## false] +[20354d7a-e4fe-47af-8ff6-187bca92f3f9 ## Pandora Lovegood ## {} ## Ravenclaw ## quarter-veela ## human ## N/A ## false ## false ## N/A ## false ## 31-12-1926 ## 1959 ## dark ## female ## white ## true] +[2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f ## Sirius Black ## {Padfoot, Snuffles} ## Gryffindor ## pure-blood ## human ## hare ## false ## false ## Gary Oldman ## false ## 03-11-1959 ## 1959 ## grey ## male ## black ## true] +[94c993f6-a443-4408-b908-6e78e223e8ac ## Aberforth Dumbledore ## {} ## Gryffindor ## half-blood ## human ## goat ## false ## false ## Jim McManus ## true ## 10-03-1960 ## 1960 ## blue ## male ## grey ## true] +[41cd0bbe-a943-431b-9bde-bb2cad3491a1 ## Lily Potter ## {} ## Gryffindor ## muggleborn ## human ## N/A ## false ## false ## Geraldine Somerville ## false ## 30-01-1960 ## 1960 ## green ## female ## blond ## true] +[933787c2-51e3-4eac-8a85-ab332cac0456 ## Miranda Gaushawk ## {} ## Gryffindor ## muggle ## human ## N/A ## false ## false ## N/A ## false ## 27-03-1960 ## 1960 ## blue ## female ## silver ## true] +[1413e1b3-2903-4a47-a2d5-e8abc5ce8014 ## Seamus Finnigan ## {O Flaherty, Seamus Finnegan} ## Gryffindor ## half-blood ## human ## N/A ## false ## true ## Devon Murray ## true ## 31-12-1926 ## 1960 ## amber ## male ## sandy ## true] +[7614cf6e-689e-47ac-a976-b1e9997637e9 ## Dudley Dursley ## {} ## Slytherin ## muggle ## human ## N/A ## false ## false ## Harry Melling ## true ## 23-06-1980 ## 1980 ## blue ## male ## blond ## false] +[9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8 ## Harry Potter ## {The Boy Who Lived, The Chosen One, Undesirable No. 1, Potty} ## Gryffindor ## half-blood ## human ## stag ## false ## true ## Daniel Radcliffe ## true ## 31-07-1980 ## 1980 ## green ## male ## black ## true] +[3a0fe4df-2e40-4541-8d7f-13586f0b9294 ## Tom Riddle ## {Tom Riddle Senior} ## Ravenclaw ## muggle ## human ## N/A ## false ## false ## N/A ## false ## 27-03-1960 ## 1980 ## brown ## male ## dark ## false] +[861c4cde-2f0f-4796-8d8f-9492e74b2573 ## Luna Lovegood ## {Loony Lovegood} ## Ravenclaw ## pure-blood ## human ## hare ## false ## true ## Evanna Lynch ## true ## 13-02-1981 ## 1981 ## silver ## female ## blonde ## true] diff --git a/tps/fonte/tp02/TP02Q11 - Counting Sort em Java/pub.in b/tps/fonte/tp02/TP02Q11 - Counting Sort em Java/pub.in new file mode 100644 index 0000000..a782d8c --- /dev/null +++ b/tps/fonte/tp02/TP02Q11 - Counting Sort em Java/pub.in @@ -0,0 +1,28 @@ +9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8 +1413e1b3-2903-4a47-a2d5-e8abc5ce8014 +ca3827f0-375a-4891-aaa5-f5e8a5bad225 +36bfefd0-e0bb-4d11-be98-d1ef6117a77a +20354d7a-e4fe-47af-8ff6-187bca92f3f9 +57fe29d4-312a-4711-bd9a-c320253d9176 +b415c867-1cff-455e-b194-748662ac2cca +5a4c95db-947d-4914-a631-41e8d466328e +861c4cde-2f0f-4796-8d8f-9492e74b2573 +2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f +41cd0bbe-a943-431b-9bde-bb2cad3491a1 +2a0615de-8aa4-41e7-9504-dd875f5f3f01 +11b5ca88-64ad-41a4-9f36-317b66c290af +eaea5eb3-48a3-41c6-9ea5-c695299bab16 +0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3 +b0620914-858d-46fc-8e6d-033c565e138b +6b59be3f-e527-422d-959d-79fcdb3b24eb +fed624df-56d9-495e-9ad4-ea77000957e8 +d58e7249-19d1-40bd-a43f-1da0497fe8aa +3a0fe4df-2e40-4541-8d7f-13586f0b9294 +6afb1960-febd-418d-b604-e50c1b59459b +efa802c8-ae18-4ae1-a524-49df21d05939 +933787c2-51e3-4eac-8a85-ab332cac0456 +94c993f6-a443-4408-b908-6e78e223e8ac +00434cd3-fcc7-44c7-8f98-7368415b4206 +7614cf6e-689e-47ac-a976-b1e9997637e9 +d59691a4-f830-4eb0-a819-a0fb00b7e80f +FIM \ No newline at end of file diff --git a/tps/fonte/tp02/TP02Q12 - Bolha em C/753045_bolha.txt b/tps/fonte/tp02/TP02Q12 - Bolha em C/753045_bolha.txt new file mode 100644 index 0000000..3ec04da --- /dev/null +++ b/tps/fonte/tp02/TP02Q12 - Bolha em C/753045_bolha.txt @@ -0,0 +1 @@ +753045 0ms 493 \ No newline at end of file diff --git a/tps/fonte/tp02/TP02Q12 - Bolha em C/Character b/tps/fonte/tp02/TP02Q12 - Bolha em C/Character new file mode 100644 index 0000000000000000000000000000000000000000..5be84db8d9b27601e3dbccb00df6d0ea2f38f529 GIT binary patch literal 27848 zcmeHwe|(hHneRzLAW+Ceg-R96s70eTA)ruLL=wori<@8y1d$)k49SE^&95di8mQRN z5S!OwFx!RQ)C>2erz1yX|O4%*w07{~}?MAC!>SCLZ+X==Blx`~!cyXe*=s_wgUnYuGoE`rH!RN0Lx zJ52|bx~955sZQwgyz-k&OCh66Q_`iY;@5#{O6Qj_JBA)q`Dv={z6?7m=cQh{+SjW5 z($&jMRenuX_F`^b?Y4z?%&n^~sH<%VwidK5T3E2~j>4A4!ufpP(@_wLsa0z#Mdy8g zd+yk?J5NPt|JS#Qp1jVJx5v4i%1vb;f22bf>8tFRUIU)w^1gN>B?Mt~z3lsGy-cP4 z{#(FTrV!0bgTDgYnL?lPPvvKE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6Rw5e?Dl#;Q&VSx zcKmr2}g_XtqoOFo@y4!^fSlJ@L%`SpIQrc^(4>GEqmO&@@a-T`P{@5^ec9uxU2 ze9BvwpM27_6CY|5ZLjxlHH|<-*mW8QCu zUwCx3Q?;B|;Mn#zA+g&#mA$4OmA`h7ijkOog{m`U#;n6t*9u~wf*`Y7`jE>exg`O70pk&e_d{+C4PW}-9GvM<2jgQpPc`5 zN%nf5TvPhpo&8GA|3wICZ<+cKU3&ji|35v+`kL4DHP|@t(e`@ZeWaYc#p6wKabDa1 zD+E+TZLjxr_5Xu3sP13ihPKn~*bQ4!(e`>D|I9rAapg;5eQSG7-+{z#FFZ=MLehw0 zHPLpOeh7ixKH`y@5oNE-nQX85PmmyAy8MG4DH%}qU1~o~qCz-zLeYz$lz&kYuI<%! zwIwB8y6n`7T`A)4QI&s@dO=L{5be$A`_|(R^$%TkHdJ5vY9nnXO)j=e=c^&=?y2mH NtEI-IB!eVY{2$|-ZLt6V literal 0 HcmV?d00001 diff --git a/tps/fonte/tp02/TP02Q12 - Bolha em C/Character.c b/tps/fonte/tp02/TP02Q12 - Bolha em C/Character.c new file mode 100644 index 0000000..56d3a8b --- /dev/null +++ b/tps/fonte/tp02/TP02Q12 - Bolha em C/Character.c @@ -0,0 +1,706 @@ +/** + * @path TP02Q02 - Classe em C/Character.c + * @description C file that implements the Character class with bubble sort algorithm + * @author Pedro Lopes - github.com/httpspedroh + * @version 1.0 + * @update 2024-04-09 + */ + +// ---------------------------------------------------------------------------------------------------- // + +// Includes +#include +#include +#include +#include +#include + +// ---------------------------------------------------------------------------------------------------- // + +// Constants +#define MAX_CHARACTERS 405 +#define FILE_PATH "/tmp/characters.csv" + +#define MAX_UUID_SIZE 37 +#define MAX_NAME_SIZE 30 +#define MAX_ALTERNATE_NAMES 10 +#define MAX_ALTERNATE_NAME_SIZE 35 +#define MAX_HOUSE_SIZE 15 +#define MAX_ANCESTRY_SIZE 15 +#define MAX_SPECIES_SIZE 20 +#define MAX_PATRONUS_SIZE 25 +#define MAX_ACTOR_NAME_SIZE 35 +#define MAX_EYE_COLOUR_SIZE 10 +#define MAX_GENDER_SIZE 10 +#define MAX_HAIR_COLOUR_SIZE 10 + +#define MAX_LINE_SIZE 300 + +// ---------------------------------------------------------------------------------------------------- // + +// Structs +typedef struct Date { + + int day; + int month; + int year; +} Date; + +typedef struct Character { + + char *id; + char *name; + char *alternateNames[MAX_ALTERNATE_NAMES]; + char *house; + char *ancestry; + char *species; + char *patronus; + bool hogwartsStaff; + bool hogwartsStudent; + char *actorName; + bool alive; + Date birthDate; + int yearOfBirth; + char *eyeColour; + char *gender; + char *hairColour; + bool wizard; +} Character; + +// ---------------------------------------------------------------------------------------------------- // + +// Global variables +Character characters[MAX_CHARACTERS]; +int charactersLength = 0; + +// ---------------------------------------------------------------------------------------------------- // + +// Functions +bool isEnd(char* line) { return line[0] == 'F' && line[1] == 'I' && line[2] == 'M'; } + +void substring(char *string, char *stringStart, int length) { + + strncpy(string, stringStart, length); + string[length] = '\0'; +} + +void proccess_attribute(char *attribute, char **substringStart, char **substringEnd, bool isFirstAttribute, bool isStringArray) { + + // Skip first comma + if(!isFirstAttribute) { + + if(*substringEnd != NULL) *substringStart = *substringEnd + 1; + else *substringStart = *substringEnd; + } + + if(!isStringArray) { + + if((*substringStart)[0] == '"') { + + *substringStart = *substringStart + 1; + *substringEnd = strchr(*substringStart, '"'); + } + else *substringEnd = strchr(*substringStart, ';'); + + // Get substring + if(*substringEnd) { + + substring(attribute, *substringStart, *substringEnd - *substringStart); + + if(*substringEnd[0] == '"') *substringEnd = *substringEnd + 1; + } + else strcpy(attribute, *substringStart); + + // Set default value if attribute is empty + if(strcmp(attribute, "") == 0 || attribute[0] == '\n' || attribute[0] == '\r' || attribute[0] == '\0') strcpy(attribute, "N/A"); + + // Clean \n from the end of the string + if(attribute[strlen(attribute) - 1] == '\n' || attribute[strlen(attribute) - 1] == '\r') attribute[strlen(attribute) - 1] = '\0'; + } + else { + + // Check if the first character is a [ + if((*substringStart)[0] == '[') { + + *substringStart = *substringStart + 1; + + if((*substringStart)[0] == ']') strcpy(attribute, ""); // Case: [] + else { + + char *tempConcat = (char *) calloc(MAX_LINE_SIZE, sizeof(char)); + + *substringStart = *substringStart - 1; + + while(1) { + + *substringStart = *substringStart + 1; + + if((*substringStart)[0] == ';') break; + else if((*substringStart)[0] == '\'') { // Case: "['example', 'example']" + + *substringStart = *substringStart + 1; + *substringEnd = strchr(*substringStart, '\''); + + // Get substring + if(*substringEnd) { + + // Create tmp name + char tmp[MAX_LINE_SIZE]; + substring(tmp, *substringStart, *substringEnd - *substringStart); + + // Concat tempConcat with tmp + strcat(tempConcat, tmp); + strcat(tempConcat, ", "); + + *substringStart = *substringEnd + 1; + } + } + } + + // Get substring + strcpy(attribute, tempConcat); + + // Clean "attribute" removing last 2 characters + attribute[strlen(attribute) - 2] = '\0'; + } + } + + *substringEnd = strchr(*substringStart, ';'); + } +} + +// ---------------------------------------------------------------------------------------------------- // + +// Methods implementations + +// Gets +char *character_getId(Character *character) { return character -> id; } +char *character_getName(Character *character) { return character -> name; } +char *character_getHouse(Character *character) { return character -> house; } +char *character_getAncestry(Character *character) { return character -> ancestry; } +char *character_getSpecies(Character *character) { return character -> species; } +char *character_getPatronus(Character *character) { return character -> patronus; } +bool character_getHogwartsStaff(Character *character) { return character -> hogwartsStaff; } +bool character_getHogwartsStudent(Character *character) { return character -> hogwartsStudent; } +char *character_getActorName(Character *character) { return character -> actorName; } +bool character_getAlive(Character *character) { return character -> alive; } +char *character_getEyeColour(Character *character) { return character -> eyeColour; } +char *character_getGender(Character *character) { return character -> gender; } +char *character_getHairColour(Character *character) { return character -> hairColour; } +bool character_getWizard(Character *character) { return character -> wizard; } + +char *character_getYearOfBirth(Character *character) { + + // "N/A" if yearOfBirth is -1 + char *yearOfBirth = (char *) calloc(15, sizeof(char)); + strcpy(yearOfBirth, "N/A"); + + if(character -> yearOfBirth != -1) sprintf(yearOfBirth, "%d", character -> yearOfBirth); + return yearOfBirth; +} + +char *character_getBirthDate(Character *character) { + + // "N/A" if day, month and year are -1 + char *birthDate = (char *) calloc(15, sizeof(char)); + strcpy(birthDate, "N/A"); + + if(character -> birthDate.day != -1 && character -> birthDate.month != -1 && character -> birthDate.year != -1) { + + sprintf(birthDate, "%02d-%02d-%04d", character -> birthDate.day, character -> birthDate.month, character -> birthDate.year); + } + + return birthDate; +} + +char *character_getAlternateNames(Character *character) { + + // Concatenate all alternate names with a comma + char *alternateNames = (char *) calloc(MAX_ALTERNATE_NAME_SIZE * MAX_ALTERNATE_NAMES, sizeof(char)); + + alternateNames[0] = '{'; + + for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) { + + if(strcmp(character -> alternateNames[i], "") != 0) { + + strcat(alternateNames, character -> alternateNames[i]); + + if(strcmp(character -> alternateNames[i + 1], "") != 0) strcat(alternateNames, ", "); + } + } + + strcat(alternateNames, "}"); + + return alternateNames; +} + +// Sets +void character_setId(Character *character, char *id) { strcpy(character -> id, id); } +void character_setName(Character *character, char *name) { strcpy(character -> name, name); } +void character_setHouse(Character *character, char *house) { strcpy(character -> house, house); } +void character_setAncestry(Character *character, char *ancestry) { strcpy(character -> ancestry, ancestry); } +void character_setSpecies(Character *character, char *species) { strcpy(character -> species, species); } +void character_setPatronus(Character *character, char *patronus) { strcpy(character -> patronus, patronus); } +void character_setHogwartsStaff(Character *character, bool hogwartsStaff) { character -> hogwartsStaff = hogwartsStaff; } +void character_setHogwartsStudent(Character *character, bool hogwartsStudent) { character -> hogwartsStudent = hogwartsStudent; } +void character_setActorName(Character *character, char *actorName) { strcpy(character -> actorName, actorName); } +void character_setAlive(Character *character, bool alive) { character -> alive = alive; } +void character_setYearOfBirth(Character *character, int yearOfBirth) { character -> yearOfBirth = yearOfBirth; } +void character_setEyeColour(Character *character, char *eyeColour) { strcpy(character -> eyeColour, eyeColour); } +void character_setGender(Character *character, char *gender) { strcpy(character -> gender, gender); } +void character_setHairColour(Character *character, char *hairColour) { strcpy(character -> hairColour, hairColour); } +void character_setWizard(Character *character, bool wizard) { character -> wizard = wizard; } + +void character_setBirthDate(Character *character, char *birthDate) { + + // Explode birthDate in format DD-MM-YYYY if in format DD-MM-YYYY + if(strlen(birthDate) >= 8 && strlen(birthDate) <= 10) { + + char *token = strtok(birthDate, "-"); + + character -> birthDate.day = atoi(token); + token = strtok(NULL, "-"); + character -> birthDate.month = atoi(token); + token = strtok(NULL, "-"); + character -> birthDate.year = atoi(token); + } +} + +void character_setAlternateNames(Character *character, char *alternateNames) { + + // Copy names to a temporary variable + char tempNames[MAX_ALTERNATE_NAME_SIZE * MAX_ALTERNATE_NAMES]; + strcpy(tempNames, alternateNames); + + // Separate names by comma + char *token = strtok(tempNames, ","); + int i = 0; + + // Copy names to the character + while (token != NULL && i < MAX_ALTERNATE_NAMES) { + + while (*token == ' ') token++; + + int len = strlen(token); + + while (len > 0 && token[len - 1] == ' ') { + + token[len - 1] = '\0'; + len--; + } + + strcpy(character -> alternateNames[i++], token); + token = strtok(NULL, ","); + } +} + +// Class +Character character_newBlank() { + + Character character; + + character.id = (char *) calloc(MAX_UUID_SIZE, sizeof(char)); + strcpy(character.id, ""); + + character.name = (char *) calloc(MAX_NAME_SIZE, sizeof(char)); + strcpy(character.name, ""); + + for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) { + + character.alternateNames[i] = (char *) calloc(MAX_ALTERNATE_NAME_SIZE, sizeof(char)); + strcpy(character.alternateNames[i], ""); + } + + character.house = (char *) calloc(MAX_HOUSE_SIZE, sizeof(char)); + strcpy(character.house, ""); + + character.ancestry = (char *) calloc(MAX_ANCESTRY_SIZE, sizeof(char)); + strcpy(character.ancestry, ""); + + character.species = (char *) calloc(MAX_SPECIES_SIZE, sizeof(char)); + strcpy(character.species, ""); + + character.patronus = (char *) calloc(MAX_PATRONUS_SIZE, sizeof(char)); + strcpy(character.patronus, ""); + + character.hogwartsStaff = false; + character.hogwartsStudent = false; + + character.actorName = (char *) calloc(MAX_ACTOR_NAME_SIZE, sizeof(char)); + strcpy(character.actorName, ""); + + character.alive = false; + + character.birthDate.day = -1; + character.birthDate.month = -1; + character.birthDate.year = -1; + + character.yearOfBirth = -1; + + character.eyeColour = (char *) calloc(MAX_EYE_COLOUR_SIZE, sizeof(char)); + strcpy(character.eyeColour, ""); + + character.gender = (char *) calloc(MAX_GENDER_SIZE, sizeof(char)); + strcpy(character.gender, ""); + + character.hairColour = (char *) calloc(MAX_HAIR_COLOUR_SIZE, sizeof(char)); + strcpy(character.hairColour, ""); + + character.wizard = false; + + return character; +} + +Character character_new(char *id, char *name, char *house, char *ancestry, char *species, char *patronus, bool hogwartsStaff, bool hogwartsStudent, char *actorName, bool alive, Date birthDate, int yearOfBirth, char *eyeColour, char *gender, char *hairColour, bool wizard) { + + Character character; + + character.id = (char *) calloc(MAX_UUID_SIZE, sizeof(char)); + strcpy(character.id, id); + + character.name = (char *) calloc(MAX_NAME_SIZE, sizeof(char)); + strcpy(character.name, name); + + for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) { + + character.alternateNames[i] = (char *) calloc(MAX_ALTERNATE_NAME_SIZE, sizeof(char)); + strcpy(character.alternateNames[i], ""); + } + + character.house = (char *) calloc(MAX_HOUSE_SIZE, sizeof(char)); + strcpy(character.house, house); + + character.ancestry = (char *) calloc(MAX_ANCESTRY_SIZE, sizeof(char)); + strcpy(character.ancestry, ancestry); + + character.species = (char *) calloc(MAX_SPECIES_SIZE, sizeof(char)); + strcpy(character.species, species); + + character.patronus = (char *) calloc(MAX_PATRONUS_SIZE, sizeof(char)); + strcpy(character.patronus, patronus); + + character.hogwartsStaff = hogwartsStaff; + character.hogwartsStudent = hogwartsStudent; + + character.actorName = (char *) calloc(MAX_ACTOR_NAME_SIZE, sizeof(char)); + strcpy(character.actorName, actorName); + + character.alive = alive; + character.birthDate = birthDate; + character.yearOfBirth = yearOfBirth; + + character.eyeColour = (char *) calloc(MAX_EYE_COLOUR_SIZE, sizeof(char)); + strcpy(character.eyeColour, eyeColour); + + character.gender = (char *) calloc(MAX_GENDER_SIZE, sizeof(char)); + strcpy(character.gender, gender); + + character.hairColour = (char *) calloc(MAX_HAIR_COLOUR_SIZE, sizeof(char)); + strcpy(character.hairColour, hairColour); + + character.wizard = wizard; + + return character; +} + +Character *character_clone(Character *character) { + + Character *clone = (Character *) malloc(sizeof(Character)); + + clone -> id = (char *) calloc(MAX_UUID_SIZE, sizeof(char)); + strcpy(clone -> id, character -> id); + + clone -> name = (char *) calloc(MAX_NAME_SIZE, sizeof(char)); + strcpy(clone -> name, character -> name); + + for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) { + + clone -> alternateNames[i] = (char *) calloc(MAX_ALTERNATE_NAME_SIZE, sizeof(char)); + strcpy(clone -> alternateNames[i], character -> alternateNames[i]); + } + + clone -> house = (char *) calloc(MAX_HOUSE_SIZE, sizeof(char)); + strcpy(clone -> house, character -> house); + + clone -> ancestry = (char *) calloc(MAX_ANCESTRY_SIZE, sizeof(char)); + strcpy(clone -> ancestry, character -> ancestry); + + clone -> species = (char *) calloc(MAX_SPECIES_SIZE, sizeof(char)); + strcpy(clone -> species, character -> species); + + clone -> patronus = (char *) calloc(MAX_PATRONUS_SIZE, sizeof(char)); + strcpy(clone -> patronus, character -> patronus); + + clone -> hogwartsStaff = character -> hogwartsStaff; + clone -> hogwartsStudent = character -> hogwartsStudent; + + clone -> actorName = (char *) calloc(MAX_ACTOR_NAME_SIZE, sizeof(char)); + strcpy(clone -> actorName, character -> actorName); + + clone -> alive = character -> alive; + clone -> birthDate = character -> birthDate; + clone -> yearOfBirth = character -> yearOfBirth; + + clone -> eyeColour = (char *) calloc(MAX_EYE_COLOUR_SIZE, sizeof(char)); + strcpy(clone -> eyeColour, character -> eyeColour); + + clone -> gender = (char *) calloc(MAX_GENDER_SIZE, sizeof(char)); + strcpy(clone -> gender, character -> gender); + + clone -> hairColour = (char *) calloc(MAX_HAIR_COLOUR_SIZE, sizeof(char)); + strcpy(clone -> hairColour, character -> hairColour); + + clone -> wizard = character -> wizard; + + return clone; +} + +void character_print(Character *character) { + + printf("[%s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s]\n", + + character_getId(character), + character_getName(character), + character_getAlternateNames(character), + character_getHouse(character), + character_getAncestry(character), + character_getSpecies(character), + character_getPatronus(character), + character_getHogwartsStaff(character) ? "true" : "false", + character_getHogwartsStudent(character) ? "true" : "false", + character_getActorName(character), + character_getAlive(character) ? "true" : "false", + character_getBirthDate(character), + character_getYearOfBirth(character), + character_getEyeColour(character), + character_getGender(character), + character_getHairColour(character), + character_getWizard(character) ? "true" : "false" + ); +} + +Character character_read(char *line) { + + Character character = character_newBlank(); + + char *substringStart = line; + char *substringEnd = NULL; + char attribute[MAX_LINE_SIZE]; + + // Get id + proccess_attribute(attribute, &substringStart, &substringEnd, true, false); + character_setId(&character, attribute); + + // Get name + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setName(&character, attribute); + + // Get alternate names + proccess_attribute(attribute, &substringStart, &substringEnd, false, true); + character_setAlternateNames(&character, attribute); + + // Get house + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setHouse(&character, attribute); + + // Get ancestry + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setAncestry(&character, attribute); + + // Get species + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setSpecies(&character, attribute); + + // Get patronus + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setPatronus(&character, attribute); + + // Get hogwarts staff + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setHogwartsStaff(&character, strcmp(attribute, "VERDADEIRO") == 0); + + // Get hogwarts student + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setHogwartsStudent(&character, strcmp(attribute, "VERDADEIRO") == 0); + + // Get actor name + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setActorName(&character, attribute); + + // Get alive + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setAlive(&character, strcmp(attribute, "VERDADEIRO") == 0); + + // Get birth date + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setBirthDate(&character, attribute); + + // Get year of birth + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setYearOfBirth(&character, atoi(attribute) == 0 ? -1 : atoi(attribute)); + + // Get eye colour + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setEyeColour(&character, attribute); + + // Get gender + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setGender(&character, attribute); + + // Get hair colour + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setHairColour(&character, attribute); + + // Get wizard + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + + // Clean \n from the end of the string + if(attribute[strlen(attribute) - 1] == '\n' || attribute[strlen(attribute) - 1] == '\r') attribute[strlen(attribute) - 1] = '\0'; + + character_setWizard(&character, strcmp(attribute, "VERDADEIRO") == 0); + return character; +} + +Character *character_searchById(char *id) { + + for(int i = 0; i < charactersLength; i++) { + + if(!strcmp(characters[i].id, id)) return &characters[i]; + } + return NULL; +} + +// General +void startCharacters() { + + // Open file + FILE *fp; + char *line = NULL; + size_t len = 0; + ssize_t read; + + fp = fopen(FILE_PATH, "r"); + + if(fp == NULL) { + + perror("x Error opening file"); + exit(EXIT_FAILURE); + } + + // Skip first line + getline(&line, &len, fp); + + // Read all lines + while((read = getline(&line, &len, fp)) != -1) { + + // Read character from line + Character character = character_read(line); + + characters[charactersLength++] = character; + + if(charactersLength >= MAX_CHARACTERS) { + + perror("x Max characters reached"); + exit(EXIT_FAILURE); + } + } + + // Close file and free memory + fclose(fp); + + if(line) free(line); +} + +// ---------------------------------------------------------------------------------------------------- // + +// Main +int main() { + + // ----------------------------------------------------------------- // + + // #1 - Start - Read all characters in CSV file + startCharacters(); + + // ----------------------------------------------------------------- // + + // #2 - Read input and print characters from pub.in id entries and add to mainCharacters array + + // Initialize mainCharacters array + Character mainCharacters[MAX_CHARACTERS]; + int m = 0; + + char id[MAX_UUID_SIZE]; + scanf(" %[^\n]s", id); + + while(true) { + + // Clean \n from the end of the string + if(id[strlen(id) - 1] == '\n' || id[strlen(id) - 1] == '\r') id[strlen(id) - 1] = '\0'; + + if(isEnd(id)) break; + else { + + Character *character = character_searchById(id); + + if(character) mainCharacters[m++] = *character; + + // ------------------------- // + + scanf(" %[^\n]s", id); + } + } + + // ----------------------------------------------------------------- // + + // #3 - Order mainCharacters array by key "hairColour", in draw case, order by key "name" + + // Start benchmark + clock_t startTime = clock(); + int comparisons = 0; + + // ----------------- // + + // Bubble sort + for(int i = 0; i < m - 1; i++) { + + for(int j = 0; j < m - i - 1; j++) { + + comparisons++; + + if(strcmp(character_getHairColour(&mainCharacters[j]), character_getHairColour(&mainCharacters[j + 1])) > 0) { + + Character aux = mainCharacters[j]; + mainCharacters[j] = mainCharacters[j + 1]; + mainCharacters[j + 1] = aux; + } + else if(strcmp(character_getHairColour(&mainCharacters[j]), character_getHairColour(&mainCharacters[j + 1])) == 0) { + + comparisons++; + + if(strcmp(character_getName(&mainCharacters[j]), character_getName(&mainCharacters[j + 1])) > 0) { + + Character aux = mainCharacters[j]; + mainCharacters[j] = mainCharacters[j + 1]; + mainCharacters[j + 1] = aux; + } + } + } + } + + // ----------------- // + + // Save benchmark in file + FILE *fp = fopen("753045_bolha.txt", "w"); + fprintf(fp, "753045\t%.0fms\t%d", (double)(clock() - startTime) / CLOCKS_PER_SEC * 1000.0, comparisons); + fclose(fp); + + // ----------------- // + + // Print mainCharacters array + for(int i = 0; i < m; i++) character_print(&mainCharacters[i]); + + // ----------------- // + + return 0; +} \ No newline at end of file diff --git a/tps/fonte/tp02/TP02Q12 - Bolha em C/pedro.out b/tps/fonte/tp02/TP02Q12 - Bolha em C/pedro.out new file mode 100644 index 0000000..8110a9e --- /dev/null +++ b/tps/fonte/tp02/TP02Q12 - Bolha em C/pedro.out @@ -0,0 +1,27 @@ +[efa802c8-ae18-4ae1-a524-49df21d05939 ## Lord Voldemort ## {Tom Riddle, Tom Marvolo Riddle, You-Know-Who, He Who Must Not Be Named, the Dark Lord, the Heir of Slytherin} ## Slytherin ## half-blood ## human ## N/A ## false ## false ## Ralph Fiennes ## false ## 31-12-1926 ## 1926 ## Scarlet ## male ## bald ## true] +[6afb1960-febd-418d-b604-e50c1b59459b ## Bellatrix Lestrange ## {Bella} ## Slytherin ## pure-blood ## human ## N/A ## false ## false ## Helena Bonham Carter ## false ## 06-02-1950 ## 1951 ## brown ## female ## black ## true] +[9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8 ## Harry Potter ## {The Boy Who Lived, The Chosen One, Undesirable No. 1, Potty} ## Gryffindor ## half-blood ## human ## stag ## false ## true ## Daniel Radcliffe ## true ## 31-07-1980 ## 1980 ## green ## male ## black ## true] +[5a4c95db-947d-4914-a631-41e8d466328e ## Kendra Dumbledore ## {} ## Slytherin ## muggleborn ## human ## N/A ## false ## false ## N/A ## false ## 31-12-1926 ## 1925 ## dark ## female ## black ## true] +[ca3827f0-375a-4891-aaa5-f5e8a5bad225 ## Minerva McGonagall ## {} ## Gryffindor ## half-blood ## human ## tabby cat ## true ## false ## Dame Maggie Smith ## true ## 04-10-1925 ## 1925 ## grey ## female ## black ## true] +[36bfefd0-e0bb-4d11-be98-d1ef6117a77a ## Rubeus Hagrid ## {Professor Hagrid, Hagger} ## Gryffindor ## half-blood ## half-giant ## N/A ## true ## false ## Robbie Coltrane ## true ## 06-12-1928 ## 1928 ## black ## male ## black ## true] +[2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f ## Sirius Black ## {Padfoot, Snuffles} ## Gryffindor ## pure-blood ## human ## hare ## false ## false ## Gary Oldman ## false ## 03-11-1959 ## 1959 ## grey ## male ## black ## true] +[0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3 ## Dobby ## {} ## Slytherin ## pure-blood ## house-elf ## N/A ## false ## false ## Toby Jones ## false ## 06-02-1950 ## 1950 ## green ## male ## blond ## false] +[7614cf6e-689e-47ac-a976-b1e9997637e9 ## Dudley Dursley ## {} ## Slytherin ## muggle ## human ## N/A ## false ## false ## Harry Melling ## true ## 23-06-1980 ## 1980 ## blue ## male ## blond ## false] +[41cd0bbe-a943-431b-9bde-bb2cad3491a1 ## Lily Potter ## {} ## Gryffindor ## muggleborn ## human ## N/A ## false ## false ## Geraldine Somerville ## false ## 30-01-1960 ## 1960 ## green ## female ## blond ## true] +[861c4cde-2f0f-4796-8d8f-9492e74b2573 ## Luna Lovegood ## {Loony Lovegood} ## Ravenclaw ## pure-blood ## human ## hare ## false ## true ## Evanna Lynch ## true ## 13-02-1981 ## 1981 ## silver ## female ## blonde ## true] +[fed624df-56d9-495e-9ad4-ea77000957e8 ## Petunia Dursley ## {} ## Slytherin ## muggle ## human ## N/A ## false ## false ## Fiona Shaw ## true ## 31-12-1926 ## 1954 ## yellow ## female ## blonde ## false] +[57fe29d4-312a-4711-bd9a-c320253d9176 ## Victoire Weasley ## {} ## Slytherin ## pure-blood ## human ## N/A ## false ## true ## N/A ## true ## 31-12-1926 ## 1926 ## grey ## female ## blonde ## true] +[d59691a4-f830-4eb0-a819-a0fb00b7e80f ## Garrick Ollivander ## {} ## Hufflepuff ## muggle ## human ## N/A ## false ## false ## John Hurt ## true ## 04-10-1925 ## 1954 ## pale, silvery ## male ## brown ## true] +[6b59be3f-e527-422d-959d-79fcdb3b24eb ## Hedwig ## {} ## Hufflepuff ## muggle ## owl ## N/A ## false ## false ## Gizmo ## false ## 07-04-1979 ## 1954 ## amber ## female ## brown ## false] +[b0620914-858d-46fc-8e6d-033c565e138b ## Mrs Norris ## {} ## Slytherin ## pure-blood ## cat ## N/A ## true ## false ## Maxime, Alanis and Tommy the cats ## true ## 31-12-1926 ## 1954 ## yellow ## female ## brown ## false] +[3a0fe4df-2e40-4541-8d7f-13586f0b9294 ## Tom Riddle ## {Tom Riddle Senior} ## Ravenclaw ## muggle ## human ## N/A ## false ## false ## N/A ## false ## 27-03-1960 ## 1980 ## brown ## male ## dark ## false] +[94c993f6-a443-4408-b908-6e78e223e8ac ## Aberforth Dumbledore ## {} ## Gryffindor ## half-blood ## human ## goat ## false ## false ## Jim McManus ## true ## 10-03-1960 ## 1960 ## blue ## male ## grey ## true] +[d58e7249-19d1-40bd-a43f-1da0497fe8aa ## Dolores Umbridge ## {} ## Slytherin ## half-blood ## human ## persian cat ## true ## false ## Imelda Staunton ## true ## 31-12-1926 ## 1926 ## brown ## female ## grey ## true] +[2a0615de-8aa4-41e7-9504-dd875f5f3f01 ## George Weasley ## {} ## Gryffindor ## pure-blood ## human ## N/A ## false ## true ## Oliver Phelps ## true ## 13-02-1981 ## 1954 ## amber ## male ## red ## true] +[eaea5eb3-48a3-41c6-9ea5-c695299bab16 ## Lisa Turpin ## {} ## Ravenclaw ## pure-blood ## human ## N/A ## false ## true ## N/A ## true ## 27-03-1960 ## 1951 ## amber ## female ## sandy ## true] +[11b5ca88-64ad-41a4-9f36-317b66c290af ## Nearly Headless Nick ## {Sir Nicholas de Mimsy-Porpington, Sir Nicholas, Nick} ## Gryffindor ## pure-blood ## ghost ## N/A ## false ## false ## John Cleese ## false ## 27-03-1960 ## 1926 ## amber ## male ## sandy ## true] +[1413e1b3-2903-4a47-a2d5-e8abc5ce8014 ## Seamus Finnigan ## {O Flaherty, Seamus Finnegan} ## Gryffindor ## half-blood ## human ## N/A ## false ## true ## Devon Murray ## true ## 31-12-1926 ## 1960 ## amber ## male ## sandy ## true] +[b415c867-1cff-455e-b194-748662ac2cca ## Albus Dumbledore ## {Professor Dumbledore} ## Gryffindor ## half-blood ## human ## Phoenix ## true ## false ## Richard Harris ## false ## 27-03-1960 ## 1881 ## blue ## male ## silver ## true] +[933787c2-51e3-4eac-8a85-ab332cac0456 ## Miranda Gaushawk ## {} ## Gryffindor ## muggle ## human ## N/A ## false ## false ## N/A ## false ## 27-03-1960 ## 1960 ## blue ## female ## silver ## true] +[00434cd3-fcc7-44c7-8f98-7368415b4206 ## Miriam Strout ## {} ## Gryffindor ## half-blood ## human ## N/A ## false ## false ## N/A ## true ## 27-03-1960 ## 1954 ## blue ## female ## silver ## true] +[20354d7a-e4fe-47af-8ff6-187bca92f3f9 ## Pandora Lovegood ## {} ## Ravenclaw ## quarter-veela ## human ## N/A ## false ## false ## N/A ## false ## 31-12-1926 ## 1959 ## dark ## female ## white ## true] diff --git a/tps/fonte/tp02/TP02Q12 - Bolha em C/pub.in b/tps/fonte/tp02/TP02Q12 - Bolha em C/pub.in new file mode 100644 index 0000000..a782d8c --- /dev/null +++ b/tps/fonte/tp02/TP02Q12 - Bolha em C/pub.in @@ -0,0 +1,28 @@ +9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8 +1413e1b3-2903-4a47-a2d5-e8abc5ce8014 +ca3827f0-375a-4891-aaa5-f5e8a5bad225 +36bfefd0-e0bb-4d11-be98-d1ef6117a77a +20354d7a-e4fe-47af-8ff6-187bca92f3f9 +57fe29d4-312a-4711-bd9a-c320253d9176 +b415c867-1cff-455e-b194-748662ac2cca +5a4c95db-947d-4914-a631-41e8d466328e +861c4cde-2f0f-4796-8d8f-9492e74b2573 +2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f +41cd0bbe-a943-431b-9bde-bb2cad3491a1 +2a0615de-8aa4-41e7-9504-dd875f5f3f01 +11b5ca88-64ad-41a4-9f36-317b66c290af +eaea5eb3-48a3-41c6-9ea5-c695299bab16 +0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3 +b0620914-858d-46fc-8e6d-033c565e138b +6b59be3f-e527-422d-959d-79fcdb3b24eb +fed624df-56d9-495e-9ad4-ea77000957e8 +d58e7249-19d1-40bd-a43f-1da0497fe8aa +3a0fe4df-2e40-4541-8d7f-13586f0b9294 +6afb1960-febd-418d-b604-e50c1b59459b +efa802c8-ae18-4ae1-a524-49df21d05939 +933787c2-51e3-4eac-8a85-ab332cac0456 +94c993f6-a443-4408-b908-6e78e223e8ac +00434cd3-fcc7-44c7-8f98-7368415b4206 +7614cf6e-689e-47ac-a976-b1e9997637e9 +d59691a4-f830-4eb0-a819-a0fb00b7e80f +FIM \ No newline at end of file diff --git a/tps/fonte/tp02/TP02Q13 - Mergesort em Java/753045_mergesort.txt b/tps/fonte/tp02/TP02Q13 - Mergesort em Java/753045_mergesort.txt new file mode 100644 index 0000000..5c091b0 --- /dev/null +++ b/tps/fonte/tp02/TP02Q13 - Mergesort em Java/753045_mergesort.txt @@ -0,0 +1 @@ +753045 24ms 98 \ No newline at end of file diff --git a/tps/fonte/tp02/TP02Q13 - Mergesort em Java/Arq.java b/tps/fonte/tp02/TP02Q13 - Mergesort em Java/Arq.java new file mode 100644 index 0000000..9a632d1 --- /dev/null +++ b/tps/fonte/tp02/TP02Q13 - Mergesort em Java/Arq.java @@ -0,0 +1,206 @@ +import java.io.*; +import java.util.Formatter; +import java.util.Scanner; +import java.io.File; +import java.nio.charset.*; + +public class Arq +{ + private static String nomeArquivo = ""; + private static String charsetArquivo = "ISO-8859-1"; + private static boolean write = false, read = false; + private static Formatter saida = null; + private static Scanner entrada = null; + + public static boolean openWrite(String nomeArq, String charset) { + boolean resp = false; + close(); + try{ + saida = new Formatter(nomeArq, charset); + nomeArquivo = nomeArq; + resp = write = true; + } catch (Exception e) {} + return resp; + } + + public static boolean openWrite(String nomeArq) { + return openWrite(nomeArq, charsetArquivo); + } + + public static boolean openWriteClose(String nomeArq, String charset, String conteudo) { + boolean resp = openWrite(nomeArq, charset); + if(resp == true){ + println(conteudo); + close(); + } + return resp; + } + + public static boolean openWriteClose(String nomeArq, String conteudo) { + return openWriteClose(nomeArq, charsetArquivo, conteudo); + } + + public static boolean openRead(String nomeArq) { + return openRead(nomeArq, charsetArquivo); + } + + public static boolean openRead(String nomeArq, String charset) { + boolean resp = false; + close(); + try{ + entrada = new Scanner(new File(nomeArq), charset); + nomeArquivo = nomeArq; + resp = read = true; + } catch (Exception e) {} + return resp; + } + + public static String openReadClose(String nomeArq){ + openRead(nomeArq); + String resp = readAll(); + close(); + return resp; + } + + public static void close() { + if(write == true){ + saida.close(); + } + if(read == true){ + entrada.close(); + } + write = read = false; + nomeArquivo = ""; + charsetArquivo = "ISO-8859-1"; + } + + public static long length(){ + long resp = -1; + if(read != write){ + File file = new File(nomeArquivo); + resp = file.length(); + } + return resp; + } + + public static void print(int x){ + if(write == true){ + saida.format( "%d", x); + } + } + + public static void print(double x){ + if(write == true){ + saida.format( "%f", x); + } + } + + public static void print(String x){ + if(write == true){ + saida.format( "%s", x); + } + } + + public static void print(boolean x){ + if(write == true){ + saida.format( "%s", ((x) ? "true" : "false")); + } + } + + public static void print(char x){ + if(write == true){ + saida.format( "%c", x); + } + } + + public static void println(int x){ + if(write == true){ + saida.format( "%d\n", x); + } + } + + public static void println(double x){ + if(write == true){ + saida.format( "%f\n", x); + } + } + + public static void println(String x){ + if(write == true){ + saida.format( "%s\n", x); + } + } + + public static void println(boolean x){ + if(write == true){ + saida.format( "%s\n", ((x) ? "true" : "false")); + } + } + + public static void println(char x){ + if(write == true){ + saida.format( "%c\n", x); + } + } + + public static int readInt(){ + int resp = -1; + try{ + resp = entrada.nextInt(); + } catch (Exception e) {} + return resp; + } + + public static char readChar(){ + char resp = ' '; + try{ + resp = (char)entrada.nextByte(); + } catch (Exception e) {} + return resp; + } + + public static double readDouble(){ + double resp = -1; + try{ + resp = Double.parseDouble(readString().replace(",",".")); + } catch (Exception e) {} + return resp; + } + + public static String readString(){ + String resp = ""; + try{ + resp = entrada.next(); + } catch (Exception e) { System.out.println(e.getMessage()); } + return resp; + } + + public static boolean readBoolean(){ + boolean resp = false; + try{ + resp = (entrada.next().equals("true")) ? true : false; + } catch (Exception e) {} + return resp; + } + + public static String readLine(){ + String resp = ""; + try{ + resp = entrada.nextLine(); + } catch (Exception e) { System.out.println(e.getMessage()); } + return resp; + } + + + public static boolean hasNext(){ + return entrada.hasNext(); + } + + public static String readAll(){ + String resp = ""; + while(hasNext()){ + resp += (readLine() + "\n"); + } + return resp; + } +} diff --git a/tps/fonte/tp02/TP02Q13 - Mergesort em Java/Character.java b/tps/fonte/tp02/TP02Q13 - Mergesort em Java/Character.java new file mode 100644 index 0000000..237ec90 --- /dev/null +++ b/tps/fonte/tp02/TP02Q13 - Mergesort em Java/Character.java @@ -0,0 +1,575 @@ +/** + * @path TP02Q01 - Classe em Java/Characters.java + * @description Java class of all characters from Harry Potter's saga with merge sort algorithm + * @author Pedro Lopes - github.com/httpspedroh + * @version 1.0 + * @update 2024-04-09 + */ + +// ---------------------------------------------------------------------------------------------------- // + +// Imports +import java.util.Scanner; +import java.util.UUID; +import java.io.BufferedReader; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Date; +import java.text.SimpleDateFormat; + +// ---------------------------------------------------------------------------------------------------- // + +public class Character { + + static SimpleDateFormat ddf = new SimpleDateFormat("dd-MM-yyyy"); + + // Global variables + public static final String FILE_PATH = "/tmp/characters.csv"; + public static ArrayList allCharacters = new ArrayList(); + + // -------------------------- // + + // Attributes + private UUID id; + private String name; + private ArrayList alternateNames; + private String house; + private String ancestry; + private String species; + private String patronus; + private boolean hogwartsStaff; + private boolean hogwartsStudent; + private String actorName; + private boolean alive; + private Date birthDate; + private int yearOfBirth; + private String eyeColour; + private String gender; + private String hairColour; + private boolean wizard; + + // -------------------------- // + + // Empty constructor + public Character() { + + this.id = UUID.randomUUID(); + this.name = ""; + this.alternateNames = new ArrayList(); + this.house = ""; + this.ancestry = ""; + this.species = ""; + this.patronus = ""; + this.hogwartsStaff = false; + this.hogwartsStudent = false; + this.actorName = ""; + this.alive = false; + this.birthDate = null; + this.yearOfBirth = 0; + this.eyeColour = ""; + this.gender = ""; + this.hairColour = ""; + this.wizard = false; + } + + // Constructor + public Character(UUID id, String name, ArrayList alternateNames, String house, String ancestry, String species, String patronus, boolean hogwartsStaff, boolean hogwartsStudent, String actorName, boolean alive, Date birthDate, int yearOfBirth, String eyeColour, String gender, String hairColour, boolean wizard) { + + this.id = id; + this.name = name; + this.alternateNames = alternateNames; + this.house = house; + this.ancestry = ancestry; + this.species = species; + this.patronus = patronus; + this.hogwartsStaff = hogwartsStaff; + this.hogwartsStudent = hogwartsStudent; + this.actorName = actorName; + this.alive = alive; + this.birthDate = birthDate; + this.yearOfBirth = yearOfBirth; + this.eyeColour = eyeColour; + this.gender = gender; + this.hairColour = hairColour; + this.wizard = wizard; + } + + // -------------------------- // + + // Gets + public UUID getId() { return this.id; } + public String getName() { return this.name; } + public String getHouse() { return this.house; } + public String getAncestry() { return this.ancestry; } + public String getSpecies() { return this.species; } + public String getPatronus() { return this.patronus; } + public boolean getHogwartsStaff() { return this.hogwartsStaff; } + public boolean getHogwartsStudent() { return this.hogwartsStudent; } + public String getActorName() { return this.actorName; } + public boolean getAlive() { return this.alive; } + public Date getBirthDate() { return this.birthDate; } + public int getYearOfBirth() { return this.yearOfBirth; } + public String getEyeColour() { return this.eyeColour; } + public String getGender() { return this.gender; } + public String getHairColour() { return this.hairColour; } + public boolean getWizard() { return this.wizard; } + + public String getAlternateNames() { + + // Construct string e.g. {item1, item2, item3} + String alternateNames = "{"; + + for(int i = 0; i < this.alternateNames.size(); i++) { + + alternateNames += this.alternateNames.get(i); + + if(i < this.alternateNames.size() - 1) alternateNames += ", "; + } + + alternateNames += "}"; + + return alternateNames; + } + + // Sets + public void setId(UUID id) { this.id = id; } + public void setName(String name) { this.name = name; } + public void setAlternateNames(ArrayList alternateNames) { this.alternateNames = alternateNames; } + public void setHouse(String house) { this.house = house; } + public void setAncestry(String ancestry) { this.ancestry = ancestry; } + public void setSpecies(String species) { this.species = species; } + public void setPatronus(String patronus) { this.patronus = patronus; } + public void setHogwartsStaff(boolean hogwartsStaff) { this.hogwartsStaff = hogwartsStaff; } + public void setHogwartsStudent(boolean hogwartsStudent) { this.hogwartsStudent = hogwartsStudent; } + public void setActorNane(String actorName) { this.actorName = actorName; } + public void setAlive(boolean alive) { this.alive = alive; } + public void setBirthDate(Date birthDate) { this.birthDate = birthDate; } + public void setYearOfBirth(int yearOfBirth) { this.yearOfBirth = yearOfBirth; } + public void setEyeColour(String eyeColour) { this.eyeColour = eyeColour; } + public void setGender(String gender) { this.gender = gender; } + public void setHairColour(String hairColour) { this.hairColour = hairColour; } + public void setWizard(boolean wizard) { this.wizard = wizard; } + + // -------------------------- // + + // Clone + public Character clone() { return new Character(this.id, this.name, this.alternateNames, this.house, this.ancestry, this.species, this.patronus, this.hogwartsStaff, this.hogwartsStudent, this.actorName, this.alive, this.birthDate, this.yearOfBirth, this.eyeColour, this.gender, this.hairColour, this.wizard); } + + // -------------------------- // + + // Print + public void print() { + + System.out.println("[" + + this.getId() + " ## " + + this.getName() + " ## " + + this.getAlternateNames() + " ## " + + (this.getHouse() == "" ? "N/A" : this.getHouse()) + " ## " + + (this.getAncestry() == "" ? "N/A" : this.getAncestry()) + " ## " + + (this.getSpecies() == "" ? "N/A" : this.getSpecies()) + " ## " + + (this.getPatronus() == "" ? "N/A" : this.getPatronus()) + " ## " + + (this.getHogwartsStaff() ? "true" : "false") + " ## " + + (this.getHogwartsStudent() ? "true" : "false") + " ## " + + (this.getActorName() == "" ? "N/A" : this.getActorName()) + " ## " + + (this.getAlive() ? "true" : "false") + " ## " + + (this.getBirthDate() == null ? "N/A" : ddf.format(this.getBirthDate())) + " ## " + + (this.getYearOfBirth() == 0 ? "N/A" : this.getYearOfBirth()) + " ## " + + (this.getEyeColour() == "" ? "N/A" : this.getEyeColour()) + " ## " + + (this.getGender() == "" ? "N/A" : this.getGender()) + " ## " + + (this.getHairColour() == "" ? "N/A" : this.getHairColour()) + " ## " + + (this.getWizard() ? "true" : "false") + "]"); + } + + // -------------------------- // + + public static ArrayList extractNames(String input) { + + ArrayList names = new ArrayList<>(); + + // Clean input + String cleanedInput = input.substring(1, input.length() - 1); + + // Check if input contains double quotes + if (cleanedInput.contains("\"\"")) { + + String[] parts = cleanedInput.split("\", "); + + for (String part : parts) names.add(part.replace("\"", "").replace("'", "")); + } + else { + + // Split input by comma and space + String[] parts = cleanedInput.split(", "); + + for (String part : parts) names.add(part.replace("'", "")); + } + return names; + } + + // -------------------------- // + + // Read + public void read(String line) { + + // ---------------------- // + + // Start position + int positionStart = 0; + int positionEnd = 0; + + // ---------------------- // + + // Set id + positionEnd = line.indexOf(";", positionStart); + this.setId(UUID.fromString(line.substring(positionStart, positionEnd))); + + // ---------------------- // + + // Set name + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setName(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set alternate names + positionStart = positionEnd + 1; + + if(line.charAt(positionStart) == '[') { // If first character is '[', has one or no names + + if(line.charAt(positionStart + 1) == ']') { + + positionEnd = positionStart + 2; + + this.setAlternateNames(new ArrayList()); + } + else { + + positionEnd = line.indexOf("];", positionStart); + + this.setAlternateNames(extractNames(line.substring(positionStart, positionEnd++))); + } + } + + // ---------------------- // + + // Set house + positionStart = positionEnd; + positionEnd = line.indexOf(";", ++positionStart); + + this.setHouse(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set ancestry + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setAncestry(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set species + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setSpecies(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set patronus + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setPatronus(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set hogwartsStaff + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setHogwartsStaff("VERDADEIRO".equals(line.substring(positionStart, positionEnd))); + + // ---------------------- // + + // Set hogwartsStudent + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setHogwartsStudent("VERDADEIRO".equals(line.substring(positionStart, positionEnd))); + + // ---------------------- // + + // Set actor name + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setActorNane(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set alive + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setAlive("VERDADEIRO".equals(line.substring(positionStart, positionEnd))); + + // ---------------------- // + + // Set date of birth + positionStart = positionEnd; + positionEnd = line.indexOf(";", ++positionStart); + + try { this.setBirthDate(ddf.parse(line.substring(positionStart, positionEnd))); } + catch(Exception e) { this.setBirthDate(null); } + + // ---------------------- // + + // Set year of birth + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + try { this.setYearOfBirth(Integer.parseInt(line.substring(positionStart, positionEnd))); } + catch(Exception e) { this.setYearOfBirth(0); } + + // ---------------------- // + + // Set eye colour + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setEyeColour(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set gender + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setGender(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set hair colour + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setHairColour(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set wizard + positionStart = positionEnd + 1; + + this.setWizard("VERDADEIRO".equals(line.substring(positionStart, line.length()))); + + // ---------------------- // + } + + // -------------------------- // + + // Compare method + public int compareTo(Character other) { + + // Compare by actorName + int actorNameComparison = this.actorName.compareTo(other.actorName); + + // If actorName is the same, compare by name + if(actorNameComparison != 0) return actorNameComparison; + else return this.name.compareTo(other.name); + } + + // ---------------------------------------------------------------------------------------------------- // + + // Read all characters function + public static void startCharacters() { + + // Initialize variables + try { + + FileInputStream fstream = new FileInputStream(FILE_PATH); + BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); + + // ---------------------- // + + // Explode CSV file + String line = br.readLine(); + + while((line = br.readLine()) != null) { + + // Initialize character + Character character = new Character(); + + // Read line + character.read(line); + + // Add character to array + allCharacters.add(character); + } + + // Close CSV file + fstream.close(); + } + catch(IOException e) { e.printStackTrace(); } + } + + // ---------------------------------------------------------------------------------------------------- // + + // Search by id function + public static Character searchById(UUID id, ArrayList characters) { + + // Search for character + for(int i = 0; i < characters.size(); i++) { + + if(characters.get(i).getId().equals(id)) return characters.get(i); + } + return null; + } + + // ---------------------------------------------------------------------------------------------------- // + + // Merge sort helper function + public static int mergeSort(ArrayList arr, int l, int r) { + + if(l < r) { + + int mid = (l + r) / 2; + + int leftComparisons = mergeSort(arr, l, mid); + int rightComparisons = mergeSort(arr, mid + 1, r); + int mergeComparisons = merge(arr, l, mid, r); + return leftComparisons + rightComparisons + mergeComparisons; + } + return 0; + } + + // Merge function + public static int merge(ArrayList arr, int l, int mid, int r) { + + int comparisons = 0; + int n1 = mid - l + 1; + int n2 = r - mid; + + ArrayList leftArr = new ArrayList(); + ArrayList rightArr = new ArrayList(); + + for(int i = 0; i < n1; i++) leftArr.add(arr.get(l + i)); + for(int i = 0; i < n2; i++) rightArr.add(arr.get(mid + 1 + i)); + + int i = 0, j = 0, k = l; + + while(i < n1 && j < n2) { + + // Compare characters and increment comparisons + comparisons++; + + if(leftArr.get(i).getActorName().compareTo(rightArr.get(j).getActorName()) < 0 || + (leftArr.get(i).getActorName().equals(rightArr.get(j).getActorName()) && + leftArr.get(i).getName().compareTo(rightArr.get(j).getName()) <= 0)) { + + arr.set(k, leftArr.get(i)); + i++; + } + else { + + arr.set(k, rightArr.get(j)); + j++; + } + + k++; + } + + while (i < n1) { + + arr.set(k, leftArr.get(i)); + i++; + k++; + } + + while (j < n2) { + + arr.set(k, rightArr.get(j)); + j++; + k++; + } + return comparisons; + } + + // ---------------------------------------------------------------------------------------------------- // + + public static void main(String[] args) { + + // ----------------------------------------------------------------- // + + // #1 - Start - Read all characters in CSV file + startCharacters(); + + // ----------------------------------------------------------------- // + + // #2 - Read input and print characters from pub.in id entries and add to mainCharacters array + + // Initialize mainCharacters array + ArrayList mainCharacters = new ArrayList(); + + // Initialize scanner + Scanner inScanner = new Scanner(System.in); + + // Initialize character + Character character = new Character(); + + // Read first line + String line = inScanner.nextLine(); + + // While line is not "FIM" + while(!line.equals("FIM")) { + + // Get id + UUID id = UUID.fromString(line); + + // Search for character + character = searchById(id, allCharacters); + + // Print character + if(character != null) mainCharacters.add(character); + + // Read line + line = inScanner.nextLine(); + } + + // ----------------------------------------------------------------- // + + // #3 - Order mainCharacters array by key "actorName" using counting sort, in draw case, order by key "name" + + // Start benchmark + long startTime = System.currentTimeMillis(); + int comparisons = 0; + + // ----------------- // + + // Merge sort + comparisons = mergeSort(mainCharacters, 0, mainCharacters.size() - 1); + + // ----------------- // + + // Save benchmark in file + Arq.openWrite("753045_mergesort.txt"); + Arq.print("753045\t" + (System.currentTimeMillis() - startTime) + "ms\t" + comparisons); + Arq.close(); + + // ----------------- // + + // Print mainCharacters array + for(int i = 0; i < mainCharacters.size(); i++) mainCharacters.get(i).print(); + + // ----------------------------------------------------------------- // + + // Close scanner + inScanner.close(); + } +} + +// ---------------------------------------------------------------------------------------------------- // \ No newline at end of file diff --git a/tps/fonte/tp02/TP02Q13 - Mergesort em Java/pedro.out b/tps/fonte/tp02/TP02Q13 - Mergesort em Java/pedro.out new file mode 100644 index 0000000..5cd070c --- /dev/null +++ b/tps/fonte/tp02/TP02Q13 - Mergesort em Java/pedro.out @@ -0,0 +1,27 @@ +[5a4c95db-947d-4914-a631-41e8d466328e ## Kendra Dumbledore ## {} ## Slytherin ## muggleborn ## human ## N/A ## false ## false ## N/A ## false ## 31-12-1926 ## 1925 ## dark ## female ## black ## true] +[eaea5eb3-48a3-41c6-9ea5-c695299bab16 ## Lisa Turpin ## {} ## Ravenclaw ## pure-blood ## human ## N/A ## false ## true ## N/A ## true ## 27-03-1960 ## 1951 ## amber ## female ## sandy ## true] +[933787c2-51e3-4eac-8a85-ab332cac0456 ## Miranda Gaushawk ## {} ## Gryffindor ## muggle ## human ## N/A ## false ## false ## N/A ## false ## 27-03-1960 ## 1960 ## blue ## female ## silver ## true] +[00434cd3-fcc7-44c7-8f98-7368415b4206 ## Miriam Strout ## {} ## Gryffindor ## half-blood ## human ## N/A ## false ## false ## N/A ## true ## 27-03-1960 ## 1954 ## blue ## female ## silver ## true] +[20354d7a-e4fe-47af-8ff6-187bca92f3f9 ## Pandora Lovegood ## {} ## Ravenclaw ## quarter-veela ## human ## N/A ## false ## false ## N/A ## false ## 31-12-1926 ## 1959 ## dark ## female ## white ## true] +[3a0fe4df-2e40-4541-8d7f-13586f0b9294 ## Tom Riddle ## {Tom Riddle Senior} ## Ravenclaw ## muggle ## human ## N/A ## false ## false ## N/A ## false ## 27-03-1960 ## 1980 ## brown ## male ## dark ## false] +[57fe29d4-312a-4711-bd9a-c320253d9176 ## Victoire Weasley ## {} ## Slytherin ## pure-blood ## human ## N/A ## false ## true ## N/A ## true ## 31-12-1926 ## 1926 ## grey ## female ## blonde ## true] +[ca3827f0-375a-4891-aaa5-f5e8a5bad225 ## Minerva McGonagall ## {} ## Gryffindor ## half-blood ## human ## tabby cat ## true ## false ## Dame Maggie Smith ## true ## 04-10-1925 ## 1925 ## grey ## female ## black ## true] +[9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8 ## Harry Potter ## {The Boy Who Lived, The Chosen One, Undesirable No. 1, Potty} ## Gryffindor ## half-blood ## human ## stag ## false ## true ## Daniel Radcliffe ## true ## 31-07-1980 ## 1980 ## green ## male ## black ## true] +[1413e1b3-2903-4a47-a2d5-e8abc5ce8014 ## Seamus Finnigan ## {O Flaherty, Seamus Finnegan} ## Gryffindor ## half-blood ## human ## N/A ## false ## true ## Devon Murray ## true ## 31-12-1926 ## 1960 ## amber ## male ## sandy ## true] +[861c4cde-2f0f-4796-8d8f-9492e74b2573 ## Luna Lovegood ## {Loony Lovegood} ## Ravenclaw ## pure-blood ## human ## hare ## false ## true ## Evanna Lynch ## true ## 13-02-1981 ## 1981 ## silver ## female ## blonde ## true] +[fed624df-56d9-495e-9ad4-ea77000957e8 ## Petunia Dursley ## {} ## Slytherin ## muggle ## human ## N/A ## false ## false ## Fiona Shaw ## true ## 31-12-1926 ## 1954 ## yellow ## female ## blonde ## false] +[2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f ## Sirius Black ## {Padfoot, Snuffles} ## Gryffindor ## pure-blood ## human ## hare ## false ## false ## Gary Oldman ## false ## 03-11-1959 ## 1959 ## grey ## male ## black ## true] +[41cd0bbe-a943-431b-9bde-bb2cad3491a1 ## Lily Potter ## {} ## Gryffindor ## muggleborn ## human ## N/A ## false ## false ## Geraldine Somerville ## false ## 30-01-1960 ## 1960 ## green ## female ## blond ## true] +[6b59be3f-e527-422d-959d-79fcdb3b24eb ## Hedwig ## {} ## Hufflepuff ## muggle ## owl ## N/A ## false ## false ## Gizmo ## false ## 07-04-1979 ## 1954 ## amber ## female ## brown ## false] +[7614cf6e-689e-47ac-a976-b1e9997637e9 ## Dudley Dursley ## {} ## Slytherin ## muggle ## human ## N/A ## false ## false ## Harry Melling ## true ## 23-06-1980 ## 1980 ## blue ## male ## blond ## false] +[6afb1960-febd-418d-b604-e50c1b59459b ## Bellatrix Lestrange ## {Bella} ## Slytherin ## pure-blood ## human ## N/A ## false ## false ## Helena Bonham Carter ## false ## 06-02-1950 ## 1951 ## brown ## female ## black ## true] +[d58e7249-19d1-40bd-a43f-1da0497fe8aa ## Dolores Umbridge ## {} ## Slytherin ## half-blood ## human ## persian cat ## true ## false ## Imelda Staunton ## true ## 31-12-1926 ## 1926 ## brown ## female ## grey ## true] +[94c993f6-a443-4408-b908-6e78e223e8ac ## Aberforth Dumbledore ## {} ## Gryffindor ## half-blood ## human ## goat ## false ## false ## Jim McManus ## true ## 10-03-1960 ## 1960 ## blue ## male ## grey ## true] +[11b5ca88-64ad-41a4-9f36-317b66c290af ## Nearly Headless Nick ## {Sir Nicholas de Mimsy-Porpington, Sir Nicholas, Nick} ## Gryffindor ## pure-blood ## ghost ## N/A ## false ## false ## John Cleese ## false ## 27-03-1960 ## 1926 ## amber ## male ## sandy ## true] +[d59691a4-f830-4eb0-a819-a0fb00b7e80f ## Garrick Ollivander ## {} ## Hufflepuff ## muggle ## human ## N/A ## false ## false ## John Hurt ## true ## 04-10-1925 ## 1954 ## pale, silvery ## male ## brown ## true] +[b0620914-858d-46fc-8e6d-033c565e138b ## Mrs Norris ## {} ## Slytherin ## pure-blood ## cat ## N/A ## true ## false ## Maxime, Alanis and Tommy the cats ## true ## 31-12-1926 ## 1954 ## yellow ## female ## brown ## false] +[2a0615de-8aa4-41e7-9504-dd875f5f3f01 ## George Weasley ## {} ## Gryffindor ## pure-blood ## human ## N/A ## false ## true ## Oliver Phelps ## true ## 13-02-1981 ## 1954 ## amber ## male ## red ## true] +[efa802c8-ae18-4ae1-a524-49df21d05939 ## Lord Voldemort ## {Tom Riddle, Tom Marvolo Riddle, You-Know-Who, He Who Must Not Be Named, the Dark Lord, the Heir of Slytherin} ## Slytherin ## half-blood ## human ## N/A ## false ## false ## Ralph Fiennes ## false ## 31-12-1926 ## 1926 ## Scarlet ## male ## bald ## true] +[b415c867-1cff-455e-b194-748662ac2cca ## Albus Dumbledore ## {Professor Dumbledore} ## Gryffindor ## half-blood ## human ## Phoenix ## true ## false ## Richard Harris ## false ## 27-03-1960 ## 1881 ## blue ## male ## silver ## true] +[36bfefd0-e0bb-4d11-be98-d1ef6117a77a ## Rubeus Hagrid ## {Professor Hagrid, Hagger} ## Gryffindor ## half-blood ## half-giant ## N/A ## true ## false ## Robbie Coltrane ## true ## 06-12-1928 ## 1928 ## black ## male ## black ## true] +[0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3 ## Dobby ## {} ## Slytherin ## pure-blood ## house-elf ## N/A ## false ## false ## Toby Jones ## false ## 06-02-1950 ## 1950 ## green ## male ## blond ## false] diff --git a/tps/fonte/tp02/TP02Q13 - Mergesort em Java/pub.in b/tps/fonte/tp02/TP02Q13 - Mergesort em Java/pub.in new file mode 100644 index 0000000..a782d8c --- /dev/null +++ b/tps/fonte/tp02/TP02Q13 - Mergesort em Java/pub.in @@ -0,0 +1,28 @@ +9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8 +1413e1b3-2903-4a47-a2d5-e8abc5ce8014 +ca3827f0-375a-4891-aaa5-f5e8a5bad225 +36bfefd0-e0bb-4d11-be98-d1ef6117a77a +20354d7a-e4fe-47af-8ff6-187bca92f3f9 +57fe29d4-312a-4711-bd9a-c320253d9176 +b415c867-1cff-455e-b194-748662ac2cca +5a4c95db-947d-4914-a631-41e8d466328e +861c4cde-2f0f-4796-8d8f-9492e74b2573 +2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f +41cd0bbe-a943-431b-9bde-bb2cad3491a1 +2a0615de-8aa4-41e7-9504-dd875f5f3f01 +11b5ca88-64ad-41a4-9f36-317b66c290af +eaea5eb3-48a3-41c6-9ea5-c695299bab16 +0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3 +b0620914-858d-46fc-8e6d-033c565e138b +6b59be3f-e527-422d-959d-79fcdb3b24eb +fed624df-56d9-495e-9ad4-ea77000957e8 +d58e7249-19d1-40bd-a43f-1da0497fe8aa +3a0fe4df-2e40-4541-8d7f-13586f0b9294 +6afb1960-febd-418d-b604-e50c1b59459b +efa802c8-ae18-4ae1-a524-49df21d05939 +933787c2-51e3-4eac-8a85-ab332cac0456 +94c993f6-a443-4408-b908-6e78e223e8ac +00434cd3-fcc7-44c7-8f98-7368415b4206 +7614cf6e-689e-47ac-a976-b1e9997637e9 +d59691a4-f830-4eb0-a819-a0fb00b7e80f +FIM \ No newline at end of file diff --git a/tps/fonte/tp02/TP02Q14 - Radixsort em C/753045_radixsort.txt b/tps/fonte/tp02/TP02Q14 - Radixsort em C/753045_radixsort.txt new file mode 100644 index 0000000..62b9093 --- /dev/null +++ b/tps/fonte/tp02/TP02Q14 - Radixsort em C/753045_radixsort.txt @@ -0,0 +1 @@ +753045 0ms 9522 \ No newline at end of file diff --git a/tps/fonte/tp02/TP02Q14 - Radixsort em C/Character b/tps/fonte/tp02/TP02Q14 - Radixsort em C/Character new file mode 100644 index 0000000000000000000000000000000000000000..cd302285371cf2c77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$x~S6|P$N{`s5)vO zupT{7#&OaTv61X*viA7QZkI+%0bp|*I?K(KTtV()qz0O!04|rqJsAA1s=h1K=&WF1 z_LK3U=dPNR2cWwkbSa#7?t+pTQ;iJi_Ksk7X%7F4t_pLF+Km5Li*V zNKYDcSo^RiuqVqzwZR#>AV*BOPLFqGC6AzYK}jcg5Y-6jW@kgFi_ljZ(4+KWD

!)HBDDlz=`IhshqE+ zg-#=fP;GTNvagSjXB3X+s{%x_D8P!LmPWh(p^KJD^hT#%EA0K$-3n04B|A6y_Gh!= z5+t!CvhDT0tEOrhX&c~mz;(?A=d{=RvzmHc+R1$c4KGIGQ|#I6^6PzEP4kr^mf3vi z?+1z%Hz9%DBfI>1zgN>9+?U)$1x3i`q_?U%3@`Awg}T?e%`NroBp0=dbPc z^KVu53zeSUx7IXYIVO90MM$-O02q}^RTsUa%1cr8TA{$9?QcTjwD&4IOIpF zaEkpd^@6IYF0U?t<~4mh#a{2LYZ}ro^2&kEU;F#J6np($Pt$Q)QPI@+`A221+fNiJ zMMaBJo?n++X^DSEf>gA<-p|*R_7$C1YW^=l=(N}S{x-E6R(eUz|5d=WS57fRm);N7 z{~w}Mea&n725da|)Ass1!ayl`OZGR(C3$WC4g^$0ZLjyK_5bIoPd&eW3~i^|u@knW zqV4r}iQbz5;>wrAzH56;KY+w(FM>*SnWR06)kNEA`Vj<9d%X|XqwIA#Q|&eXITGYc zm%lG4C4I`iL+$rTR0yw5DEcKR literal 0 HcmV?d00001 diff --git a/tps/fonte/tp02/TP02Q14 - Radixsort em C/Character.c b/tps/fonte/tp02/TP02Q14 - Radixsort em C/Character.c new file mode 100644 index 0000000..72e6a4a --- /dev/null +++ b/tps/fonte/tp02/TP02Q14 - Radixsort em C/Character.c @@ -0,0 +1,765 @@ +/** + * @path TP02Q02 - Classe em C/Character.c + * @description C file that implements the Character class with radix sort algorithm + * @author Pedro Lopes - github.com/httpspedroh + * @version 1.0 + * @update 2024-04-09 + */ + +// ---------------------------------------------------------------------------------------------------- // + +// Includes +#include +#include +#include +#include +#include + +// ---------------------------------------------------------------------------------------------------- // + +// Constants +#define MAX_CHARACTERS 405 +#define FILE_PATH "/tmp/characters.csv" + +#define MAX_UUID_SIZE 37 +#define MAX_NAME_SIZE 30 +#define MAX_ALTERNATE_NAMES 10 +#define MAX_ALTERNATE_NAME_SIZE 35 +#define MAX_HOUSE_SIZE 15 +#define MAX_ANCESTRY_SIZE 15 +#define MAX_SPECIES_SIZE 20 +#define MAX_PATRONUS_SIZE 25 +#define MAX_ACTOR_NAME_SIZE 35 +#define MAX_EYE_COLOUR_SIZE 10 +#define MAX_GENDER_SIZE 10 +#define MAX_HAIR_COLOUR_SIZE 10 + +#define MAX_LINE_SIZE 300 + +// ---------------------------------------------------------------------------------------------------- // + +// Structs +typedef struct Date { + + int day; + int month; + int year; +} Date; + +typedef struct Character { + + char *id; + char *name; + char *alternateNames[MAX_ALTERNATE_NAMES]; + char *house; + char *ancestry; + char *species; + char *patronus; + bool hogwartsStaff; + bool hogwartsStudent; + char *actorName; + bool alive; + Date birthDate; + int yearOfBirth; + char *eyeColour; + char *gender; + char *hairColour; + bool wizard; +} Character; + +// ---------------------------------------------------------------------------------------------------- // + +// Global variables +Character characters[MAX_CHARACTERS]; +int charactersLength = 0; + +// ---------------------------------------------------------------------------------------------------- // + +// Functions +bool isEnd(char* line) { return line[0] == 'F' && line[1] == 'I' && line[2] == 'M'; } + +void substring(char *string, char *stringStart, int length) { + + strncpy(string, stringStart, length); + string[length] = '\0'; +} + +void proccess_attribute(char *attribute, char **substringStart, char **substringEnd, bool isFirstAttribute, bool isStringArray) { + + // Skip first comma + if(!isFirstAttribute) { + + if(*substringEnd != NULL) *substringStart = *substringEnd + 1; + else *substringStart = *substringEnd; + } + + if(!isStringArray) { + + if((*substringStart)[0] == '"') { + + *substringStart = *substringStart + 1; + *substringEnd = strchr(*substringStart, '"'); + } + else *substringEnd = strchr(*substringStart, ';'); + + // Get substring + if(*substringEnd) { + + substring(attribute, *substringStart, *substringEnd - *substringStart); + + if(*substringEnd[0] == '"') *substringEnd = *substringEnd + 1; + } + else strcpy(attribute, *substringStart); + + // Set default value if attribute is empty + if(strcmp(attribute, "") == 0 || attribute[0] == '\n' || attribute[0] == '\r' || attribute[0] == '\0') strcpy(attribute, "N/A"); + + // Clean \n from the end of the string + if(attribute[strlen(attribute) - 1] == '\n' || attribute[strlen(attribute) - 1] == '\r') attribute[strlen(attribute) - 1] = '\0'; + } + else { + + // Check if the first character is a [ + if((*substringStart)[0] == '[') { + + *substringStart = *substringStart + 1; + + if((*substringStart)[0] == ']') strcpy(attribute, ""); // Case: [] + else { + + char *tempConcat = (char *) calloc(MAX_LINE_SIZE, sizeof(char)); + + *substringStart = *substringStart - 1; + + while(1) { + + *substringStart = *substringStart + 1; + + if((*substringStart)[0] == ';') break; + else if((*substringStart)[0] == '\'') { // Case: "['example', 'example']" + + *substringStart = *substringStart + 1; + *substringEnd = strchr(*substringStart, '\''); + + // Get substring + if(*substringEnd) { + + // Create tmp name + char tmp[MAX_LINE_SIZE]; + substring(tmp, *substringStart, *substringEnd - *substringStart); + + // Concat tempConcat with tmp + strcat(tempConcat, tmp); + strcat(tempConcat, ", "); + + *substringStart = *substringEnd + 1; + } + } + } + + // Get substring + strcpy(attribute, tempConcat); + + // Clean "attribute" removing last 2 characters + attribute[strlen(attribute) - 2] = '\0'; + } + } + + *substringEnd = strchr(*substringStart, ';'); + } +} + +// ---------------------------------------------------------------------------------------------------- // + +// Methods implementations + +// Gets +char *character_getId(Character *character) { return character -> id; } +char *character_getName(Character *character) { return character -> name; } +char *character_getHouse(Character *character) { return character -> house; } +char *character_getAncestry(Character *character) { return character -> ancestry; } +char *character_getSpecies(Character *character) { return character -> species; } +char *character_getPatronus(Character *character) { return character -> patronus; } +bool character_getHogwartsStaff(Character *character) { return character -> hogwartsStaff; } +bool character_getHogwartsStudent(Character *character) { return character -> hogwartsStudent; } +char *character_getActorName(Character *character) { return character -> actorName; } +bool character_getAlive(Character *character) { return character -> alive; } +char *character_getEyeColour(Character *character) { return character -> eyeColour; } +char *character_getGender(Character *character) { return character -> gender; } +char *character_getHairColour(Character *character) { return character -> hairColour; } +bool character_getWizard(Character *character) { return character -> wizard; } + +char *character_getYearOfBirth(Character *character) { + + // "N/A" if yearOfBirth is -1 + char *yearOfBirth = (char *) calloc(15, sizeof(char)); + strcpy(yearOfBirth, "N/A"); + + if(character -> yearOfBirth != -1) sprintf(yearOfBirth, "%d", character -> yearOfBirth); + return yearOfBirth; +} + +char *character_getBirthDate(Character *character) { + + // "N/A" if day, month and year are -1 + char *birthDate = (char *) calloc(15, sizeof(char)); + strcpy(birthDate, "N/A"); + + if(character -> birthDate.day != -1 && character -> birthDate.month != -1 && character -> birthDate.year != -1) { + + sprintf(birthDate, "%02d-%02d-%04d", character -> birthDate.day, character -> birthDate.month, character -> birthDate.year); + } + + return birthDate; +} + +char *character_getAlternateNames(Character *character) { + + // Concatenate all alternate names with a comma + char *alternateNames = (char *) calloc(MAX_ALTERNATE_NAME_SIZE * MAX_ALTERNATE_NAMES, sizeof(char)); + + alternateNames[0] = '{'; + + for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) { + + if(strcmp(character -> alternateNames[i], "") != 0) { + + strcat(alternateNames, character -> alternateNames[i]); + + if(strcmp(character -> alternateNames[i + 1], "") != 0) strcat(alternateNames, ", "); + } + } + + strcat(alternateNames, "}"); + + return alternateNames; +} + +// Sets +void character_setId(Character *character, char *id) { strcpy(character -> id, id); } +void character_setName(Character *character, char *name) { strcpy(character -> name, name); } +void character_setHouse(Character *character, char *house) { strcpy(character -> house, house); } +void character_setAncestry(Character *character, char *ancestry) { strcpy(character -> ancestry, ancestry); } +void character_setSpecies(Character *character, char *species) { strcpy(character -> species, species); } +void character_setPatronus(Character *character, char *patronus) { strcpy(character -> patronus, patronus); } +void character_setHogwartsStaff(Character *character, bool hogwartsStaff) { character -> hogwartsStaff = hogwartsStaff; } +void character_setHogwartsStudent(Character *character, bool hogwartsStudent) { character -> hogwartsStudent = hogwartsStudent; } +void character_setActorName(Character *character, char *actorName) { strcpy(character -> actorName, actorName); } +void character_setAlive(Character *character, bool alive) { character -> alive = alive; } +void character_setYearOfBirth(Character *character, int yearOfBirth) { character -> yearOfBirth = yearOfBirth; } +void character_setEyeColour(Character *character, char *eyeColour) { strcpy(character -> eyeColour, eyeColour); } +void character_setGender(Character *character, char *gender) { strcpy(character -> gender, gender); } +void character_setHairColour(Character *character, char *hairColour) { strcpy(character -> hairColour, hairColour); } +void character_setWizard(Character *character, bool wizard) { character -> wizard = wizard; } + +void character_setBirthDate(Character *character, char *birthDate) { + + // Explode birthDate in format DD-MM-YYYY if in format DD-MM-YYYY + if(strlen(birthDate) >= 8 && strlen(birthDate) <= 10) { + + char *token = strtok(birthDate, "-"); + + character -> birthDate.day = atoi(token); + token = strtok(NULL, "-"); + character -> birthDate.month = atoi(token); + token = strtok(NULL, "-"); + character -> birthDate.year = atoi(token); + } +} + +void character_setAlternateNames(Character *character, char *alternateNames) { + + // Copy names to a temporary variable + char tempNames[MAX_ALTERNATE_NAME_SIZE * MAX_ALTERNATE_NAMES]; + strcpy(tempNames, alternateNames); + + // Separate names by comma + char *token = strtok(tempNames, ","); + int i = 0; + + // Copy names to the character + while (token != NULL && i < MAX_ALTERNATE_NAMES) { + + while (*token == ' ') token++; + + int len = strlen(token); + + while (len > 0 && token[len - 1] == ' ') { + + token[len - 1] = '\0'; + len--; + } + + strcpy(character -> alternateNames[i++], token); + token = strtok(NULL, ","); + } +} + +// Class +Character character_newBlank() { + + Character character; + + character.id = (char *) calloc(MAX_UUID_SIZE, sizeof(char)); + strcpy(character.id, ""); + + character.name = (char *) calloc(MAX_NAME_SIZE, sizeof(char)); + strcpy(character.name, ""); + + for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) { + + character.alternateNames[i] = (char *) calloc(MAX_ALTERNATE_NAME_SIZE, sizeof(char)); + strcpy(character.alternateNames[i], ""); + } + + character.house = (char *) calloc(MAX_HOUSE_SIZE, sizeof(char)); + strcpy(character.house, ""); + + character.ancestry = (char *) calloc(MAX_ANCESTRY_SIZE, sizeof(char)); + strcpy(character.ancestry, ""); + + character.species = (char *) calloc(MAX_SPECIES_SIZE, sizeof(char)); + strcpy(character.species, ""); + + character.patronus = (char *) calloc(MAX_PATRONUS_SIZE, sizeof(char)); + strcpy(character.patronus, ""); + + character.hogwartsStaff = false; + character.hogwartsStudent = false; + + character.actorName = (char *) calloc(MAX_ACTOR_NAME_SIZE, sizeof(char)); + strcpy(character.actorName, ""); + + character.alive = false; + + character.birthDate.day = -1; + character.birthDate.month = -1; + character.birthDate.year = -1; + + character.yearOfBirth = -1; + + character.eyeColour = (char *) calloc(MAX_EYE_COLOUR_SIZE, sizeof(char)); + strcpy(character.eyeColour, ""); + + character.gender = (char *) calloc(MAX_GENDER_SIZE, sizeof(char)); + strcpy(character.gender, ""); + + character.hairColour = (char *) calloc(MAX_HAIR_COLOUR_SIZE, sizeof(char)); + strcpy(character.hairColour, ""); + + character.wizard = false; + + return character; +} + +Character character_new(char *id, char *name, char *house, char *ancestry, char *species, char *patronus, bool hogwartsStaff, bool hogwartsStudent, char *actorName, bool alive, Date birthDate, int yearOfBirth, char *eyeColour, char *gender, char *hairColour, bool wizard) { + + Character character; + + character.id = (char *) calloc(MAX_UUID_SIZE, sizeof(char)); + strcpy(character.id, id); + + character.name = (char *) calloc(MAX_NAME_SIZE, sizeof(char)); + strcpy(character.name, name); + + for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) { + + character.alternateNames[i] = (char *) calloc(MAX_ALTERNATE_NAME_SIZE, sizeof(char)); + strcpy(character.alternateNames[i], ""); + } + + character.house = (char *) calloc(MAX_HOUSE_SIZE, sizeof(char)); + strcpy(character.house, house); + + character.ancestry = (char *) calloc(MAX_ANCESTRY_SIZE, sizeof(char)); + strcpy(character.ancestry, ancestry); + + character.species = (char *) calloc(MAX_SPECIES_SIZE, sizeof(char)); + strcpy(character.species, species); + + character.patronus = (char *) calloc(MAX_PATRONUS_SIZE, sizeof(char)); + strcpy(character.patronus, patronus); + + character.hogwartsStaff = hogwartsStaff; + character.hogwartsStudent = hogwartsStudent; + + character.actorName = (char *) calloc(MAX_ACTOR_NAME_SIZE, sizeof(char)); + strcpy(character.actorName, actorName); + + character.alive = alive; + character.birthDate = birthDate; + character.yearOfBirth = yearOfBirth; + + character.eyeColour = (char *) calloc(MAX_EYE_COLOUR_SIZE, sizeof(char)); + strcpy(character.eyeColour, eyeColour); + + character.gender = (char *) calloc(MAX_GENDER_SIZE, sizeof(char)); + strcpy(character.gender, gender); + + character.hairColour = (char *) calloc(MAX_HAIR_COLOUR_SIZE, sizeof(char)); + strcpy(character.hairColour, hairColour); + + character.wizard = wizard; + + return character; +} + +Character *character_clone(Character *character) { + + Character *clone = (Character *) malloc(sizeof(Character)); + + clone -> id = (char *) calloc(MAX_UUID_SIZE, sizeof(char)); + strcpy(clone -> id, character -> id); + + clone -> name = (char *) calloc(MAX_NAME_SIZE, sizeof(char)); + strcpy(clone -> name, character -> name); + + for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) { + + clone -> alternateNames[i] = (char *) calloc(MAX_ALTERNATE_NAME_SIZE, sizeof(char)); + strcpy(clone -> alternateNames[i], character -> alternateNames[i]); + } + + clone -> house = (char *) calloc(MAX_HOUSE_SIZE, sizeof(char)); + strcpy(clone -> house, character -> house); + + clone -> ancestry = (char *) calloc(MAX_ANCESTRY_SIZE, sizeof(char)); + strcpy(clone -> ancestry, character -> ancestry); + + clone -> species = (char *) calloc(MAX_SPECIES_SIZE, sizeof(char)); + strcpy(clone -> species, character -> species); + + clone -> patronus = (char *) calloc(MAX_PATRONUS_SIZE, sizeof(char)); + strcpy(clone -> patronus, character -> patronus); + + clone -> hogwartsStaff = character -> hogwartsStaff; + clone -> hogwartsStudent = character -> hogwartsStudent; + + clone -> actorName = (char *) calloc(MAX_ACTOR_NAME_SIZE, sizeof(char)); + strcpy(clone -> actorName, character -> actorName); + + clone -> alive = character -> alive; + clone -> birthDate = character -> birthDate; + clone -> yearOfBirth = character -> yearOfBirth; + + clone -> eyeColour = (char *) calloc(MAX_EYE_COLOUR_SIZE, sizeof(char)); + strcpy(clone -> eyeColour, character -> eyeColour); + + clone -> gender = (char *) calloc(MAX_GENDER_SIZE, sizeof(char)); + strcpy(clone -> gender, character -> gender); + + clone -> hairColour = (char *) calloc(MAX_HAIR_COLOUR_SIZE, sizeof(char)); + strcpy(clone -> hairColour, character -> hairColour); + + clone -> wizard = character -> wizard; + + return clone; +} + +void character_print(Character *character) { + + printf("[%s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s]\n", + + character_getId(character), + character_getName(character), + character_getAlternateNames(character), + character_getHouse(character), + character_getAncestry(character), + character_getSpecies(character), + character_getPatronus(character), + character_getHogwartsStaff(character) ? "true" : "false", + character_getHogwartsStudent(character) ? "true" : "false", + character_getActorName(character), + character_getAlive(character) ? "true" : "false", + character_getBirthDate(character), + character_getYearOfBirth(character), + character_getEyeColour(character), + character_getGender(character), + character_getHairColour(character), + character_getWizard(character) ? "true" : "false" + ); +} + +Character character_read(char *line) { + + Character character = character_newBlank(); + + char *substringStart = line; + char *substringEnd = NULL; + char attribute[MAX_LINE_SIZE]; + + // Get id + proccess_attribute(attribute, &substringStart, &substringEnd, true, false); + character_setId(&character, attribute); + + // Get name + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setName(&character, attribute); + + // Get alternate names + proccess_attribute(attribute, &substringStart, &substringEnd, false, true); + character_setAlternateNames(&character, attribute); + + // Get house + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setHouse(&character, attribute); + + // Get ancestry + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setAncestry(&character, attribute); + + // Get species + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setSpecies(&character, attribute); + + // Get patronus + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setPatronus(&character, attribute); + + // Get hogwarts staff + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setHogwartsStaff(&character, strcmp(attribute, "VERDADEIRO") == 0); + + // Get hogwarts student + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setHogwartsStudent(&character, strcmp(attribute, "VERDADEIRO") == 0); + + // Get actor name + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setActorName(&character, attribute); + + // Get alive + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setAlive(&character, strcmp(attribute, "VERDADEIRO") == 0); + + // Get birth date + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setBirthDate(&character, attribute); + + // Get year of birth + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setYearOfBirth(&character, atoi(attribute) == 0 ? -1 : atoi(attribute)); + + // Get eye colour + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setEyeColour(&character, attribute); + + // Get gender + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setGender(&character, attribute); + + // Get hair colour + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setHairColour(&character, attribute); + + // Get wizard + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + + // Clean \n from the end of the string + if(attribute[strlen(attribute) - 1] == '\n' || attribute[strlen(attribute) - 1] == '\r') attribute[strlen(attribute) - 1] = '\0'; + + character_setWizard(&character, strcmp(attribute, "VERDADEIRO") == 0); + return character; +} + +Character *character_searchById(char *id) { + + for(int i = 0; i < charactersLength; i++) { + + if(!strcmp(characters[i].id, id)) return &characters[i]; + } + return NULL; +} + +// General +void startCharacters() { + + // Open file + FILE *fp; + char *line = NULL; + size_t len = 0; + ssize_t read; + + fp = fopen(FILE_PATH, "r"); + + if(fp == NULL) { + + perror("x Error opening file"); + exit(EXIT_FAILURE); + } + + // Skip first line + getline(&line, &len, fp); + + // Read all lines + while((read = getline(&line, &len, fp)) != -1) { + + // Read character from line + Character character = character_read(line); + + characters[charactersLength++] = character; + + if(charactersLength >= MAX_CHARACTERS) { + + perror("x Max characters reached"); + exit(EXIT_FAILURE); + } + } + + // Close file and free memory + fclose(fp); + + if(line) free(line); +} + +// ---------------------------------------------------------------------------------------------------- // + +// Function to get the character at a specific position in a string +char getCharAtIndex(char *str, int index) { + + if(index >= strlen(str)) return '\0'; + else return str[strlen(str) - index - 1]; // Read characters from right to left +} + +// Radix Sort for string keys +void radixSort(Character arr[], int n, int *comparisons) { + + const int NUM_CHARS = 128; // Assuming ASCII characters + + // Initialize buckets + Character output[n]; + int *count = (int *)calloc(NUM_CHARS, sizeof(int)); + + if(count == NULL) { + + fprintf(stderr, "Memory allocation failed\n"); + exit(EXIT_FAILURE); + } + + // Find the maximum length of the id string + int maxLen = 0; + + for(int i = 0; i < n; i++) { + + (*comparisons) += 2; // For and if + + int len = strlen(character_getId(&arr[i])); + + if(len > maxLen) maxLen = len; + } + + // Perform counting sort for each character position + for(int pos = 0; pos < maxLen; pos++) { + + (*comparisons)++; // For + + // Count occurrences of characters + for(int i = 0; i < n; i++) { + + (*comparisons) += 2; // For and getCharAtIndex + + char ch = getCharAtIndex(character_getId(&arr[i]), pos); + count[ch]++; + } + + // Accumulate counts to get positions + for(int i = 1; i < NUM_CHARS; i++) { + + (*comparisons)++; // For + + count[i] += count[i - 1]; + } + + // Build the output array + for(int i = n - 1; i >= 0; i--) { + + (*comparisons) += 2; // For and getCharAtIndex + + char ch = getCharAtIndex(character_getId(&arr[i]), pos); + output[count[ch] - 1] = arr[i]; + count[ch]--; + } + + // Copy output array back to the original array + for(int i = 0; i < n; i++) { + + (*comparisons)++; // For + + arr[i] = output[i]; + } + + // Reset count array + memset(count, 0, NUM_CHARS * sizeof(int)); + } + + free(count); +} + +// ---------------------------------------------------------------------------------------------------- // + +// Main +int main() { + + // ----------------------------------------------------------------- // + + // #1 - Start - Read all characters in CSV file + startCharacters(); + + // ----------------------------------------------------------------- // + + // #2 - Read input and print characters from pub.in id entries and add to mainCharacters array + + // Initialize mainCharacters array + Character mainCharacters[MAX_CHARACTERS]; + int m = 0; + + char id[MAX_UUID_SIZE]; + scanf(" %[^\n]s", id); + + while(true) { + + // Clean \n from the end of the string + if(id[strlen(id) - 1] == '\n' || id[strlen(id) - 1] == '\r') id[strlen(id) - 1] = '\0'; + + if(isEnd(id)) break; + else { + + Character *character = character_searchById(id); + + if(character) mainCharacters[m++] = *character; + + // ------------------------- // + + scanf(" %[^\n]s", id); + } + } + + // ----------------------------------------------------------------- // + + // #3 - Order mainCharacters array by key "id" + + // Start benchmark + clock_t startTime = clock(); + int comparisons = 0; + + // ----------------- // + + // Radix sort + radixSort(mainCharacters, m, &comparisons); + + // ----------------- // + + // Save benchmark in file + FILE *fp = fopen("753045_radixsort.txt", "w"); + fprintf(fp, "753045\t%.0fms\t%d", (double)(clock() - startTime) / CLOCKS_PER_SEC * 1000.0, comparisons); + fclose(fp); + + // ----------------- // + + // Print mainCharacters array + for(int i = 0; i < m; i++) character_print(&mainCharacters[i]); + + // ----------------- // + + return 0; +} \ No newline at end of file diff --git a/tps/fonte/tp02/TP02Q14 - Radixsort em C/pedro.out b/tps/fonte/tp02/TP02Q14 - Radixsort em C/pedro.out new file mode 100644 index 0000000..e380da3 --- /dev/null +++ b/tps/fonte/tp02/TP02Q14 - Radixsort em C/pedro.out @@ -0,0 +1,27 @@ +[00434cd3-fcc7-44c7-8f98-7368415b4206 ## Miriam Strout ## {} ## Gryffindor ## half-blood ## human ## N/A ## false ## false ## N/A ## true ## 27-03-1960 ## 1954 ## blue ## female ## silver ## true] +[0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3 ## Dobby ## {} ## Slytherin ## pure-blood ## house-elf ## N/A ## false ## false ## Toby Jones ## false ## 06-02-1950 ## 1950 ## green ## male ## blond ## false] +[11b5ca88-64ad-41a4-9f36-317b66c290af ## Nearly Headless Nick ## {Sir Nicholas de Mimsy-Porpington, Sir Nicholas, Nick} ## Gryffindor ## pure-blood ## ghost ## N/A ## false ## false ## John Cleese ## false ## 27-03-1960 ## 1926 ## amber ## male ## sandy ## true] +[1413e1b3-2903-4a47-a2d5-e8abc5ce8014 ## Seamus Finnigan ## {O Flaherty, Seamus Finnegan} ## Gryffindor ## half-blood ## human ## N/A ## false ## true ## Devon Murray ## true ## 31-12-1926 ## 1960 ## amber ## male ## sandy ## true] +[20354d7a-e4fe-47af-8ff6-187bca92f3f9 ## Pandora Lovegood ## {} ## Ravenclaw ## quarter-veela ## human ## N/A ## false ## false ## N/A ## false ## 31-12-1926 ## 1959 ## dark ## female ## white ## true] +[2a0615de-8aa4-41e7-9504-dd875f5f3f01 ## George Weasley ## {} ## Gryffindor ## pure-blood ## human ## N/A ## false ## true ## Oliver Phelps ## true ## 13-02-1981 ## 1954 ## amber ## male ## red ## true] +[2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f ## Sirius Black ## {Padfoot, Snuffles} ## Gryffindor ## pure-blood ## human ## hare ## false ## false ## Gary Oldman ## false ## 03-11-1959 ## 1959 ## grey ## male ## black ## true] +[36bfefd0-e0bb-4d11-be98-d1ef6117a77a ## Rubeus Hagrid ## {Professor Hagrid, Hagger} ## Gryffindor ## half-blood ## half-giant ## N/A ## true ## false ## Robbie Coltrane ## true ## 06-12-1928 ## 1928 ## black ## male ## black ## true] +[3a0fe4df-2e40-4541-8d7f-13586f0b9294 ## Tom Riddle ## {Tom Riddle Senior} ## Ravenclaw ## muggle ## human ## N/A ## false ## false ## N/A ## false ## 27-03-1960 ## 1980 ## brown ## male ## dark ## false] +[41cd0bbe-a943-431b-9bde-bb2cad3491a1 ## Lily Potter ## {} ## Gryffindor ## muggleborn ## human ## N/A ## false ## false ## Geraldine Somerville ## false ## 30-01-1960 ## 1960 ## green ## female ## blond ## true] +[57fe29d4-312a-4711-bd9a-c320253d9176 ## Victoire Weasley ## {} ## Slytherin ## pure-blood ## human ## N/A ## false ## true ## N/A ## true ## 31-12-1926 ## 1926 ## grey ## female ## blonde ## true] +[5a4c95db-947d-4914-a631-41e8d466328e ## Kendra Dumbledore ## {} ## Slytherin ## muggleborn ## human ## N/A ## false ## false ## N/A ## false ## 31-12-1926 ## 1925 ## dark ## female ## black ## true] +[6afb1960-febd-418d-b604-e50c1b59459b ## Bellatrix Lestrange ## {Bella} ## Slytherin ## pure-blood ## human ## N/A ## false ## false ## Helena Bonham Carter ## false ## 06-02-1950 ## 1951 ## brown ## female ## black ## true] +[6b59be3f-e527-422d-959d-79fcdb3b24eb ## Hedwig ## {} ## Hufflepuff ## muggle ## owl ## N/A ## false ## false ## Gizmo ## false ## 07-04-1979 ## 1954 ## amber ## female ## brown ## false] +[7614cf6e-689e-47ac-a976-b1e9997637e9 ## Dudley Dursley ## {} ## Slytherin ## muggle ## human ## N/A ## false ## false ## Harry Melling ## true ## 23-06-1980 ## 1980 ## blue ## male ## blond ## false] +[861c4cde-2f0f-4796-8d8f-9492e74b2573 ## Luna Lovegood ## {Loony Lovegood} ## Ravenclaw ## pure-blood ## human ## hare ## false ## true ## Evanna Lynch ## true ## 13-02-1981 ## 1981 ## silver ## female ## blonde ## true] +[933787c2-51e3-4eac-8a85-ab332cac0456 ## Miranda Gaushawk ## {} ## Gryffindor ## muggle ## human ## N/A ## false ## false ## N/A ## false ## 27-03-1960 ## 1960 ## blue ## female ## silver ## true] +[94c993f6-a443-4408-b908-6e78e223e8ac ## Aberforth Dumbledore ## {} ## Gryffindor ## half-blood ## human ## goat ## false ## false ## Jim McManus ## true ## 10-03-1960 ## 1960 ## blue ## male ## grey ## true] +[9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8 ## Harry Potter ## {The Boy Who Lived, The Chosen One, Undesirable No. 1, Potty} ## Gryffindor ## half-blood ## human ## stag ## false ## true ## Daniel Radcliffe ## true ## 31-07-1980 ## 1980 ## green ## male ## black ## true] +[b0620914-858d-46fc-8e6d-033c565e138b ## Mrs Norris ## {} ## Slytherin ## pure-blood ## cat ## N/A ## true ## false ## Maxime, Alanis and Tommy the cats ## true ## 31-12-1926 ## 1954 ## yellow ## female ## brown ## false] +[b415c867-1cff-455e-b194-748662ac2cca ## Albus Dumbledore ## {Professor Dumbledore} ## Gryffindor ## half-blood ## human ## Phoenix ## true ## false ## Richard Harris ## false ## 27-03-1960 ## 1881 ## blue ## male ## silver ## true] +[ca3827f0-375a-4891-aaa5-f5e8a5bad225 ## Minerva McGonagall ## {} ## Gryffindor ## half-blood ## human ## tabby cat ## true ## false ## Dame Maggie Smith ## true ## 04-10-1925 ## 1925 ## grey ## female ## black ## true] +[d58e7249-19d1-40bd-a43f-1da0497fe8aa ## Dolores Umbridge ## {} ## Slytherin ## half-blood ## human ## persian cat ## true ## false ## Imelda Staunton ## true ## 31-12-1926 ## 1926 ## brown ## female ## grey ## true] +[d59691a4-f830-4eb0-a819-a0fb00b7e80f ## Garrick Ollivander ## {} ## Hufflepuff ## muggle ## human ## N/A ## false ## false ## John Hurt ## true ## 04-10-1925 ## 1954 ## pale, silvery ## male ## brown ## true] +[eaea5eb3-48a3-41c6-9ea5-c695299bab16 ## Lisa Turpin ## {} ## Ravenclaw ## pure-blood ## human ## N/A ## false ## true ## N/A ## true ## 27-03-1960 ## 1951 ## amber ## female ## sandy ## true] +[efa802c8-ae18-4ae1-a524-49df21d05939 ## Lord Voldemort ## {Tom Riddle, Tom Marvolo Riddle, You-Know-Who, He Who Must Not Be Named, the Dark Lord, the Heir of Slytherin} ## Slytherin ## half-blood ## human ## N/A ## false ## false ## Ralph Fiennes ## false ## 31-12-1926 ## 1926 ## Scarlet ## male ## bald ## true] +[fed624df-56d9-495e-9ad4-ea77000957e8 ## Petunia Dursley ## {} ## Slytherin ## muggle ## human ## N/A ## false ## false ## Fiona Shaw ## true ## 31-12-1926 ## 1954 ## yellow ## female ## blonde ## false] diff --git a/tps/fonte/tp02/TP02Q14 - Radixsort em C/pub.in b/tps/fonte/tp02/TP02Q14 - Radixsort em C/pub.in new file mode 100644 index 0000000..a782d8c --- /dev/null +++ b/tps/fonte/tp02/TP02Q14 - Radixsort em C/pub.in @@ -0,0 +1,28 @@ +9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8 +1413e1b3-2903-4a47-a2d5-e8abc5ce8014 +ca3827f0-375a-4891-aaa5-f5e8a5bad225 +36bfefd0-e0bb-4d11-be98-d1ef6117a77a +20354d7a-e4fe-47af-8ff6-187bca92f3f9 +57fe29d4-312a-4711-bd9a-c320253d9176 +b415c867-1cff-455e-b194-748662ac2cca +5a4c95db-947d-4914-a631-41e8d466328e +861c4cde-2f0f-4796-8d8f-9492e74b2573 +2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f +41cd0bbe-a943-431b-9bde-bb2cad3491a1 +2a0615de-8aa4-41e7-9504-dd875f5f3f01 +11b5ca88-64ad-41a4-9f36-317b66c290af +eaea5eb3-48a3-41c6-9ea5-c695299bab16 +0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3 +b0620914-858d-46fc-8e6d-033c565e138b +6b59be3f-e527-422d-959d-79fcdb3b24eb +fed624df-56d9-495e-9ad4-ea77000957e8 +d58e7249-19d1-40bd-a43f-1da0497fe8aa +3a0fe4df-2e40-4541-8d7f-13586f0b9294 +6afb1960-febd-418d-b604-e50c1b59459b +efa802c8-ae18-4ae1-a524-49df21d05939 +933787c2-51e3-4eac-8a85-ab332cac0456 +94c993f6-a443-4408-b908-6e78e223e8ac +00434cd3-fcc7-44c7-8f98-7368415b4206 +7614cf6e-689e-47ac-a976-b1e9997637e9 +d59691a4-f830-4eb0-a819-a0fb00b7e80f +FIM \ No newline at end of file diff --git a/tps/fonte/tp02/TP02Q15 - Ordenação PARCIAL por Seleção em Java/753045_selecaoParcial.txt b/tps/fonte/tp02/TP02Q15 - Ordenação PARCIAL por Seleção em Java/753045_selecaoParcial.txt new file mode 100644 index 0000000..f6b66b8 --- /dev/null +++ b/tps/fonte/tp02/TP02Q15 - Ordenação PARCIAL por Seleção em Java/753045_selecaoParcial.txt @@ -0,0 +1 @@ +753045 23ms 215 \ No newline at end of file diff --git a/tps/fonte/tp02/TP02Q15 - Ordenação PARCIAL por Seleção em Java/Arq.java b/tps/fonte/tp02/TP02Q15 - Ordenação PARCIAL por Seleção em Java/Arq.java new file mode 100644 index 0000000..9a632d1 --- /dev/null +++ b/tps/fonte/tp02/TP02Q15 - Ordenação PARCIAL por Seleção em Java/Arq.java @@ -0,0 +1,206 @@ +import java.io.*; +import java.util.Formatter; +import java.util.Scanner; +import java.io.File; +import java.nio.charset.*; + +public class Arq +{ + private static String nomeArquivo = ""; + private static String charsetArquivo = "ISO-8859-1"; + private static boolean write = false, read = false; + private static Formatter saida = null; + private static Scanner entrada = null; + + public static boolean openWrite(String nomeArq, String charset) { + boolean resp = false; + close(); + try{ + saida = new Formatter(nomeArq, charset); + nomeArquivo = nomeArq; + resp = write = true; + } catch (Exception e) {} + return resp; + } + + public static boolean openWrite(String nomeArq) { + return openWrite(nomeArq, charsetArquivo); + } + + public static boolean openWriteClose(String nomeArq, String charset, String conteudo) { + boolean resp = openWrite(nomeArq, charset); + if(resp == true){ + println(conteudo); + close(); + } + return resp; + } + + public static boolean openWriteClose(String nomeArq, String conteudo) { + return openWriteClose(nomeArq, charsetArquivo, conteudo); + } + + public static boolean openRead(String nomeArq) { + return openRead(nomeArq, charsetArquivo); + } + + public static boolean openRead(String nomeArq, String charset) { + boolean resp = false; + close(); + try{ + entrada = new Scanner(new File(nomeArq), charset); + nomeArquivo = nomeArq; + resp = read = true; + } catch (Exception e) {} + return resp; + } + + public static String openReadClose(String nomeArq){ + openRead(nomeArq); + String resp = readAll(); + close(); + return resp; + } + + public static void close() { + if(write == true){ + saida.close(); + } + if(read == true){ + entrada.close(); + } + write = read = false; + nomeArquivo = ""; + charsetArquivo = "ISO-8859-1"; + } + + public static long length(){ + long resp = -1; + if(read != write){ + File file = new File(nomeArquivo); + resp = file.length(); + } + return resp; + } + + public static void print(int x){ + if(write == true){ + saida.format( "%d", x); + } + } + + public static void print(double x){ + if(write == true){ + saida.format( "%f", x); + } + } + + public static void print(String x){ + if(write == true){ + saida.format( "%s", x); + } + } + + public static void print(boolean x){ + if(write == true){ + saida.format( "%s", ((x) ? "true" : "false")); + } + } + + public static void print(char x){ + if(write == true){ + saida.format( "%c", x); + } + } + + public static void println(int x){ + if(write == true){ + saida.format( "%d\n", x); + } + } + + public static void println(double x){ + if(write == true){ + saida.format( "%f\n", x); + } + } + + public static void println(String x){ + if(write == true){ + saida.format( "%s\n", x); + } + } + + public static void println(boolean x){ + if(write == true){ + saida.format( "%s\n", ((x) ? "true" : "false")); + } + } + + public static void println(char x){ + if(write == true){ + saida.format( "%c\n", x); + } + } + + public static int readInt(){ + int resp = -1; + try{ + resp = entrada.nextInt(); + } catch (Exception e) {} + return resp; + } + + public static char readChar(){ + char resp = ' '; + try{ + resp = (char)entrada.nextByte(); + } catch (Exception e) {} + return resp; + } + + public static double readDouble(){ + double resp = -1; + try{ + resp = Double.parseDouble(readString().replace(",",".")); + } catch (Exception e) {} + return resp; + } + + public static String readString(){ + String resp = ""; + try{ + resp = entrada.next(); + } catch (Exception e) { System.out.println(e.getMessage()); } + return resp; + } + + public static boolean readBoolean(){ + boolean resp = false; + try{ + resp = (entrada.next().equals("true")) ? true : false; + } catch (Exception e) {} + return resp; + } + + public static String readLine(){ + String resp = ""; + try{ + resp = entrada.nextLine(); + } catch (Exception e) { System.out.println(e.getMessage()); } + return resp; + } + + + public static boolean hasNext(){ + return entrada.hasNext(); + } + + public static String readAll(){ + String resp = ""; + while(hasNext()){ + resp += (readLine() + "\n"); + } + return resp; + } +} diff --git a/tps/fonte/tp02/TP02Q15 - Ordenação PARCIAL por Seleção em Java/Character.java b/tps/fonte/tp02/TP02Q15 - Ordenação PARCIAL por Seleção em Java/Character.java new file mode 100644 index 0000000..f92bc9e --- /dev/null +++ b/tps/fonte/tp02/TP02Q15 - Ordenação PARCIAL por Seleção em Java/Character.java @@ -0,0 +1,526 @@ +/** + * @path TP02Q01 - Classe em Java/Characters.java + * @description Java class of all characters from Harry Potter's saga with partial selection sort + * @author Pedro Lopes - github.com/httpspedroh + * @version 1.0 + * @update 2024-04-09 + */ + +// ---------------------------------------------------------------------------------------------------- // + +// Imports +import java.util.Scanner; +import java.util.UUID; +import java.io.BufferedReader; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Date; +import java.text.SimpleDateFormat; + +// ---------------------------------------------------------------------------------------------------- // + +public class Character { + + static SimpleDateFormat ddf = new SimpleDateFormat("dd-MM-yyyy"); + + // Global variables + public static final String FILE_PATH = "/tmp/characters.csv"; + public static ArrayList allCharacters = new ArrayList(); + + // -------------------------- // + + // Attributes + private UUID id; + private String name; + private ArrayList alternateNames; + private String house; + private String ancestry; + private String species; + private String patronus; + private boolean hogwartsStaff; + private boolean hogwartsStudent; + private String actorName; + private boolean alive; + private Date birthDate; + private int yearOfBirth; + private String eyeColour; + private String gender; + private String hairColour; + private boolean wizard; + + // -------------------------- // + + // Empty constructor + public Character() { + + this.id = UUID.randomUUID(); + this.name = ""; + this.alternateNames = new ArrayList(); + this.house = ""; + this.ancestry = ""; + this.species = ""; + this.patronus = ""; + this.hogwartsStaff = false; + this.hogwartsStudent = false; + this.actorName = ""; + this.alive = false; + this.birthDate = null; + this.yearOfBirth = 0; + this.eyeColour = ""; + this.gender = ""; + this.hairColour = ""; + this.wizard = false; + } + + // Constructor + public Character(UUID id, String name, ArrayList alternateNames, String house, String ancestry, String species, String patronus, boolean hogwartsStaff, boolean hogwartsStudent, String actorName, boolean alive, Date birthDate, int yearOfBirth, String eyeColour, String gender, String hairColour, boolean wizard) { + + this.id = id; + this.name = name; + this.alternateNames = alternateNames; + this.house = house; + this.ancestry = ancestry; + this.species = species; + this.patronus = patronus; + this.hogwartsStaff = hogwartsStaff; + this.hogwartsStudent = hogwartsStudent; + this.actorName = actorName; + this.alive = alive; + this.birthDate = birthDate; + this.yearOfBirth = yearOfBirth; + this.eyeColour = eyeColour; + this.gender = gender; + this.hairColour = hairColour; + this.wizard = wizard; + } + + // -------------------------- // + + // Gets + public UUID getId() { return this.id; } + public String getName() { return this.name; } + public String getHouse() { return this.house; } + public String getAncestry() { return this.ancestry; } + public String getSpecies() { return this.species; } + public String getPatronus() { return this.patronus; } + public boolean getHogwartsStaff() { return this.hogwartsStaff; } + public boolean getHogwartsStudent() { return this.hogwartsStudent; } + public String getActorName() { return this.actorName; } + public boolean getAlive() { return this.alive; } + public Date getBirthDate() { return this.birthDate; } + public int getYearOfBirth() { return this.yearOfBirth; } + public String getEyeColour() { return this.eyeColour; } + public String getGender() { return this.gender; } + public String getHairColour() { return this.hairColour; } + public boolean getWizard() { return this.wizard; } + + public String getAlternateNames() { + + // Construct string e.g. {item1, item2, item3} + String alternateNames = "{"; + + for(int i = 0; i < this.alternateNames.size(); i++) { + + alternateNames += this.alternateNames.get(i); + + if(i < this.alternateNames.size() - 1) alternateNames += ", "; + } + + alternateNames += "}"; + + return alternateNames; + } + + // Sets + public void setId(UUID id) { this.id = id; } + public void setName(String name) { this.name = name; } + public void setAlternateNames(ArrayList alternateNames) { this.alternateNames = alternateNames; } + public void setHouse(String house) { this.house = house; } + public void setAncestry(String ancestry) { this.ancestry = ancestry; } + public void setSpecies(String species) { this.species = species; } + public void setPatronus(String patronus) { this.patronus = patronus; } + public void setHogwartsStaff(boolean hogwartsStaff) { this.hogwartsStaff = hogwartsStaff; } + public void setHogwartsStudent(boolean hogwartsStudent) { this.hogwartsStudent = hogwartsStudent; } + public void setActorNane(String actorName) { this.actorName = actorName; } + public void setAlive(boolean alive) { this.alive = alive; } + public void setBirthDate(Date birthDate) { this.birthDate = birthDate; } + public void setYearOfBirth(int yearOfBirth) { this.yearOfBirth = yearOfBirth; } + public void setEyeColour(String eyeColour) { this.eyeColour = eyeColour; } + public void setGender(String gender) { this.gender = gender; } + public void setHairColour(String hairColour) { this.hairColour = hairColour; } + public void setWizard(boolean wizard) { this.wizard = wizard; } + + // -------------------------- // + + // Clone + public Character clone() { return new Character(this.id, this.name, this.alternateNames, this.house, this.ancestry, this.species, this.patronus, this.hogwartsStaff, this.hogwartsStudent, this.actorName, this.alive, this.birthDate, this.yearOfBirth, this.eyeColour, this.gender, this.hairColour, this.wizard); } + + // -------------------------- // + + // Print + public void print() { + + System.out.println("[" + + this.getId() + " ## " + + this.getName() + " ## " + + this.getAlternateNames() + " ## " + + (this.getHouse() == "" ? "N/A" : this.getHouse()) + " ## " + + (this.getAncestry() == "" ? "N/A" : this.getAncestry()) + " ## " + + (this.getSpecies() == "" ? "N/A" : this.getSpecies()) + " ## " + + (this.getPatronus() == "" ? "N/A" : this.getPatronus()) + " ## " + + (this.getHogwartsStaff() ? "true" : "false") + " ## " + + (this.getHogwartsStudent() ? "true" : "false") + " ## " + + (this.getActorName() == "" ? "N/A" : this.getActorName()) + " ## " + + (this.getAlive() ? "true" : "false") + " ## " + + (this.getBirthDate() == null ? "N/A" : ddf.format(this.getBirthDate())) + " ## " + + (this.getYearOfBirth() == 0 ? "N/A" : this.getYearOfBirth()) + " ## " + + (this.getEyeColour() == "" ? "N/A" : this.getEyeColour()) + " ## " + + (this.getGender() == "" ? "N/A" : this.getGender()) + " ## " + + (this.getHairColour() == "" ? "N/A" : this.getHairColour()) + " ## " + + (this.getWizard() ? "true" : "false") + "]"); + } + + // -------------------------- // + + public static ArrayList extractNames(String input) { + + ArrayList names = new ArrayList<>(); + + // Clean input + String cleanedInput = input.substring(1, input.length() - 1); + + // Check if input contains double quotes + if (cleanedInput.contains("\"\"")) { + + String[] parts = cleanedInput.split("\", "); + + for (String part : parts) names.add(part.replace("\"", "").replace("'", "")); + } + else { + + // Split input by comma and space + String[] parts = cleanedInput.split(", "); + + for (String part : parts) names.add(part.replace("'", "")); + } + return names; + } + + // -------------------------- // + + // Read + public void read(String line) { + + // ---------------------- // + + // Start position + int positionStart = 0; + int positionEnd = 0; + + // ---------------------- // + + // Set id + positionEnd = line.indexOf(";", positionStart); + this.setId(UUID.fromString(line.substring(positionStart, positionEnd))); + + // ---------------------- // + + // Set name + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setName(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set alternate names + positionStart = positionEnd + 1; + + if(line.charAt(positionStart) == '[') { // If first character is '[', has one or no names + + if(line.charAt(positionStart + 1) == ']') { + + positionEnd = positionStart + 2; + + this.setAlternateNames(new ArrayList()); + } + else { + + positionEnd = line.indexOf("];", positionStart); + + this.setAlternateNames(extractNames(line.substring(positionStart, positionEnd++))); + } + } + + // ---------------------- // + + // Set house + positionStart = positionEnd; + positionEnd = line.indexOf(";", ++positionStart); + + this.setHouse(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set ancestry + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setAncestry(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set species + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setSpecies(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set patronus + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setPatronus(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set hogwartsStaff + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setHogwartsStaff("VERDADEIRO".equals(line.substring(positionStart, positionEnd))); + + // ---------------------- // + + // Set hogwartsStudent + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setHogwartsStudent("VERDADEIRO".equals(line.substring(positionStart, positionEnd))); + + // ---------------------- // + + // Set actor name + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setActorNane(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set alive + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setAlive("VERDADEIRO".equals(line.substring(positionStart, positionEnd))); + + // ---------------------- // + + // Set date of birth + positionStart = positionEnd; + positionEnd = line.indexOf(";", ++positionStart); + + try { this.setBirthDate(ddf.parse(line.substring(positionStart, positionEnd))); } + catch(Exception e) { this.setBirthDate(null); } + + // ---------------------- // + + // Set year of birth + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + try { this.setYearOfBirth(Integer.parseInt(line.substring(positionStart, positionEnd))); } + catch(Exception e) { this.setYearOfBirth(0); } + + // ---------------------- // + + // Set eye colour + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setEyeColour(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set gender + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setGender(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set hair colour + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setHairColour(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set wizard + positionStart = positionEnd + 1; + + this.setWizard("VERDADEIRO".equals(line.substring(positionStart, line.length()))); + + // ---------------------- // + } + + // -------------------------- // + + // Compare method + public int compareTo(Character other) { + + // Compare by actorName + int actorNameComparison = this.actorName.compareTo(other.actorName); + + // If actorName is the same, compare by name + if(actorNameComparison != 0) return actorNameComparison; + else return this.name.compareTo(other.name); + } + + // ---------------------------------------------------------------------------------------------------- // + + // Read all characters function + public static void startCharacters() { + + // Initialize variables + try { + + FileInputStream fstream = new FileInputStream(FILE_PATH); + BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); + + // ---------------------- // + + // Explode CSV file + String line = br.readLine(); + + while((line = br.readLine()) != null) { + + // Initialize character + Character character = new Character(); + + // Read line + character.read(line); + + // Add character to array + allCharacters.add(character); + } + + // Close CSV file + fstream.close(); + } + catch(IOException e) { e.printStackTrace(); } + } + + // ---------------------------------------------------------------------------------------------------- // + + // Search by id function + public static Character searchById(UUID id, ArrayList characters) { + + // Search for character + for(int i = 0; i < characters.size(); i++) { + + if(characters.get(i).getId().equals(id)) return characters.get(i); + } + return null; + } + + // ---------------------------------------------------------------------------------------------------- // + + public static void main(String[] args) { + + // ----------------------------------------------------------------- // + + // #1 - Start - Read all characters in CSV file + startCharacters(); + + // ----------------------------------------------------------------- // + + // #2 - Read input and print characters from pub.in id entries and add to mainCharacters array + + // Initialize mainCharacters array + ArrayList mainCharacters = new ArrayList(); + + // Initialize scanner + Scanner inScanner = new Scanner(System.in); + + // Initialize character + Character character = new Character(); + + // Read first line + String line = inScanner.nextLine(); + + // While line is not "FIM" + while(!line.equals("FIM")) { + + // Get id + UUID id = UUID.fromString(line); + + // Search for character + character = searchById(id, allCharacters); + + // Print character + if(character != null) mainCharacters.add(character); + + // Read line + line = inScanner.nextLine(); + } + + // ----------------------------------------------------------------- // + + // #3 - Order mainCharacters array by key "name" using parcial selection sort + + // Start benchmark + long startTime = System.currentTimeMillis(); + int comparisons = 0; + + // ----------------- // + + // Parcial selection sort with k = 10 + int k = 10; + + for(int i = 0; i < Math.min(k, mainCharacters.size() - 1); i++) { + + // Initialize min + int min = i; + + // Find min among remaining unsorted elements + for(int j = i + 1; j < mainCharacters.size(); j++) { + + // Compare + if(mainCharacters.get(j).getName().compareTo(mainCharacters.get(min).getName()) < 0) min = j; + + comparisons++; + } + + // Swap + Character temp = mainCharacters.get(i); + mainCharacters.set(i, mainCharacters.get(min)); + mainCharacters.set(min, temp); + } + + // ----------------- // + + // Save benchmark in file + Arq.openWrite("753045_selecaoParcial.txt"); + Arq.print("753045\t" + (System.currentTimeMillis() - startTime) + "ms\t" + comparisons); + Arq.close(); + + // ----------------- // + + // Print mainCharacters array + for(int i = 0; i < k; i++) mainCharacters.get(i).print(); + + // ----------------------------------------------------------------- // + + // Close scanner + inScanner.close(); + } +} + +// ---------------------------------------------------------------------------------------------------- // \ No newline at end of file diff --git a/tps/fonte/tp02/TP02Q15 - Ordenação PARCIAL por Seleção em Java/pedro.out b/tps/fonte/tp02/TP02Q15 - Ordenação PARCIAL por Seleção em Java/pedro.out new file mode 100644 index 0000000..4531250 --- /dev/null +++ b/tps/fonte/tp02/TP02Q15 - Ordenação PARCIAL por Seleção em Java/pedro.out @@ -0,0 +1,10 @@ +[94c993f6-a443-4408-b908-6e78e223e8ac ## Aberforth Dumbledore ## {} ## Gryffindor ## half-blood ## human ## goat ## false ## false ## Jim McManus ## true ## 10-03-1960 ## 1960 ## blue ## male ## grey ## true] +[b415c867-1cff-455e-b194-748662ac2cca ## Albus Dumbledore ## {Professor Dumbledore} ## Gryffindor ## half-blood ## human ## Phoenix ## true ## false ## Richard Harris ## false ## 27-03-1960 ## 1881 ## blue ## male ## silver ## true] +[6afb1960-febd-418d-b604-e50c1b59459b ## Bellatrix Lestrange ## {Bella} ## Slytherin ## pure-blood ## human ## N/A ## false ## false ## Helena Bonham Carter ## false ## 06-02-1950 ## 1951 ## brown ## female ## black ## true] +[0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3 ## Dobby ## {} ## Slytherin ## pure-blood ## house-elf ## N/A ## false ## false ## Toby Jones ## false ## 06-02-1950 ## 1950 ## green ## male ## blond ## false] +[d58e7249-19d1-40bd-a43f-1da0497fe8aa ## Dolores Umbridge ## {} ## Slytherin ## half-blood ## human ## persian cat ## true ## false ## Imelda Staunton ## true ## 31-12-1926 ## 1926 ## brown ## female ## grey ## true] +[7614cf6e-689e-47ac-a976-b1e9997637e9 ## Dudley Dursley ## {} ## Slytherin ## muggle ## human ## N/A ## false ## false ## Harry Melling ## true ## 23-06-1980 ## 1980 ## blue ## male ## blond ## false] +[d59691a4-f830-4eb0-a819-a0fb00b7e80f ## Garrick Ollivander ## {} ## Hufflepuff ## muggle ## human ## N/A ## false ## false ## John Hurt ## true ## 04-10-1925 ## 1954 ## pale, silvery ## male ## brown ## true] +[2a0615de-8aa4-41e7-9504-dd875f5f3f01 ## George Weasley ## {} ## Gryffindor ## pure-blood ## human ## N/A ## false ## true ## Oliver Phelps ## true ## 13-02-1981 ## 1954 ## amber ## male ## red ## true] +[9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8 ## Harry Potter ## {The Boy Who Lived, The Chosen One, Undesirable No. 1, Potty} ## Gryffindor ## half-blood ## human ## stag ## false ## true ## Daniel Radcliffe ## true ## 31-07-1980 ## 1980 ## green ## male ## black ## true] +[6b59be3f-e527-422d-959d-79fcdb3b24eb ## Hedwig ## {} ## Hufflepuff ## muggle ## owl ## N/A ## false ## false ## Gizmo ## false ## 07-04-1979 ## 1954 ## amber ## female ## brown ## false] diff --git a/tps/fonte/tp02/TP02Q15 - Ordenação PARCIAL por Seleção em Java/pub.in b/tps/fonte/tp02/TP02Q15 - Ordenação PARCIAL por Seleção em Java/pub.in new file mode 100644 index 0000000..a782d8c --- /dev/null +++ b/tps/fonte/tp02/TP02Q15 - Ordenação PARCIAL por Seleção em Java/pub.in @@ -0,0 +1,28 @@ +9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8 +1413e1b3-2903-4a47-a2d5-e8abc5ce8014 +ca3827f0-375a-4891-aaa5-f5e8a5bad225 +36bfefd0-e0bb-4d11-be98-d1ef6117a77a +20354d7a-e4fe-47af-8ff6-187bca92f3f9 +57fe29d4-312a-4711-bd9a-c320253d9176 +b415c867-1cff-455e-b194-748662ac2cca +5a4c95db-947d-4914-a631-41e8d466328e +861c4cde-2f0f-4796-8d8f-9492e74b2573 +2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f +41cd0bbe-a943-431b-9bde-bb2cad3491a1 +2a0615de-8aa4-41e7-9504-dd875f5f3f01 +11b5ca88-64ad-41a4-9f36-317b66c290af +eaea5eb3-48a3-41c6-9ea5-c695299bab16 +0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3 +b0620914-858d-46fc-8e6d-033c565e138b +6b59be3f-e527-422d-959d-79fcdb3b24eb +fed624df-56d9-495e-9ad4-ea77000957e8 +d58e7249-19d1-40bd-a43f-1da0497fe8aa +3a0fe4df-2e40-4541-8d7f-13586f0b9294 +6afb1960-febd-418d-b604-e50c1b59459b +efa802c8-ae18-4ae1-a524-49df21d05939 +933787c2-51e3-4eac-8a85-ab332cac0456 +94c993f6-a443-4408-b908-6e78e223e8ac +00434cd3-fcc7-44c7-8f98-7368415b4206 +7614cf6e-689e-47ac-a976-b1e9997637e9 +d59691a4-f830-4eb0-a819-a0fb00b7e80f +FIM \ No newline at end of file diff --git a/tps/fonte/tp02/TP02Q16 - Ordenação PARCIAL por Inserção em C/753045_insercaoParcial.txt b/tps/fonte/tp02/TP02Q16 - Ordenação PARCIAL por Inserção em C/753045_insercaoParcial.txt new file mode 100644 index 0000000..513f60a --- /dev/null +++ b/tps/fonte/tp02/TP02Q16 - Ordenação PARCIAL por Inserção em C/753045_insercaoParcial.txt @@ -0,0 +1 @@ +753045 0ms 334 \ No newline at end of file diff --git a/tps/fonte/tp02/TP02Q16 - Ordenação PARCIAL por Inserção em C/Character b/tps/fonte/tp02/TP02Q16 - Ordenação PARCIAL por Inserção em C/Character new file mode 100644 index 0000000000000000000000000000000000000000..6c4edd0c0d9d524ccc766031947fb43daab61670 GIT binary patch literal 31944 zcmeHw4R}=Lo$s3ofj}V>6*N^^#t$@V69W~5MKl8$Ja~hqKoI$GG9(jb+I&ot84M^k zHpJ#|7)iUZH+A8;@wT^{?!DWky-Mj8WcZLo+TBg9deOx;b!|J5sHsIaZjrga|NC*~ z%*jkPKKtB0&wZX8o|*so|NbBEd;ahL{W$LlbH3&&U*X8gV(Q3d_b}o*uMkM76lf87 z0#eErvCrW9^XzkMGWbax6Y^3)AQeZzkxFwVo()QNJyLF`v!$M*s)j_#F4HkQRY(+N z`GP0A96FV}ofgd#dWtH!Szd*ZGaGG@=N)x5MRmc3NJ~-6qJ>2Z?<{U^ES}G=dnPJEId$IymF$I* z9k2iF?Y|kEy4$m4Rq5h)uIhUBb*eYjf&7sU9i%Udm1HLK)-jD&`mI43k4ETx+_`VD{4Ik;Q z>pkEueC)?Y0O|B+Wx%h?fZv+|4`#q`$bj$6fN#lw&&YsNe@%B?p8$8^V?WvfP@P5{ z&Ecl%(01l;35Hp7s43VGu4T22A%6o4`J0*=o0!)dY;LSxyx7}Z?Q5uI&~G4v>bl12 zuMih*{0gh~)qy3h4u-XmHTzk;30C{U%olD9l0tQ$iPg$-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(x4#aF)!rrT6|IuxD+f0BY@f}(Ro)mXs_Luk6|d+)o4vYkR`i>4Uq#ug z@)iFbo4tDOrRY?pC@IC-dVC)YU7~6~weO&4iS7DTy_Jt2+U)J`<2_)rx0iq1X0P_i z6{WwovmW;HUxJYKmZ=ZXq4rPJdwT8qidXbi*f{Y~_G;gKSZ<^!J@QS5;uZZ3Fsh=m zSNpo^J;(#{`qgD9JJpU|uq72`ulDiJc>&@m6vX^i_KLm(iPhfR@2?UjF+t34WvA#L zA+g%4eYRMYFj4hX4wRkZKgJ1Rs{ZQvL4TFd=#=|uf@933P9(hyO68Z>aAhyIt4%5C z(qW}$?g|nA9;p&ciF`mz^APRLsQRn%hx&)AyRZ=7h4q3K(Bxt{RJjVG?w-!RRL-qa JYz8)#{a+FRZ6E*u literal 0 HcmV?d00001 diff --git a/tps/fonte/tp02/TP02Q16 - Ordenação PARCIAL por Inserção em C/Character.c b/tps/fonte/tp02/TP02Q16 - Ordenação PARCIAL por Inserção em C/Character.c new file mode 100644 index 0000000..394c92b --- /dev/null +++ b/tps/fonte/tp02/TP02Q16 - Ordenação PARCIAL por Inserção em C/Character.c @@ -0,0 +1,719 @@ +/** + * @path TP02Q02 - Classe em C/Character.c + * @description C file that implements the Character class with partial insertion sort algorithm + * @author Pedro Lopes - github.com/httpspedroh + * @version 1.0 + * @update 2024-04-09 + */ + +// ---------------------------------------------------------------------------------------------------- // + +// Includes +#include +#include +#include +#include +#include + +// ---------------------------------------------------------------------------------------------------- // + +// Constants +#define MAX_CHARACTERS 405 +#define FILE_PATH "/tmp/characters.csv" + +#define MAX_UUID_SIZE 37 +#define MAX_NAME_SIZE 30 +#define MAX_ALTERNATE_NAMES 10 +#define MAX_ALTERNATE_NAME_SIZE 35 +#define MAX_HOUSE_SIZE 15 +#define MAX_ANCESTRY_SIZE 15 +#define MAX_SPECIES_SIZE 20 +#define MAX_PATRONUS_SIZE 25 +#define MAX_ACTOR_NAME_SIZE 35 +#define MAX_EYE_COLOUR_SIZE 10 +#define MAX_GENDER_SIZE 10 +#define MAX_HAIR_COLOUR_SIZE 10 + +#define MAX_LINE_SIZE 300 + +// ---------------------------------------------------------------------------------------------------- // + +// Structs +typedef struct Date { + + int day; + int month; + int year; +} Date; + +typedef struct Character { + + char *id; + char *name; + char *alternateNames[MAX_ALTERNATE_NAMES]; + char *house; + char *ancestry; + char *species; + char *patronus; + bool hogwartsStaff; + bool hogwartsStudent; + char *actorName; + bool alive; + Date birthDate; + int yearOfBirth; + char *eyeColour; + char *gender; + char *hairColour; + bool wizard; +} Character; + +// ---------------------------------------------------------------------------------------------------- // + +// Global variables +Character characters[MAX_CHARACTERS]; +int charactersLength = 0; + +// ---------------------------------------------------------------------------------------------------- // + +// Functions +bool isEnd(char* line) { return line[0] == 'F' && line[1] == 'I' && line[2] == 'M'; } + +void substring(char *string, char *stringStart, int length) { + + strncpy(string, stringStart, length); + string[length] = '\0'; +} + +void proccess_attribute(char *attribute, char **substringStart, char **substringEnd, bool isFirstAttribute, bool isStringArray) { + + // Skip first comma + if(!isFirstAttribute) { + + if(*substringEnd != NULL) *substringStart = *substringEnd + 1; + else *substringStart = *substringEnd; + } + + if(!isStringArray) { + + if((*substringStart)[0] == '"') { + + *substringStart = *substringStart + 1; + *substringEnd = strchr(*substringStart, '"'); + } + else *substringEnd = strchr(*substringStart, ';'); + + // Get substring + if(*substringEnd) { + + substring(attribute, *substringStart, *substringEnd - *substringStart); + + if(*substringEnd[0] == '"') *substringEnd = *substringEnd + 1; + } + else strcpy(attribute, *substringStart); + + // Set default value if attribute is empty + if(strcmp(attribute, "") == 0 || attribute[0] == '\n' || attribute[0] == '\r' || attribute[0] == '\0') strcpy(attribute, "N/A"); + + // Clean \n from the end of the string + if(attribute[strlen(attribute) - 1] == '\n' || attribute[strlen(attribute) - 1] == '\r') attribute[strlen(attribute) - 1] = '\0'; + } + else { + + // Check if the first character is a [ + if((*substringStart)[0] == '[') { + + *substringStart = *substringStart + 1; + + if((*substringStart)[0] == ']') strcpy(attribute, ""); // Case: [] + else { + + char *tempConcat = (char *) calloc(MAX_LINE_SIZE, sizeof(char)); + + *substringStart = *substringStart - 1; + + while(1) { + + *substringStart = *substringStart + 1; + + if((*substringStart)[0] == ';') break; + else if((*substringStart)[0] == '\'') { // Case: "['example', 'example']" + + *substringStart = *substringStart + 1; + *substringEnd = strchr(*substringStart, '\''); + + // Get substring + if(*substringEnd) { + + // Create tmp name + char tmp[MAX_LINE_SIZE]; + substring(tmp, *substringStart, *substringEnd - *substringStart); + + // Concat tempConcat with tmp + strcat(tempConcat, tmp); + strcat(tempConcat, ", "); + + *substringStart = *substringEnd + 1; + } + } + } + + // Get substring + strcpy(attribute, tempConcat); + + // Clean "attribute" removing last 2 characters + attribute[strlen(attribute) - 2] = '\0'; + } + } + + *substringEnd = strchr(*substringStart, ';'); + } +} + +// ---------------------------------------------------------------------------------------------------- // + +// Methods implementations + +// Gets +char *character_getId(Character *character) { return character -> id; } +char *character_getName(Character *character) { return character -> name; } +char *character_getHouse(Character *character) { return character -> house; } +char *character_getAncestry(Character *character) { return character -> ancestry; } +char *character_getSpecies(Character *character) { return character -> species; } +char *character_getPatronus(Character *character) { return character -> patronus; } +bool character_getHogwartsStaff(Character *character) { return character -> hogwartsStaff; } +bool character_getHogwartsStudent(Character *character) { return character -> hogwartsStudent; } +char *character_getActorName(Character *character) { return character -> actorName; } +bool character_getAlive(Character *character) { return character -> alive; } +char *character_getEyeColour(Character *character) { return character -> eyeColour; } +char *character_getGender(Character *character) { return character -> gender; } +char *character_getHairColour(Character *character) { return character -> hairColour; } +bool character_getWizard(Character *character) { return character -> wizard; } + +char *character_getYearOfBirth(Character *character) { + + // "N/A" if yearOfBirth is -1 + char *yearOfBirth = (char *) calloc(15, sizeof(char)); + strcpy(yearOfBirth, "N/A"); + + if(character -> yearOfBirth != -1) sprintf(yearOfBirth, "%d", character -> yearOfBirth); + return yearOfBirth; +} + +char *character_getBirthDate(Character *character) { + + // "N/A" if day, month and year are -1 + char *birthDate = (char *) calloc(15, sizeof(char)); + strcpy(birthDate, "N/A"); + + if(character -> birthDate.day != -1 && character -> birthDate.month != -1 && character -> birthDate.year != -1) { + + sprintf(birthDate, "%02d-%02d-%04d", character -> birthDate.day, character -> birthDate.month, character -> birthDate.year); + } + + return birthDate; +} + +char *character_getAlternateNames(Character *character) { + + // Concatenate all alternate names with a comma + char *alternateNames = (char *) calloc(MAX_ALTERNATE_NAME_SIZE * MAX_ALTERNATE_NAMES, sizeof(char)); + + alternateNames[0] = '{'; + + for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) { + + if(strcmp(character -> alternateNames[i], "") != 0) { + + strcat(alternateNames, character -> alternateNames[i]); + + if(strcmp(character -> alternateNames[i + 1], "") != 0) strcat(alternateNames, ", "); + } + } + + strcat(alternateNames, "}"); + + return alternateNames; +} + +// Sets +void character_setId(Character *character, char *id) { strcpy(character -> id, id); } +void character_setName(Character *character, char *name) { strcpy(character -> name, name); } +void character_setHouse(Character *character, char *house) { strcpy(character -> house, house); } +void character_setAncestry(Character *character, char *ancestry) { strcpy(character -> ancestry, ancestry); } +void character_setSpecies(Character *character, char *species) { strcpy(character -> species, species); } +void character_setPatronus(Character *character, char *patronus) { strcpy(character -> patronus, patronus); } +void character_setHogwartsStaff(Character *character, bool hogwartsStaff) { character -> hogwartsStaff = hogwartsStaff; } +void character_setHogwartsStudent(Character *character, bool hogwartsStudent) { character -> hogwartsStudent = hogwartsStudent; } +void character_setActorName(Character *character, char *actorName) { strcpy(character -> actorName, actorName); } +void character_setAlive(Character *character, bool alive) { character -> alive = alive; } +void character_setYearOfBirth(Character *character, int yearOfBirth) { character -> yearOfBirth = yearOfBirth; } +void character_setEyeColour(Character *character, char *eyeColour) { strcpy(character -> eyeColour, eyeColour); } +void character_setGender(Character *character, char *gender) { strcpy(character -> gender, gender); } +void character_setHairColour(Character *character, char *hairColour) { strcpy(character -> hairColour, hairColour); } +void character_setWizard(Character *character, bool wizard) { character -> wizard = wizard; } + +void character_setBirthDate(Character *character, char *birthDate) { + + // Explode birthDate in format DD-MM-YYYY if in format DD-MM-YYYY + if(strlen(birthDate) >= 8 && strlen(birthDate) <= 10) { + + char *token = strtok(birthDate, "-"); + + character -> birthDate.day = atoi(token); + token = strtok(NULL, "-"); + character -> birthDate.month = atoi(token); + token = strtok(NULL, "-"); + character -> birthDate.year = atoi(token); + } +} + +void character_setAlternateNames(Character *character, char *alternateNames) { + + // Copy names to a temporary variable + char tempNames[MAX_ALTERNATE_NAME_SIZE * MAX_ALTERNATE_NAMES]; + strcpy(tempNames, alternateNames); + + // Separate names by comma + char *token = strtok(tempNames, ","); + int i = 0; + + // Copy names to the character + while (token != NULL && i < MAX_ALTERNATE_NAMES) { + + while (*token == ' ') token++; + + int len = strlen(token); + + while (len > 0 && token[len - 1] == ' ') { + + token[len - 1] = '\0'; + len--; + } + + strcpy(character -> alternateNames[i++], token); + token = strtok(NULL, ","); + } +} + +// Class +Character character_newBlank() { + + Character character; + + character.id = (char *) calloc(MAX_UUID_SIZE, sizeof(char)); + strcpy(character.id, ""); + + character.name = (char *) calloc(MAX_NAME_SIZE, sizeof(char)); + strcpy(character.name, ""); + + for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) { + + character.alternateNames[i] = (char *) calloc(MAX_ALTERNATE_NAME_SIZE, sizeof(char)); + strcpy(character.alternateNames[i], ""); + } + + character.house = (char *) calloc(MAX_HOUSE_SIZE, sizeof(char)); + strcpy(character.house, ""); + + character.ancestry = (char *) calloc(MAX_ANCESTRY_SIZE, sizeof(char)); + strcpy(character.ancestry, ""); + + character.species = (char *) calloc(MAX_SPECIES_SIZE, sizeof(char)); + strcpy(character.species, ""); + + character.patronus = (char *) calloc(MAX_PATRONUS_SIZE, sizeof(char)); + strcpy(character.patronus, ""); + + character.hogwartsStaff = false; + character.hogwartsStudent = false; + + character.actorName = (char *) calloc(MAX_ACTOR_NAME_SIZE, sizeof(char)); + strcpy(character.actorName, ""); + + character.alive = false; + + character.birthDate.day = -1; + character.birthDate.month = -1; + character.birthDate.year = -1; + + character.yearOfBirth = -1; + + character.eyeColour = (char *) calloc(MAX_EYE_COLOUR_SIZE, sizeof(char)); + strcpy(character.eyeColour, ""); + + character.gender = (char *) calloc(MAX_GENDER_SIZE, sizeof(char)); + strcpy(character.gender, ""); + + character.hairColour = (char *) calloc(MAX_HAIR_COLOUR_SIZE, sizeof(char)); + strcpy(character.hairColour, ""); + + character.wizard = false; + + return character; +} + +Character character_new(char *id, char *name, char *house, char *ancestry, char *species, char *patronus, bool hogwartsStaff, bool hogwartsStudent, char *actorName, bool alive, Date birthDate, int yearOfBirth, char *eyeColour, char *gender, char *hairColour, bool wizard) { + + Character character; + + character.id = (char *) calloc(MAX_UUID_SIZE, sizeof(char)); + strcpy(character.id, id); + + character.name = (char *) calloc(MAX_NAME_SIZE, sizeof(char)); + strcpy(character.name, name); + + for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) { + + character.alternateNames[i] = (char *) calloc(MAX_ALTERNATE_NAME_SIZE, sizeof(char)); + strcpy(character.alternateNames[i], ""); + } + + character.house = (char *) calloc(MAX_HOUSE_SIZE, sizeof(char)); + strcpy(character.house, house); + + character.ancestry = (char *) calloc(MAX_ANCESTRY_SIZE, sizeof(char)); + strcpy(character.ancestry, ancestry); + + character.species = (char *) calloc(MAX_SPECIES_SIZE, sizeof(char)); + strcpy(character.species, species); + + character.patronus = (char *) calloc(MAX_PATRONUS_SIZE, sizeof(char)); + strcpy(character.patronus, patronus); + + character.hogwartsStaff = hogwartsStaff; + character.hogwartsStudent = hogwartsStudent; + + character.actorName = (char *) calloc(MAX_ACTOR_NAME_SIZE, sizeof(char)); + strcpy(character.actorName, actorName); + + character.alive = alive; + character.birthDate = birthDate; + character.yearOfBirth = yearOfBirth; + + character.eyeColour = (char *) calloc(MAX_EYE_COLOUR_SIZE, sizeof(char)); + strcpy(character.eyeColour, eyeColour); + + character.gender = (char *) calloc(MAX_GENDER_SIZE, sizeof(char)); + strcpy(character.gender, gender); + + character.hairColour = (char *) calloc(MAX_HAIR_COLOUR_SIZE, sizeof(char)); + strcpy(character.hairColour, hairColour); + + character.wizard = wizard; + + return character; +} + +Character *character_clone(Character *character) { + + Character *clone = (Character *) malloc(sizeof(Character)); + + clone -> id = (char *) calloc(MAX_UUID_SIZE, sizeof(char)); + strcpy(clone -> id, character -> id); + + clone -> name = (char *) calloc(MAX_NAME_SIZE, sizeof(char)); + strcpy(clone -> name, character -> name); + + for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) { + + clone -> alternateNames[i] = (char *) calloc(MAX_ALTERNATE_NAME_SIZE, sizeof(char)); + strcpy(clone -> alternateNames[i], character -> alternateNames[i]); + } + + clone -> house = (char *) calloc(MAX_HOUSE_SIZE, sizeof(char)); + strcpy(clone -> house, character -> house); + + clone -> ancestry = (char *) calloc(MAX_ANCESTRY_SIZE, sizeof(char)); + strcpy(clone -> ancestry, character -> ancestry); + + clone -> species = (char *) calloc(MAX_SPECIES_SIZE, sizeof(char)); + strcpy(clone -> species, character -> species); + + clone -> patronus = (char *) calloc(MAX_PATRONUS_SIZE, sizeof(char)); + strcpy(clone -> patronus, character -> patronus); + + clone -> hogwartsStaff = character -> hogwartsStaff; + clone -> hogwartsStudent = character -> hogwartsStudent; + + clone -> actorName = (char *) calloc(MAX_ACTOR_NAME_SIZE, sizeof(char)); + strcpy(clone -> actorName, character -> actorName); + + clone -> alive = character -> alive; + clone -> birthDate = character -> birthDate; + clone -> yearOfBirth = character -> yearOfBirth; + + clone -> eyeColour = (char *) calloc(MAX_EYE_COLOUR_SIZE, sizeof(char)); + strcpy(clone -> eyeColour, character -> eyeColour); + + clone -> gender = (char *) calloc(MAX_GENDER_SIZE, sizeof(char)); + strcpy(clone -> gender, character -> gender); + + clone -> hairColour = (char *) calloc(MAX_HAIR_COLOUR_SIZE, sizeof(char)); + strcpy(clone -> hairColour, character -> hairColour); + + clone -> wizard = character -> wizard; + + return clone; +} + +void character_print(Character *character) { + + printf("[%s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s]\n", + + character_getId(character), + character_getName(character), + character_getAlternateNames(character), + character_getHouse(character), + character_getAncestry(character), + character_getSpecies(character), + character_getPatronus(character), + character_getHogwartsStaff(character) ? "true" : "false", + character_getHogwartsStudent(character) ? "true" : "false", + character_getActorName(character), + character_getAlive(character) ? "true" : "false", + character_getBirthDate(character), + character_getYearOfBirth(character), + character_getEyeColour(character), + character_getGender(character), + character_getHairColour(character), + character_getWizard(character) ? "true" : "false" + ); +} + +Character character_read(char *line) { + + Character character = character_newBlank(); + + char *substringStart = line; + char *substringEnd = NULL; + char attribute[MAX_LINE_SIZE]; + + // Get id + proccess_attribute(attribute, &substringStart, &substringEnd, true, false); + character_setId(&character, attribute); + + // Get name + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setName(&character, attribute); + + // Get alternate names + proccess_attribute(attribute, &substringStart, &substringEnd, false, true); + character_setAlternateNames(&character, attribute); + + // Get house + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setHouse(&character, attribute); + + // Get ancestry + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setAncestry(&character, attribute); + + // Get species + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setSpecies(&character, attribute); + + // Get patronus + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setPatronus(&character, attribute); + + // Get hogwarts staff + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setHogwartsStaff(&character, strcmp(attribute, "VERDADEIRO") == 0); + + // Get hogwarts student + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setHogwartsStudent(&character, strcmp(attribute, "VERDADEIRO") == 0); + + // Get actor name + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setActorName(&character, attribute); + + // Get alive + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setAlive(&character, strcmp(attribute, "VERDADEIRO") == 0); + + // Get birth date + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setBirthDate(&character, attribute); + + // Get year of birth + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setYearOfBirth(&character, atoi(attribute) == 0 ? -1 : atoi(attribute)); + + // Get eye colour + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setEyeColour(&character, attribute); + + // Get gender + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setGender(&character, attribute); + + // Get hair colour + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setHairColour(&character, attribute); + + // Get wizard + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + + // Clean \n from the end of the string + if(attribute[strlen(attribute) - 1] == '\n' || attribute[strlen(attribute) - 1] == '\r') attribute[strlen(attribute) - 1] = '\0'; + + character_setWizard(&character, strcmp(attribute, "VERDADEIRO") == 0); + return character; +} + +Character *character_searchById(char *id) { + + for(int i = 0; i < charactersLength; i++) { + + if(!strcmp(characters[i].id, id)) return &characters[i]; + } + return NULL; +} + +// General +void startCharacters() { + + // Open file + FILE *fp; + char *line = NULL; + size_t len = 0; + ssize_t read; + + fp = fopen(FILE_PATH, "r"); + + if(fp == NULL) { + + perror("x Error opening file"); + exit(EXIT_FAILURE); + } + + // Skip first line + getline(&line, &len, fp); + + // Read all lines + while((read = getline(&line, &len, fp)) != -1) { + + // Read character from line + Character character = character_read(line); + + characters[charactersLength++] = character; + + if(charactersLength >= MAX_CHARACTERS) { + + perror("x Max characters reached"); + exit(EXIT_FAILURE); + } + } + + // Close file and free memory + fclose(fp); + + if(line) free(line); +} + +// ---------------------------------------------------------------------------------------------------- // + +// Main +int main() { + + // ----------------------------------------------------------------- // + + // #1 - Start - Read all characters in CSV file + startCharacters(); + + // ----------------------------------------------------------------- // + + // #2 - Read input and print characters from pub.in id entries and add to mainCharacters array + + // Initialize mainCharacters array + Character mainCharacters[MAX_CHARACTERS]; + int m = 0; + + char id[MAX_UUID_SIZE]; + scanf(" %[^\n]s", id); + + while(true) { + + // Clean \n from the end of the string + if(id[strlen(id) - 1] == '\n' || id[strlen(id) - 1] == '\r') id[strlen(id) - 1] = '\0'; + + if(isEnd(id)) break; + else { + + Character *character = character_searchById(id); + + if(character) mainCharacters[m++] = *character; + + // ------------------------- // + + scanf(" %[^\n]s", id); + } + } + + // ----------------------------------------------------------------- // + + // #3 - Order mainCharacters array by key "dateOfBirth" using partial insertion sort + + // Start benchmark + clock_t startTime = clock(); + int comparisons = 0; + + // ----------------- // + + // Partial insertion sort with k = 10, order by dateOfBirth, including day, month and year, and name as secondary key + int k = 10; + + for(int i = 1; i < m; i++) { + + Character key = mainCharacters[i]; + int j = i - 1; + + while(j >= 0 && mainCharacters[j].birthDate.year > key.birthDate.year) { + + mainCharacters[j + 1] = mainCharacters[j]; + j--; + comparisons += 2; + } + + while(j >= 0 && mainCharacters[j].birthDate.year == key.birthDate.year && mainCharacters[j].birthDate.month > key.birthDate.month) { + + mainCharacters[j + 1] = mainCharacters[j]; + j--; + comparisons += 2; + } + + while(j >= 0 && mainCharacters[j].birthDate.year == key.birthDate.year && mainCharacters[j].birthDate.month == key.birthDate.month && mainCharacters[j].birthDate.day > key.birthDate.day) { + + mainCharacters[j + 1] = mainCharacters[j]; + j--; + comparisons += 2; + } + + // In case of a tie, order by name + while(j >= 0 && mainCharacters[j].birthDate.year == key.birthDate.year && mainCharacters[j].birthDate.month == key.birthDate.month && mainCharacters[j].birthDate.day == key.birthDate.day && strcmp(character_getName(&mainCharacters[j]), character_getName(&key)) > 0) { + + mainCharacters[j + 1] = mainCharacters[j]; + j--; + comparisons += 2; + } + + mainCharacters[j + 1] = key; + } + + // ----------------- // + + // Save benchmark in file + FILE *fp = fopen("753045_insercaoParcial.txt", "w"); + fprintf(fp, "753045\t%.0fms\t%d", (double)(clock() - startTime) / CLOCKS_PER_SEC * 1000.0, comparisons); + fclose(fp); + + // ----------------- // + + // Print mainCharacters array + for(int i = 0; i < k; i++) character_print(&mainCharacters[i]); + + // ----------------- // + + return 0; +} \ No newline at end of file diff --git a/tps/fonte/tp02/TP02Q16 - Ordenação PARCIAL por Inserção em C/pedro.out b/tps/fonte/tp02/TP02Q16 - Ordenação PARCIAL por Inserção em C/pedro.out new file mode 100644 index 0000000..f7e99ae --- /dev/null +++ b/tps/fonte/tp02/TP02Q16 - Ordenação PARCIAL por Inserção em C/pedro.out @@ -0,0 +1,10 @@ +[d59691a4-f830-4eb0-a819-a0fb00b7e80f ## Garrick Ollivander ## {} ## Hufflepuff ## muggle ## human ## N/A ## false ## false ## John Hurt ## true ## 04-10-1925 ## 1954 ## pale, silvery ## male ## brown ## true] +[ca3827f0-375a-4891-aaa5-f5e8a5bad225 ## Minerva McGonagall ## {} ## Gryffindor ## half-blood ## human ## tabby cat ## true ## false ## Dame Maggie Smith ## true ## 04-10-1925 ## 1925 ## grey ## female ## black ## true] +[d58e7249-19d1-40bd-a43f-1da0497fe8aa ## Dolores Umbridge ## {} ## Slytherin ## half-blood ## human ## persian cat ## true ## false ## Imelda Staunton ## true ## 31-12-1926 ## 1926 ## brown ## female ## grey ## true] +[5a4c95db-947d-4914-a631-41e8d466328e ## Kendra Dumbledore ## {} ## Slytherin ## muggleborn ## human ## N/A ## false ## false ## N/A ## false ## 31-12-1926 ## 1925 ## dark ## female ## black ## true] +[efa802c8-ae18-4ae1-a524-49df21d05939 ## Lord Voldemort ## {Tom Riddle, Tom Marvolo Riddle, You-Know-Who, He Who Must Not Be Named, the Dark Lord, the Heir of Slytherin} ## Slytherin ## half-blood ## human ## N/A ## false ## false ## Ralph Fiennes ## false ## 31-12-1926 ## 1926 ## Scarlet ## male ## bald ## true] +[b0620914-858d-46fc-8e6d-033c565e138b ## Mrs Norris ## {} ## Slytherin ## pure-blood ## cat ## N/A ## true ## false ## Maxime, Alanis and Tommy the cats ## true ## 31-12-1926 ## 1954 ## yellow ## female ## brown ## false] +[20354d7a-e4fe-47af-8ff6-187bca92f3f9 ## Pandora Lovegood ## {} ## Ravenclaw ## quarter-veela ## human ## N/A ## false ## false ## N/A ## false ## 31-12-1926 ## 1959 ## dark ## female ## white ## true] +[fed624df-56d9-495e-9ad4-ea77000957e8 ## Petunia Dursley ## {} ## Slytherin ## muggle ## human ## N/A ## false ## false ## Fiona Shaw ## true ## 31-12-1926 ## 1954 ## yellow ## female ## blonde ## false] +[1413e1b3-2903-4a47-a2d5-e8abc5ce8014 ## Seamus Finnigan ## {O Flaherty, Seamus Finnegan} ## Gryffindor ## half-blood ## human ## N/A ## false ## true ## Devon Murray ## true ## 31-12-1926 ## 1960 ## amber ## male ## sandy ## true] +[57fe29d4-312a-4711-bd9a-c320253d9176 ## Victoire Weasley ## {} ## Slytherin ## pure-blood ## human ## N/A ## false ## true ## N/A ## true ## 31-12-1926 ## 1926 ## grey ## female ## blonde ## true] diff --git a/tps/fonte/tp02/TP02Q16 - Ordenação PARCIAL por Inserção em C/pub.in b/tps/fonte/tp02/TP02Q16 - Ordenação PARCIAL por Inserção em C/pub.in new file mode 100644 index 0000000..a782d8c --- /dev/null +++ b/tps/fonte/tp02/TP02Q16 - Ordenação PARCIAL por Inserção em C/pub.in @@ -0,0 +1,28 @@ +9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8 +1413e1b3-2903-4a47-a2d5-e8abc5ce8014 +ca3827f0-375a-4891-aaa5-f5e8a5bad225 +36bfefd0-e0bb-4d11-be98-d1ef6117a77a +20354d7a-e4fe-47af-8ff6-187bca92f3f9 +57fe29d4-312a-4711-bd9a-c320253d9176 +b415c867-1cff-455e-b194-748662ac2cca +5a4c95db-947d-4914-a631-41e8d466328e +861c4cde-2f0f-4796-8d8f-9492e74b2573 +2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f +41cd0bbe-a943-431b-9bde-bb2cad3491a1 +2a0615de-8aa4-41e7-9504-dd875f5f3f01 +11b5ca88-64ad-41a4-9f36-317b66c290af +eaea5eb3-48a3-41c6-9ea5-c695299bab16 +0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3 +b0620914-858d-46fc-8e6d-033c565e138b +6b59be3f-e527-422d-959d-79fcdb3b24eb +fed624df-56d9-495e-9ad4-ea77000957e8 +d58e7249-19d1-40bd-a43f-1da0497fe8aa +3a0fe4df-2e40-4541-8d7f-13586f0b9294 +6afb1960-febd-418d-b604-e50c1b59459b +efa802c8-ae18-4ae1-a524-49df21d05939 +933787c2-51e3-4eac-8a85-ab332cac0456 +94c993f6-a443-4408-b908-6e78e223e8ac +00434cd3-fcc7-44c7-8f98-7368415b4206 +7614cf6e-689e-47ac-a976-b1e9997637e9 +d59691a4-f830-4eb0-a819-a0fb00b7e80f +FIM \ No newline at end of file diff --git a/tps/fonte/tp02/TP02Q17 - Heapsort PARCIAL em C/753045_heapsortParcial.txt b/tps/fonte/tp02/TP02Q17 - Heapsort PARCIAL em C/753045_heapsortParcial.txt new file mode 100644 index 0000000..abbe2c5 --- /dev/null +++ b/tps/fonte/tp02/TP02Q17 - Heapsort PARCIAL em C/753045_heapsortParcial.txt @@ -0,0 +1 @@ +753045 0ms 746 \ No newline at end of file diff --git a/tps/fonte/tp02/TP02Q17 - Heapsort PARCIAL em C/Character b/tps/fonte/tp02/TP02Q17 - Heapsort PARCIAL em C/Character new file mode 100644 index 0000000000000000000000000000000000000000..48947a3d2479e56164c1dbf422d4fdb1e1cbe8af GIT binary patch literal 31976 zcmeHweRx#Wwf9LvAW+Dp3Q7^n_<_a`LZpIl5zRmb4>V{Lf{5T`NG42bKE`B5gMy6> zu{j<_(_ZP5df|CvYx}16y|+bQMQ-cI07{~@ZM5o*UfQNDZ6^{nwP<56GWWOE-uuis zlT5_h=ia|^crxp(-}>15tiAWz`|LB!`Kq^MWoAZ(rj9J_8jZO2&kLkT3W5=N0#c+c z);@>NFKXv%6TwelOvsA_fm9s9Oef8ecorzxbxCbgkNyr#vA<}JFips~JS0bloYRD}A}b=Q|^Z~EW* z)|WlsFJJW0_NlL(_uAiP4L@-U)tl--{z!)o(wD_Codurc;=1m`nWhm&$E$&#%a#y) z>gT@#I3pDVPu*%X{59Zi{G}dL{%H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6u`X{wfZOCDjyaagn)ZpoToj)A79{t#MdA3t$WFnR5`0AZg4!^0d zgYs22*876~x=MTrAXz3Vx4gO`94tnecxBWLsC{L1oez<+M~^PSSugytQZ$Nc_b%X4SKHnB} zGBGxjmI!UKH;I_>OSc>vDHVXtE$AXQRB?cu)kqynwhB00j&@=2dvo>O{sw!1fmv6^ zFDP?FQoO9p0ih$|-DD0ZSujOpL}#~+SBK{4*XRhCbNE4I^=F4CnNS=U$CO@==BTRk zwQH8>Dh)NKm!??=<-w#so-KF``NL^c;J{N;eJ7HOcnzCFjot(20DQi6YgVYNPp?)R z^?hl{3ecsqclvHjbroI|XZ1~pYy-*h&NoMqNefg}_5-b;aa(QJzXcS{$~36X@XoqG zLr5#As}BbXw$?=ownVD&g2(Dg33-;UniuwO)mRqvHwLwW%58PJv)ilvPDR}g?d1Ksf{Ss2|Bza*%2)fMimLLloaIC9ze`rT z9w+n+K-sJPQ$^dP0LwSNc4aS@Yub9?xMj2K)jq4D>Nx{c_@G$ZkK4e|p1vwy?Z+xg z@u5T2U+F1&J7n~XK=EqdR#ABy$?qPevQ_=bCmna=kJ?1ptNmR?W029I%2)R4`gcM` z_RC2LhuR0WsJvjZr=@$U{e!?rh2qNxZmsQ9F83ukvi%K6?DlSHuV}d}UpYu|uRWY% z-z;w&6;<_B_KH_@Z;HLTuU7Q8a^FSStMV29-4uKETujl)N>NgZwf*=37`jB&erjJr z(ZZDLSM^pt9#64PeP1u_)7g*I@}Ez!SNrISs(!3|q?Z2@1hn@|V~7s5zpCEjo2svP zMc;sp3xCR9?aL3zjTWUxzUffBqQ3=3eNWk|eP8vSH +#include +#include +#include +#include + +// ---------------------------------------------------------------------------------------------------- // + +// Constants +#define MAX_CHARACTERS 405 +#define FILE_PATH "/tmp/characters.csv" + +#define MAX_UUID_SIZE 37 +#define MAX_NAME_SIZE 30 +#define MAX_ALTERNATE_NAMES 10 +#define MAX_ALTERNATE_NAME_SIZE 35 +#define MAX_HOUSE_SIZE 15 +#define MAX_ANCESTRY_SIZE 15 +#define MAX_SPECIES_SIZE 20 +#define MAX_PATRONUS_SIZE 25 +#define MAX_ACTOR_NAME_SIZE 35 +#define MAX_EYE_COLOUR_SIZE 10 +#define MAX_GENDER_SIZE 10 +#define MAX_HAIR_COLOUR_SIZE 10 + +#define MAX_LINE_SIZE 300 + +// ---------------------------------------------------------------------------------------------------- // + +// Structs +typedef struct Date { + + int day; + int month; + int year; +} Date; + +typedef struct Character { + + char *id; + char *name; + char *alternateNames[MAX_ALTERNATE_NAMES]; + char *house; + char *ancestry; + char *species; + char *patronus; + bool hogwartsStaff; + bool hogwartsStudent; + char *actorName; + bool alive; + Date birthDate; + int yearOfBirth; + char *eyeColour; + char *gender; + char *hairColour; + bool wizard; +} Character; + +// ---------------------------------------------------------------------------------------------------- // + +// Global variables +Character characters[MAX_CHARACTERS]; +int charactersLength = 0; + +// ---------------------------------------------------------------------------------------------------- // + +// Functions +bool isEnd(char* line) { return line[0] == 'F' && line[1] == 'I' && line[2] == 'M'; } + +void substring(char *string, char *stringStart, int length) { + + strncpy(string, stringStart, length); + string[length] = '\0'; +} + +void proccess_attribute(char *attribute, char **substringStart, char **substringEnd, bool isFirstAttribute, bool isStringArray) { + + // Skip first comma + if(!isFirstAttribute) { + + if(*substringEnd != NULL) *substringStart = *substringEnd + 1; + else *substringStart = *substringEnd; + } + + if(!isStringArray) { + + if((*substringStart)[0] == '"') { + + *substringStart = *substringStart + 1; + *substringEnd = strchr(*substringStart, '"'); + } + else *substringEnd = strchr(*substringStart, ';'); + + // Get substring + if(*substringEnd) { + + substring(attribute, *substringStart, *substringEnd - *substringStart); + + if(*substringEnd[0] == '"') *substringEnd = *substringEnd + 1; + } + else strcpy(attribute, *substringStart); + + // Set default value if attribute is empty + if(strcmp(attribute, "") == 0 || attribute[0] == '\n' || attribute[0] == '\r' || attribute[0] == '\0') strcpy(attribute, "N/A"); + + // Clean \n from the end of the string + if(attribute[strlen(attribute) - 1] == '\n' || attribute[strlen(attribute) - 1] == '\r') attribute[strlen(attribute) - 1] = '\0'; + } + else { + + // Check if the first character is a [ + if((*substringStart)[0] == '[') { + + *substringStart = *substringStart + 1; + + if((*substringStart)[0] == ']') strcpy(attribute, ""); // Case: [] + else { + + char *tempConcat = (char *) calloc(MAX_LINE_SIZE, sizeof(char)); + + *substringStart = *substringStart - 1; + + while(1) { + + *substringStart = *substringStart + 1; + + if((*substringStart)[0] == ';') break; + else if((*substringStart)[0] == '\'') { // Case: "['example', 'example']" + + *substringStart = *substringStart + 1; + *substringEnd = strchr(*substringStart, '\''); + + // Get substring + if(*substringEnd) { + + // Create tmp name + char tmp[MAX_LINE_SIZE]; + substring(tmp, *substringStart, *substringEnd - *substringStart); + + // Concat tempConcat with tmp + strcat(tempConcat, tmp); + strcat(tempConcat, ", "); + + *substringStart = *substringEnd + 1; + } + } + } + + // Get substring + strcpy(attribute, tempConcat); + + // Clean "attribute" removing last 2 characters + attribute[strlen(attribute) - 2] = '\0'; + } + } + + *substringEnd = strchr(*substringStart, ';'); + } +} + +// ---------------------------------------------------------------------------------------------------- // + +// Methods implementations + +// Gets +char *character_getId(Character *character) { return character -> id; } +char *character_getName(Character *character) { return character -> name; } +char *character_getHouse(Character *character) { return character -> house; } +char *character_getAncestry(Character *character) { return character -> ancestry; } +char *character_getSpecies(Character *character) { return character -> species; } +char *character_getPatronus(Character *character) { return character -> patronus; } +bool character_getHogwartsStaff(Character *character) { return character -> hogwartsStaff; } +bool character_getHogwartsStudent(Character *character) { return character -> hogwartsStudent; } +char *character_getActorName(Character *character) { return character -> actorName; } +bool character_getAlive(Character *character) { return character -> alive; } +char *character_getEyeColour(Character *character) { return character -> eyeColour; } +char *character_getGender(Character *character) { return character -> gender; } +char *character_getHairColour(Character *character) { return character -> hairColour; } +bool character_getWizard(Character *character) { return character -> wizard; } + +char *character_getYearOfBirth(Character *character) { + + // "N/A" if yearOfBirth is -1 + char *yearOfBirth = (char *) calloc(15, sizeof(char)); + strcpy(yearOfBirth, "N/A"); + + if(character -> yearOfBirth != -1) sprintf(yearOfBirth, "%d", character -> yearOfBirth); + return yearOfBirth; +} + +char *character_getBirthDate(Character *character) { + + // "N/A" if day, month and year are -1 + char *birthDate = (char *) calloc(15, sizeof(char)); + strcpy(birthDate, "N/A"); + + if(character -> birthDate.day != -1 && character -> birthDate.month != -1 && character -> birthDate.year != -1) { + + sprintf(birthDate, "%02d-%02d-%04d", character -> birthDate.day, character -> birthDate.month, character -> birthDate.year); + } + + return birthDate; +} + +char *character_getAlternateNames(Character *character) { + + // Concatenate all alternate names with a comma + char *alternateNames = (char *) calloc(MAX_ALTERNATE_NAME_SIZE * MAX_ALTERNATE_NAMES, sizeof(char)); + + alternateNames[0] = '{'; + + for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) { + + if(strcmp(character -> alternateNames[i], "") != 0) { + + strcat(alternateNames, character -> alternateNames[i]); + + if(strcmp(character -> alternateNames[i + 1], "") != 0) strcat(alternateNames, ", "); + } + } + + strcat(alternateNames, "}"); + + return alternateNames; +} + +// Sets +void character_setId(Character *character, char *id) { strcpy(character -> id, id); } +void character_setName(Character *character, char *name) { strcpy(character -> name, name); } +void character_setHouse(Character *character, char *house) { strcpy(character -> house, house); } +void character_setAncestry(Character *character, char *ancestry) { strcpy(character -> ancestry, ancestry); } +void character_setSpecies(Character *character, char *species) { strcpy(character -> species, species); } +void character_setPatronus(Character *character, char *patronus) { strcpy(character -> patronus, patronus); } +void character_setHogwartsStaff(Character *character, bool hogwartsStaff) { character -> hogwartsStaff = hogwartsStaff; } +void character_setHogwartsStudent(Character *character, bool hogwartsStudent) { character -> hogwartsStudent = hogwartsStudent; } +void character_setActorName(Character *character, char *actorName) { strcpy(character -> actorName, actorName); } +void character_setAlive(Character *character, bool alive) { character -> alive = alive; } +void character_setYearOfBirth(Character *character, int yearOfBirth) { character -> yearOfBirth = yearOfBirth; } +void character_setEyeColour(Character *character, char *eyeColour) { strcpy(character -> eyeColour, eyeColour); } +void character_setGender(Character *character, char *gender) { strcpy(character -> gender, gender); } +void character_setHairColour(Character *character, char *hairColour) { strcpy(character -> hairColour, hairColour); } +void character_setWizard(Character *character, bool wizard) { character -> wizard = wizard; } + +void character_setBirthDate(Character *character, char *birthDate) { + + // Explode birthDate in format DD-MM-YYYY if in format DD-MM-YYYY + if(strlen(birthDate) >= 8 && strlen(birthDate) <= 10) { + + char *token = strtok(birthDate, "-"); + + character -> birthDate.day = atoi(token); + token = strtok(NULL, "-"); + character -> birthDate.month = atoi(token); + token = strtok(NULL, "-"); + character -> birthDate.year = atoi(token); + } +} + +void character_setAlternateNames(Character *character, char *alternateNames) { + + // Copy names to a temporary variable + char tempNames[MAX_ALTERNATE_NAME_SIZE * MAX_ALTERNATE_NAMES]; + strcpy(tempNames, alternateNames); + + // Separate names by comma + char *token = strtok(tempNames, ","); + int i = 0; + + // Copy names to the character + while (token != NULL && i < MAX_ALTERNATE_NAMES) { + + while (*token == ' ') token++; + + int len = strlen(token); + + while (len > 0 && token[len - 1] == ' ') { + + token[len - 1] = '\0'; + len--; + } + + strcpy(character -> alternateNames[i++], token); + token = strtok(NULL, ","); + } +} + +// Class +Character character_newBlank() { + + Character character; + + character.id = (char *) calloc(MAX_UUID_SIZE, sizeof(char)); + strcpy(character.id, ""); + + character.name = (char *) calloc(MAX_NAME_SIZE, sizeof(char)); + strcpy(character.name, ""); + + for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) { + + character.alternateNames[i] = (char *) calloc(MAX_ALTERNATE_NAME_SIZE, sizeof(char)); + strcpy(character.alternateNames[i], ""); + } + + character.house = (char *) calloc(MAX_HOUSE_SIZE, sizeof(char)); + strcpy(character.house, ""); + + character.ancestry = (char *) calloc(MAX_ANCESTRY_SIZE, sizeof(char)); + strcpy(character.ancestry, ""); + + character.species = (char *) calloc(MAX_SPECIES_SIZE, sizeof(char)); + strcpy(character.species, ""); + + character.patronus = (char *) calloc(MAX_PATRONUS_SIZE, sizeof(char)); + strcpy(character.patronus, ""); + + character.hogwartsStaff = false; + character.hogwartsStudent = false; + + character.actorName = (char *) calloc(MAX_ACTOR_NAME_SIZE, sizeof(char)); + strcpy(character.actorName, ""); + + character.alive = false; + + character.birthDate.day = -1; + character.birthDate.month = -1; + character.birthDate.year = -1; + + character.yearOfBirth = -1; + + character.eyeColour = (char *) calloc(MAX_EYE_COLOUR_SIZE, sizeof(char)); + strcpy(character.eyeColour, ""); + + character.gender = (char *) calloc(MAX_GENDER_SIZE, sizeof(char)); + strcpy(character.gender, ""); + + character.hairColour = (char *) calloc(MAX_HAIR_COLOUR_SIZE, sizeof(char)); + strcpy(character.hairColour, ""); + + character.wizard = false; + + return character; +} + +Character character_new(char *id, char *name, char *house, char *ancestry, char *species, char *patronus, bool hogwartsStaff, bool hogwartsStudent, char *actorName, bool alive, Date birthDate, int yearOfBirth, char *eyeColour, char *gender, char *hairColour, bool wizard) { + + Character character; + + character.id = (char *) calloc(MAX_UUID_SIZE, sizeof(char)); + strcpy(character.id, id); + + character.name = (char *) calloc(MAX_NAME_SIZE, sizeof(char)); + strcpy(character.name, name); + + for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) { + + character.alternateNames[i] = (char *) calloc(MAX_ALTERNATE_NAME_SIZE, sizeof(char)); + strcpy(character.alternateNames[i], ""); + } + + character.house = (char *) calloc(MAX_HOUSE_SIZE, sizeof(char)); + strcpy(character.house, house); + + character.ancestry = (char *) calloc(MAX_ANCESTRY_SIZE, sizeof(char)); + strcpy(character.ancestry, ancestry); + + character.species = (char *) calloc(MAX_SPECIES_SIZE, sizeof(char)); + strcpy(character.species, species); + + character.patronus = (char *) calloc(MAX_PATRONUS_SIZE, sizeof(char)); + strcpy(character.patronus, patronus); + + character.hogwartsStaff = hogwartsStaff; + character.hogwartsStudent = hogwartsStudent; + + character.actorName = (char *) calloc(MAX_ACTOR_NAME_SIZE, sizeof(char)); + strcpy(character.actorName, actorName); + + character.alive = alive; + character.birthDate = birthDate; + character.yearOfBirth = yearOfBirth; + + character.eyeColour = (char *) calloc(MAX_EYE_COLOUR_SIZE, sizeof(char)); + strcpy(character.eyeColour, eyeColour); + + character.gender = (char *) calloc(MAX_GENDER_SIZE, sizeof(char)); + strcpy(character.gender, gender); + + character.hairColour = (char *) calloc(MAX_HAIR_COLOUR_SIZE, sizeof(char)); + strcpy(character.hairColour, hairColour); + + character.wizard = wizard; + + return character; +} + +Character *character_clone(Character *character) { + + Character *clone = (Character *) malloc(sizeof(Character)); + + clone -> id = (char *) calloc(MAX_UUID_SIZE, sizeof(char)); + strcpy(clone -> id, character -> id); + + clone -> name = (char *) calloc(MAX_NAME_SIZE, sizeof(char)); + strcpy(clone -> name, character -> name); + + for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) { + + clone -> alternateNames[i] = (char *) calloc(MAX_ALTERNATE_NAME_SIZE, sizeof(char)); + strcpy(clone -> alternateNames[i], character -> alternateNames[i]); + } + + clone -> house = (char *) calloc(MAX_HOUSE_SIZE, sizeof(char)); + strcpy(clone -> house, character -> house); + + clone -> ancestry = (char *) calloc(MAX_ANCESTRY_SIZE, sizeof(char)); + strcpy(clone -> ancestry, character -> ancestry); + + clone -> species = (char *) calloc(MAX_SPECIES_SIZE, sizeof(char)); + strcpy(clone -> species, character -> species); + + clone -> patronus = (char *) calloc(MAX_PATRONUS_SIZE, sizeof(char)); + strcpy(clone -> patronus, character -> patronus); + + clone -> hogwartsStaff = character -> hogwartsStaff; + clone -> hogwartsStudent = character -> hogwartsStudent; + + clone -> actorName = (char *) calloc(MAX_ACTOR_NAME_SIZE, sizeof(char)); + strcpy(clone -> actorName, character -> actorName); + + clone -> alive = character -> alive; + clone -> birthDate = character -> birthDate; + clone -> yearOfBirth = character -> yearOfBirth; + + clone -> eyeColour = (char *) calloc(MAX_EYE_COLOUR_SIZE, sizeof(char)); + strcpy(clone -> eyeColour, character -> eyeColour); + + clone -> gender = (char *) calloc(MAX_GENDER_SIZE, sizeof(char)); + strcpy(clone -> gender, character -> gender); + + clone -> hairColour = (char *) calloc(MAX_HAIR_COLOUR_SIZE, sizeof(char)); + strcpy(clone -> hairColour, character -> hairColour); + + clone -> wizard = character -> wizard; + + return clone; +} + +void character_print(Character *character) { + + printf("[%s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s]\n", + + character_getId(character), + character_getName(character), + character_getAlternateNames(character), + character_getHouse(character), + character_getAncestry(character), + character_getSpecies(character), + character_getPatronus(character), + character_getHogwartsStaff(character) ? "true" : "false", + character_getHogwartsStudent(character) ? "true" : "false", + character_getActorName(character), + character_getAlive(character) ? "true" : "false", + character_getBirthDate(character), + character_getYearOfBirth(character), + character_getEyeColour(character), + character_getGender(character), + character_getHairColour(character), + character_getWizard(character) ? "true" : "false" + ); +} + +Character character_read(char *line) { + + Character character = character_newBlank(); + + char *substringStart = line; + char *substringEnd = NULL; + char attribute[MAX_LINE_SIZE]; + + // Get id + proccess_attribute(attribute, &substringStart, &substringEnd, true, false); + character_setId(&character, attribute); + + // Get name + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setName(&character, attribute); + + // Get alternate names + proccess_attribute(attribute, &substringStart, &substringEnd, false, true); + character_setAlternateNames(&character, attribute); + + // Get house + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setHouse(&character, attribute); + + // Get ancestry + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setAncestry(&character, attribute); + + // Get species + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setSpecies(&character, attribute); + + // Get patronus + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setPatronus(&character, attribute); + + // Get hogwarts staff + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setHogwartsStaff(&character, strcmp(attribute, "VERDADEIRO") == 0); + + // Get hogwarts student + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setHogwartsStudent(&character, strcmp(attribute, "VERDADEIRO") == 0); + + // Get actor name + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setActorName(&character, attribute); + + // Get alive + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setAlive(&character, strcmp(attribute, "VERDADEIRO") == 0); + + // Get birth date + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setBirthDate(&character, attribute); + + // Get year of birth + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setYearOfBirth(&character, atoi(attribute) == 0 ? -1 : atoi(attribute)); + + // Get eye colour + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setEyeColour(&character, attribute); + + // Get gender + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setGender(&character, attribute); + + // Get hair colour + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + character_setHairColour(&character, attribute); + + // Get wizard + proccess_attribute(attribute, &substringStart, &substringEnd, false, false); + + // Clean \n from the end of the string + if(attribute[strlen(attribute) - 1] == '\n' || attribute[strlen(attribute) - 1] == '\r') attribute[strlen(attribute) - 1] = '\0'; + + character_setWizard(&character, strcmp(attribute, "VERDADEIRO") == 0); + return character; +} + +Character *character_searchById(char *id) { + + for(int i = 0; i < charactersLength; i++) { + + if(!strcmp(characters[i].id, id)) return &characters[i]; + } + return NULL; +} + +// General +void startCharacters() { + + // Open file + FILE *fp; + char *line = NULL; + size_t len = 0; + ssize_t read; + + fp = fopen(FILE_PATH, "r"); + + if(fp == NULL) { + + perror("x Error opening file"); + exit(EXIT_FAILURE); + } + + // Skip first line + getline(&line, &len, fp); + + // Read all lines + while((read = getline(&line, &len, fp)) != -1) { + + // Read character from line + Character character = character_read(line); + + characters[charactersLength++] = character; + + if(charactersLength >= MAX_CHARACTERS) { + + perror("x Max characters reached"); + exit(EXIT_FAILURE); + } + } + + // Close file and free memory + fclose(fp); + + if(line) free(line); +} + +// Heapify function +void heapify(Character *characters, int n, int i, int *comparisons) { + + int largest = i; + int l = 2 * i + 1; + int r = 2 * i + 2; + + if(l < n) { + + // Compare hairColour of characters + int comparison = strcmp(character_getHairColour(&characters[l]), character_getHairColour(&characters[largest])); + + // In draw case, compare names + if(comparison == 0) comparison = strcmp(character_getName(&characters[l]), character_getName(&characters[largest])); + if(comparison > 0) largest = l; + + (*comparisons) += 2; + } + + if(r < n) { + + // Compare hairColour of characters + int comparison = strcmp(character_getHairColour(&characters[r]), character_getHairColour(&characters[largest])); + + // In draw case, compare names + if(comparison == 0) comparison = strcmp(character_getName(&characters[r]), character_getName(&characters[largest])); + if(comparison > 0) largest = r; + + (*comparisons) += 2; + } + + if(largest != i) { + + Character aux = characters[i]; + characters[i] = characters[largest]; + characters[largest] = aux; + heapify(characters, n, largest, comparisons); + } + + (*comparisons) += 3; +} + +// ---------------------------------------------------------------------------------------------------- // + +// Main +int main() { + + // ----------------------------------------------------------------- // + + // #1 - Start - Read all characters in CSV file + startCharacters(); + + // ----------------------------------------------------------------- // + + // #2 - Read input and print characters from pub.in id entries and add to mainCharacters array + + // Initialize mainCharacters array + Character mainCharacters[MAX_CHARACTERS]; + int m = 0; + + char id[MAX_UUID_SIZE]; + scanf(" %[^\n]s", id); + + while(true) { + + // Clean \n from the end of the string + if(id[strlen(id) - 1] == '\n' || id[strlen(id) - 1] == '\r') id[strlen(id) - 1] = '\0'; + + if(isEnd(id)) break; + else { + + Character *character = character_searchById(id); + + if(character) mainCharacters[m++] = *character; + + // ------------------------- // + + scanf(" %[^\n]s", id); + } + } + + // ----------------------------------------------------------------- // + + // #3 - Order mainCharacters array by key "id" + + // Start benchmark + clock_t startTime = clock(); + int comparisons = 0; + + // ----------------- // + + // Heapsort with k = 10 + int k = 10; + + for(int i = m / 2 - 1; i >= 0; i--) { + + comparisons++; + heapify(mainCharacters, m, i, &comparisons); + } + + for(int i = m - 1; i >= 0; i--) { + + comparisons++; + + Character aux = mainCharacters[0]; + mainCharacters[0] = mainCharacters[i]; + mainCharacters[i] = aux; + + heapify(mainCharacters, i, 0, &comparisons); + } + + // ----------------- // + + // Save benchmark in file + FILE *fp = fopen("753045_heapsortParcial.txt", "w"); + fprintf(fp, "753045\t%.0fms\t%d", (double)(clock() - startTime) / CLOCKS_PER_SEC * 1000.0, comparisons); + fclose(fp); + + // ----------------- // + + // Print mainCharacters array + for(int i = 0; i < k; i++) character_print(&mainCharacters[i]); + + // ----------------- // + + return 0; +} \ No newline at end of file diff --git a/tps/fonte/tp02/TP02Q17 - Heapsort PARCIAL em C/pedro.out b/tps/fonte/tp02/TP02Q17 - Heapsort PARCIAL em C/pedro.out new file mode 100644 index 0000000..270b9dd --- /dev/null +++ b/tps/fonte/tp02/TP02Q17 - Heapsort PARCIAL em C/pedro.out @@ -0,0 +1,10 @@ +[efa802c8-ae18-4ae1-a524-49df21d05939 ## Lord Voldemort ## {Tom Riddle, Tom Marvolo Riddle, You-Know-Who, He Who Must Not Be Named, the Dark Lord, the Heir of Slytherin} ## Slytherin ## half-blood ## human ## N/A ## false ## false ## Ralph Fiennes ## false ## 31-12-1926 ## 1926 ## Scarlet ## male ## bald ## true] +[6afb1960-febd-418d-b604-e50c1b59459b ## Bellatrix Lestrange ## {Bella} ## Slytherin ## pure-blood ## human ## N/A ## false ## false ## Helena Bonham Carter ## false ## 06-02-1950 ## 1951 ## brown ## female ## black ## true] +[9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8 ## Harry Potter ## {The Boy Who Lived, The Chosen One, Undesirable No. 1, Potty} ## Gryffindor ## half-blood ## human ## stag ## false ## true ## Daniel Radcliffe ## true ## 31-07-1980 ## 1980 ## green ## male ## black ## true] +[5a4c95db-947d-4914-a631-41e8d466328e ## Kendra Dumbledore ## {} ## Slytherin ## muggleborn ## human ## N/A ## false ## false ## N/A ## false ## 31-12-1926 ## 1925 ## dark ## female ## black ## true] +[ca3827f0-375a-4891-aaa5-f5e8a5bad225 ## Minerva McGonagall ## {} ## Gryffindor ## half-blood ## human ## tabby cat ## true ## false ## Dame Maggie Smith ## true ## 04-10-1925 ## 1925 ## grey ## female ## black ## true] +[36bfefd0-e0bb-4d11-be98-d1ef6117a77a ## Rubeus Hagrid ## {Professor Hagrid, Hagger} ## Gryffindor ## half-blood ## half-giant ## N/A ## true ## false ## Robbie Coltrane ## true ## 06-12-1928 ## 1928 ## black ## male ## black ## true] +[2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f ## Sirius Black ## {Padfoot, Snuffles} ## Gryffindor ## pure-blood ## human ## hare ## false ## false ## Gary Oldman ## false ## 03-11-1959 ## 1959 ## grey ## male ## black ## true] +[0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3 ## Dobby ## {} ## Slytherin ## pure-blood ## house-elf ## N/A ## false ## false ## Toby Jones ## false ## 06-02-1950 ## 1950 ## green ## male ## blond ## false] +[7614cf6e-689e-47ac-a976-b1e9997637e9 ## Dudley Dursley ## {} ## Slytherin ## muggle ## human ## N/A ## false ## false ## Harry Melling ## true ## 23-06-1980 ## 1980 ## blue ## male ## blond ## false] +[41cd0bbe-a943-431b-9bde-bb2cad3491a1 ## Lily Potter ## {} ## Gryffindor ## muggleborn ## human ## N/A ## false ## false ## Geraldine Somerville ## false ## 30-01-1960 ## 1960 ## green ## female ## blond ## true] diff --git a/tps/fonte/tp02/TP02Q17 - Heapsort PARCIAL em C/pub.in b/tps/fonte/tp02/TP02Q17 - Heapsort PARCIAL em C/pub.in new file mode 100644 index 0000000..a782d8c --- /dev/null +++ b/tps/fonte/tp02/TP02Q17 - Heapsort PARCIAL em C/pub.in @@ -0,0 +1,28 @@ +9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8 +1413e1b3-2903-4a47-a2d5-e8abc5ce8014 +ca3827f0-375a-4891-aaa5-f5e8a5bad225 +36bfefd0-e0bb-4d11-be98-d1ef6117a77a +20354d7a-e4fe-47af-8ff6-187bca92f3f9 +57fe29d4-312a-4711-bd9a-c320253d9176 +b415c867-1cff-455e-b194-748662ac2cca +5a4c95db-947d-4914-a631-41e8d466328e +861c4cde-2f0f-4796-8d8f-9492e74b2573 +2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f +41cd0bbe-a943-431b-9bde-bb2cad3491a1 +2a0615de-8aa4-41e7-9504-dd875f5f3f01 +11b5ca88-64ad-41a4-9f36-317b66c290af +eaea5eb3-48a3-41c6-9ea5-c695299bab16 +0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3 +b0620914-858d-46fc-8e6d-033c565e138b +6b59be3f-e527-422d-959d-79fcdb3b24eb +fed624df-56d9-495e-9ad4-ea77000957e8 +d58e7249-19d1-40bd-a43f-1da0497fe8aa +3a0fe4df-2e40-4541-8d7f-13586f0b9294 +6afb1960-febd-418d-b604-e50c1b59459b +efa802c8-ae18-4ae1-a524-49df21d05939 +933787c2-51e3-4eac-8a85-ab332cac0456 +94c993f6-a443-4408-b908-6e78e223e8ac +00434cd3-fcc7-44c7-8f98-7368415b4206 +7614cf6e-689e-47ac-a976-b1e9997637e9 +d59691a4-f830-4eb0-a819-a0fb00b7e80f +FIM \ No newline at end of file diff --git a/tps/fonte/tp02/TP02Q18 - Quicksort PARCIAL em Java/753045_quicksortParcial.txt b/tps/fonte/tp02/TP02Q18 - Quicksort PARCIAL em Java/753045_quicksortParcial.txt new file mode 100644 index 0000000..5406b8d --- /dev/null +++ b/tps/fonte/tp02/TP02Q18 - Quicksort PARCIAL em Java/753045_quicksortParcial.txt @@ -0,0 +1 @@ +753045 31ms 325 \ No newline at end of file diff --git a/tps/fonte/tp02/TP02Q18 - Quicksort PARCIAL em Java/Arq.java b/tps/fonte/tp02/TP02Q18 - Quicksort PARCIAL em Java/Arq.java new file mode 100644 index 0000000..9a632d1 --- /dev/null +++ b/tps/fonte/tp02/TP02Q18 - Quicksort PARCIAL em Java/Arq.java @@ -0,0 +1,206 @@ +import java.io.*; +import java.util.Formatter; +import java.util.Scanner; +import java.io.File; +import java.nio.charset.*; + +public class Arq +{ + private static String nomeArquivo = ""; + private static String charsetArquivo = "ISO-8859-1"; + private static boolean write = false, read = false; + private static Formatter saida = null; + private static Scanner entrada = null; + + public static boolean openWrite(String nomeArq, String charset) { + boolean resp = false; + close(); + try{ + saida = new Formatter(nomeArq, charset); + nomeArquivo = nomeArq; + resp = write = true; + } catch (Exception e) {} + return resp; + } + + public static boolean openWrite(String nomeArq) { + return openWrite(nomeArq, charsetArquivo); + } + + public static boolean openWriteClose(String nomeArq, String charset, String conteudo) { + boolean resp = openWrite(nomeArq, charset); + if(resp == true){ + println(conteudo); + close(); + } + return resp; + } + + public static boolean openWriteClose(String nomeArq, String conteudo) { + return openWriteClose(nomeArq, charsetArquivo, conteudo); + } + + public static boolean openRead(String nomeArq) { + return openRead(nomeArq, charsetArquivo); + } + + public static boolean openRead(String nomeArq, String charset) { + boolean resp = false; + close(); + try{ + entrada = new Scanner(new File(nomeArq), charset); + nomeArquivo = nomeArq; + resp = read = true; + } catch (Exception e) {} + return resp; + } + + public static String openReadClose(String nomeArq){ + openRead(nomeArq); + String resp = readAll(); + close(); + return resp; + } + + public static void close() { + if(write == true){ + saida.close(); + } + if(read == true){ + entrada.close(); + } + write = read = false; + nomeArquivo = ""; + charsetArquivo = "ISO-8859-1"; + } + + public static long length(){ + long resp = -1; + if(read != write){ + File file = new File(nomeArquivo); + resp = file.length(); + } + return resp; + } + + public static void print(int x){ + if(write == true){ + saida.format( "%d", x); + } + } + + public static void print(double x){ + if(write == true){ + saida.format( "%f", x); + } + } + + public static void print(String x){ + if(write == true){ + saida.format( "%s", x); + } + } + + public static void print(boolean x){ + if(write == true){ + saida.format( "%s", ((x) ? "true" : "false")); + } + } + + public static void print(char x){ + if(write == true){ + saida.format( "%c", x); + } + } + + public static void println(int x){ + if(write == true){ + saida.format( "%d\n", x); + } + } + + public static void println(double x){ + if(write == true){ + saida.format( "%f\n", x); + } + } + + public static void println(String x){ + if(write == true){ + saida.format( "%s\n", x); + } + } + + public static void println(boolean x){ + if(write == true){ + saida.format( "%s\n", ((x) ? "true" : "false")); + } + } + + public static void println(char x){ + if(write == true){ + saida.format( "%c\n", x); + } + } + + public static int readInt(){ + int resp = -1; + try{ + resp = entrada.nextInt(); + } catch (Exception e) {} + return resp; + } + + public static char readChar(){ + char resp = ' '; + try{ + resp = (char)entrada.nextByte(); + } catch (Exception e) {} + return resp; + } + + public static double readDouble(){ + double resp = -1; + try{ + resp = Double.parseDouble(readString().replace(",",".")); + } catch (Exception e) {} + return resp; + } + + public static String readString(){ + String resp = ""; + try{ + resp = entrada.next(); + } catch (Exception e) { System.out.println(e.getMessage()); } + return resp; + } + + public static boolean readBoolean(){ + boolean resp = false; + try{ + resp = (entrada.next().equals("true")) ? true : false; + } catch (Exception e) {} + return resp; + } + + public static String readLine(){ + String resp = ""; + try{ + resp = entrada.nextLine(); + } catch (Exception e) { System.out.println(e.getMessage()); } + return resp; + } + + + public static boolean hasNext(){ + return entrada.hasNext(); + } + + public static String readAll(){ + String resp = ""; + while(hasNext()){ + resp += (readLine() + "\n"); + } + return resp; + } +} diff --git a/tps/fonte/tp02/TP02Q18 - Quicksort PARCIAL em Java/Character.java b/tps/fonte/tp02/TP02Q18 - Quicksort PARCIAL em Java/Character.java new file mode 100644 index 0000000..e8bf10b --- /dev/null +++ b/tps/fonte/tp02/TP02Q18 - Quicksort PARCIAL em Java/Character.java @@ -0,0 +1,529 @@ +/** + * @path TP02Q01 - Classe em Java/Characters.java + * @description Java class of all characters from Harry Potter's saga with partial quicksort algorithm + * @author Pedro Lopes - github.com/httpspedroh + * @version 1.0 + * @update 2024-04-09 + */ + +// ---------------------------------------------------------------------------------------------------- // + +// Imports +import java.util.Scanner; +import java.util.UUID; +import java.io.BufferedReader; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Date; +import java.text.SimpleDateFormat; + +// ---------------------------------------------------------------------------------------------------- // + +public class Character { + + static SimpleDateFormat ddf = new SimpleDateFormat("dd-MM-yyyy"); + + // Global variables + public static final String FILE_PATH = "/tmp/characters.csv"; + public static ArrayList allCharacters = new ArrayList(); + + // -------------------------- // + + // Attributes + private UUID id; + private String name; + private ArrayList alternateNames; + private String house; + private String ancestry; + private String species; + private String patronus; + private boolean hogwartsStaff; + private boolean hogwartsStudent; + private String actorName; + private boolean alive; + private Date birthDate; + private int yearOfBirth; + private String eyeColour; + private String gender; + private String hairColour; + private boolean wizard; + + // -------------------------- // + + // Empty constructor + public Character() { + + this.id = UUID.randomUUID(); + this.name = ""; + this.alternateNames = new ArrayList(); + this.house = ""; + this.ancestry = ""; + this.species = ""; + this.patronus = ""; + this.hogwartsStaff = false; + this.hogwartsStudent = false; + this.actorName = ""; + this.alive = false; + this.birthDate = null; + this.yearOfBirth = 0; + this.eyeColour = ""; + this.gender = ""; + this.hairColour = ""; + this.wizard = false; + } + + // Constructor + public Character(UUID id, String name, ArrayList alternateNames, String house, String ancestry, String species, String patronus, boolean hogwartsStaff, boolean hogwartsStudent, String actorName, boolean alive, Date birthDate, int yearOfBirth, String eyeColour, String gender, String hairColour, boolean wizard) { + + this.id = id; + this.name = name; + this.alternateNames = alternateNames; + this.house = house; + this.ancestry = ancestry; + this.species = species; + this.patronus = patronus; + this.hogwartsStaff = hogwartsStaff; + this.hogwartsStudent = hogwartsStudent; + this.actorName = actorName; + this.alive = alive; + this.birthDate = birthDate; + this.yearOfBirth = yearOfBirth; + this.eyeColour = eyeColour; + this.gender = gender; + this.hairColour = hairColour; + this.wizard = wizard; + } + + // -------------------------- // + + // Gets + public UUID getId() { return this.id; } + public String getName() { return this.name; } + public String getHouse() { return this.house; } + public String getAncestry() { return this.ancestry; } + public String getSpecies() { return this.species; } + public String getPatronus() { return this.patronus; } + public boolean getHogwartsStaff() { return this.hogwartsStaff; } + public boolean getHogwartsStudent() { return this.hogwartsStudent; } + public String getActorName() { return this.actorName; } + public boolean getAlive() { return this.alive; } + public Date getBirthDate() { return this.birthDate; } + public int getYearOfBirth() { return this.yearOfBirth; } + public String getEyeColour() { return this.eyeColour; } + public String getGender() { return this.gender; } + public String getHairColour() { return this.hairColour; } + public boolean getWizard() { return this.wizard; } + + public String getAlternateNames() { + + // Construct string e.g. {item1, item2, item3} + String alternateNames = "{"; + + for(int i = 0; i < this.alternateNames.size(); i++) { + + alternateNames += this.alternateNames.get(i); + + if(i < this.alternateNames.size() - 1) alternateNames += ", "; + } + + alternateNames += "}"; + + return alternateNames; + } + + // Sets + public void setId(UUID id) { this.id = id; } + public void setName(String name) { this.name = name; } + public void setAlternateNames(ArrayList alternateNames) { this.alternateNames = alternateNames; } + public void setHouse(String house) { this.house = house; } + public void setAncestry(String ancestry) { this.ancestry = ancestry; } + public void setSpecies(String species) { this.species = species; } + public void setPatronus(String patronus) { this.patronus = patronus; } + public void setHogwartsStaff(boolean hogwartsStaff) { this.hogwartsStaff = hogwartsStaff; } + public void setHogwartsStudent(boolean hogwartsStudent) { this.hogwartsStudent = hogwartsStudent; } + public void setActorNane(String actorName) { this.actorName = actorName; } + public void setAlive(boolean alive) { this.alive = alive; } + public void setBirthDate(Date birthDate) { this.birthDate = birthDate; } + public void setYearOfBirth(int yearOfBirth) { this.yearOfBirth = yearOfBirth; } + public void setEyeColour(String eyeColour) { this.eyeColour = eyeColour; } + public void setGender(String gender) { this.gender = gender; } + public void setHairColour(String hairColour) { this.hairColour = hairColour; } + public void setWizard(boolean wizard) { this.wizard = wizard; } + + // -------------------------- // + + // Clone + public Character clone() { return new Character(this.id, this.name, this.alternateNames, this.house, this.ancestry, this.species, this.patronus, this.hogwartsStaff, this.hogwartsStudent, this.actorName, this.alive, this.birthDate, this.yearOfBirth, this.eyeColour, this.gender, this.hairColour, this.wizard); } + + // -------------------------- // + + // Print + public void print() { + + System.out.println("[" + + this.getId() + " ## " + + this.getName() + " ## " + + this.getAlternateNames() + " ## " + + (this.getHouse() == "" ? "N/A" : this.getHouse()) + " ## " + + (this.getAncestry() == "" ? "N/A" : this.getAncestry()) + " ## " + + (this.getSpecies() == "" ? "N/A" : this.getSpecies()) + " ## " + + (this.getPatronus() == "" ? "N/A" : this.getPatronus()) + " ## " + + (this.getHogwartsStaff() ? "true" : "false") + " ## " + + (this.getHogwartsStudent() ? "true" : "false") + " ## " + + (this.getActorName() == "" ? "N/A" : this.getActorName()) + " ## " + + (this.getAlive() ? "true" : "false") + " ## " + + (this.getBirthDate() == null ? "N/A" : ddf.format(this.getBirthDate())) + " ## " + + (this.getYearOfBirth() == 0 ? "N/A" : this.getYearOfBirth()) + " ## " + + (this.getEyeColour() == "" ? "N/A" : this.getEyeColour()) + " ## " + + (this.getGender() == "" ? "N/A" : this.getGender()) + " ## " + + (this.getHairColour() == "" ? "N/A" : this.getHairColour()) + " ## " + + (this.getWizard() ? "true" : "false") + "]"); + } + + // -------------------------- // + + public static ArrayList extractNames(String input) { + + ArrayList names = new ArrayList<>(); + + // Clean input + String cleanedInput = input.substring(1, input.length() - 1); + + // Check if input contains double quotes + if (cleanedInput.contains("\"\"")) { + + String[] parts = cleanedInput.split("\", "); + + for (String part : parts) names.add(part.replace("\"", "").replace("'", "")); + } + else { + + // Split input by comma and space + String[] parts = cleanedInput.split(", "); + + for (String part : parts) names.add(part.replace("'", "")); + } + return names; + } + + // -------------------------- // + + // Read + public void read(String line) { + + // ---------------------- // + + // Start position + int positionStart = 0; + int positionEnd = 0; + + // ---------------------- // + + // Set id + positionEnd = line.indexOf(";", positionStart); + this.setId(UUID.fromString(line.substring(positionStart, positionEnd))); + + // ---------------------- // + + // Set name + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setName(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set alternate names + positionStart = positionEnd + 1; + + if(line.charAt(positionStart) == '[') { // If first character is '[', has one or no names + + if(line.charAt(positionStart + 1) == ']') { + + positionEnd = positionStart + 2; + + this.setAlternateNames(new ArrayList()); + } + else { + + positionEnd = line.indexOf("];", positionStart); + + this.setAlternateNames(extractNames(line.substring(positionStart, positionEnd++))); + } + } + + // ---------------------- // + + // Set house + positionStart = positionEnd; + positionEnd = line.indexOf(";", ++positionStart); + + this.setHouse(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set ancestry + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setAncestry(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set species + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setSpecies(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set patronus + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setPatronus(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set hogwartsStaff + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setHogwartsStaff("VERDADEIRO".equals(line.substring(positionStart, positionEnd))); + + // ---------------------- // + + // Set hogwartsStudent + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setHogwartsStudent("VERDADEIRO".equals(line.substring(positionStart, positionEnd))); + + // ---------------------- // + + // Set actor name + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setActorNane(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set alive + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setAlive("VERDADEIRO".equals(line.substring(positionStart, positionEnd))); + + // ---------------------- // + + // Set date of birth + positionStart = positionEnd; + positionEnd = line.indexOf(";", ++positionStart); + + try { this.setBirthDate(ddf.parse(line.substring(positionStart, positionEnd))); } + catch(Exception e) { this.setBirthDate(null); } + + // ---------------------- // + + // Set year of birth + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + try { this.setYearOfBirth(Integer.parseInt(line.substring(positionStart, positionEnd))); } + catch(Exception e) { this.setYearOfBirth(0); } + + // ---------------------- // + + // Set eye colour + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setEyeColour(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set gender + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setGender(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set hair colour + positionStart = positionEnd + 1; + positionEnd = line.indexOf(";", positionStart); + + this.setHairColour(line.substring(positionStart, positionEnd)); + + // ---------------------- // + + // Set wizard + positionStart = positionEnd + 1; + + this.setWizard("VERDADEIRO".equals(line.substring(positionStart, line.length()))); + + // ---------------------- // + } + + // compareTo + public int compareTo(Character other) { + + // Compare by house + int houseComparison = this.house.compareTo(other.house); + + if(houseComparison != 0) return houseComparison; + else return this.name.compareTo(other.name); + } + + // ---------------------------------------------------------------------------------------------------- // + + // Read all characters function + public static void startCharacters() { + + // Initialize variables + try { + + FileInputStream fstream = new FileInputStream(FILE_PATH); + BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); + + // ---------------------- // + + // Explode CSV file + String line = br.readLine(); + + while((line = br.readLine()) != null) { + + // Initialize character + Character character = new Character(); + + // Read line + character.read(line); + + // Add character to array + allCharacters.add(character); + } + + // Close CSV file + fstream.close(); + } + catch(IOException e) { e.printStackTrace(); } + } + + // ---------------------------------------------------------------------------------------------------- // + + // Search by id function + public static Character searchById(UUID id, ArrayList characters) { + + // Search for character + for(int i = 0; i < characters.size(); i++) { + + if(characters.get(i).getId().equals(id)) return characters.get(i); + } + return null; + } + + // ---------------------------------------------------------------------------------------------------- // + + public static void main(String[] args) { + + // ----------------------------------------------------------------- // + + // #1 - Start - Read all characters in CSV file + startCharacters(); + + // ----------------------------------------------------------------- // + + // #2 - Read input and print characters from pub.in id entries and add to mainCharacters array + + // Initialize mainCharacters array + ArrayList mainCharacters = new ArrayList(); + + // Initialize scanner + Scanner inScanner = new Scanner(System.in); + + // Initialize character + Character character = new Character(); + + // Read first line + String line = inScanner.nextLine(); + + // While line is not "FIM" + while(!line.equals("FIM")) { + + // Get id + UUID id = UUID.fromString(line); + + // Search for character + character = searchById(id, allCharacters); + + // Add character to mainCharacters array + if(character != null) mainCharacters.add(character); + + // Read line + line = inScanner.nextLine(); + } + + // ----------------------------------------------------------------- // + + // #3 - Order mainCharacters array by key "house" using insertion sort, in draw case, order by key "name" + + // Start benchmark + long startTime = System.currentTimeMillis(); + int comparisons = 0; + + // ----------------- // + + // Parcial quicksort with k = 10 + int k = 10; + + for(int i = 0; i < mainCharacters.size() - 1; i++) { + + for(int j = i + 1; j < mainCharacters.size() - 1; j++) { + + comparisons++; + + if(mainCharacters.get(i).compareTo(mainCharacters.get(j)) > 0) { + + Character temp = mainCharacters.get(i); + mainCharacters.set(i, mainCharacters.get(j)); + mainCharacters.set(j, temp); + } + else if(mainCharacters.get(i).compareTo(mainCharacters.get(j)) == 0) { + + comparisons++; + + if(mainCharacters.get(i).getName().compareTo(mainCharacters.get(j).getName()) > 0) { + + Character temp = mainCharacters.get(i); + mainCharacters.set(i, mainCharacters.get(j)); + mainCharacters.set(j, temp); + } + } + } + } + + // ----------------- // + + // Save benchmark in file + Arq.openWrite("753045_quicksortParcial.txt"); + Arq.print("753045\t" + (System.currentTimeMillis() - startTime) + "ms\t" + comparisons); + Arq.close(); + + // ----------------- // + + // Print mainCharacters array + for(int i = 0; i < k; i++) mainCharacters.get(i).print(); + + // ----------------------------------------------------------------- // + + // Close scanner + inScanner.close(); + } +} + +// ---------------------------------------------------------------------------------------------------- // diff --git a/tps/fonte/tp02/TP02Q18 - Quicksort PARCIAL em Java/pedro.out b/tps/fonte/tp02/TP02Q18 - Quicksort PARCIAL em Java/pedro.out new file mode 100644 index 0000000..a8cdcce --- /dev/null +++ b/tps/fonte/tp02/TP02Q18 - Quicksort PARCIAL em Java/pedro.out @@ -0,0 +1,10 @@ +[94c993f6-a443-4408-b908-6e78e223e8ac ## Aberforth Dumbledore ## {} ## Gryffindor ## half-blood ## human ## goat ## false ## false ## Jim McManus ## true ## 10-03-1960 ## 1960 ## blue ## male ## grey ## true] +[b415c867-1cff-455e-b194-748662ac2cca ## Albus Dumbledore ## {Professor Dumbledore} ## Gryffindor ## half-blood ## human ## Phoenix ## true ## false ## Richard Harris ## false ## 27-03-1960 ## 1881 ## blue ## male ## silver ## true] +[2a0615de-8aa4-41e7-9504-dd875f5f3f01 ## George Weasley ## {} ## Gryffindor ## pure-blood ## human ## N/A ## false ## true ## Oliver Phelps ## true ## 13-02-1981 ## 1954 ## amber ## male ## red ## true] +[9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8 ## Harry Potter ## {The Boy Who Lived, The Chosen One, Undesirable No. 1, Potty} ## Gryffindor ## half-blood ## human ## stag ## false ## true ## Daniel Radcliffe ## true ## 31-07-1980 ## 1980 ## green ## male ## black ## true] +[41cd0bbe-a943-431b-9bde-bb2cad3491a1 ## Lily Potter ## {} ## Gryffindor ## muggleborn ## human ## N/A ## false ## false ## Geraldine Somerville ## false ## 30-01-1960 ## 1960 ## green ## female ## blond ## true] +[ca3827f0-375a-4891-aaa5-f5e8a5bad225 ## Minerva McGonagall ## {} ## Gryffindor ## half-blood ## human ## tabby cat ## true ## false ## Dame Maggie Smith ## true ## 04-10-1925 ## 1925 ## grey ## female ## black ## true] +[933787c2-51e3-4eac-8a85-ab332cac0456 ## Miranda Gaushawk ## {} ## Gryffindor ## muggle ## human ## N/A ## false ## false ## N/A ## false ## 27-03-1960 ## 1960 ## blue ## female ## silver ## true] +[00434cd3-fcc7-44c7-8f98-7368415b4206 ## Miriam Strout ## {} ## Gryffindor ## half-blood ## human ## N/A ## false ## false ## N/A ## true ## 27-03-1960 ## 1954 ## blue ## female ## silver ## true] +[11b5ca88-64ad-41a4-9f36-317b66c290af ## Nearly Headless Nick ## {Sir Nicholas de Mimsy-Porpington, Sir Nicholas, Nick} ## Gryffindor ## pure-blood ## ghost ## N/A ## false ## false ## John Cleese ## false ## 27-03-1960 ## 1926 ## amber ## male ## sandy ## true] +[36bfefd0-e0bb-4d11-be98-d1ef6117a77a ## Rubeus Hagrid ## {Professor Hagrid, Hagger} ## Gryffindor ## half-blood ## half-giant ## N/A ## true ## false ## Robbie Coltrane ## true ## 06-12-1928 ## 1928 ## black ## male ## black ## true] diff --git a/tps/fonte/tp02/TP02Q18 - Quicksort PARCIAL em Java/pub.in b/tps/fonte/tp02/TP02Q18 - Quicksort PARCIAL em Java/pub.in new file mode 100644 index 0000000..a782d8c --- /dev/null +++ b/tps/fonte/tp02/TP02Q18 - Quicksort PARCIAL em Java/pub.in @@ -0,0 +1,28 @@ +9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8 +1413e1b3-2903-4a47-a2d5-e8abc5ce8014 +ca3827f0-375a-4891-aaa5-f5e8a5bad225 +36bfefd0-e0bb-4d11-be98-d1ef6117a77a +20354d7a-e4fe-47af-8ff6-187bca92f3f9 +57fe29d4-312a-4711-bd9a-c320253d9176 +b415c867-1cff-455e-b194-748662ac2cca +5a4c95db-947d-4914-a631-41e8d466328e +861c4cde-2f0f-4796-8d8f-9492e74b2573 +2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f +41cd0bbe-a943-431b-9bde-bb2cad3491a1 +2a0615de-8aa4-41e7-9504-dd875f5f3f01 +11b5ca88-64ad-41a4-9f36-317b66c290af +eaea5eb3-48a3-41c6-9ea5-c695299bab16 +0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3 +b0620914-858d-46fc-8e6d-033c565e138b +6b59be3f-e527-422d-959d-79fcdb3b24eb +fed624df-56d9-495e-9ad4-ea77000957e8 +d58e7249-19d1-40bd-a43f-1da0497fe8aa +3a0fe4df-2e40-4541-8d7f-13586f0b9294 +6afb1960-febd-418d-b604-e50c1b59459b +efa802c8-ae18-4ae1-a524-49df21d05939 +933787c2-51e3-4eac-8a85-ab332cac0456 +94c993f6-a443-4408-b908-6e78e223e8ac +00434cd3-fcc7-44c7-8f98-7368415b4206 +7614cf6e-689e-47ac-a976-b1e9997637e9 +d59691a4-f830-4eb0-a819-a0fb00b7e80f +FIM \ No newline at end of file