Kotlin Examples Problems

Overview

Kotlin Examples Problems

--->>> Repo: Getting Started Kotlin <<<---

--->>> Repo Kotlin Koans <<<---

--->>> Repo: GameBoy Emulator Enviroment <<<---

--->>> Repo: Kotlin Mobile <<<---

--->>> Repo: Kotlin JavaScript <<<---

--->>> Repo: Kotlin Native - iOS <<<---

--->>> Repo: Ktor Examples <<<---

These are the simple solutions of the kotlin example problems ON LINE. If you want to add your answer, you can make a PR.

Indexes for examples problems online

Problems

Problems

Sum - online


Your task is to implement the sum() function so that it computes the sum of
all elements in the given array a.

Solution 1

fun sum(a: IntArray): Int {
 return a.filter { it != null }.sum()
}

Solution 2

fun sum(a: IntArray): Int {
  var _sum = 0
  for(element in a)
    _sum += element       
    
  return _sum

Solution 3

fun sum(a: IntArray): Int {
  val iterator = a.iterator()
  var _sum: Int = 0
  while (iterator.hasNext()){
    _sum += iterator.next()
  }
  
   return _sum
}

Index of Maximum - online

Your task is to implement the indexOfMax() function so that it returns
the index of the largest element in the array, or null if the array is empty

Solution

fun _indexOfMax(a: IntArray): Int? { 
        var maxIndex = 0
            for(elem in a.indices){
            val newElem = a[elem]
            if (newElem >= a[maxIndex]){
                 maxIndex = elem; 
            }
    	}
        return maxIndex
    }
    return  if(a.size != 0) _indexOfMax(a) else null

Runs - online

 Any array may be viewed as a number of "runs" of equal numbers.
 For example, the following array has two runs:
  1, 1, 1, 2, 2
 Three 1's in a row form the first run, and two 2's form the second.
 This array has two runs of length one:
  3, 4
 And this one has five runs:
  1, 0, 1, 1, 1, 2, 0, 0, 0, 0, 0, 0, 0
 Your task is to implement the runs() function so that it returns the number
 of runs in the given array.

Solution

fun runs(a: IntArray): Int {
    var res = 0;
    if (a.size == res)
    	return res

    var base = a[res]
    a.forEach({
        if(it != base){
            res ++
            base = it
        }
    })
 
    return (++res);   
}

Palindrome - online

 Your task is to implement a palindrome test.
 
 A string is called a palindrome when it reads the same way left-to-right
 and right-to-left.

Solution 1

fun isPalindrome(s: String): Boolean {
    return s == s.reversed()
}

Solution 2

fun isPalindrome(s: String): Boolean {
  var reversed = ""
  for(i in s.indices.reversed())
    reversed = "$reversed${s.charAt(i)}"

    return s == reversed  
}

Solution 3

fun isPalindrome(s: String): Boolean {
  val iterator = s.iterator()
  var _reversed = ""
  while(iterator.hasNext()){
      val _char = iterator.next()
      _reversed = "$_char$_reversed"
  }
    return s == _reversed
}

Pairless - online


 Think of a perfect world where everybody has a soulmate.
 Now, the real world is imperfect: there is exactly one number in the array
 that does not have a pair. A pair is an element with the same value.
 For example in this array:
  1, 2, 1, 2
 every number has a pair, but in this one:
  1, 1, 1
 one of the ones is lonely.
 
 Your task is to implement the findPairless() function so that it finds the
 lonely number and returns it.
 
 A hint: there's a solution that looks at each element only once and uses no
 data structures like collections or trees.

Solution

INPUT 	OUTPUT
A 	B 	A XOR B
0 	0 	   0      OK
0 	1 	   1
1 	0 	   1
1 	1 	   0      OK


fun findPairless(a: IntArray): Int {
    return a.reduce { a, b -> a xor b }
}
  • @Author: Victor Bolinches Marin
You might also like...
A coding examples project about Kotlin Programming language. 🇰
A coding examples project about Kotlin Programming language. 🇰

Kotlin Tutorial 👨🏻‍💻 What is Kotlin ❓ Kotlin is a new programming language, developed by JetBrains. Jetbrains is a popular software development com

Examples of Getting Started vĂ­deos

Getting Started Kotlin Learn the basics of getting started with kotlin --- Repo: Kotlin Koans --- --- Repo: Problems Kotlin --- --- Rep

Mocking for Kotlin/Native and Kotlin Multiplatform using the Kotlin Symbol Processing API (KSP)

Mockative Mocking for Kotlin/Native and Kotlin Multiplatform using the Kotlin Symbol Processing API (KSP). Installation Mockative uses KSP to generate

Kotlin-oop - Repositório criado para ser utilizado pelo projeto de Kotlin OOP desenvolvido em Kotlin nas aulas feitas através da plataforma Alura.

Projeto React OOP Repositório criado para ser utilizado pelo projeto de Kotlin OOP desenvolvido em Kotlin nas aulas feitas através da plataforma Alura

Kotlin-koans - Kotlin Koans are a series of exercises to get you familiar with the Kotlin Syntax

kotlin-koans-edu Kotlin Koans are a series of exercises to get you familiar with

Kotlin TodoMVC – full-stack Kotlin application demo

Kotlin full stack TodoMVC This project is an example implementation of the TodoMVC app written in Kotlin. More specifically, it's the Kotlin port of t

Integration Testing Kotlin Multiplatform Kata for Kotlin Developers. The main goal is to practice integration testing using Ktor and Ktor Client Mock
Integration Testing Kotlin Multiplatform Kata for Kotlin Developers. The main goal is to practice integration testing using Ktor and Ktor Client Mock

This kata is a Kotlin multiplatform version of the kata KataTODOApiClientKotlin of Karumi. We are here to practice integration testing using HTTP stub

Small kotlin library for persisting _single instances_ of kotlin data classes
Small kotlin library for persisting _single instances_ of kotlin data classes

PerSista Small library for persisting single instances of kotlin data classes. NB: PerSista uses typeOf() internally which is marked as @ExperimentalS

Kotlin Leaning Notes from Udacity Course | Kotlin Bootcamp for Programmers by Google
Kotlin Leaning Notes from Udacity Course | Kotlin Bootcamp for Programmers by Google

Kotlin Beginners Notes These are all personal notes taken from the Udacity Course (ud9011) of Kotlin Bootcamp for Programmers by Google as well as oth

Owner
Victor Bolinches
I build things in software, sometimes with a lot of craziness. Game/Tooling programmer. Twitter - @vicboma1
Victor Bolinches
Ninety-Nine Problems in Kotlin

Ninety-Nine Kotlin Problems Table of Contents Introduction Lists Arithmetic Logic and Codes Binary Trees Multiway Trees Graphs Miscellaneous Introduct

Dmitry Kandalov 618 Nov 30, 2022
This repo contains my solutions to some data structures and algorithms problems on leetcode.

DSA Playground This repository contains solutions to dsa problems in kotlin. NOTE: This file will get long, please consider using <Ctrl>F DSA With Kun

Hardik Sachan 2 Dec 9, 2021
Solutions to Hackerrank and CoderByte practice problems

Solutions to Hackerrank and CoderByte practice problems This repository contains solutions to CoderByte and Hackerrank practice problems with Kotlin.

Mert Toptas 2 Jul 5, 2022
It is a project that contains lessons and examples about Kotlin programming language. 🇰

Kotlin Tutorials What is Kotlin? I added the platforms it supports and great resources. You can access the article from the link below: https://medium

Halil Ă–zel 94 Dec 22, 2022
Kotlin Unit Testing Examples

Kotlin Unit Testing Examples Table of Contents Application Gradle, Kotlin & Groovy Junit4 Junit5 KotlinTest Spek Mockito Mockito-Kotlin Mockk Strikt T

Jarosław 110 Dec 11, 2022
kotlin koans examples

Kotlin Koans Build --->>> Repo: Getting Started Kotlin <<<--- --->>> Repo: Problems Kotlin <<<--- --->>> Repo: GameBoy Emulator Enviroment <<<--- --->

Victor Bolinches 121 Oct 3, 2022
101 examples for Kotlin Programming language.

This is a collection of runnable console applications that highlights the features of Kotlin programming language. The use of console application enab

Dody Gunawinata 192 Dec 1, 2022
Full stack examples of how to use Hotwire JS in Kotlin services

hotwire-kt A collection of Kotlin examples using the Hotwire JS framework to build interactive web apps with a Kotlin Armeria server backend. Using Ho

Andrew (Paradi) Alexander 9 Dec 14, 2022
Getting started Kotlin - Examples and explanations

Getting started Kotlin I'm learning Kotlin, so I have been updating it with examples and explanations about the language that I'm using at work. Proje

Jean Jacques Nascimento Barros 2 Apr 25, 2022
Examples for using Kotlin at a basic level for Android application development.

Kotlin Android Jetpack Basics Ejemplos para usar Kotlin a nivel básico para el desarrollo de aplicaciones Android. Kotlin Android Jetpack Basics Acerc

José Luis González Sánchez 2 Jun 28, 2022