Skip to content
This repository was archived by the owner on Dec 28, 2024. It is now read-only.

Commit 050163f

Browse files
committed
Практические работы 27-30
1 parent f3cc5b9 commit 050163f

File tree

21 files changed

+688
-0
lines changed

21 files changed

+688
-0
lines changed

students/23K0354/23K0354-p27/pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<artifactId>23K0354</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0354-p27</artifactId>
12+
<description> Изучение Java Collection Framework: нелинейные структуры данных </description>
13+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package ru.mirea.practices0000001.task1;
2+
3+
public abstract class Application {
4+
public static void main(String[] args) {
5+
SimpleHashTable myHashTable = new SimpleHashTable(10);
6+
myHashTable.insert("item1", "data1");
7+
myHashTable.insert("item2", "data2");
8+
System.out.println(myHashTable.retrieve("item1"));
9+
myHashTable.remove("item1");
10+
System.out.println(myHashTable.retrieve("item1"));
11+
}
12+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package ru.mirea.practices0000001.task1;
2+
3+
import java.util.LinkedList;
4+
5+
class SimpleHashTable {
6+
private LinkedList<HashEntry>[] buckets;
7+
private int capacity;
8+
9+
private static class HashEntry {
10+
String identifier;
11+
String data;
12+
13+
HashEntry(String identifier, String data) {
14+
this.identifier = identifier;
15+
this.data = data;
16+
}
17+
}
18+
19+
public SimpleHashTable(int capacity) {
20+
this.capacity = capacity;
21+
buckets = new LinkedList[capacity];
22+
for (int i = 0; i < capacity; i++) {
23+
buckets[i] = new LinkedList<>();
24+
}
25+
}
26+
27+
private int computeHash(String identifier) {
28+
return Math.abs(identifier.hashCode()) % capacity;
29+
}
30+
31+
public void insert(String identifier, String data) {
32+
int index = computeHash(identifier);
33+
LinkedList<HashEntry> entries = buckets[index];
34+
35+
for (HashEntry entry : entries) {
36+
if (entry.identifier.equals(identifier)) {
37+
entry.data = data;
38+
return;
39+
}
40+
}
41+
entries.add(new HashEntry(identifier, data));
42+
}
43+
44+
public String retrieve(String identifier) {
45+
int index = computeHash(identifier);
46+
LinkedList<HashEntry> entries = buckets[index];
47+
48+
for (HashEntry entry : entries) {
49+
if (entry.identifier.equals(identifier)) {
50+
return entry.data;
51+
}
52+
}
53+
return null;
54+
}
55+
56+
public void remove(String identifier) {
57+
int index = computeHash(identifier);
58+
LinkedList<HashEntry> entries = buckets[index];
59+
60+
entries.removeIf(entry -> entry.identifier.equals(identifier));
61+
}
62+
63+
public void reset() {
64+
buckets = new LinkedList[capacity];
65+
for (int i = 0; i < capacity; i++) {
66+
buckets[i] = new LinkedList<>();
67+
}
68+
}
69+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package ru.mirea.practices0000001.task2;
2+
3+
import java.util.LinkedList;
4+
5+
class HashTable {
6+
private LinkedList<Entry>[] table;
7+
private int size;
8+
9+
private static class Entry {
10+
String key;
11+
String value;
12+
13+
Entry(String key, String value) {
14+
this.key = key;
15+
this.value = value;
16+
}
17+
}
18+
19+
public HashTable(int size) {
20+
this.size = size;
21+
table = new LinkedList[size];
22+
for (int i = 0; i < size; i++) {
23+
table[i] = new LinkedList<>();
24+
}
25+
}
26+
27+
private int hashtabHash(String key) {
28+
return Math.abs(key.hashCode()) % size;
29+
}
30+
31+
public void hashtabAdd(String key, String value) {
32+
int index = hashtabHash(key);
33+
LinkedList<Entry> entries = table[index];
34+
35+
for (Entry entry : entries) {
36+
if (entry.key.equals(key)) {
37+
entry.value = value;
38+
return;
39+
}
40+
}
41+
entries.add(new Entry(key, value));
42+
}
43+
44+
public String hashtabLookup(String key) {
45+
int index = hashtabHash(key);
46+
LinkedList<Entry> entries = table[index];
47+
48+
for (Entry entry : entries) {
49+
if (entry.key.equals(key)) {
50+
return entry.value;
51+
}
52+
}
53+
return null;
54+
}
55+
56+
public void hashtabDelete(String key) {
57+
int index = hashtabHash(key);
58+
LinkedList<Entry> entries = table[index];
59+
60+
entries.removeIf(entry -> entry.key.equals(key));
61+
}
62+
63+
public void hashtabInit() {
64+
table = new LinkedList[size];
65+
for (int i = 0; i < size; i++) {
66+
table[i] = new LinkedList<>();
67+
}
68+
}
69+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package ru.mirea.practices0000001.task2;
2+
3+
public abstract class HashTableTest {
4+
public static void main(String[] args) {
5+
HashTable hashTable = new HashTable(10);
6+
7+
hashTable.hashtabAdd("key1", "value1");
8+
hashTable.hashtabAdd("key2", "value2");
9+
hashTable.hashtabAdd("key3", "value3");
10+
hashTable.hashtabAdd("key4", "value4");
11+
hashTable.hashtabAdd("key5", "value5");
12+
hashTable.hashtabAdd("key6", "value6");
13+
hashTable.hashtabAdd("key7", "value7");
14+
hashTable.hashtabAdd("key8", "value8");
15+
hashTable.hashtabAdd("key9", "value9");
16+
hashTable.hashtabAdd("key10", "value10");
17+
18+
System.out.println("Вывод:");
19+
for (int i = 1; i <= 10; i++) {
20+
String key = "key" + i;
21+
String value = hashTable.hashtabLookup(key);
22+
System.out.println(key + ": " + value);
23+
}
24+
25+
hashTable.hashtabDelete("key5");
26+
System.out.println("\nПосле удаления key5:");
27+
for (int i = 1; i <= 10; i++) {
28+
String key = "key" + i;
29+
String value = hashTable.hashtabLookup(key);
30+
System.out.println(key + ": " + value);
31+
}
32+
}
33+
}

students/23K0354/23K0354-p28/pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<artifactId>23K0354</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0354-p28</artifactId>
12+
<description> Изучение Java Collection Framework: нелинейные структуры данных </description>
13+
</project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package ru.mirea.practices0000001.task1;
2+
3+
import java.util.HashSet;
4+
import java.util.TreeSet;
5+
6+
public abstract class HashSetToTreeSetExample {
7+
public static void main(String[] args) {
8+
9+
HashSet<String> hashSet = new HashSet<>();
10+
11+
hashSet.add("geeks");
12+
hashSet.add("practice");
13+
hashSet.add("contribute");
14+
hashSet.add("ide");
15+
16+
System.out.println("HashSet: " + hashSet);
17+
18+
TreeSet<String> treeSet = new TreeSet<>(hashSet);
19+
20+
System.out.println("TreeSet: " + treeSet);
21+
}
22+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package ru.mirea.practices0000001.task2;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public abstract class NameMap {
7+
8+
public static void main(String[] args) {
9+
Map<String, String> nameMap = createMap();
10+
11+
int sameFirstNameCount = getSameFirstNameCount(nameMap);
12+
int sameLastNameCount = getSameLastNameCount(nameMap);
13+
14+
System.out.println("Количество людей с одинаковыми именами: " + sameFirstNameCount);
15+
System.out.println("Количество людей с одинаковыми фамилиями: " + sameLastNameCount);
16+
}
17+
18+
public static Map<String, String> createMap() {
19+
Map<String, String> nameMap = new HashMap<>();
20+
nameMap.put("Иванов", "Иван");
21+
nameMap.put("Петров", "Петр");
22+
nameMap.put("Сидоров", "Сидор");
23+
nameMap.put("Кузнецов", "Иван");
24+
nameMap.put("Смирнов", "Алексей");
25+
nameMap.put("Васильев", "Петр");
26+
nameMap.put("Попов", "Сидор");
27+
nameMap.put("Зайцев", "Иван");
28+
nameMap.put("Федоров", "Алексей");
29+
nameMap.put("Морозов", "Петр");
30+
return nameMap;
31+
}
32+
33+
public static int getSameFirstNameCount(Map<String, String> nameMap) {
34+
Map<String, Integer> firstNameCount = new HashMap<>();
35+
36+
for (String firstName : nameMap.values()) {
37+
firstNameCount.put(firstName, firstNameCount.getOrDefault(firstName, 0) + 1);
38+
}
39+
40+
int count = 0;
41+
for (int occurrences : firstNameCount.values()) {
42+
if (occurrences > 1) {
43+
count += occurrences;
44+
}
45+
}
46+
return count;
47+
}
48+
49+
public static int getSameLastNameCount(Map<String, String> nameMap) {
50+
Map<String, Integer> lastNameCount = new HashMap<>();
51+
52+
for (String lastName : nameMap.keySet()) {
53+
lastNameCount.put(lastName, lastNameCount.getOrDefault(lastName, 0) + 1);
54+
}
55+
56+
int count = 0;
57+
for (int occurrences : lastNameCount.values()) {
58+
if (occurrences > 1) {
59+
count += occurrences;
60+
}
61+
}
62+
return count;
63+
}
64+
}

students/23K0354/23K0354-p29/pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<artifactId>23K0354</artifactId>
7+
<groupId>ru.mirea.practice</groupId>
8+
<version>2024.1</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>23K0354-p29</artifactId>
12+
<description> Города и дороги </description>
13+
</project>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package ru.mirea.practices0000001.task1;
2+
3+
import java.util.Scanner;
4+
5+
public abstract class CitiesAndRoads {
6+
public static void main(String[] args) {
7+
try (Scanner scanner = new Scanner(System.in)) {
8+
System.out.print("Введите количество городов (n): ");
9+
int numCities = scanner.nextInt();
10+
11+
if (numCities < 0 || numCities > 100) {
12+
System.out.println("Количество городов должно быть в диапазоне от 0 до 100.");
13+
return;
14+
}
15+
16+
CityRoads cityRoads = new CityRoads(numCities);
17+
cityRoads.fillMatrix(scanner);
18+
19+
int totalRoads = cityRoads.countRoads();
20+
System.out.println("Количество дорог на планете 'Neptune': " + totalRoads);
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)