diff --git a/src/UE07_221024_RegExp3/ChemicalEquations.java b/src/UE07_221024_RegExp3/ChemicalEquations.java new file mode 100644 index 0000000..79ae165 --- /dev/null +++ b/src/UE07_221024_RegExp3/ChemicalEquations.java @@ -0,0 +1,68 @@ +package UE07_221024_RegExp3; + +import java.util.Map; +import java.util.Scanner; +import java.util.TreeMap; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class ChemicalEquations { + public static void main(String[] args) { +/* +HCl + Na -> NaCl + H2 +2 HCl + 2 Na -> 2 NaCl + H2 +12 CO2 + 6 H2O -> 2 C6H12O6 + 12 O2 +N2 + H2 -> NH3 +2 KClO3 -> 2 KCl + 3 O2 +CaCO3 + HCl -> CaCl2 + H2O + CO2 +C3H8 + 5 O2 -> 3 CO2 + 4 H2O +2 Ag + O2 -> 2 AgO +Fe2O3 + 3 CO -> 2 Fe + 3 CO2 +Mg + O2 -> MgO +*/ + Scanner scanner = new Scanner(System.in); + String input; + while (!(input = scanner.nextLine()).equals("end")) + System.out.println((analyze(input) ? "formally correct" : "incorrect") + "\n"); + } + + public static boolean analyze(String formular) { + String[] s = formular.split("->"); + try { + Map first = mapElements(s[0]); + Map second = mapElements(s[1]); + if (first.isEmpty() || second.isEmpty()) return false; + return first.equals(second); + } catch (Exception e) { + return false; + } + } + + public static Map mapElements(String s) { + Map elements = new TreeMap<>(); + String[] parts = s.replaceAll(" ", "").split("\\+"); + Pattern pattern = Pattern.compile("\\d*([A-Z][a-z]?|[A-Z])(\\d*)"); + Pattern patternC = Pattern.compile("^(\\d+)[a-zA-Z]+"); + int coefficient; + int number; + for (String part : parts) { + coefficient = 1; + Matcher matcher = pattern.matcher(part); + Matcher matcherC = patternC.matcher(part); + while (matcherC.find()) try { + coefficient = Integer.parseInt(matcherC.group(1)); + } catch (Exception e) { + coefficient = 1; + } + while (matcher.find()) { + try { + number = Integer.parseInt(matcher.group(2)); + } catch (Exception e) { + number = 1; + } + elements.put(matcher.group(1), elements.getOrDefault(matcher.group(1), 0) + (number * coefficient)); + } + } + return elements; + } +} diff --git a/src/UE09_191124_Rekursion2/UE09_Rekursion2.java b/src/UE09_191124_Rekursion2/UE09_Rekursion2.java new file mode 100644 index 0000000..3ea016f --- /dev/null +++ b/src/UE09_191124_Rekursion2/UE09_Rekursion2.java @@ -0,0 +1,57 @@ +package UE09_191124_Rekursion2; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; + +public class UE09_Rekursion2 { + public static void main(String[] args) { +// int[][] file1 = readFile("src/UE09_191124_Rekursion2/UE09_triangle1.txt"); +// int[][] file2 = readFile("src/UE09_191124_Rekursion2/UE09_triangle2.txt"); +// int[][] file3 = readFile("src/UE09_191124_Rekursion2/UE09_triangle.txt"); +// System.out.println("\n" + maxTotalRekursion(file1, 0, 0)); +// System.out.println("\n" + maxTotalRekursion(file2, 0, 0)); +// System.out.println("\n" + maxTotalRekursion(file3, 0, 0)); //Braucht ewig lange, da File sehr groß + System.out.println(collatz()); + } + + public static int[][] readFile(String file) { + try { + List lines = Files.readAllLines(Path.of(file)); + int[][] output = new int[lines.size()][]; + for (int i = 0; i < output.length; i++) { + String[] line = lines.get(i).trim().split(" "); + output[i] = new int[line.length]; + for (int j = 0; j < line.length; j++) output[i][j] = Integer.parseInt(line[j]); + } + return output; + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + public static long maxTotalRekursion(int[][] input, int x, int y) { + return (x >= input.length - 1) ? input[x][y] : Long.max(maxTotalRekursion(input, x + 1, y), maxTotalRekursion(input, x + 1, y + 1)) + input[x][y]; + } + + public static long collatz() { + long startingNumber = 0, maxLength = 0; + for (int i = 1; i < 1000000; i++) { + long length = longestCollatzSequence(new ArrayList<>(), i).size(); + if (length > maxLength) { + maxLength = length; + startingNumber = i; + } + } + return startingNumber; + } + + public static List longestCollatzSequence(List l, long n) { + if (n == 1) return l; + l.add(n); + n = ((n & 1) == 0) ? (n / 2) : (3 * n + 1); + return longestCollatzSequence(l, n); + } +} diff --git a/src/UE09_191124_Rekursion2/UE09_triangle1.txt b/src/UE09_191124_Rekursion2/UE09_triangle1.txt new file mode 100644 index 0000000..ac777c8 --- /dev/null +++ b/src/UE09_191124_Rekursion2/UE09_triangle1.txt @@ -0,0 +1,4 @@ +3 +7 4 +2 4 6 +8 5 9 3 \ No newline at end of file diff --git a/src/UE09_191124_Rekursion2/UE09_triangle2.txt b/src/UE09_191124_Rekursion2/UE09_triangle2.txt new file mode 100644 index 0000000..bc0ed83 --- /dev/null +++ b/src/UE09_191124_Rekursion2/UE09_triangle2.txt @@ -0,0 +1,15 @@ +75 +95 64 +17 47 82 +18 35 87 10 +20 04 82 47 65 +19 01 23 75 03 34 +88 02 77 73 07 63 67 +99 65 04 28 06 16 70 92 +41 41 26 56 83 40 80 70 33 +41 48 72 33 47 32 37 16 94 29 +53 71 44 65 25 43 91 52 97 51 14 +70 11 33 28 77 73 17 78 39 68 17 57 +91 71 52 38 17 14 91 43 58 50 27 29 48 +63 66 04 68 89 53 67 30 73 16 69 87 40 31 +04 62 98 27 23 09 70 98 73 93 38 53 60 04 23 \ No newline at end of file