ProductPromotion
Logo

Kotlin

made by https://0x3d.site

GitHub - vicboma1/Kotlin-Examples-Problems: Kotlin Examples Problems
Kotlin Examples Problems. Contribute to vicboma1/Kotlin-Examples-Problems development by creating an account on GitHub.
Visit Site

GitHub - vicboma1/Kotlin-Examples-Problems: Kotlin Examples Problems

GitHub - vicboma1/Kotlin-Examples-Problems: Kotlin Examples Problems

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

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory