Created
December 18, 2020 18:32
-
-
Save efemoney/7a308e65947ab34d9f686ef0f51911b9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
# Please write a function that gets a text and tries to guess the possible languages of the text. | |
# | |
# Pangrams: Contains all the letters in an alpahabet of a language. | |
# | |
# English => The quick brown fox jumps over the lazy dog | |
# German => Zwölf Boxkämpfer jagen Viktor quer über den großen Sylter Deich | |
# Turkish => Pijamalı hasta yağız şoföre çabucak güvendi | |
# | |
# Examples: | |
# | |
# Fix => [English, German] | |
# Foo bar => [English, German, Turkish] | |
# Oranienburger Straße => [German] | |
# Jägermeister => [German] | |
# çubuk => [Turkish] | |
# çwiß => [] | |
*/ | |
val eng = "English" | |
val ger = "German" | |
val tur = "Turkish" | |
fun pangram() { | |
val english = "The quick brown fox jumps over the lazy dog" | |
.filterNot { it.isWhitespace() }.toLowerCase() | |
val german = "Zwölf Boxkämpfer jagen Viktor quer über den großen Sylter Deich" | |
.filterNot { it.isWhitespace() }.toLowerCase() | |
val turkish = "Pijamalı hasta yağız şoföre çabucak güvendi" | |
.filterNot { it.isWhitespace() }.toLowerCase() | |
val dictionary: Map<Char, MutableSet<String>> = buildMap { | |
for (c in english) getOrPut(c) { mutableSetOf() }.add(eng) | |
for (c in german) getOrPut(c) { mutableSetOf() }.add(ger) | |
for (c in turkish) getOrPut(c) { mutableSetOf() }.add(tur) | |
} | |
val input = listOf("Fix", "Foo bar", "Oranienburger Straße", "Jägermeister", "çubuk", "çwiß") | |
for (s in input) { | |
var languages = setOf(eng, ger, tur) | |
for (c in s.toLowerCase().filterNot { it.isWhitespace() }) { | |
val applicableLanguages = dictionary[c].orEmpty() | |
languages = languages.intersect(applicableLanguages) | |
} | |
println("$s -> $languages") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment