diff --git a/tps/gabaritos/tp02/TP02Q03 - Pesquisa sequencial em Java/753045_sequencial.txt b/tps/gabaritos/tp02/TP02Q03 - Pesquisa sequencial em Java/753045_sequencial.txt new file mode 100644 index 0000000..4846eef --- /dev/null +++ b/tps/gabaritos/tp02/TP02Q03 - Pesquisa sequencial em Java/753045_sequencial.txt @@ -0,0 +1 @@ +753045 24ms 1933 \ No newline at end of file diff --git a/tps/gabaritos/tp02/TP02Q03 - Pesquisa sequencial em Java/Arq.java b/tps/gabaritos/tp02/TP02Q03 - Pesquisa sequencial em Java/Arq.java new file mode 100644 index 0000000..01f3146 --- /dev/null +++ b/tps/gabaritos/tp02/TP02Q03 - Pesquisa sequencial em Java/Arq.java @@ -0,0 +1,204 @@ +import java.util.Formatter; +import java.util.Scanner; +import java.io.File; + +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/gabaritos/tp02/TP02Q03 - Pesquisa sequencial em Java/Game.java b/tps/gabaritos/tp02/TP02Q03 - Pesquisa sequencial em Java/Game.java new file mode 100644 index 0000000..cd344b0 --- /dev/null +++ b/tps/gabaritos/tp02/TP02Q03 - Pesquisa sequencial em Java/Game.java @@ -0,0 +1,595 @@ +import java.io.BufferedReader; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.text.DecimalFormat; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.Locale; +import java.util.Scanner; + +// ----------------------------------------------------------------------------------------------------------------- // + +class Game { + + static SimpleDateFormat default_dateFormat = new SimpleDateFormat("MMM/yyyy", Locale.ENGLISH); + + private String name, owners, website, developers; + private ArrayList languages, genres; + private Date release_date; + private int app_id, age, dlcs, avg_playtime; + private float price, upvotes; + private boolean windows, mac, linux; + + public Game() { + + this.name = this.owners = this.website = this.developers = null; + this.languages = new ArrayList(); + this.genres = new ArrayList(); + this.release_date = null; + this.app_id = this.age = this.dlcs = this.avg_playtime = -1; + this.price = this.upvotes = -1; + this.windows = this.mac = this.linux = false; + } + + public Game(String name, String owners, String website, String developers, ArrayList languages, ArrayList genres, Date release_date, int app_id, int age, int dlcs, int upvotes, int avg_playtime, float price, boolean windows, boolean mac, boolean linux) { + + this.name = name; + this.owners = owners; + this.website = website; + this.developers = developers; + this.languages = languages; + this.genres = genres; + this.release_date = release_date; + this.app_id = app_id; + this.age = age; + this.dlcs = dlcs; + this.upvotes = upvotes; + this.avg_playtime = avg_playtime; + this.price = price; + this.windows = windows; + this.mac = mac; + this.linux = linux; + } + + public void setName(String name) { this.name = name; } + public void setOwners(String owners) { this.owners = owners; } + public void setWebsite(String website) { this.website = website; } + public void setDevelopers(String developers) { this.developers = developers; } + public void setLanguages(ArrayList languages) { this.languages = languages; } + public void setGenres(ArrayList genres) { this.genres = genres; } + public void setReleaseDate(Date release_date) { this.release_date = release_date; } + public void setAppId(int app_id) { this.app_id = app_id; } + public void setAge(int age) { this.age = age; } + public void setDlcs(int dlcs) { this.dlcs = dlcs; } + public void setAvgPlaytime(int avg_playtime) { this.avg_playtime = avg_playtime; } + public void setPrice(float price) { this.price = price; } + public void setUpvotes(float upvotes) { this.upvotes = upvotes; } + public void setWindows(boolean windows) { this.windows = windows; } + public void setMac(boolean mac) { this.mac = mac; } + public void setLinux(boolean linux) { this.linux = linux; } + + public String getName() { return this.name; } + public String getOwners() { return this.owners; } + public String getWebsite() { return this.website; } + public String getDevelopers() { return this.developers; } + public ArrayList getLanguages() { return this.languages; } + public ArrayList getGenres() { return this.genres; } + public Date getReleaseDate() { return this.release_date; } + public int getAppId() { return this.app_id; } + public int getAge() { return this.age; } + public int getDlcs() { return this.dlcs; } + public int getAvgPlaytime() { return this.avg_playtime; } + public float getPrice() { return this.price; } + public float getUpvotes() { return this.upvotes; } + public boolean getWindows() { return this.windows; } + public boolean getMac() { return this.mac; } + public boolean getLinux() { return this.linux; } + + public Game clone() { + + Game cloned = new Game(); + + cloned.name = this.name; + cloned.owners = this.owners; + cloned.website = this.website; + cloned.developers = this.developers; + cloned.languages = this.languages; + cloned.genres = this.genres; + cloned.release_date = this.release_date; + cloned.app_id = this.app_id; + cloned.age = this.age; + cloned.dlcs = this.dlcs; + cloned.avg_playtime = this.avg_playtime; + cloned.price = this.price; + cloned.upvotes = this.upvotes; + cloned.windows = this.windows; + cloned.mac = this.mac; + cloned.linux = this.linux; + + return cloned; + } + + public void read(String line) { + + char c_search; + int index = 0, atr_index = 0; + + // ---------------------------------- // + + // Find "AppID" + while(true) { + + index++; + + if(line.charAt(index) == ',') { + + this.app_id = Integer.parseInt(line.substring(atr_index, index)); + + atr_index = ++index; + break; + } + } + + // ---------------------------------- // + + // Find "Name" + if(line.charAt(atr_index) != ',') { + + if(line.charAt(atr_index) == '\"') { + + atr_index++; + c_search = '\"'; + } + else c_search = ','; + + while(true) { + + index++; + + if(line.charAt(index) == c_search) { + + this.name = line.substring(atr_index, index); + + if(c_search == ',') index++; + else if(c_search == '\"') index += 2; + + atr_index = index; + break; + } + } + } + else atr_index = ++index; + + // ---------------------------------- // + + // Find release date + if(line.charAt(atr_index) != ',') { + + SimpleDateFormat df; + + if(line.charAt(atr_index) == '\"') { + + df = new SimpleDateFormat("MMM dd, yyyy", Locale.ENGLISH); + + atr_index++; + c_search = '\"'; + } + else { + + df = new SimpleDateFormat("MMM yyyy", Locale.ENGLISH); + + c_search = ','; + } + + while(true) { + + index++; + + if(line.charAt(index) == c_search) { + + try { this.release_date = df.parse(line.substring(atr_index, index)); } + catch (java.text.ParseException e) { e.printStackTrace(); } + + if(c_search == ',') index++; + else if(c_search == '\"') index += 2; + + atr_index = index; + break; + } + } + } + else atr_index = ++index; + + // ---------------------------------- // + + // Find "Owners" + while(true) { + + index++; + + if(line.charAt(index) == ',') { + + this.owners = line.substring(atr_index, index); + + atr_index = ++index; + break; + } + } + + // ---------------------------------- // + + // Find "Age" + while(true) { + + index++; + + if(line.charAt(index) == ',') { + + this.age = Integer.parseInt(line.substring(atr_index, index)); + + atr_index = ++index; + break; + } + } + + // ---------------------------------- // + + // Find "Price" + while(true) { + + index++; + + if(line.charAt(index) == ',') { + + this.price = Float.parseFloat(line.substring(atr_index, index)); + + atr_index = ++index; + break; + } + } + + // ---------------------------------- // + + // Find "DLCs" + while(true) { + + index++; + + if(line.charAt(index) == ',') { + + this.dlcs = Integer.parseInt(line.substring(atr_index, index)); + + atr_index = ++index; + break; + } + } + + // ---------------------------------- // + + // Find "Languages" + while(true) { + + index++; + + if(line.charAt(index) == ']') { + + index++; + + if(line.charAt(index) == ',') index++; + else if(line.charAt(index) == '\"') index += 2; + + atr_index = index; + break; + } + else if(line.charAt(index) == '\'') { + + int wordStart = index + 1; + + while(true) { + + index++; + + if(line.charAt(index) == '\'') { + + this.languages.add(line.substring(wordStart, index)); + break; + } + } + } + } + + // ---------------------------------- // + + // Find "Website" + if(line.charAt(atr_index) != ',') { + + if(line.charAt(atr_index) == '\"') { + + atr_index++; + c_search = '\"'; + } + else c_search = ','; + + while(true) { + + index++; + + if(line.charAt(index) == c_search) { + + this.website = line.substring(atr_index, index); + + atr_index = ++index; + break; + } + } + } + else atr_index = ++index; + + // ---------------------------------- // + + // Find "Windows" + while(true) { + + index++; + + if(line.charAt(index) == ',') { + + this.windows = Boolean.parseBoolean(line.substring(atr_index, index)); + + atr_index = ++index; + break; + } + } + + // Find "Mac" + while(true) { + + index++; + + if(line.charAt(index) == ',') { + + this.mac = Boolean.parseBoolean(line.substring(atr_index, index)); + + atr_index = ++index; + break; + } + } + + // Find "Linux" + while(true) { + + index++; + + if(line.charAt(index) == ',') { + + this.linux = Boolean.parseBoolean(line.substring(atr_index, index)); + + atr_index = ++index; + break; + } + } + + // ---------------------------------- // + + // Find "Upvotes" + int positives, negatives; + + while(true) { + + index++; + + if(line.charAt(index) == ',') { + + positives = Integer.parseInt(line.substring(atr_index, index)); + + atr_index = ++index; + break; + } + } + + while(true) { + + index++; + + if(line.charAt(index) == ',') { + + negatives = Integer.parseInt(line.substring(atr_index, index)); + + atr_index = ++index; + break; + } + } + + this.upvotes = (float)(positives * 100) / (float)(positives + negatives); + + // ---------------------------------- // + + // Find "AVG Playtime" + while(true) { + + index++; + + if(line.charAt(index) == ',') { + + this.avg_playtime = Integer.parseInt(line.substring(atr_index, index)); + + atr_index = ++index; + break; + } + } + + // ---------------------------------- // + + // Find "Developers" + if(line.charAt(atr_index) != ',') { + + if(line.charAt(atr_index) == '\"') { + + atr_index++; + c_search = '\"'; + } + else c_search = ','; + + while(true) { + + index++; + + if(line.charAt(index) == c_search) { + + this.developers = line.substring(atr_index, index); + + atr_index = ++index; + break; + } + } + } + else atr_index = ++index; + + // ---------------------------------- // + + // Find "Genres" + if(index < line.length() - 1) { + + if(line.charAt(index) == ',') atr_index = ++index; + if(line.charAt(atr_index) == '\"') { + + atr_index++; + + while(true) { + + index++; + + if(line.charAt(index) == ',') { + + this.genres.add(line.substring(atr_index, index)); + + atr_index = ++index; + } + else if(line.charAt(index) == '\"') { + + this.genres.add(line.substring(atr_index, line.length() - 1)); + break; + } + } + } + else this.genres.add(line.substring(atr_index, line.length())); + } + + // -------------------------------------------------------------------------------- // + } + + public void print() { + + String avg_pt = null; + + if(this.avg_playtime == 0) avg_pt = "null "; + else if(this.avg_playtime < 60) avg_pt = this.avg_playtime + "m "; + else { + + if(this.avg_playtime % 60 == 0) avg_pt = this.avg_playtime / 60 + "h "; + else avg_pt = (this.avg_playtime / 60) + "h " + (this.avg_playtime % 60) + "m "; + } + + DecimalFormat df = new DecimalFormat("##"); + + System.out.println(this.app_id + " " + this.name + " " + default_dateFormat.format(this.release_date) + " " + this.owners + " " + this.age + " " + String.format(Locale.ENGLISH, "%.2f", this.price) + " " + this.dlcs + " " + this.languages + " " + this.website + " " + this.windows + " " + this.mac + " " + this.linux + " " + (Float.isNaN(this.upvotes) ? "0% " : df.format(this.upvotes) + "% ") + avg_pt + this.developers + " " + this.genres); + } + + // -------------------------------------------------------------------------------------- // + + public static void main(String[] args) throws Exception { + + Scanner scr = new Scanner(System.in); + ArrayList gamesToAdd = new ArrayList(); + ArrayList games = new ArrayList(); + String line = scr.nextLine(); + + // ------------------------------------------------------------------------------ // + + // Fill list with requested ids + while(true) { + + if(line.equals("FIM")) break; + + gamesToAdd.add(Integer.parseInt(line)); + + line = scr.nextLine(); + } + + // ------------------------------------------------------------------------------ // + + // Add requested ids to games list + try { + + // Read CSV file + FileInputStream fstream = new FileInputStream("/tmp/games.csv"); + BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); + + // ------------------------------------ // + + // Start to explode CSV file + while((line = br.readLine()) != null) { + + if(gamesToAdd.contains(Integer.parseInt(line.substring(0, line.indexOf(','))))) { + + Game game = new Game(); + + game.read(line); + games.add(game); + } + } + + // Close CSV file + fstream.close(); + } + catch(IOException e) { e.printStackTrace(); } + + // ---------------------------------------------------------------------------------------------- // + + // Initialize bench log + int c_compares = 0; + long t_begin = System.currentTimeMillis(); + + // Search in games list by name + line = scr.nextLine(); + + while(true) { + + if(line.equals("FIM")) break; + + // ------------------------------------ // + + boolean found = false; + + for(int i = 0; i < games.size(); i++) { + + c_compares++; + + if(games.get(i).name.compareTo(line) == 0) { + + found = true; + break; + } + } + + // ------------------------------------ // + + System.out.println(found ? "SIM" : "NAO"); + + line = scr.nextLine(); + } + + Arq.openWrite("753045_sequencial.txt"); + Arq.print("753045\t" + (System.currentTimeMillis() - t_begin) + "ms\t" + c_compares); + Arq.close(); + + scr.close(); + + // ---------------------------------------------------------------------------------------------- // + + } + + // ---------------------------------------------------------------------------------------------- // +} \ No newline at end of file diff --git a/tps/gabaritos/tp02/TP02Q03 - Pesquisa sequencial em Java/pedro.out b/tps/gabaritos/tp02/TP02Q03 - Pesquisa sequencial em Java/pedro.out new file mode 100644 index 0000000..ed66dd8 --- /dev/null +++ b/tps/gabaritos/tp02/TP02Q03 - Pesquisa sequencial em Java/pedro.out @@ -0,0 +1,39 @@ +SIM +NAO +NAO +NAO +NAO +NAO +NAO +SIM +NAO +NAO +NAO +NAO +NAO +NAO +NAO +NAO +NAO +SIM +NAO +NAO +NAO +NAO +NAO +NAO +NAO +NAO +NAO +NAO +NAO +NAO +NAO +NAO +NAO +NAO +NAO +NAO +NAO +NAO +NAO diff --git a/tps/gabaritos/tp02/TP02Q03 - Pesquisa sequencial em Java/pub.in b/tps/gabaritos/tp02/TP02Q03 - Pesquisa sequencial em Java/pub.in new file mode 100644 index 0000000..6e08b8d --- /dev/null +++ b/tps/gabaritos/tp02/TP02Q03 - Pesquisa sequencial em Java/pub.in @@ -0,0 +1,91 @@ +840010 +866510 +1970560 +411740 +1509590 +458520 +920210 +1444020 +1232460 +2026070 +258880 +295250 +299500 +574080 +598780 +1217190 +257730 +1807730 +773670 +1803150 +1573070 +513930 +385250 +261880 +533300 +542340 +1209040 +8790 +8000 +415150 +2012500 +691150 +283370 +249650 +268870 +1172510 +927890 +25700 +281610 +485610 +1276850 +722340 +1499640 +1848450 +615700 +744980 +10180 +234390 +1641670 +427520 +FIM +Bobbi Adventure +The Test: Hypothesis Rising +Epic Cards Battle 2 (TCG) +Omensight: Definitive Edition +Gray Matter +The Legend of Bean +Armored Warfare +Void Slayer +RacetronicVR +The Amazing American Circus +Your Quest +HistoryMaker VR +WE ARE FOOTBALL +Rise of Insanity +Aragami +BONEWORKS +CAGE +Saku Saku: Love Blooms with the Cherry Blossoms +Kill The Bad Guy +Deisim +Redactem +Xuan-Yuan Sword: The Gate of Firmament +Subspace Continuum +DRAGON BALL XENOVERSE +RiotZ +Stephen's Sausage Roll +Tavern Tycoon - Dragon's Hangover +Child of Light +Heart and Seoul +Tank Destroyer +Claire +MONOLISK +Cinderella Escape 2 Revenge +Flat Heroes +The Political Machine 2016 +AFFECTED: The Manor - The Complete Edition +QUAKE III: Team Arena +Woodcutter Simulator 2013 +Dawn of Man +FIM \ No newline at end of file diff --git a/tps/gabaritos/tp02/TP02Q03 - Pesquisa sequencial em Java/pub.out b/tps/gabaritos/tp02/TP02Q03 - Pesquisa sequencial em Java/pub.out new file mode 100644 index 0000000..ed66dd8 --- /dev/null +++ b/tps/gabaritos/tp02/TP02Q03 - Pesquisa sequencial em Java/pub.out @@ -0,0 +1,39 @@ +SIM +NAO +NAO +NAO +NAO +NAO +NAO +SIM +NAO +NAO +NAO +NAO +NAO +NAO +NAO +NAO +NAO +SIM +NAO +NAO +NAO +NAO +NAO +NAO +NAO +NAO +NAO +NAO +NAO +NAO +NAO +NAO +NAO +NAO +NAO +NAO +NAO +NAO +NAO