Challenge: Improving DragonSpeak

Hello,

Here is my solution for the “Improving DragonSpeak” challenge:

private fun toDragonSpeak(phrase: String) = phrase.replace(Regex("[aeiouAEIOU]")) {
    when(it.value) {
        "a" -> "4"
        "e" -> "3"
        "i" -> "1"
        "o" -> "0"
        "u" -> "|_|"
        "A" -> "4"
        "E" -> "3"
        "I" -> "1"
        "O" -> "0"
        "U" -> "|_|"
        else -> it.value
    }
}

All I did was to add the capital cases of the characters to the list. I was just wondering if there was a “better” or “more professional” way of solving this challenge?

Thanks,
Max

My solution looked slightly different to reduce the number of cases. The Regex was the same, but I converted the value to be lowercase

private fun toDragonSpeak(phrase: String) =
    phrase.replace(Regex("[aeiouAEIOU]")) {
        when (it.value.toLowerCase()) {
            "a" -> "4"
            "e" -> "3"
            "i" -> "1"
            "o" -> "0"
            "u" -> "|_|"
            else -> it.value
        }
    }
1 Like

That’s actually a nice way to do it I think. Idk why I didn’t think of that lol. Thanks

Here is my solution

private fun toDragonSpeak(phase: String) =
phase.toUpperCase().replace(Regex("[AEIOU]")) {
when (it.value) {
“A” -> “4”
“E” -> “3”
“I” -> “1”
“O” -> “0”
“U” -> “|_|”
else -> it.value
}
}

const val TAVERN_NAME = "Taernyl's Folly"

fun main() {
    placeOrder("SHANDY,dRAGON'S bREATH,5.91")
    //placeOrder("elixir,Shirley's Temple,4.12")
}

private fun placeOrder(menuData: String) {
    val (type, name, price) = menuData.split(',')
    val phrase = if (name.equals("dragon's breath", true)) {
        "Madrigal exclaims ${toDragonSpeak("Ah, delicious $name!")}"
    } else {
        "Madrigal says: Thanks for the $name."
    }
    println(phrase)
}

private fun toDragonSpeak(phrase: String) =
    phrase.replace(Regex("(?i)[aeiou]")) {
        when (it.value.toLowerCase()) {
            "a" -> "4"
            "e" -> "3"
            "i" -> "1"
            "o" -> "0"
            "u" -> "|_|"
            else -> it.value
        }
    }

PonchoTonic buys Dragon’s Breath.
PonchoTonic exclaims: “H3R3 1S MY S0L|_|T10N, TR4V3L3RS!”.

const val TAVERN_NAME = "Taernyl's Folly"

fun main(args: Array<String>) {
    placeOrder("shandy,Dragon's Breath,5.91")
    //placeOrder("elixir,Shirley's Temple,4.12")

    // Challenge
    val capitalPhrase = "DRAGON'S BREATH: IT'S GOT WHAT ADVENTURERS CRAVE!"
    println(toDragonSpeak(capitalPhrase))
}

private fun toDragonSpeak(phrase: String) =
    phrase.replace(Regex("[aeiouAEIOU]")) {
        when (it.value) {
            "a", "A" -> "4"
            "e", "E" -> "3"
            "i", "I" -> "1"
            "o", "O" -> "0"
            "u", "U" -> "|_|"
            else -> it.value
        }
    }

private fun placeOrder(menuData: String) {
    val indexOfApostrophe = TAVERN_NAME.indexOf('\'')
    val tavernMaster = TAVERN_NAME.substring(0 until indexOfApostrophe)
    println("Madrigal speaks with $tavernMaster about their order.")

    val data = menuData.split(',')
    val type = data[0]
    val name = data[1]
    val price = data[2]
    val message = "Madrigal buys a $name ($type) for $price."
    println(message)

    val phrase = if (name == "Dragon's Breath") {
        "Madrigal exclaims: ${toDragonSpeak("Ah, delicious $name!")}"
    } else {
        "Madrigal says: Thanks for the $name."
    }

    println(phrase)
}

Use the constructor Regex(pattern: String, option: RegexOption). Use ignore case for the option so that both lowercase and uppercase will match. Next, convert it to lowercase.

private fun toDragonSpeak(phrase: String): String {
    return phrase.replace(Regex("[aeiou]", RegexOption.IGNORE_CASE)) {
        when (it.value.toLowerCase()) {
            "a" -> "4"
            "e" -> "3"
            "i" -> "1"
            "o" -> "0"
            "u" -> "|_|"
            else -> it.value
        }
    }
}
3 Likes
    private fun toDragonSpeak(phrase: String) =
        phrase.toLowerCase().replace(Regex("[aeiou]")) {
            when (it.value) {
                "a" -> "4"
                "e" -> "3"
                "i" -> "1"
                "o" -> "0"
                "u" -> "|_|"
                else -> it.value
            }
        }

I liked Zetzaus’s solution also, it taught me about this regex option

1 Like