-
Notifications
You must be signed in to change notification settings - Fork 0
Java05. ДЗ 01, Егоров Антон #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
|
|
||
| <groupId>1</groupId> | ||
| <artifactId>hw_01</artifactId> | ||
| <version>0.0.1-SNAPSHOT</version> | ||
| <packaging>jar</packaging> | ||
|
|
||
| <name>hw_01</name> | ||
| <url>http://maven.apache.org</url> | ||
|
|
||
| <properties> | ||
| <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
| <source>1.8</source> | ||
| <target>1.8</target> | ||
| </properties> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>junit</groupId> | ||
| <artifactId>junit</artifactId> | ||
| <version>3.8.1</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| </dependencies> | ||
| </project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| package hw_01; | ||
|
|
||
| public class Bor implements Trie { | ||
| final private Node bor; | ||
| private int size = 0; | ||
|
|
||
| public Bor() { | ||
| bor = new Node(); | ||
| } | ||
|
|
||
| public boolean add(String element) { | ||
| if (contains(element)) { | ||
| return false; | ||
| } | ||
|
|
||
| Node p = bor; | ||
|
|
||
| p.nwords++; | ||
| for (int i = 0; i < element.length(); i++) { | ||
| int currIdx = arrNum(element.charAt(i)); | ||
|
|
||
| if (p.lifes[currIdx] == null) { | ||
| p.lifes[currIdx] = new Node(); | ||
| size++; | ||
| } | ||
| p = p.lifes[currIdx]; | ||
| p.nwords++; | ||
| } | ||
|
|
||
| p.isTerminal = true; | ||
| return true; | ||
| } | ||
|
|
||
| public boolean contains(String element) { | ||
| Node p = bor; | ||
| for (int i = 0; i < element.length(); i++) { | ||
| int currIdx = arrNum(element.charAt(i)); | ||
|
|
||
| if (p.lifes[currIdx] == null) { | ||
| return false; | ||
| } | ||
| p = p.lifes[currIdx]; | ||
| } | ||
|
|
||
| return p.isTerminal; | ||
| } | ||
|
|
||
| public boolean remove(String element) { | ||
| if (!contains(element)) { | ||
| return false; | ||
| } | ||
|
|
||
| Node p = bor; | ||
| p.nwords--; | ||
| for (int i = 0; i < element.length(); i++) { | ||
| int currIdx = arrNum(element.charAt(i)); | ||
|
|
||
| if (p.lifes[currIdx].nwords == 1) { | ||
| size -= element.length() - i; | ||
| p.lifes[currIdx] = null; | ||
| return true; | ||
| } | ||
| p = p.lifes[currIdx]; | ||
| p.nwords--; | ||
| } | ||
|
|
||
| p.isTerminal = false; | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| public int size() { | ||
| return size; | ||
| } | ||
|
|
||
| public int howManyStartsWithPrefix(String prefix) { | ||
| Node p = bor; | ||
| for (int i = 0; i < prefix.length(); i++) { | ||
| int currIdx = arrNum(prefix.charAt(i)); | ||
|
|
||
| if (p.lifes[currIdx] == null) { | ||
| return 0; | ||
| } | ||
| p = p.lifes[currIdx]; | ||
| } | ||
|
|
||
| return p.nwords; | ||
| } | ||
|
|
||
| private int arrNum(char a) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. на буквах экономить не стоит, особенно если они помогают понимать код, что верно почти всегда |
||
| if ('a' <= a && a <= 'z') { | ||
| return a - 'a'; | ||
| } | ||
|
|
||
| if ('A' <= a && a <= 'Z') { | ||
| return a - 'A' + 'z' - 'a' + 1; | ||
| } | ||
|
|
||
| return -1; | ||
| } | ||
|
|
||
| private class Node { | ||
| public boolean isTerminal; | ||
| public int nwords; | ||
| Node[] lifes = new Node[2*('z' - 'a' + 1)]; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package hw_01; | ||
|
|
||
| public interface Trie { | ||
|
|
||
| /** | ||
| * Expected complexity: O(|element|) | ||
| * @return <tt>true</tt> if this set did not already contain the specified | ||
| * element | ||
| */ | ||
| boolean add(String element); | ||
|
|
||
| /** | ||
| * Expected complexity: O(|element|) | ||
| */ | ||
| boolean contains(String element); | ||
|
|
||
| /** | ||
| * Expected complexity: O(|element|) | ||
| * @return <tt>true</tt> if this set contained the specified element | ||
| */ | ||
| boolean remove(String element); | ||
|
|
||
| /** | ||
| * Expected complexity: O(1) | ||
| */ | ||
| int size(); | ||
|
|
||
| /** | ||
| * Expected complexity: O(|prefix|) | ||
| */ | ||
| int howManyStartsWithPrefix(String prefix); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| package hw_01; | ||
|
|
||
| import junit.framework.TestCase; | ||
|
|
||
| public class TrieImplTest extends TestCase { | ||
| private Bor b; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. можно бор создавать в каждом тестовом методе, будет чуть длиньше файл, зато "чище" |
||
|
|
||
| public void setUp() { | ||
| b = new Bor(); | ||
| } | ||
|
|
||
| public void testAddSizeContains() { | ||
| assertFalse(b.contains("Hello")); | ||
| assertTrue(b.add("Hello")); | ||
| assertTrue(b.contains("Hello")); | ||
| assertEquals(5, b.size()); | ||
| assertEquals(1, b.howManyStartsWithPrefix("Hell")); | ||
| } | ||
|
|
||
| public void testAddContainsPrefix() { | ||
| b.add("Hello"); | ||
| assertFalse(b.contains("Hell")); | ||
| assertTrue(b.add("Hell")); | ||
| assertTrue(b.contains("Hell")); | ||
| assertEquals(5, b.size()); | ||
| assertEquals(2, b.howManyStartsWithPrefix("Hell")); | ||
| } | ||
|
|
||
| public void testAddOutstandingWord() { | ||
| b.add("Hello"); | ||
| assertTrue(b.add("Head")); | ||
| assertEquals(7, b.size()); | ||
| } | ||
|
|
||
| public void testRemovePrefix() { | ||
| b.add("Hello"); | ||
| b.add("Hell"); | ||
| assertFalse(b.remove("He")); | ||
| assertTrue(b.remove("Hell")); | ||
| assertEquals(5, b.size()); | ||
| assertEquals(1, b.howManyStartsWithPrefix("Hell")); | ||
| } | ||
|
|
||
| public void testTemoveSuffix() { | ||
| b.add("Hello"); | ||
| b.add("Hell"); | ||
| assertTrue(b.remove("Hello")); | ||
| assertEquals(4, b.size()); | ||
| } | ||
| /* | ||
| public void testBorBigTest() { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ненужный код лучше удалять, чтобы он не мозолил глаза |
||
| //b = new Bor(); | ||
| //Добавление | ||
| assertFalse(b.contains("Hello")); | ||
| assertTrue(b.add("Hello")); | ||
| assertTrue(b.contains("Hello")); | ||
| assertEquals(5, b.size()); | ||
| assertFalse(b.add("Hello")); | ||
| assertEquals(5, b.size()); | ||
| assertFalse(b.contains("Hell")); | ||
| assertTrue(b.add("Hell")); | ||
| assertTrue(b.contains("Hell")); | ||
| assertEquals(5, b.size()); | ||
| assertFalse(b.add("Hell")); | ||
| assertEquals(5, b.size()); | ||
| assertFalse(b.contains("Head")); | ||
| assertTrue(b.add("Head")); | ||
| assertTrue(b.contains("Head")); | ||
| assertEquals(7, b.size()); | ||
| assertFalse(b.contains("")); | ||
| assertTrue(b.add("")); | ||
| assertTrue(b.contains("")); | ||
| assertEquals(7, b.size()); | ||
|
|
||
| //Проверка префиксов | ||
| assertEquals(3, b.howManyStartsWithPrefix("H")); | ||
| assertEquals(4, b.howManyStartsWithPrefix("")); | ||
| assertEquals(1, b.howManyStartsWithPrefix("Hea")); | ||
| assertEquals(2, b.howManyStartsWithPrefix("Hell")); | ||
| assertEquals(0, b.howManyStartsWithPrefix("h")); | ||
|
|
||
| //Удаление | ||
| assertFalse(b.remove("h")); | ||
| assertFalse(b.remove("H")); | ||
| assertTrue(b.remove("Hell")); | ||
| assertFalse(b.remove("Hell")); | ||
| assertFalse(b.contains("Hell")); | ||
| assertEquals(7, b.size()); | ||
| assertEquals(1, b.howManyStartsWithPrefix("Hell")); | ||
|
|
||
| assertTrue(b.remove("")); | ||
| assertFalse(b.remove("")); | ||
| assertEquals(7, b.size()); | ||
| assertEquals(2, b.howManyStartsWithPrefix("")); | ||
|
|
||
| assertTrue(b.remove("Hello")); | ||
| assertFalse(b.remove("Hello")); | ||
| assertEquals(4, b.size()); | ||
|
|
||
| assertTrue(b.contains("Head")); | ||
| assertTrue(b.add("HeadZzZ")); | ||
| assertEquals(7, b.size()); | ||
| assertEquals(2, b.howManyStartsWithPrefix("Head")); | ||
| assertEquals(1, b.howManyStartsWithPrefix("HeadZzZ")); | ||
| assertFalse(b.remove("HeadZ")); | ||
| assertTrue(b.remove("HeadZzZ")); | ||
| assertEquals(4, b.size()); | ||
| assertEquals(1, b.howManyStartsWithPrefix("Head")); | ||
| assertEquals(1, b.howManyStartsWithPrefix("")); | ||
| assertEquals(0, b.howManyStartsWithPrefix("HeadZ")); | ||
|
|
||
| assertTrue(b.remove("Head")); | ||
| assertEquals(0, b.size()); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. тесты стоит представлять в виде наименее возможных тестовых случаев, чтобы их можно было читать, понять, что проверено, а что нет |
||
| } | ||
| */ | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
странное форматирование, табы/пробелы?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Вроде тут только табы.