From dc3d7b44e7e6e68a869e105e13128a87d5882474 Mon Sep 17 00:00:00 2001 From: pedrovfigueiredo Date: Sun, 25 Feb 2018 00:57:51 +0100 Subject: [PATCH] Solutions --- BasicJavaRecap-2.iml | 12 + out/production/BasicJavaRecap-2/dummy1.txt | 3 + out/production/BasicJavaRecap-2/dummy2.txt | 3 + out/production/BasicJavaRecap-2/dummy3.txt | 4 + out/production/BasicJavaRecap-2/filenames.txt | 3 + out/production/BasicJavaRecap-2/numbers1.txt | 10 + out/production/BasicJavaRecap-2/numbers2.txt | 7 + out/production/BasicJavaRecap-2/numbers3.txt | 6 + src/com/elte/Board.java | 67 +++++ src/com/elte/ComparatorInts.java | 18 ++ src/com/elte/Coordinate.java | 29 ++ src/com/elte/Main.java | 282 ++++++++++++++++++ src/com/elte/Match.java | 34 +++ src/com/elte/Player.java | 22 ++ src/com/elte/Tournament.java | 53 ++++ src/dummy1.txt | 3 + src/dummy2.txt | 3 + src/dummy3.txt | 4 + src/filenames.txt | 3 + src/numbers1.txt | 10 + src/numbers2.txt | 7 + src/numbers3.txt | 6 + 22 files changed, 589 insertions(+) create mode 100644 BasicJavaRecap-2.iml create mode 100644 out/production/BasicJavaRecap-2/dummy1.txt create mode 100644 out/production/BasicJavaRecap-2/dummy2.txt create mode 100644 out/production/BasicJavaRecap-2/dummy3.txt create mode 100644 out/production/BasicJavaRecap-2/filenames.txt create mode 100644 out/production/BasicJavaRecap-2/numbers1.txt create mode 100644 out/production/BasicJavaRecap-2/numbers2.txt create mode 100644 out/production/BasicJavaRecap-2/numbers3.txt create mode 100644 src/com/elte/Board.java create mode 100644 src/com/elte/ComparatorInts.java create mode 100644 src/com/elte/Coordinate.java create mode 100644 src/com/elte/Main.java create mode 100644 src/com/elte/Match.java create mode 100644 src/com/elte/Player.java create mode 100644 src/com/elte/Tournament.java create mode 100644 src/dummy1.txt create mode 100644 src/dummy2.txt create mode 100644 src/dummy3.txt create mode 100644 src/filenames.txt create mode 100644 src/numbers1.txt create mode 100644 src/numbers2.txt create mode 100644 src/numbers3.txt diff --git a/BasicJavaRecap-2.iml b/BasicJavaRecap-2.iml new file mode 100644 index 0000000..d5c0743 --- /dev/null +++ b/BasicJavaRecap-2.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/out/production/BasicJavaRecap-2/dummy1.txt b/out/production/BasicJavaRecap-2/dummy1.txt new file mode 100644 index 0000000..2ddb5b9 --- /dev/null +++ b/out/production/BasicJavaRecap-2/dummy1.txt @@ -0,0 +1,3 @@ +This is a test file. +It's content is going to be copied do the dummy2 file. +Let's test it out! \ No newline at end of file diff --git a/out/production/BasicJavaRecap-2/dummy2.txt b/out/production/BasicJavaRecap-2/dummy2.txt new file mode 100644 index 0000000..4414893 --- /dev/null +++ b/out/production/BasicJavaRecap-2/dummy2.txt @@ -0,0 +1,3 @@ +This is a test file. +It's content is going to be copied do the dummy2 file. +Let's test it out! diff --git a/out/production/BasicJavaRecap-2/dummy3.txt b/out/production/BasicJavaRecap-2/dummy3.txt new file mode 100644 index 0000000..4eaf23f --- /dev/null +++ b/out/production/BasicJavaRecap-2/dummy3.txt @@ -0,0 +1,4 @@ + +!tuo ti tset s'teL +.elif 2ymmud eht od deipoc eb ot gniog si tnetnoc s'tI +.elif tset a si sihT \ No newline at end of file diff --git a/out/production/BasicJavaRecap-2/filenames.txt b/out/production/BasicJavaRecap-2/filenames.txt new file mode 100644 index 0000000..179fc87 --- /dev/null +++ b/out/production/BasicJavaRecap-2/filenames.txt @@ -0,0 +1,3 @@ +src/numbers1.txt +src/numbers2.txt +src/numbers3.txt \ No newline at end of file diff --git a/out/production/BasicJavaRecap-2/numbers1.txt b/out/production/BasicJavaRecap-2/numbers1.txt new file mode 100644 index 0000000..8808cab --- /dev/null +++ b/out/production/BasicJavaRecap-2/numbers1.txt @@ -0,0 +1,10 @@ +-12 +0 +302 +21 +10 +11 +12 +13 +14 +1 \ No newline at end of file diff --git a/out/production/BasicJavaRecap-2/numbers2.txt b/out/production/BasicJavaRecap-2/numbers2.txt new file mode 100644 index 0000000..ab4a930 --- /dev/null +++ b/out/production/BasicJavaRecap-2/numbers2.txt @@ -0,0 +1,7 @@ +-20 +1 +300 +20 +10 +-2 +-30000 \ No newline at end of file diff --git a/out/production/BasicJavaRecap-2/numbers3.txt b/out/production/BasicJavaRecap-2/numbers3.txt new file mode 100644 index 0000000..521f5da --- /dev/null +++ b/out/production/BasicJavaRecap-2/numbers3.txt @@ -0,0 +1,6 @@ +-200 +10 +300 +20 +10 +-200 \ No newline at end of file diff --git a/src/com/elte/Board.java b/src/com/elte/Board.java new file mode 100644 index 0000000..7486fb0 --- /dev/null +++ b/src/com/elte/Board.java @@ -0,0 +1,67 @@ +package com.elte; + +import java.util.Arrays; +import java.util.List; + +public class Board { + + boolean[][] board_; + int nLines_; + int nColumns_; + + public int getnLines_() { + return nLines_; + } + + @Override + public String toString() { + + StringBuilder board = new StringBuilder(); + + for (int line = 0; line < this.nLines_; line++){ + for (int column = 0; column < this.nColumns_; column++){ + if (!this.board_[line][column]){ + board.append('.'); + }else{ + board.append('X'); + } + board.append('\t'); + } + board.append('\n'); + } + return board.toString(); + } + + public int getnColumns_() { + return nColumns_; + } + + public Board(int lines, int columns) { + this.board_ = new boolean[lines][columns]; + this.nLines_ = lines; + this.nColumns_ = columns; + } + + public boolean markHitFields(List coords){ + boolean returnStatus = true; + for (Coordinate coord: coords) { + if (!this.markHitField(coord)){ + returnStatus = false; + } + } + return returnStatus; + } + + public boolean markHitField(Coordinate a){ + if (((int) a.getX() < 0) || ((int) a.getX() >= this.nLines_) || + ((int) a.getY() < 0) || ((int) a.getY() >= this.nColumns_) || + this.board_[(int) a.getX()][(int) a.getY()]){ + // Invalid hit + System.out.println("Invalid hit. Unconsidered."); + return false; + } + + this.board_[(int) a.getX()][(int) a.getY()] = true; + return true; + } +} diff --git a/src/com/elte/ComparatorInts.java b/src/com/elte/ComparatorInts.java new file mode 100644 index 0000000..bfaccc4 --- /dev/null +++ b/src/com/elte/ComparatorInts.java @@ -0,0 +1,18 @@ +package com.elte; + +import java.util.Comparator; +import java.util.Map; + +public class ComparatorInts implements Comparator { + + Map base; + + public ComparatorInts(Map base) { + this.base = base; + } + + @Override + public int compare(String s1, String s2) { + return base.get(s1).compareTo(base.get(s2)); + } +} diff --git a/src/com/elte/Coordinate.java b/src/com/elte/Coordinate.java new file mode 100644 index 0000000..a9b2696 --- /dev/null +++ b/src/com/elte/Coordinate.java @@ -0,0 +1,29 @@ +package com.elte; + +public class Coordinate { + + private double x_; + private double y_; + + public Coordinate(double x, double y){ + x_ = x; + y_ = y; + } + + public String toString() { + return "(" + Double.toString(x_) + "," + Double.toString(y_) + ")"; + } + + double getX(){ + return x_; + } + + double getY(){ + return y_; + } + + double getDistanceFrom(Coordinate b){ + return Math.sqrt( Math.pow((double)(b.getX() - x_), 2) + Math.pow((double)(b.getY() - y_), 2)); + } +} + diff --git a/src/com/elte/Main.java b/src/com/elte/Main.java new file mode 100644 index 0000000..33c2a93 --- /dev/null +++ b/src/com/elte/Main.java @@ -0,0 +1,282 @@ +package com.elte; + + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.PrintWriter; +import java.net.Inet4Address; +import java.util.*; + +public class Main { + public static void main(String[] args) { + + // 1 + + String[] argsCoordinates = {"2.4","2", "1", "1", "10", "9.8", "-9", "-9.8"}; + List coords = parseCoordinatesFromCommandLine(argsCoordinates); + + System.out.println("\nNumber 1:\n"); + Coordinate auxiliarCoord = new Coordinate(0f,0f); + + for (Coordinate coord : coords) { + if (coord.getDistanceFrom(new Coordinate(0,0)) > auxiliarCoord.getDistanceFrom(new Coordinate(0,0))){ + auxiliarCoord = coord; + } + } + + System.out.println("Furthest point from (0,0) is: " + auxiliarCoord); + + // 2 + System.out.println("\nNumber 2:\n"); + + auxiliarCoord = coords.get(0); + + for (int i = 1; i < coords.size(); i++) { + if (coords.get(i).getDistanceFrom(coords.get(0)) > auxiliarCoord.getDistanceFrom(coords.get(0))){ + auxiliarCoord = coords.get(i); + } + } + + System.out.println("Furthest point from " + coords.get(0) + " is: " + auxiliarCoord); + + // 3 + System.out.println("\nNumber 3:\n"); + + String[] argsA = {"aajsdvhaaaaj","aaa3321asd", "1asssaaasda", "1asasassasasasasasaas"}; + + System.out.println("Without repetition: "); + for (String arg : argsA){ + System.out.println(arg.replaceAll("[a]+", "a")); + } + + // 4 + System.out.println("\nNumber 4:\n"); + + String[] argsText = {"My name is Pedro. I am 20 years old.","y0", "a1", "m6"}; + + String text = argsText[0]; + + for (int i = 1; i < argsText.length; i++){ + text = text.replaceAll(String.valueOf((argsText[i].toCharArray())[0]), + String.valueOf((argsText[i].toCharArray())[1])); + } + + System.out.println("Text after alterations: " + text); + + // 5 + System.out.println("\nNumber 5:\n"); + + int argsN = 10; + + System.out.print("Pascal's up to " + argsN + " row: \n"); + for (int i = 1 ; i < argsN; i++){ + for (Integer element: getRowPascalTriangle(i)) { + System.out.print(element + " "); + } + System.out.println(); + } + + // 6 + System.out.println("\nNumber 6:\n"); + + String[] argsBoard = {"10","20", "3", "3.2", "4", "2", "9.9", "19.9", "6", "0", "3", "17", "4", "9"}; + + Board board = new Board(Integer.parseInt(argsBoard[0]),Integer.parseInt(argsBoard[1])); + coords = parseCoordinatesFromCommandLine(Arrays.copyOfRange(argsBoard, 2, argsBoard.length)); + if (board.markHitFields(coords)){ + System.out.println("All coordinates are valid.\n Printing board:"); + }else{ + System.out.println("One or more coordinates aren't valid.\n Printing board:"); + } + System.out.println(board); + + // 7 + System.out.println("\nNumber 7:\n"); + + String[] argsTournament = {"8", "J1", "J2", "J3", "J4", "J5", "J6", "J7", "J8", + "1", "1", "1", "2", "1", "2", "2"}; + + Tournament t1 = new Tournament(argsTournament); + t1.printMatches(); + + // Homework + Scanner sc = new Scanner(System.in); + + System.out.println("\nHomework - Number 1:\n"); + + // 1 + System.out.println("Type anything:"); + String nextLine1 = sc.nextLine(); + System.out.println(nextLine1); + + + System.out.println("Type two integers: "); + int a = sc.nextInt(); + int b = sc.nextInt(); + System.out.println("Sum: " + (a + b)); + + sc.nextLine(); + System.out.println("Type a phrase:"); + String nextLine2 = sc.nextLine(); + System.out.println("Number of characters: " + nextLine2.toCharArray().length); + System.out.println("Number of words: " + nextLine2.split("[ ]+").length); + + System.out.println("Type a number for Fibonacci and Pascal calculation: "); + int n = sc.nextInt(); + System.out.println("Fibonacci's " + n + " number: " + getNthFib(n)); + System.out.print("Pascal's " + n + " row: "); + for (Integer element: getRowPascalTriangle(n)) { + System.out.print(element + " "); + } + System.out.println(); + + + // 2 + System.out.println("\nHomework - Number 2:\n"); + + PrintWriter pw1; + PrintWriter pw2; + + try { + sc.close(); + sc = new Scanner(new File("src/dummy1.txt")); + pw1 = new PrintWriter("src/dummy2.txt"); + pw2 = new PrintWriter("src/dummy3.txt"); + } catch (FileNotFoundException e) { + System.out.println("Error while reading/writing on files."); + e.printStackTrace(); + return; + } + + // Normal + while (sc.hasNext()){ + pw1.println(sc.nextLine()); + } + pw1.close(); + + sc.close(); + try{sc = new Scanner(new File("src/dummy1.txt"));} catch (FileNotFoundException e){e.printStackTrace();return;} + + // Reversed + Stack stack = new Stack<>(); + while (sc.hasNext()){ + for (Character c : sc.nextLine().toCharArray()){ + stack.push(c); + } + stack.push('\n'); + } + + while (!stack.empty()){ + pw2.print(stack.pop()); + } + + pw2.close(); + sc.close(); + + System.out.println("Finished copying to files."); + + + // 3 + System.out.println("\nHomework - Number 3:\n"); + + try{sc = new Scanner(new File("src/filenames.txt"));} catch (FileNotFoundException e){e.printStackTrace();return;} + + List filenames = new ArrayList<>(); + + while(sc.hasNext()){ + filenames.add(sc.nextLine()); + } + + sc.close(); + + Integer smallestNumber = Integer.MAX_VALUE; + int smallestNumberIndex = -1; + List allNumbers = new ArrayList<>(); + Set auxSet = new HashSet<>(); + List nonDuplicateFilenamesIndexes = new ArrayList<>(); + Map< String ,Integer > fileSums = new HashMap<>(); + + for (int i = 0; i < filenames.size(); i++){ + try{sc = new Scanner(new File(filenames.get(i)));} catch (FileNotFoundException e){e.printStackTrace();return;} + int sum = 0; + while (sc.hasNext()){ + int nextInt = sc.nextInt(); + allNumbers.add(nextInt); + auxSet.add(nextInt); + sum += nextInt; + + if (nextInt < smallestNumber){ + smallestNumber = nextInt; + smallestNumberIndex = i; + } + } + + fileSums.put(filenames.get(i), sum); + if (auxSet.size() == allNumbers.size()){ + nonDuplicateFilenamesIndexes.add(i); + } + auxSet.clear(); + allNumbers.clear(); + } + + ComparatorInts cp = new ComparatorInts(fileSums); + + Map< String ,Integer > orderedMap = new TreeMap<>(cp); + orderedMap.putAll(fileSums); + + + System.out.println("Smallest number filename: " + filenames.get(smallestNumberIndex)); + System.out.print("Non duplicate filenames: "); + for (Integer index : nonDuplicateFilenamesIndexes){ + System.out.print(filenames.get(index) + " "); + } + System.out.println(); + System.out.println("Sum of filenames by increasing order: "); + for (String filename: orderedMap.keySet()) { + System.out.println("Filename: " + filename + " Sum: " + fileSums.get(filename)); + } + + + + } + + private static List getRowPascalTriangle(int n){ + List row = new ArrayList<>(); + + row.add(1); + + for (int k = 0; k < n; k++){ + row.add(row.get(k) * (n - k) / (k + 1)); + } + + return row; + } + + private static int getNthFib(int n){ + int i = 0; + int j = 1; + + for (int k = 2; k < n; k++){ + int aux = j; + j = i + j; + i = aux; + } + return j; + } + + public static List parseCoordinatesFromCommandLine(String[] s){ + // Adding coordinates from command-line + List coords = new ArrayList<>(); + + double aux = 0f; + for (int i = 1; i <= s.length; i++) { + if (i % 2 == 0) { + coords.add(new Coordinate(aux, Double.parseDouble(s[i - 1]))); + } else { + aux = Double.parseDouble(s[i - 1]); + } + } + return coords; + } +} + diff --git a/src/com/elte/Match.java b/src/com/elte/Match.java new file mode 100644 index 0000000..84651e4 --- /dev/null +++ b/src/com/elte/Match.java @@ -0,0 +1,34 @@ +package com.elte; + +public class Match { + Player winner_; + Player loser_; + int round_; + boolean isFinal_; + + public Player getWinner_() { + return winner_; + } + + @Override + public String toString() { + if (!isFinal_){ + return "round " + round_ + ", players: " + winner_ + " and " + loser_ + + ", advances: " + winner_ + ", eliminated: " + loser_; + }else{ + return "round " + round_ + " (final), players: " + winner_ + " and " + loser_ + + ", champion: " + winner_ + ", finalist: " + loser_; + } + } + + public Player getLoser_() { + return loser_; + } + + public Match(Player winner, Player loser, int round, boolean isFinal) { + this.winner_ = winner; + this.loser_ = loser; + this.round_ = round; + this.isFinal_ = isFinal; + } +} diff --git a/src/com/elte/Player.java b/src/com/elte/Player.java new file mode 100644 index 0000000..f46eab6 --- /dev/null +++ b/src/com/elte/Player.java @@ -0,0 +1,22 @@ +package com.elte; + +public class Player { + String name_; + + public Player(String name) { + this.name_ = name; + } + + @Override + public String toString() { + return this.name_; + } + + public String getName_() { + return name_; + } + + public void setName_(String name_) { + this.name_ = name_; + } +} diff --git a/src/com/elte/Tournament.java b/src/com/elte/Tournament.java new file mode 100644 index 0000000..60f0bd5 --- /dev/null +++ b/src/com/elte/Tournament.java @@ -0,0 +1,53 @@ +package com.elte; + +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class Tournament { + List players_; + List matches_; + + public Tournament(String[] arguments) { + + this.players_ = new ArrayList<>(); + this.matches_ = new ArrayList<>(); + Queue auxiliaryQueue = new LinkedList<>(); + + int nPlayers = Integer.parseInt(arguments[0]); + + for (int i = 1; i <= nPlayers; i++) { + Player p = new Player(arguments[i]); + this.players_.add(p); + auxiliaryQueue.add(p); + } + + int threshold = nPlayers + 1; + for (int i = threshold, round = 1; i < arguments.length; i++){ + + // Round increase + if (i - (threshold) >= nPlayers/(Math.pow(2,round))){ + threshold = i; + round++; + } + + Player p1 = auxiliaryQueue.remove(); + Player p2 = auxiliaryQueue.remove(); + + if (arguments[i] == "1"){ + matches_.add(new Match(p1,p2, round, i == arguments.length - 1 )); + auxiliaryQueue.add(p1); + }else{ + matches_.add(new Match(p2,p1, round, i == arguments.length - 1 )); + auxiliaryQueue.add(p2); + } + } + } + + public void printMatches(){ + for (Match match: matches_) { + System.out.println(match); + } + } +} diff --git a/src/dummy1.txt b/src/dummy1.txt new file mode 100644 index 0000000..2ddb5b9 --- /dev/null +++ b/src/dummy1.txt @@ -0,0 +1,3 @@ +This is a test file. +It's content is going to be copied do the dummy2 file. +Let's test it out! \ No newline at end of file diff --git a/src/dummy2.txt b/src/dummy2.txt new file mode 100644 index 0000000..4414893 --- /dev/null +++ b/src/dummy2.txt @@ -0,0 +1,3 @@ +This is a test file. +It's content is going to be copied do the dummy2 file. +Let's test it out! diff --git a/src/dummy3.txt b/src/dummy3.txt new file mode 100644 index 0000000..4eaf23f --- /dev/null +++ b/src/dummy3.txt @@ -0,0 +1,4 @@ + +!tuo ti tset s'teL +.elif 2ymmud eht od deipoc eb ot gniog si tnetnoc s'tI +.elif tset a si sihT \ No newline at end of file diff --git a/src/filenames.txt b/src/filenames.txt new file mode 100644 index 0000000..179fc87 --- /dev/null +++ b/src/filenames.txt @@ -0,0 +1,3 @@ +src/numbers1.txt +src/numbers2.txt +src/numbers3.txt \ No newline at end of file diff --git a/src/numbers1.txt b/src/numbers1.txt new file mode 100644 index 0000000..8808cab --- /dev/null +++ b/src/numbers1.txt @@ -0,0 +1,10 @@ +-12 +0 +302 +21 +10 +11 +12 +13 +14 +1 \ No newline at end of file diff --git a/src/numbers2.txt b/src/numbers2.txt new file mode 100644 index 0000000..ab4a930 --- /dev/null +++ b/src/numbers2.txt @@ -0,0 +1,7 @@ +-20 +1 +300 +20 +10 +-2 +-30000 \ No newline at end of file diff --git a/src/numbers3.txt b/src/numbers3.txt new file mode 100644 index 0000000..521f5da --- /dev/null +++ b/src/numbers3.txt @@ -0,0 +1,6 @@ +-200 +10 +300 +20 +10 +-200 \ No newline at end of file