Skip to content
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

Lecture03 #145

Open
wants to merge 6 commits into
base: lecture03
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,41 +1,56 @@
package io.rybalkinsd.kotlinbootcamp.practice

/**
* NATO phonetic alphabet
*/
val alphabet = setOf("Alfa", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot", "Golf", "Hotel", "India", "Juliett", "Kilo", "Lima", "Mike", "November", "Oscar", "Papa", "Quebec", "Romeo", "Sierra", "Tango", "Uniform", "Victor", "Whiskey", "Xray", "Yankee", "Zulu")

/**
* A mapping for english characters to phonetic alphabet.
* [ a -> Alfa, b -> Bravo, ...]
*/
val association: Map<Char, String> = TODO()

/**
* Extension function for String which encode it according to `association` mapping
*
* @return encoded string
*
* Example:
* "abc".encode() == "AlfaBravoCharlie"
*
*/
fun String.encode(): String = TODO()

/**
* A reversed mapping for association
* [ alpha -> a, bravo -> b, ...]
*/
val reversedAssociation: Map<String, Char> = TODO()

/**
* Extension function for String which decode it according to `reversedAssociation` mapping
*
* @return encoded string or null if it is impossible to decode
*
* Example:
* "alphabravocharlie".decode() == "abc"
* "charliee".decode() == null
*
*/
fun String.decode(): String? = TODO()
package io.rybalkinsd.kotlinbootcamp.practice
/**
* NATO phonetic alphabet
*/
val alphabet = setOf("Alfa", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot", "Golf", "Hotel", "India", "Juliett", "Kilo", "Lima", "Mike", "November", "Oscar", "Papa", "Quebec", "Romeo", "Sierra", "Tango", "Uniform", "Victor", "Whiskey", "Xray", "Yankee", "Zulu")
/**
* A mapping for english characters to phonetic alphabet.
* [ a -> Alfa, b -> Bravo, ...]
*/
val association: Map<Char, String> = alphabet.associate { it[0].toLowerCase() to it }
/**
* Extension function for String which encode it according to `association` mapping
*
* @return encoded string
*
* Example:
* "abc".encode() == "AlfaBravoCharlie"
*
*/
fun String.encode(): String = map { it.toLowerCase() }.map { association[it] ?: it }.joinToString("")
/**
* A reversed mapping for association
* [ alpha -> a, bravo -> b, ...]
*/
val reversedAssociation: Map<String, Char> = alphabet.associate { it to it[0] }
/**
* Extension function for String which decode it according to `reversedAssociation` mapping
*
* @return encoded string or null if it is impossible to decode
*
* Example:
* "alphabravocharlie".encode() == "abc"
* "charliee".decode() == null
*
*/
fun String.decode(): String? {
var res = ""
var input = this
var word = ""
for (letter in input) {
if (letter in '0'..'9' || letter == ' ') {
res += letter
continue
}
word += letter
if (reversedAssociation[word] != null) {
res += (reversedAssociation[word])
res = res.toLowerCase()
word = ""
}
}
when (word) {
"" -> return res
else -> return null
}
}