Skip to content

Commit

Permalink
Commited files
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan-Shindov committed Sep 21, 2021
1 parent 8fe9422 commit db3e0b3
Show file tree
Hide file tree
Showing 41 changed files with 1,495 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions src/AssocArraysLambdaAndStreams/ExerciseSolutions/AMinerTask.java
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())));
}
}
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)));
});
}
}
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 src/AssocArraysLambdaAndStreams/ExerciseSolutions/Courses.java
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 src/AssocArraysLambdaAndStreams/ExerciseSolutions/Demo.java
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 src/AssocArraysLambdaAndStreams/ExerciseSolutions/ForceBook.java
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));
});
}
}
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 src/AssocArraysLambdaAndStreams/ExerciseSolutions/Orders.java
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));
}
}
}
Loading

0 comments on commit db3e0b3

Please sign in to comment.