diff --git a/Java/lab_5/Sentence.kt b/Java/lab_5/Sentence.kt index c9a725c..6e6b231 100644 --- a/Java/lab_5/Sentence.kt +++ b/Java/lab_5/Sentence.kt @@ -1,18 +1,12 @@ package OOP.Java.lab_5 class Sentence(sentenceString: String) { - var sentenceArray = arrayOf>() - val arrayOfWords: Array = splitWords(sentenceString) - val arrayOfPunctuations: Array = splitPunctuation(sentenceString) + var sentenceArray = Pair, Array>(splitWords(sentenceString), splitPunctuation(sentenceString)) init { // val arrayOfWords: Array = splitWords(sentenceString) // val arrayOfPunctuations: Array = splitPunctuation(sentenceString) - - for (i in arrayOfWords.indices) { - sentenceArray += Pair(arrayOfWords[i], arrayOfPunctuations[i]) - } } private fun splitWords(sentenceString: String): Array { @@ -23,9 +17,20 @@ class Sentence(sentenceString: String) { return sentenceString.split(" ").map { word -> Punctuation(word.last().toString()) }.toTypedArray() } + fun getAllLetters(): Array { + var allLetters = arrayOf() + this.sentenceArray.first.forEach { allLetters += it.letters } + /*for (i: Word in this.sentenceArray.first) { + println("${i} ${i::class.simpleName} ${i.letters}") + }*/ + return allLetters + } + override fun toString(): String { var sentence = arrayOf() - this.sentenceArray.forEach { sentence += it.first.toString() + it.second.toString() } + for (i in this.sentenceArray.first.indices) { + sentence += this.sentenceArray.first[i].toString() + this.sentenceArray.second[i].toString() + } return sentence.joinToString(" ") } } \ No newline at end of file diff --git a/Java/lab_5/Text.kt b/Java/lab_5/Text.kt index 0d6fb81..159c45c 100644 --- a/Java/lab_5/Text.kt +++ b/Java/lab_5/Text.kt @@ -1,23 +1,7 @@ package OOP.Java.lab_5 class Text(textString: String) { - var textArray = arrayOf>() - var rawText: String = "" -// val arrayOfSentences = splitSentences(textString) -// val arrayOfPunctuations: Array = splitPunctuation(textString) - - init { - val arrayOfSentences = splitSentences(textString) - val arrayOfPunctuations: Array = splitPunctuation(textString) - - for (i in arrayOfSentences.indices) { - textArray += Pair(arrayOfSentences[i], arrayOfPunctuations[i]) - } - - this.rawText = getRawTextString(arrayOfSentences) - - - } + var textArray = Pair(splitSentences(textString), splitPunctuation(textString)) private fun splitSentences(textString: String): Array { return textString.split("[.!?] ?".toRegex()).filter { it.isNotEmpty() }.map { sentence -> Sentence(sentence) }.toTypedArray() @@ -27,29 +11,54 @@ class Text(textString: String) { return textString.split("(?<=[.!?]) ?".toRegex()).filter { it.isNotEmpty() }.map { sentence -> Punctuation(sentence.last().toString()) }.toTypedArray() } - private fun getRawTextString(arrayOfSentences: Array): String { - return arrayOfSentences.joinToString("") { sentence -> sentence.arrayOfWords.joinToString("") } + fun getAllLetters(): Array { + var allLetters = arrayOf() + this.textArray.first.forEach { allLetters += it.getAllLetters() } + return allLetters } fun palindromeSearch(): String { var results = arrayOf() - for (leftBoundary in 0 until this.rawText.length) { - for (rightBoundary in this.rawText.length downTo leftBoundary + 1) { - val subToC = this.rawText.substring(leftBoundary, rightBoundary) + val letters = this.getAllLetters() + for (leftBoundary in letters.indices) { + for (rightBoundary in letters.lastIndex downTo leftBoundary + 1) { + val subToC = letters.sliceArray(leftBoundary..rightBoundary) +// println(subToC.joinToString("")) - if (subToC.first().equals(subToC.last(), true) && subToC.equals(subToC.reversed(), true) && subToC.length >= 3) { -// println(subToC) - results += subToC + if (subToC.first().equals(subToC.last(), true) && subToC.size >= 3 && this.checkReverse(subToC)) { +// println(subToC.joinToString("")) + results += subToC.joinToString("") } } } return results.maxBy { it.length } } + private fun checkReverse(substring: Array): Boolean { + var leftBoundary = 0 + var rightBoundary = substring.lastIndex + var result = false + var correction = 0 + + if (substring.size % 2 != 0) { + correction = 1 + } + + while (leftBoundary < substring.size / 2 && rightBoundary >= substring.size / 2 + correction) { + leftBoundary++ + rightBoundary-- + result = substring[leftBoundary].equals(substring[rightBoundary], true) + } + + return result + } + override fun toString(): String { var text = arrayOf() - this.textArray.forEach { text += it.first.toString() + it.second.toString() } + for (i in this.textArray.first.indices) { + text += this.textArray.first[i].toString() + this.textArray.second[i].toString() + } return text.joinToString(" ") } } \ No newline at end of file