add: TP02Q03 solved

This commit is contained in:
Pedro Lopes 2023-10-24 08:43:36 -03:00
parent c5309df2e4
commit 001503138c
No known key found for this signature in database
GPG Key ID: 1B416E26DD161BE9
6 changed files with 817 additions and 0 deletions

View File

@ -0,0 +1 @@
753045 60ms 3724

View File

@ -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;
}
}

View File

@ -0,0 +1,259 @@
/**
* @path TP02Q03 - Pesquisa Sequencial em Java/Player.java
* @description Player class implemented with sequential search
* @author Pedro Lopes - github.com/httpspedroh
* @version 1.0
* @update 2023-09-27
*/
// ---------------------------------------------------------------------------------------------------- //
// Imports
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
// ---------------------------------------------------------------------------------------------------- //
public class Player {
// Global variables
public static final String FILE_PATH = "/tmp/players.csv";
public static ArrayList<Player> allPlayers = new ArrayList<Player>();
// -------------------------- //
// Attributes
private int id;
private String name;
private int height;
private int weight;
private String college;
private int yearOfBirth;
private String birthCity;
private String birthState;
// Empty constructor
public Player() {
this.id = 0;
this.name = "";
this.height = 0;
this.weight = 0;
this.college = "";
this.yearOfBirth = 0;
this.birthCity = "";
this.birthState = "";
}
// Constructor
public Player(int id, String name, int height, int weight, String college, int yearOfBirth, String birthCity, String birthState) {
this.id = id;
this.name = name;
this.height = height;
this.weight = weight;
this.college = college;
this.yearOfBirth = yearOfBirth;
this.birthCity = birthCity;
this.birthState = birthState;
}
// Gets
public int getId() { return this.id; }
public String getName() { return this.name; }
public int getHeight() { return this.height; }
public int getWeight() { return this.weight; }
public String getCollege() { return this.college; }
public int getYearOfBirth() { return this.yearOfBirth; }
public String getBirthCity() { return this.birthCity; }
public String getBirthState() { return this.birthState; }
// Sets
public void setId(int id) { this.id = id; }
public void setName(String name) { this.name = name; }
public void setHeight(int height) { this.height = height; }
public void setWeight(int weight) { this.weight = weight; }
public void setCollege(String college) { this.college = college; }
public void setYearOfBirth(int yearOfBirth) { this.yearOfBirth = yearOfBirth; }
public void setBirthCity(String birthCity) { this.birthCity = birthCity; }
public void setBirthState(String birthState) { this.birthState = birthState; }
// Clone
public Player clone() { return new Player(this.id, this.name, this.height, this.weight, this.college, this.yearOfBirth, this.birthCity, this.birthState); }
// Print
public void print() {
System.out.printf("[%d ## %s ## %d ## %d ## %d ## %s ## %s ## %s]\n",
this.id, this.name, this.height, this.weight, this.yearOfBirth, this.college, this.birthCity, this.birthState);
}
// Read
public void read(String line) {
// Split line by ","
String[] splitted = line.split(",", -1);
// Fill empty attributes
for(int i = 0; i < splitted.length; i++) {
if(splitted[i].equals("")) splitted[i] = "nao informado";
}
// Set attributes
this.id = Integer.parseInt(splitted[0]);
this.name = splitted[1];
this.height = Integer.parseInt(splitted[2]);
this.weight = Integer.parseInt(splitted[3]);
this.college = splitted[4];
this.yearOfBirth = Integer.parseInt(splitted[5]);
this.birthCity = splitted[6];
this.birthState = splitted[7];
}
// ---------------------------------------------------------------------------------------------------- //
// Read all players function
public static void startPlayers() {
// 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 player
Player player = new Player();
// Read line
player.read(line);
// Add player to array
allPlayers.add(player);
}
// Close CSV file
fstream.close();
}
catch(IOException e) { e.printStackTrace(); }
}
// ---------------------------------------------------------------------------------------------------- //
// Search by id
public static Player searchById(int id, ArrayList<Player> players) {
// Search for player
for(int i = 0; i < players.size(); i++) {
if(players.get(i).getId() == id) return players.get(i);
}
return null;
}
// ---------------------------------------------------------------------------------------------------- //
public static void main(String[] args) {
// ----------------------------------------------------------------- //
// #1 - Start - Read all players in CSV file
startPlayers();
// ----------------------------------------------------------------- //
// #2 - Read input and print players from pub.in id entries and add to mainPlayers array
// Initialize mainPlayers array
ArrayList<Player> mainPlayers = new ArrayList<Player>();
// Initialize scanner
Scanner inScanner = new Scanner(System.in);
// Initialize player
Player player = new Player();
// Read first line
String line = inScanner.nextLine();
// While line is not "FIM"
while(!line.equals("FIM")) {
// Get id
int id = Integer.parseInt(line);
// Search for player
player = searchById(id, allPlayers);
// Print player
if(player != null) mainPlayers.add(player);
// Read line
line = inScanner.nextLine();
}
// ----------------------------------------------------------------- //
// #3 - Read input and search for players from pub.in name entries in mainPlayers 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")) {
Player foundPlayer = null;
// Search for player
for(int i = 0; i < mainPlayers.size(); i++) {
// Increment comparisons
comparisons++;
// If player is found
if(mainPlayers.get(i).getName().equals(line)) {
foundPlayer = mainPlayers.get(i);
break;
}
}
if(foundPlayer != 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();
}
}
// ---------------------------------------------------------------------------------------------------- //

View File

@ -0,0 +1,100 @@
NAO
NAO
SIM
NAO
SIM
SIM
SIM
SIM
SIM
NAO
SIM
SIM
SIM
SIM
NAO
SIM
NAO
SIM
SIM
SIM
SIM
SIM
NAO
NAO
NAO
SIM
NAO
NAO
NAO
NAO
NAO
NAO
NAO
NAO
SIM
NAO
SIM
NAO
NAO
SIM
SIM
SIM
NAO
SIM
NAO
NAO
NAO
NAO
NAO
SIM
SIM
NAO
SIM
SIM
SIM
NAO
SIM
SIM
SIM
SIM
SIM
SIM
SIM
SIM
NAO
NAO
SIM
NAO
NAO
NAO
NAO
NAO
NAO
NAO
SIM
NAO
SIM
NAO
SIM
NAO
NAO
NAO
NAO
SIM
NAO
NAO
SIM
NAO
NAO
NAO
NAO
SIM
SIM
SIM
SIM
SIM
NAO
SIM
SIM
NAO

View File

@ -0,0 +1,151 @@
1
104
1047
1087
1124
119
1425
1456
1487
149
1494
1525
1565
1572
1583
1702
1731
1742
1748
1786
1796
1809
1868
1880
193
1998
2011
2035
2083
217
2177
2183
2203
228
401
412
448
479
53
533
557
627
687
721
797
812
848
950
98
FIM
A.J. Bramlett
Aaron Williams
Adrian Smith
Al Ferrari
Alec Kessler
Andy Johnson
Ariel Maughan
Art Burris
Ben McDonald
Ben Poquette
Bill Ebben
Bill Laimbeer
Bob Kinney
Bob Schafer
Bones McKinney
Carl Shaeffer
Cheick Diallo
Chris Harris
Chris Welp
Cliff Barker
Craig Dykema
Curtis Kitchen
Damian Jones
Damjan Rudez
Darnell Mee
Darren Tillis
Darryl Johnson
Dejounte Murray
Devin Booker
Devyn Marble
Dick Schnittker
Dijon Thompson
Donatas Motiejunas
Ed Dahler
Eddie Phillips
Eric Riley
Frank Johnson
Frank Selvy
Gaylon Nickerson
George Reynolds
Glenn McDonald
Harold Pressley
Hassan Whiteside
Herb Williams
Hiram Fuller
Ira Newble
Jack George
Jack Molinas
James Blackwell
Jaren Jackson
Jeff Sanders
Jeremy Evans
Jerry Fleishman
Joe Buckhalter
Joe Caldwell
Joe Young
John Johnson
John Stroud
Johnny Jones
Johnny Orr
Ken Johnson
Ken Norman
Kevin Grevey
Kevin Willis
Larry Drew
Larry Sykes
Leo Kubiak
Lionel Chalmers
Lorenzen Wright
Luigi Datome
Luis Flores
Mamadou N'Diaye
Marcus Fizer
Mark Strickland
Marvin Barnes
Mason Plumlee
Michael Phelps
Michael Stewart
Mike Davis
Montrezl Harrell
Nick Fazekas
Nikola Vucevic
Nolan Smith
Ollie Johnson
Omri Casspi
Othello Hunter
Paul Walther
Peyton Siva
Predrag Drobnjak
Quincy Acy
Ralph Drollinger
Robert Horry
Ron Harper
Russ Schoene
Sarunas Marciulionis*
Stephen Thompson
Thaddeus Young
Toby Kimball
Tony Bennett
Walter Dukes
FIM

View File

@ -0,0 +1,100 @@
NAO
NAO
SIM
NAO
SIM
SIM
SIM
SIM
SIM
NAO
SIM
SIM
SIM
SIM
NAO
SIM
NAO
SIM
SIM
SIM
SIM
SIM
NAO
NAO
NAO
SIM
NAO
NAO
NAO
NAO
NAO
NAO
NAO
NAO
SIM
NAO
SIM
NAO
NAO
SIM
SIM
SIM
NAO
SIM
NAO
NAO
NAO
NAO
NAO
SIM
SIM
NAO
SIM
SIM
SIM
NAO
SIM
SIM
SIM
SIM
SIM
SIM
SIM
SIM
NAO
NAO
SIM
NAO
NAO
NAO
NAO
NAO
NAO
NAO
SIM
NAO
SIM
NAO
SIM
NAO
NAO
NAO
NAO
SIM
NAO
NAO
SIM
NAO
NAO
NAO
NAO
SIM
SIM
SIM
SIM
SIM
NAO
SIM
SIM
NAO