From 49dfcfe4570e4f4100bb9d98022b699fb076c178 Mon Sep 17 00:00:00 2001 From: Rhinemann Date: Mon, 5 Jun 2023 21:46:40 +0300 Subject: [PATCH] Lab 5, technically working prototype. It's horrid, though. --- Java/lab_5/Punctuation.kt | 2 +- Java/lab_5/Sentence.kt | 4 ++-- Java/lab_5/Text.kt | 27 +++++++++++++++++++++++++-- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/Java/lab_5/Punctuation.kt b/Java/lab_5/Punctuation.kt index b58913c..785fe3c 100644 --- a/Java/lab_5/Punctuation.kt +++ b/Java/lab_5/Punctuation.kt @@ -4,7 +4,7 @@ class Punctuation(var punctuationMark: String) { private val punctuationMarkOptions: Regex = Regex("\\p{Punct}") init { - if (!punctuationMarkOptions.containsMatchIn(punctuationMark)){ + if (!punctuationMarkOptions.containsMatchIn(punctuationMark)) { this.punctuationMark = "" } } diff --git a/Java/lab_5/Sentence.kt b/Java/lab_5/Sentence.kt index 3a6b597..c9a725c 100644 --- a/Java/lab_5/Sentence.kt +++ b/Java/lab_5/Sentence.kt @@ -15,11 +15,11 @@ class Sentence(sentenceString: String) { } } - private fun splitWords(sentenceString: String): Array{ + private fun splitWords(sentenceString: String): Array { return sentenceString.split("[\\p{Punct}\\s]+".toRegex()).filter { it.isNotEmpty() }.map { word -> Word(word) }.toTypedArray() } - private fun splitPunctuation(sentenceString: String): Array{ + private fun splitPunctuation(sentenceString: String): Array { return sentenceString.split(" ").map { word -> Punctuation(word.last().toString()) }.toTypedArray() } diff --git a/Java/lab_5/Text.kt b/Java/lab_5/Text.kt index 6777cde..be6a774 100644 --- a/Java/lab_5/Text.kt +++ b/Java/lab_5/Text.kt @@ -3,23 +3,27 @@ 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){ + for (i in arrayOfSentences.indices) { textArray += Pair(arrayOfSentences[i], arrayOfPunctuations[i]) } this.rawText = getRawTextString(arrayOfSentences) + + } private fun splitSentences(textString: String): Array { return textString.split("[.!?] ?".toRegex()).filter { it.isNotEmpty() }.map { sentence -> Sentence(sentence) }.toTypedArray() } - private fun splitPunctuation(textString: String): Array{ + private fun splitPunctuation(textString: String): Array { return textString.split("(?<=[.!?]) ?".toRegex()).filter { it.isNotEmpty() }.map { sentence -> Punctuation(sentence.last().toString()) }.toTypedArray() } @@ -27,6 +31,25 @@ class Text(textString: String) { return arrayOfSentences.joinToString("") { sentence -> sentence.arrayOfWords.joinToString("") } } + 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) + + if (subToC.first().equals(subToC.last(), true) && subToC.equals(subToC.reversed(), true) && subToC.length >= 3) { +// println(subToC) + results += subToC + } + } + } + return results.maxBy { it.length } + } + + fun checkStringInBoundaries(stringToCheck: String): Boolean { + return stringToCheck.equals(stringToCheck.reversed(), true) + } + override fun toString(): String { var text = arrayOf() this.textArray.forEach { text += it.first.toString() + it.second.toString() }