BBK#4: Spin My Words...

problemsolving Nov 24, 2020

Don't be a Java-ish dev in Kotlin-ish world, improve your knowledge about Koltin Standard Library and write better Kotlin code. ✌🏻 If your Java dev migrating to Kotlin this will help you learn alot!

Question: Write a function that reversed all words in the String whose length is five or more letter words reversed.

spinWords( "Hey fellow warriors" ) => returns "Hey wollef sroirraw" 
spinWords( "This is a test") => returns "This is a test" 
spinWords( "This is another test" )=> returns "This is rehtona test"

Try it put yourself :
πŸ‘¨πŸ»β€πŸ’»πŸ‘‰ Β https://www.codewars.com/kata/5667e8f4e3f572a8f2000039/train/kotlin



Java-ish Solution 1:

fun spinWords1(input:String):String{
  val words = input.split(" ")
  var result = ""
  for (word in words) {
      if(word.length > 4){
         result = "$result ${word.reversed()}" 
      }else{
          result = "$result $word"
      }
 	}
  return result
 }

// Performance : took 10.4ms on my machine

I have explained issue with these approach in multiple post now, You can checkout its gist with issues with accumulator pattern and alternative approach from below article.

Accumulator Pattern for beginners | Fold vs Reduce | AndroidBites
Accumulator pattern in kotlin | kotlin how to reduce | kotlin how to fold | reduce vs fold | best practice to reduce | transformation in kotlin | collection transformation | java vs koltin | java for loop anti patterns

Solution 2:

Using Map and JoinToString, Checkout advance stuff we can do with it!

map - Kotlin Programming Language
fun spinWords(input:String):String = input.split(" ")
    .map{word-> if(word.length>5) word.reversed() else word}
    .joinToString(" ")

// Performance : took 15.5ms on my machine

minute performance hit is due to Join operation.

Solution 3:

Another advance way to use JoinToString,

fun spinWords(input: String) = input.split(" ")
	.joinToString(" ") { 
    	if (it.length > 4) it.reversed() else it
    }


// Performance : took 15.5ms on my machine

Performance is same as Solution 2.

To learn more advance cases checkout of JoinToString checkout

Snippets | List to String with Examples | Androidbites
kotlin string | kotlin variable in string | kotlin join strings with delimiter | kotlin map to string | kotlin string interpolation | kotlin string collection join | list to string and string to list kotlin | kotlin string format


Conclusion

Aim of these articles is not hate on Java, but to help people learn various ways in which they can write same logic better and more Kotlin standard library focused way.

Hope you find it informative and if you have any feedback or post request or want to subscribe to my mailing list forms are below.

Until next time. Happy Hacking! πŸ‘©β€πŸ’»

Solve Previous:

BigBrainKotlin - Find Max Min
Don’t be a Java-ish dev in Kotlin-ish world, improve your knowledge about Koltin Standard Library and write better Kotlin code. ✌🏻 If your Java dev migrating to Kotlin this will help you learn a lot!

Solve Next:

BigBrainKotlin - Among us
Don’t be a Java-ish dev in Kotlin-ish world, improve your knowledge about Koltin Standard Library and write better Kotlin code. ✌🏻 If your Java dev migrating to Kotlin this will help you learn a lot!

Enjoy the Post?

a clap is much appreciated if you enjoyed. No sign up or cost associated :)




Chetan gupta

Hi there! call me Ch8n, I'm a mobile technology enthusiast! love Android #kotlinAlltheWay, CodingMantra: #cleanCoder #TDD #SOLID #designpatterns

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.