Giter VIP home page Giter VIP logo

kotlin-examples-problems's Introduction

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

kotlin-examples-problems's People

Contributors

vicboma1 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

kotlin-examples-problems's Issues

The Index of Maximum

fun indexOfMax(a: IntArray): Int? {
    return a.indices.maxBy { a[it] } ?: null
}

dose this works?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.