Skip to content

Commit

Permalink
5 tasks from lesson 5 are done.
Browse files Browse the repository at this point in the history
  • Loading branch information
Iksburg committed Oct 30, 2020
1 parent bbe599a commit 022f332
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 27 deletions.
53 changes: 26 additions & 27 deletions src/lesson6/task1/Parse.kt
Original file line number Diff line number Diff line change
Expand Up @@ -79,35 +79,34 @@ fun main() {
*/
fun dateStrToDigit(str: String): String {
val calendar = mapOf(
"января" to ".01.",
"февраля" to ".02.",
"марта" to ".03.",
"апреля" to ".04.",
"мая" to ".05.",
"июня" to ".06.",
"июля" to ".07.",
"августа" to ".08.",
"сентября" to ".09.",
"октбяря" to ".10.",
"ноября" to ".11.",
"декабря" to ".12."
"января" to "1",
"февраля" to "2",
"марта" to "3",
"апреля" to "4",
"мая" to "5",
"июня" to "6",
"июля" to "7",
"августа" to "8",
"сентября" to "9",
"октбяря" to "10",
"ноября" to "11",
"декабря" to "12"
)
val parts = str.split(" ")
val result = StringBuilder()
for (part in parts) {
when {
parts.size != 3 -> {
return ""
}
part in calendar -> {
result.append(calendar[part])
}
else -> {
result.append(part)
}
}
if (parts.size != 3) {
return ""
}
val month = calendar[parts[1]]
return if (month != null &&
(month in listOf("1", "3", "5", "7", "8", "10", "12") && parts[0].toInt() < 32
|| month in listOf("4", "6", "9", "11") && parts[0].toInt() < 31
|| month == "2" && (parts[2].toInt() % 4 == 0 && parts[0].toInt() < 30
|| parts[2].toInt() % 4 != 0 && parts[0].toInt() < 29))
) {
String.format("%02d.%02d.%04d", parts[0].toInt(), month.toInt(), parts[2].toInt())
} else {
""
}
return result.toString()
}

/**
Expand Down Expand Up @@ -147,7 +146,7 @@ fun flattenPhoneNumber(phone: String): String {
currentString = currentString.replace("(", "")
currentString = currentString.replace(")", "")
currentString = currentString.replace("-", "")
if (currentString.isNotEmpty() && currentString.toLongOrNull() == null) {
if (currentString.isNotEmpty() && currentString.toIntOrNull() == null) {
return ""
}
result.append(currentString)
Expand Down
1 change: 1 addition & 0 deletions test/lesson6/task1/Tests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class Tests {
assertEquals("+42566789", flattenPhoneNumber("+42(56 -- 67)89"))
assertEquals("", flattenPhoneNumber("ab-123"))
assertEquals("", flattenPhoneNumber("134_+874"))
assertEquals("", flattenPhoneNumber("\n"))
}

@Test
Expand Down

0 comments on commit 022f332

Please sign in to comment.