Advertisement
jules0707

isNice.kt

Jun 23rd, 2021
1,099
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 0.94 KB | None | 0 0
  1. package nicestring
  2.  
  3. fun String.isNice(): Boolean {
  4. // It doesn't contain substrings bu, ba or be;
  5. /*fun String.condition1(): Boolean = !(contains("bu") || contains("ba")        || contains("be"))*/
  6.     val doesNotContain = setOf("bu", "ba", "be").none { this.contains(it) }
  7.  
  8.  
  9. // It contains at least three vowels (vowels are a, e, i, o and u);
  10. /*fun String.condition2():Boolean = filter { it in "aeiou" }.count() >= 3*/
  11.     val hasThreeVowels = count { it in "aeiou" } >= 3
  12.  
  13.  
  14. /*It contains a double letter (at least two similar letters following
  15. one another), like b in "abba"*/
  16. /*fun String.condition3():Boolean = zipWithNext() { a, b -> a == b }.filter { it }.count() >= 1*/
  17.     val hasDouble = zipWithNext().any { it.first == it.second }
  18.  
  19.     /*return ((condition1() && condition2()) || (condition1() && condition3()) || (condition2() && condition3())) }*/
  20.     return listOf(doesNotContain, hasThreeVowels, hasDouble).count { it } >= 2
  21.  
  22. }
  23.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement