10 Kotlin Code Challenges for Beginners

0
32
10 Kotlin Code Challenges for Beginners

If you are interested in mobile development, then you have probably already heard of Kotlin as a popular language for developing Android apps. Maybe you even started an online Kotlin course to give yourself a head start in your new development career.

But the learning does not end when you finish the course. If you want to keep your programming skills sharp, it is important to constantly challenge yourself by solving problems using programming languages ​​like Kotlin.

That’s why we’ve put together these 10 Kotlin Code Challenges for Beginners to give you a way to apply your knowledge for practical use and prepare you for your next technical interview. Just select a challenge below, jump into a workspace, and get started.

1. Number steps

Many programming languages ​​have built-in functions that output speech sequences separated by steps. Create your own Kotlin function from scratch that creates a sequence list with numbers from 0 to a maximum input value and input step. For example, a maximum value of 8 and a step value of 2 should return an output of [2, 4, 6, 8].

If you are looking for more of a challenge, create a Fibonacci sequence generator. Remember that the following numbers in a Fibonacci sequence are the sum of the two preceding ones: [1, 1, 2, 3, 5, 8, 13].

Consistently capitalized

An important part of data preparation is to ensure that data strings are uniformly formatted and in capital letters. For a given input string, create a function that accepts a string input as well as an argument string that is either “upper” or “lower”.

If “upper”, the function should convert everything in the string to uppercase, while the argument “lower” should return the string with each lowercase character.

To give yourself an extra challenge, add the arguments “snake” and “camel”, which convert a series of words separated by spaces to snake box or camel box.

thisishose box

ThisIsCamelCase

The longest word

For a given input string of words, create a function that returns the longest word in the string. For example, for the input phrase “I love Codecademy”, the output must be “Codecademy”.

For an extra challenge, you can create an output that shows the longest word along with the number of characters in the word. So for the string “I love Codecademy,” output would see something along the lines of, “word: Codecademy count: 10.”

4. Caesar cipher

In cryptography, a Caesar cipher is a simple encryption technique that moves each letter in a message by a certain number of letters. The number of letters to be shifted is known as the key or shift parameter. For example, if the key is 1, then the message:

“Codecademy is amazing!”

becomes:

“Dpefdbefnz jt bxftpnf!”

Note how each letter in the encrypted message is one letter after each letter in the original message.

Create a Caesar cipher function that accepts an integer key and a message string as input. If the encrypted letter goes beyond the letter z, it must be wrapped to the beginning of the alphabet.

For an additional challenge, create another function that decrypts an encrypted message for a given key and encrypted string.

5. Train route

Create a function that displays the stations on a train route based on an input of stations. For example, an input of (“NY Penn”) should print “The train stops at NY Penn.” For an input on (“NY Penn,” “Woodside,” “Forest Hills”), the output must be “The train stops at NY Penn, Woodside, and Forest Hills.”

If you want to challenge yourself a little more, add another input that includes the train departure time, train number and destination station. So with the entrances (234, “10:00”, “Montauk”) and (“NY Penn,” “Babylon,” “Sayville”), the exit should be “10:00 train number 234 to Montauk stops at NY Penn, Babylon and Sayville. “

Remember that the train number must be entered as an integer, but read out as part of a string.

6. Permutation palindrome

A palindrome is a word or phrase that can be spelled the same back and forth. Examples of palindromes include the words “level” and “race car” as well as the phrase “taco cat.”

Create a function that returns TRUE if an input string is a permutation of a palindrome – that is, if the string could somehow be rearranged into a palindrome. For example, the strings “racercar”, “carrace” and “gghhk” should return TRUE, while words like “tammy” and “code” should return FALSE.

For an extra challenge, allow your input to include sentence strings. You must find a way to remove all spaces in the input string before running your function.

7. FizzBuzz

The FizzBuzz challenge is a popular problem in technical interviews. For a given maximum input value n, create a function that emits integers from 1 to n.

But if an integer is divisible by three, the number must be replaced by the word “Fizz”. Numbers divisible by five should say “Buzz” instead. And numbers divisible by both three and five should say “FizzBuzz”.

For example, with an input of 17, the output should be:

[1, 2 , ”Fizz”, 4, “Buzz”, “Fizz”, 7, 8, “Fizz“, “Buzz“, 11, “Fizz”, 13, 14, “FizzBuzz”, 16, 17]

Step 8

For a given integer input n, create a function that returns a string output of generated steps using the # character. For example, for an input of 3, the output should look like this:

‘#’

‘##’

‘###’

If you want to “step up” this challenge, add a string input that allows the function to generate steps for a given single character.

9. Triangles etc.

Come up with a function that creates a triangle based on an integer input to specify the number of rows and a string input to determine the “building blocks” of the triangle. If you want to challenge yourself more, add a third input that allows someone to specify different shapes such as a circle, square or trapezoid.

10. Pascals triangle

If you are ready for an intermediate challenge after completing Challenge 9 (triangles, etc.), try creating Pascal’s triangle.

You may remember learning about Pascal’s triangle in algebra lessons. It’s a quick way to solve binomial extensions – (x + y) n – where nrow in Pascal’s triangle gives you the coefficients for xy brought up to various powers. Pascal’s triangle is also used in probability theory, and you can read more about how it was created here.

For a given integer input -one, create Pascal’s triangle with -one rows. The first row must match -one = 0.

For example, for an input value of 2, the output should look something like this:

1

1.1

1,2,1

If you are pursuing a career in mobile development, learning Kotlin is a great way to get started. Or, if you want to learn more about Android development, check out courses like:

Note that Kotlin also has applications in web development, so it is also a great tool for anyone who wants to become a front-end, back-end or full-stack engineer. To learn more, take a look through our article on what Kotlin is used for.


Mobile development courses and tutorials | Code Academy

Every year, more and more people are relying on mobile devices to meet their needs. Where websites used to be the gold standard, people are now relying on mobile apps. The technologies used to create these apps are expanding and improving rapidly, so it’s an exciting time to start learning mobile development!

LEAVE A REPLY

Please enter your comment!
Please enter your name here