-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8fe9422
commit db3e0b3
Showing
41 changed files
with
1,495 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
28 changes: 28 additions & 0 deletions
28
src/AssocArraysLambdaAndStreams/ExerciseSolutions/AMinerTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
import java.util.Scanner; | ||
|
||
public class AMinerTask { | ||
public static void main(String[] args) { | ||
Scanner scanner = new Scanner(System.in); | ||
|
||
String input = scanner.nextLine(); | ||
|
||
LinkedHashMap<String, Integer> resources = new LinkedHashMap<>(); | ||
while (!input.equals("stop")) { | ||
int quantity = Integer.parseInt(scanner.nextLine()); | ||
|
||
resources.putIfAbsent(input, 0); | ||
int newQuantity = resources.get(input) + quantity; | ||
resources.put(input, newQuantity); | ||
|
||
input = scanner.nextLine(); | ||
} | ||
|
||
resources | ||
.entrySet() | ||
.forEach(e -> System.out.println(String.format("%s -> %d", | ||
e.getKey() | ||
, e.getValue()))); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/AssocArraysLambdaAndStreams/ExerciseSolutions/CompanyUsers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import org.w3c.dom.ls.LSOutput; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Scanner; | ||
import java.util.TreeMap; | ||
|
||
public class CompanyUsers { | ||
public static void main(String[] args) { | ||
|
||
Scanner scanner = new Scanner(System.in); | ||
|
||
String input = scanner.nextLine(); | ||
TreeMap<String, ArrayList<String>> company = new TreeMap<>(); | ||
|
||
while(!input.equals("End")) { | ||
String[] tokens = input.split(" -> "); | ||
String companyName = tokens[0]; | ||
String employeeID = tokens[1]; | ||
|
||
company.putIfAbsent(companyName, new ArrayList<>()); | ||
if (!company.get(companyName).contains(employeeID)) { | ||
company.get(companyName).add(employeeID); | ||
} | ||
|
||
input = scanner.nextLine(); | ||
} | ||
|
||
company.entrySet() | ||
.stream() | ||
.forEach(c -> { | ||
System.out.println(String.format("%s",c.getKey())); | ||
c.getValue() | ||
.stream() | ||
.forEach(e -> System.out.println(String.format("-- %s" | ||
,e))); | ||
}); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/AssocArraysLambdaAndStreams/ExerciseSolutions/CountCharsInString.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
import java.util.Scanner; | ||
|
||
public class CountCharsInString { | ||
public static void main(String[] args) { | ||
Scanner scanner = new Scanner(System.in); | ||
|
||
char[] text = scanner.nextLine().toCharArray(); | ||
LinkedHashMap<Character,Integer> count = new LinkedHashMap<>(); | ||
|
||
for (int i = 0; i < text.length; i++) { | ||
if (text[i] == ' ') { | ||
continue; | ||
} | ||
|
||
count.putIfAbsent(text[i], 0); | ||
int newCount = count.get(text[i]) + 1; | ||
count.put(text[i], newCount); | ||
} | ||
|
||
for (Map.Entry<Character, Integer> entry : count.entrySet()) { | ||
System.out.println(String.format("%s -> %s", entry.getKey(), entry.getValue())); | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/AssocArraysLambdaAndStreams/ExerciseSolutions/Courses.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import java.util.ArrayList; | ||
import java.util.LinkedHashMap; | ||
import java.util.Scanner; | ||
|
||
public class Courses { | ||
public static void main(String[] args) { | ||
Scanner scanner = new Scanner(System.in); | ||
|
||
String input = scanner.nextLine(); | ||
LinkedHashMap<String, ArrayList<String>> courses = new LinkedHashMap<>(); | ||
|
||
while (!input.equals("end")) { | ||
String[] tokens = input.split(" : "); | ||
String courseName = tokens[0]; | ||
String studentName = tokens[1]; | ||
|
||
courses.putIfAbsent(courseName, new ArrayList<>()); | ||
courses.get(courseName).add(studentName); | ||
|
||
input = scanner.nextLine(); | ||
} | ||
courses.entrySet() | ||
.stream() | ||
.sorted((s1, s2) -> Integer.compare(s2.getValue().size(), s1.getValue().size())) | ||
.forEach(c -> { | ||
System.out.println(String.format("%s: %d" | ||
, c.getKey() | ||
, c.getValue().size())); | ||
c.getValue() | ||
.stream() | ||
.sorted((s1, s2) -> s1.compareTo(s2)) | ||
.forEach(s -> System.out.println(String.format("-- %s", s))); | ||
}); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/AssocArraysLambdaAndStreams/ExerciseSolutions/Demo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import java.util.Scanner; | ||
|
||
public class Demo { | ||
public static void main(String[] args) { | ||
Scanner scanner = new Scanner(System.in); | ||
|
||
String line = scanner.nextLine(); | ||
|
||
if (line.contains("|")) { | ||
line = line.toUpperCase(); | ||
} else if (line.contains("->")) { | ||
line = line.replace('-', '>'); | ||
} | ||
System.out.println(line); | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
src/AssocArraysLambdaAndStreams/ExerciseSolutions/ForceBook.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import java.util.ArrayList; | ||
import java.util.Map; | ||
import java.util.Scanner; | ||
import java.util.TreeMap; | ||
|
||
public class ForceBook { | ||
public static void main(String[] args) { | ||
Scanner scanner = new Scanner(System.in); | ||
|
||
String input = scanner.nextLine(); | ||
TreeMap<String, ArrayList<String>> forceUsers = new TreeMap<>(); | ||
|
||
while(!input.equals("Lumpawaroo")) { | ||
String[] tokens; | ||
String user; | ||
String forceSide; | ||
|
||
if (input.contains("|")) { | ||
tokens = input.split(" \\| "); | ||
forceSide = tokens[0]; | ||
user = tokens[1]; | ||
|
||
boolean check = false; | ||
|
||
for(Map.Entry<String, ArrayList<String>> entry : forceUsers.entrySet()){ | ||
if(entry.getValue().contains(user)){ | ||
check = true; | ||
break; | ||
} | ||
} | ||
|
||
if(!check){ | ||
if(!forceUsers.containsKey(forceSide)){ | ||
forceUsers.put(forceSide, new ArrayList<>()); | ||
forceUsers.get(forceSide).add(user); | ||
} | ||
else if(forceUsers.containsKey(forceSide) && !forceUsers.get(forceSide).contains(user)){ | ||
forceUsers.get(forceSide).add(user); | ||
} | ||
} | ||
|
||
} else if (input.contains("->")) { | ||
tokens = input.split(" -> "); | ||
user = tokens[0]; | ||
forceSide = tokens[1]; | ||
|
||
for (Map.Entry<String, ArrayList<String>> entry : forceUsers.entrySet()) { | ||
if (entry.getValue().contains(user)) { | ||
forceUsers.get(entry.getKey()).remove(user); | ||
break; | ||
} | ||
} | ||
|
||
if (!forceUsers.containsKey(forceSide)) { | ||
forceUsers.put(forceSide, new ArrayList<>()); | ||
forceUsers.get(forceSide).add(user); | ||
System.out.printf("%s joins the %s side!%n", user, forceSide); | ||
} | ||
else if (forceUsers.containsKey(forceSide) && !forceUsers.get(forceSide).contains(user)) { | ||
forceUsers.get(forceSide).add(user); | ||
System.out.printf("%s joins the %s side!%n", user, forceSide); | ||
} | ||
} | ||
|
||
input = scanner.nextLine(); | ||
} | ||
|
||
forceUsers.entrySet() | ||
.stream() | ||
.filter(u -> u.getValue().size() > 0) | ||
.sorted((x1,x2) -> Integer.compare(x2.getValue().size(),x1.getValue().size())) | ||
.forEach(u -> { | ||
System.out.println(String.format("Side: %s, Members: %d" | ||
, u.getKey() | ||
, u.getValue().size())); | ||
u.getValue() | ||
.stream() | ||
.sorted((x1,x2) -> x1.compareTo(x2)) | ||
.forEach(p -> System.out.printf("! %s%n", p)); | ||
}); | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
src/AssocArraysLambdaAndStreams/ExerciseSolutions/LegendaryFarming.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import java.util.Scanner; | ||
import java.util.TreeMap; | ||
|
||
public class LegendaryFarming { | ||
public static void main(String[] args) { | ||
Scanner scanner = new Scanner(System.in); | ||
|
||
TreeMap<String, Integer> items = new TreeMap<>() {{ | ||
put("shards", 0); | ||
put("fragments", 0); | ||
put("motes", 0); | ||
}}; | ||
TreeMap<String, Integer> junk = new TreeMap<>(); | ||
|
||
boolean isFound = false; | ||
|
||
while (!isFound) { | ||
String[] tokens = scanner.nextLine().split("\\s+"); | ||
for (int i = 0; i < tokens.length; i += 2) { | ||
int count = Integer.parseInt(tokens[i]); | ||
String material = tokens[i + 1].toLowerCase(); | ||
|
||
if (items.containsKey(material)) { | ||
addMaterial(items, material, count); | ||
isFound = hasLegendaryItem(items, material); | ||
if (isFound) { | ||
break; | ||
} | ||
} else { | ||
addMaterial(junk, material, count); | ||
} | ||
} | ||
} | ||
items.entrySet() | ||
.stream() | ||
.sorted((i1,i2) -> Integer.compare(i2.getValue(), i1.getValue())) | ||
.forEach(i -> System.out.println(String.format("%s: %d" | ||
,i.getKey() | ||
,i.getValue()))); | ||
|
||
junk.entrySet() | ||
.forEach(j -> System.out.println(String.format("%s: %d" | ||
,j.getKey() | ||
,j.getValue()))); | ||
} | ||
|
||
private static boolean hasLegendaryItem(TreeMap<String, Integer> items, String key) { | ||
int count = items.get(key); | ||
if (count >= 250) { | ||
items.put(key, count - 250); | ||
switch (key) { | ||
case "shards": | ||
System.out.println("Shadowmourne obtained!"); | ||
return true; | ||
case "fragments": | ||
System.out.println("Valanyr obtained!"); | ||
return true; | ||
case "motes": | ||
System.out.println("Dragonwrath obtained!"); | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
|
||
private static void addMaterial(TreeMap<String, Integer> items, String key, int value) { | ||
items.putIfAbsent(key, 0); | ||
int newCount = items.get(key) + value; | ||
items.put(key, newCount); | ||
} | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
src/AssocArraysLambdaAndStreams/ExerciseSolutions/Orders.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
import java.util.Scanner; | ||
|
||
public class Orders { | ||
public static void main(String[] args) { | ||
Scanner scanner = new Scanner(System.in); | ||
|
||
String input = scanner.nextLine(); | ||
LinkedHashMap<String, Double> products = new LinkedHashMap<>(); | ||
LinkedHashMap<String, Integer> productsQuantity = new LinkedHashMap<>(); | ||
|
||
while (!input.equals("buy")) { | ||
String[] tokens = input.split("\\s+"); | ||
String nameProduct = tokens[0]; | ||
double price = Double.parseDouble(tokens[1]); | ||
int quantity = Integer.parseInt(tokens[2]); | ||
|
||
if (!products.containsKey(nameProduct)) { | ||
products.put(nameProduct, price); | ||
productsQuantity.put(nameProduct, quantity); | ||
|
||
} else { | ||
int newQuantity = productsQuantity.get(nameProduct) + quantity; | ||
productsQuantity.put(nameProduct, newQuantity); | ||
|
||
if (products.get(nameProduct) != price) { | ||
products.put(nameProduct, price); | ||
} | ||
} | ||
input = scanner.nextLine(); | ||
} | ||
|
||
for (Map.Entry<String, Double> entry : products.entrySet()) { | ||
double price = products.get(entry.getKey()) * productsQuantity.get(entry.getKey()); | ||
System.out.println(String.format("%s -> %.2f", entry.getKey(), price)); | ||
} | ||
} | ||
} |
Oops, something went wrong.