numbers are twice, but two different numbers are only once. ex) {1, 5, 6, 6, 1, 2} Find the algorithm that can find the two numbers which has time complexity O(n) and space complexity O(1). No sort No radix sort Not sorted XOR 3
all the numbers = 1^5^6^6^1^2 = 2^5 = 010^101 = 111 2. Choose any bit which is 1 • Iterate all the bits or try this: The smallest 1 = x & -x 3. Find all the numbers the bit is 0, and XOR to find one answer • G0 = {6, 6, 2}, XOR(G0) = 6^6^2 = 2 4. Find all the numbers the bit is 1, and XOR to find the other answer • G1 = {1, 5, 1}, XOR(G1) = 1^5^1 = 5 4
reduce def solution(array): xor = reduce(x, y: x ^ y, array, 0) bit = xor & -xor a = reduce(x, y: x ^ y, filter(x: not x & bit, array), 0) b = reduce(x, y: x ^ y, filter(x: x & bit, array), 0) return a, b print(solution([1, 5, 6, 6, 1, 2])) # (2, 5) 5
mistake. It was the invention of the null reference in 1965. At that time, I was designing the first comprehensive type system for references in an object oriented language (ALGOL W). My goal was to ensure that all use of references should be absolutely safe, with checking performed automatically by the compiler. But I couldn't resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years. - Tony Hoare, 2009 Kotlin's type system is aimed at eliminating the danger of null references from code, also known as the The Billion Dollar Mistake. 8
to • Java virtual machine bytecode • JavaScript source code • Designed by JetBrains • First appeared in 2011 • Fully supported by Google on the Android OS (Android Studio 3.0) 9
Decoupling package and file location • No semicolon (;) at the end of lines • No ‘static’ keyword • Null-aware types • Functional programming • Simplified and modern features added version of Java 11
Int operator fun get(index: Int): T operator fun set(index: Int, value: T): Unit operator fun iterator(): Iterator<T> // ... } • val x: IntArray = intArrayOf(1, 2, 3) 20
• val text = """ |Tell me and I forget. |Teach me and I remember. |Involve me and I learn. |(Benjamin Franklin) """.trimMargin() • val s = "abc" println("$s.length is ${s.length}") // prints "abc.length is 3" 21
of Kotlin • Unit • There is no return value (similar to void, but a type) • val unit: Unit = kotlin.Unit • Nothing • There is no return value / Never returns • fun myFun(): Nothing { while (true) {...} } fun myFun2(): Nothing { throw Exception() } 22
Customer(val name: String, val email: String) • getters and setters for all properties • name = c.name vs name = c.getName() • c.name = “name” vs c.setName(“name”) • equals() • hashCode() • toString() • copy() • component1(), … for all properties 30
A function that takes functions as params or return functions • Lambda Expression • max(strings, { a, b -> a.length < b.length }) • LINQ Style Code • students.filter { it.avgGrade > 4.0 } .sortedBy { it.avgGrade } .take(10) .sortedWith(compareBy( { it.surname }, { it.name })) • Anonymous Function • fun(x: Int, y: Int): Int = x + y • Closures • var sum = 0 ints.filter { it > 0 }.forEach { sum += it } print(sum) 33
from the 'main' source set import kotlinx.android.synthetic.main.activity_main.* class MyActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Instead of findViewById<TextView>(R.id.textView) textView.setText("Hello, world!") } } 37