├── Assessments ├── Chapter02 │ └── Answers.md ├── Chapter03 │ └── Answers.md ├── Chapter04 │ └── Answers.md ├── Chapter05 │ └── Answers.md ├── Chapter06 │ └── Answers.md ├── Chapter07 │ └── Answers.md └── Chapter08 │ └── Answers.md ├── Chapter01 ├── AsymptoticBehavior.kt ├── CountingInstruction.kt ├── Examples.kt ├── UpperCasedList.kt └── User.kt ├── Chapter02 ├── ArrayAccessExample.kt ├── ArrayCreateExample.kt ├── ArrayIterationExample.kt ├── ArrayListExample.kt ├── ArrayUpdateExample.kt ├── ImmutableDS.kt ├── ImmutableList.kt ├── Matrix.kt ├── MultiDimeArrays.kt └── Vector.kt ├── Chapter03 ├── CircularLinkyList.kt ├── DoublyLinkyList.kt └── LinkyList.kt ├── Chapter04 ├── CircularFixedQueue.kt ├── Deque.kt ├── FixedQueue.kt ├── FixedStack.kt ├── LinkedQueue.kt ├── LinkedStack.kt ├── Queue.kt └── Stack.kt ├── Chapter05 ├── ArrayMap.kt ├── HashFunction.kt ├── HashMap.kt └── IntMap.kt ├── Chapter06 ├── KMPSearch.kt ├── NaivePatternSearch.kt ├── RabinKarp.kt └── Search.kt ├── Chapter07 ├── BubbleSort.kt ├── HeapSort.kt ├── InsertionSort.kt ├── MergeSort.kt ├── QuickSort.kt └── SelectionSort.kt ├── Chapter08 ├── .classpath ├── .gradle │ ├── 3.5-rc-2 │ │ ├── file-changes │ │ │ └── last-build.bin │ │ └── taskHistory │ │ │ ├── fileHashes.bin │ │ │ ├── fileSnapshots.bin │ │ │ ├── taskHistory.bin │ │ │ └── taskHistory.lock │ ├── 4.4 │ │ ├── fileChanges │ │ │ └── last-build.bin │ │ ├── fileHashes │ │ │ ├── fileHashes.bin │ │ │ └── fileHashes.lock │ │ └── taskHistory │ │ │ ├── taskHistory.bin │ │ │ └── taskHistory.lock │ └── buildOutputCleanup │ │ ├── buildOutputCleanup.lock │ │ ├── cache.properties │ │ └── outputFiles.bin ├── .idea │ ├── .name │ ├── compiler.xml │ ├── gradle.xml │ ├── libraries │ │ ├── Gradle__org_jetbrains_annotations_13_0.xml │ │ ├── Gradle__org_jetbrains_kotlin_kotlin_stdlib_1_2_20.xml │ │ ├── Gradle__org_jetbrains_kotlin_kotlin_stdlib_jdk7_1_2_20.xml │ │ └── Gradle__org_jetbrains_kotlin_kotlin_stdlib_jdk8_1_2_20.xml │ ├── misc.xml │ ├── modules.xml │ ├── modules │ │ ├── Collections.iml │ │ ├── Collections_main.iml │ │ └── Collections_test.iml │ ├── uiDesigner.xml │ └── workspace.xml ├── .project ├── .settings │ └── org.eclipse.buildship.core.prefs ├── CustomObjectSet.kt ├── CustomSort.kt ├── FunctionalSort.kt ├── LinkedList.kt ├── Maps.kt ├── MutableAccess.kt ├── MutableList.kt ├── OpDrop.kt ├── OpFilter.kt ├── OpFlatMap.kt ├── OpMap.kt ├── OpTake.kt ├── OpZip.kt ├── Search.kt ├── Set.kt ├── Sort.kt └── Sorted.kt ├── Chapter09 ├── build.gradle ├── gradle.properties ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── src │ └── main │ └── kotlin │ ├── arrowexamples │ ├── either-example.kt │ ├── option-example-2.kt │ └── option-example.kt │ ├── category │ ├── applicative-1.kt │ ├── functor-1.kt │ ├── functor-map-either.kt │ ├── functor-map-for-monad.kt │ ├── functor-map.kt │ └── monad-flatmap.kt │ └── fpbasics │ ├── Calculator.kt │ ├── callback-example.kt │ ├── hof.kt │ ├── immutability-example.kt │ ├── immutable-collection.kt │ └── lambda.kt ├── LICENSE └── README.md /Assessments/Chapter02/Answers.md: -------------------------------------------------------------------------------- 1 | #### 1. Create an array of numbers between 0 to 500 that are multiples of 10. 2 | 3 | ``` 4 | val multiplesOfTen = Array(51) { i -> i * 10 } 5 | ``` 6 | 7 | #### 2. Create an array with all the even indexed numbers of given array. 8 | 9 | ``` 10 | fun evenIndexedNumbers(arr: Array): IntArray { 11 | return arr.filterIndexed { index, _ -> 12 | index % 2 == 0 13 | }.toIntArray() 14 | } 15 | ``` 16 | 17 | #### 3. Write a snippet for finding a transpose of a matrix. 18 | 19 | ``` 20 | fun transpose(matrix: Array): Array? { 21 | var transpose: Array? = null 22 | // Considering it is a valid matrix 23 | val row = matrix.size 24 | if (row > 0) { 25 | val column = matrix[0].size 26 | if (column > 0) { 27 | transpose = Array(column) { IntArray(row) } 28 | for (i in 0..row - 1) { 29 | for (j in 0..column - 1) { 30 | transpose[j][i] = matrix[i][j] 31 | } 32 | } 33 | } 34 | } 35 | return transpose 36 | } 37 | ``` 38 | 39 | #### 4. Write a snippet to append elements of two arrays. 40 | 41 | ``` 42 | val arr1 = arrayOf(10, 20, 30) 43 | val arr2 = arrayOf(100, 200, 300) 44 | val result = arr1 + arr2 45 | ``` 46 | 47 | #### 5. Write snippet to convert wrapper typed array to a primitive array. 48 | 49 | ``` 50 | val intArray = arrayOf(1, 2, 3, 4, 5) 51 | val output = intArray.toIntArray() 52 | 53 | val doubleArray = arrayOf(1.2, 3.5, 4.8, 9.0) 54 | val output1 = doubleArray.toDoubleArray() 55 | ``` -------------------------------------------------------------------------------- /Assessments/Chapter03/Answers.md: -------------------------------------------------------------------------------- 1 | #### 1. Write an API in linked list (LinkyList in this book) which adds all the elements of an array to the list. 2 | 3 | ``` 4 | fun addAll(index: Int, arr: Array): Boolean where T : E { 5 | 6 | validatePositionIndex(index) 7 | 8 | val numNew = arr.size 9 | if (numNew == 0) return false 10 | var pred: Node? 11 | var succ: Node? 12 | when (index) { 13 | 0 -> { 14 | succ = head 15 | pred = null 16 | } 17 | size -> { 18 | succ = null 19 | pred = tail 20 | } 21 | else -> { 22 | pred = node(index - 1) 23 | succ = pred.next 24 | } 25 | } 26 | 27 | for (item in arr) { 28 | val e = item as E 29 | val newNode = Node(e, null) 30 | if (pred == null) 31 | head = newNode 32 | else 33 | pred.next = newNode 34 | pred = newNode 35 | } 36 | 37 | if (succ == null) { 38 | tail = pred 39 | } else { 40 | pred!!.next = succ 41 | } 42 | size += numNew 43 | return true 44 | } 45 | ``` 46 | 47 | #### 2. Write the above API for doubly linked list (DoublyLinkyList in this book). 48 | 49 | ``` 50 | fun addAll(index: Int, arr: Array): Boolean where T : E { 51 | validatePositionIndex(index) 52 | 53 | val numNew = arr.size 54 | if (numNew == 0) return false 55 | 56 | var pred: Node? 57 | var succ: Node? 58 | when (index) { 59 | 0 -> { 60 | succ = head 61 | pred = null 62 | } 63 | size -> { 64 | succ = null 65 | pred = tail 66 | } 67 | else -> { 68 | succ = node(index) 69 | pred = succ.prev 70 | } 71 | } 72 | 73 | for (item in arr) { 74 | val e = item as E 75 | val newNode = Node(pred, e, null) 76 | if (pred == null) 77 | head = newNode 78 | else 79 | pred.next = newNode 80 | pred = newNode 81 | } 82 | 83 | if (succ == null) { 84 | tail = pred 85 | } else { 86 | pred!!.next = succ 87 | succ!!.prev = pred 88 | } 89 | 90 | size += numNew 91 | return true 92 | } 93 | ``` 94 | 95 | #### 3. Write a snippet to link a node at head of a circular linked list. 96 | 97 | ``` 98 | private fun linkHead(element: E) { 99 | val h = head 100 | val newNode = Node(tail, element, h) 101 | head = newNode 102 | if (h == null) { 103 | tail = newNode 104 | newNode!!.prev = tail 105 | newNode.next = head 106 | } else { 107 | h.prev = newNode 108 | tail!!.next = newNode 109 | } 110 | size++ 111 | } 112 | ``` 113 | 114 | #### 4. Write a snippet to unlink a node from circular linked list. 115 | 116 | ``` 117 | private fun unlink(curr: Node): E { 118 | val element = curr.element 119 | val next = curr.next 120 | val prev = curr.prev 121 | 122 | if (curr == head) { 123 | head = next 124 | } 125 | if (curr == tail) { 126 | tail = prev 127 | } 128 | prev?.next = next 129 | next?.prev = prev 130 | curr.prev = null 131 | curr.next = null 132 | size-- 133 | if (size == 0) { 134 | head = null 135 | tail = null 136 | } 137 | return element 138 | } 139 | ``` 140 | 141 | #### 5. Implement toString() method of circular linked list. 142 | 143 | ``` 144 | override fun toString(): String { 145 | var curr = head 146 | if (curr == null) return "[]" 147 | else { 148 | val sb = StringBuilder() 149 | sb.append('[') 150 | for (i in 0 until size) { 151 | sb.append(curr!!.element) 152 | curr = curr.next 153 | if (curr == head) { 154 | sb.append(']') 155 | } else { 156 | sb.append(',').append(' ') 157 | } 158 | } 159 | return sb.toString() 160 | } 161 | } 162 | ``` -------------------------------------------------------------------------------- /Assessments/Chapter04/Answers.md: -------------------------------------------------------------------------------- 1 | #### 1. Write an API to push all the elements of an array to a stack. 2 | 3 | ``` 4 | fun pushAll(newElements: Array) { 5 | val newSize = size + newElements.size 6 | if (elements.size < newSize) { 7 | // New sizing can be of any logic as per requirement 8 | val newArray = arrayOfNulls(newSize + minCapacityIncrement) 9 | System.arraycopy(elements, 0, newArray, 0, size) 10 | elements = newArray 11 | } 12 | System.arraycopy(newElements, 0, elements, size, newElements.size) 13 | size = newSize 14 | } 15 | ``` 16 | 17 | #### 2. Write an API to enqueue all the elements of an array to a queue. 18 | 19 | ``` 20 | fun enqueueAll(newElements: Array) { 21 | val newSize = size + newElements.size 22 | if (elements.size < newSize) { 23 | // New sizing can be of any logic as per requirement 24 | val newArray = arrayOfNulls(newSize + minCapacityIncrement) 25 | System.arraycopy(elements, 0, newArray, 0, size) 26 | elements = newArray 27 | } 28 | System.arraycopy(newElements, 0, elements, size, newElements.size) 29 | size = newSize 30 | } 31 | ``` 32 | 33 | #### 3. Write an API to pop a given number of elements from the stack. 34 | 35 | ``` 36 | fun pop(count: Int) { 37 | if (size == 0 || size < count) throw StackUnderflowException() 38 | for (i in 0 until count) { 39 | elements[--size] = null 40 | } 41 | } 42 | ``` 43 | 44 | #### 4. Write an API to dequeue a given number of elements from the queue. 45 | 46 | ``` 47 | fun dequeue(count: Int) { 48 | if (size == 0 || size < count) throw QueueUnderflowException() 49 | System.arraycopy(elements, count, elements, 0, size - count) 50 | size -= count 51 | for (i in 0 until count) { 52 | elements[size + i] = null 53 | } 54 | } 55 | ``` 56 | 57 | #### 5. Write APIs called stackOf() and queueOf() which works similar to arrayOf(). 58 | We need to create a constructor in both Stack and Queue class, which accepts an array. 59 | 60 | ``` 61 | constructor(elements: Array) { 62 | this.elements = elements as Array 63 | size += elements.size 64 | } 65 | ``` 66 | 67 | **stackOf()** API in Stack.kt: 68 | ``` 69 | inline fun stackOf(vararg elements: T) = Stack(elements as Array) 70 | ``` 71 | 72 | **queueOf()** API in Queue.kt: 73 | ``` 74 | inline fun queueOf(vararg elements: T) = Queue(elements as Array) 75 | ``` 76 | -------------------------------------------------------------------------------- /Assessments/Chapter05/Answers.md: -------------------------------------------------------------------------------- 1 | #### 1. Modify HashMap to have a constructor which accepts a Map object to create itself. 2 | 3 | This constrcutor can be found in HashMap.kt file of Chapter04 section of this repository. 4 | 5 | ``` 6 | constructor(map: Map) { 7 | val size = map.size 8 | val newSize = when { 9 | size < minCapacity -> minCapacity 10 | else -> fetchNearestCapacity(size) 11 | } 12 | this.table = arrayOfNulls(newSize) 13 | if (size > 0) { 14 | for (entry in map) { 15 | putVal(entry.key, entry.value) 16 | } 17 | } 18 | } 19 | ``` 20 | 21 | #### 2. Implement a Map whose keys are primitive integers. 22 | 23 | ``` 24 | import java.util.* 25 | 26 | class IntMap constructor(capacity: Int = 10) { 27 | 28 | private var hashes: IntArray 29 | private var array: Array 30 | 31 | var size = 0 32 | private set 33 | 34 | init { 35 | hashes = IntArray(if (capacity < 0) 0 else capacity) 36 | array = arrayOfNulls(if (capacity < 0) 0 else capacity) 37 | } 38 | 39 | fun isEmpty() = size <= 0 40 | 41 | operator fun get(key: Int) = get(key, null) 42 | 43 | fun get(key: Int, valueIfKeyNotFound: V?): V? { 44 | val index = Arrays.binarySearch(hashes, 0, size, key) 45 | return if (index < 0) valueIfKeyNotFound else array[index] as V 46 | } 47 | 48 | fun delete(key: Int) { 49 | val index = Arrays.binarySearch(hashes, 0, size, key) 50 | if (index >= 0) removeAt(index) 51 | } 52 | 53 | fun removeAt(index: Int) { 54 | System.arraycopy(hashes, index + 1, hashes, index, size - (index + 1)) 55 | System.arraycopy(array, index + 1, array, index, size - (index + 1)); 56 | size-- 57 | } 58 | 59 | fun put(key: Int, value: V) { 60 | var index = Arrays.binarySearch(hashes, 0, size, key) 61 | if (index >= 0) array[index] = value 62 | else { 63 | index = index.inv() 64 | if (size >= hashes.size) { 65 | val newSize = if (size < 4) 4 else size + (size shr 1) 66 | val tempHashes = hashes 67 | val tempArray = array 68 | allocArrays(newSize) 69 | 70 | System.arraycopy(tempHashes, 0, hashes, 0, tempHashes.size) 71 | System.arraycopy(tempArray, 0, array, 0, tempArray.size) 72 | } 73 | 74 | if (index < size) { 75 | System.arraycopy(hashes, index, hashes, index + 1, size - index) 76 | System.arraycopy(array, index, array, index + 1, size - index) 77 | } 78 | 79 | hashes[index] = key 80 | array[index] = value 81 | size++ 82 | } 83 | } 84 | 85 | fun indexOfKey(key: Int): Int { 86 | return Arrays.binarySearch(hashes, 0, size, key) 87 | } 88 | 89 | fun containsKey(key: Int) = indexOfKey(key) >= 0 90 | 91 | fun keyAt(index: Int) = hashes[index] 92 | 93 | fun valueAt(index: Int) = array[index] as V 94 | 95 | fun setValueAt(index: Int, value: V) { 96 | array[index] = value 97 | } 98 | 99 | private fun allocArrays(size: Int) { 100 | hashes = IntArray(size) 101 | array = arrayOfNulls(size) 102 | } 103 | } 104 | ``` 105 | -------------------------------------------------------------------------------- /Assessments/Chapter06/Answers.md: -------------------------------------------------------------------------------- 1 | #### 1. Write an algorithm to find kth largest element of an integer array. 2 | 3 | ``` 4 | fun kthLargest(arr: Array, k: Int): Int { 5 | arr.descending() 6 | return arr[k - 1] 7 | } 8 | 9 | fun > Array.descending() { 10 | val len = size 11 | for (i in 0 until (len - 1)) { 12 | for (j in 0 until (len - i - 1)) { 13 | if (this[j].compareTo(this[j + 1]) < 0) { 14 | val temp = this[j] 15 | this[j] = this[j + 1] 16 | this[j + 1] = temp 17 | } 18 | } 19 | } 20 | } 21 | ``` 22 | 23 | NOTE: Here we have sorted the input array in descending order using bubble sort, but you can use any sorting algorithm you want. 24 | 25 | #### 2. Write a snippet which tells whether a given array or list has a duplicate element or not. 26 | 27 | ``` 28 | fun > Array.hasDuplicate(): Boolean { 29 | for (i in 0..(size - 1)) { 30 | for (j in (i + 1)..(size - 1)) { 31 | if (this[i] == this[j]) { 32 | return true 33 | } 34 | } 35 | } 36 | return false 37 | } 38 | ``` 39 | 40 | #### 3. Find all the occurrences of a pattern from a text using any pattern matching algorithm discussed in the chapter. 41 | 42 | ``` 43 | fun search(text: String, pattern: String): IntArray { 44 | val result = ArrayList() 45 | val prefixArr = preparePrefixArray(pattern) 46 | val textLen = text.length 47 | val patternLen = pattern.length 48 | 49 | var patternIndex = 0 50 | var textIndex = 0 51 | while ((textIndex < textLen) and (patternIndex < patternLen)) { 52 | if (pattern[patternIndex] == text[textIndex]) { 53 | textIndex++ 54 | patternIndex++ 55 | } else { 56 | if (patternIndex != 0) patternIndex = prefixArr[patternIndex - 1] 57 | else textIndex++ 58 | } 59 | if (patternIndex == patternLen) { 60 | // We found the pattern 61 | result.add(textIndex - patternIndex) 62 | patternIndex = 0 63 | } 64 | } 65 | return result.toIntArray() 66 | } 67 | 68 | fun preparePrefixArray(pattern: String): IntArray { 69 | val patternLen = pattern.length 70 | val arr = IntArray(patternLen) 71 | var index = 0 72 | var i = 1 73 | while(i < patternLen) { 74 | if (pattern[i] == pattern[index]) { 75 | arr[i] = index + 1 76 | index++ 77 | i++ 78 | } else { 79 | if (index != 0) index = arr[index - 1] 80 | else { 81 | arr[i] = 0 82 | i++ 83 | } 84 | } 85 | } 86 | return arr 87 | } 88 | ``` 89 | 90 | NOTE: We have used KMP search algorithm here. But you can choose any pattern searching algorithm as per your choice. -------------------------------------------------------------------------------- /Assessments/Chapter07/Answers.md: -------------------------------------------------------------------------------- 1 | #### 1. Implement Quick Sort in descending order. 2 | 3 | Below snippet is an implementation of sorting an array in descending order. 4 | 5 | ``` 6 | fun > Array.descending() { 7 | descending(this, 0, size - 1) 8 | } 9 | 10 | private fun > descending(arr: Array, low: Int, high: Int) { 11 | if (low < high) { 12 | val partitionIndex = descendingPartition(arr, low, high) 13 | 14 | descending(arr, low, partitionIndex - 1) 15 | descending(arr, partitionIndex + 1, high) 16 | } 17 | } 18 | 19 | private fun > descendingPartition(arr: Array, low: Int, high: Int): Int { 20 | val pivot = arr[high] 21 | var i = low - 1 22 | for (j in low until high) { 23 | if (arr[j] >= pivot) { 24 | i++ 25 | arr[i] = arr[j].also { arr[j] = arr[i] } 26 | } 27 | } 28 | arr[i + 1] = arr[high].also { arr[high] = arr[i + 1] } 29 | return i + 1; 30 | } 31 | ``` 32 | 33 | Below snippet is an implementation of sorting an immutable list in descending order. 34 | ``` 35 | fun > MutableList.descending() { 36 | descending(this, 0, size - 1) 37 | } 38 | 39 | private fun > descending(arr: MutableList, low: Int, high: Int) { 40 | if (low < high) { 41 | val partitionIndex = descendingPartition(arr, low, high) 42 | 43 | descending(arr, low, partitionIndex - 1) 44 | descending(arr, partitionIndex + 1, high) 45 | } 46 | } 47 | 48 | private fun > descendingPartition(arr: MutableList, low: Int, high: Int): Int { 49 | val pivot = arr[high] 50 | var i = low - 1 51 | for (j in low until high) { 52 | if (arr[j] >= pivot) { 53 | i++ 54 | arr[i] = arr[j].also { arr[j] = arr[i] } 55 | } 56 | } 57 | arr[i + 1] = arr[high].also { arr[high] = arr[i + 1] } 58 | return i + 1; 59 | } 60 | ``` 61 | 62 | #### 2. Implement all the sorting algorithms discussed above for immutable list. 63 | 64 | Bubble sort with an immutable list API: 65 | ``` 66 | fun > List.bubbleSort(): List { 67 | val len = size 68 | val resultList = toMutableList() 69 | for (i in 0 until (len - 1)) { 70 | for (j in 0 until (len - i - 1)) { 71 | if (resultList[j].compareTo(resultList[j + 1]) > 0) { 72 | val temp = resultList[j] 73 | resultList[j] = resultList[j + 1] 74 | resultList[j + 1] = temp 75 | } 76 | } 77 | } 78 | return resultList 79 | } 80 | ``` 81 | 82 | Selection sort with immutable list API: 83 | ``` 84 | fun > List.selectionSort(): List { 85 | val len = size 86 | val resultList = toMutableList() 87 | // Find the minimum value of the array 88 | for (i in 0 until (len - 1)) { 89 | // Getting the index where minimum value is present 90 | var minIndex = i 91 | for (j in (i + 1) until len) { 92 | if (resultList[j].compareTo(resultList[minIndex]) < 0) minIndex = j 93 | } 94 | 95 | // We got the minimum element, now swap that to first element 96 | val temp = resultList[minIndex] 97 | resultList[minIndex] = resultList[i] 98 | resultList[i] = temp 99 | } 100 | return resultList 101 | } 102 | ``` 103 | 104 | Insertion sort with immutable list API: 105 | ``` 106 | fun > List.insertionSort(): List { 107 | val len = size 108 | val resultList = toMutableList() 109 | for (i in 1 until len) { 110 | var key = resultList[i] 111 | var j = i - 1; 112 | 113 | while(j >= 0 && resultList[j].compareTo(key) > 0) { 114 | resultList[j + 1] = resultList[j] 115 | j-- 116 | } 117 | resultList[j + 1] = key 118 | } 119 | return resultList 120 | } 121 | ``` 122 | 123 | Quick sort with immutable list API: 124 | ``` 125 | fun > List.quickSort(): List { 126 | val resultList = toMutableList() 127 | sort(resultList, 0, size - 1) 128 | return resultList 129 | } 130 | 131 | private fun > sort(arr: MutableList, low: Int, high: Int) { 132 | if (low < high) { 133 | val partitionIndex = partition(arr, low, high) 134 | 135 | sort(arr, low, partitionIndex - 1) 136 | sort(arr, partitionIndex + 1, high) 137 | } 138 | } 139 | 140 | private fun > partition(arr: MutableList, low: Int, high: Int): Int { 141 | val pivot = arr[high] 142 | var i = low - 1 143 | for (j in low until high) { 144 | if (arr[j] <= pivot) { 145 | i++ 146 | arr[i] = arr[j].also { arr[j] = arr[i] } 147 | } 148 | } 149 | arr[i + 1] = arr[high].also { arr[high] = arr[i + 1] } 150 | return i + 1; 151 | } 152 | ``` 153 | -------------------------------------------------------------------------------- /Assessments/Chapter08/Answers.md: -------------------------------------------------------------------------------- 1 | ### 1. Create a list of users and sort them 2 | The below snipet is a data class to hold user information 3 | ``` 4 | data class User ( 5 | val id: Int, 6 | val name: String, 7 | val dob: String 8 | ) 9 | ``` 10 | #### a. By ID 11 | The below snipet shows you how to sort list of users by ID 12 | ``` 13 | val list: List = getDataIntoTheList() 14 | list.sortedBy { it.id } 15 | ``` 16 | 17 | #### b. By Name 18 | The below snipet shows you how to sort list of users by Name 19 | ``` 20 | val list: List = getDataIntoTheList() 21 | list.sortedBy { it.name } 22 | ``` 23 | 24 | #### c. By DOB 25 | The below snipet shows you how to sort list of users by DOB (Considering it is in yyyy-mm-dd format) 26 | ``` 27 | val list: List = getDataIntoTheList() 28 | list.sortedBy { it.dob } 29 | ``` 30 | ### 2. Create a list of employees and filter out those whose salary is less than $800. 31 | Below is the code snipet which filters out employees with salaries below $800 and will return only those employees with a higher salary. 32 | ``` 33 | data class Employee ( 34 | val id: Int, 35 | val name: String, 36 | val salary: Int 37 | ) 38 | ``` 39 | ``` 40 | val list = getDataIntoTheList() 41 | list.filter { 42 | it.salary>800 43 | } 44 | ``` 45 | ### 3. Let's assume a queue is there to buy movie tickets. Write a snippet that gives a ticket until the theater is house full. Assume the theatre has 200 seats. 46 | Please refer to the directory Answer3 for this question. 47 | 48 | ### 4. Create a list of only prime numbers between 1 to 100. 49 | ``` 50 | val Int.isPrime: Boolean 51 | get() { 52 | for (i in 2..this/2) { 53 | if(this%i == 0) return false 54 | } 55 | return true 56 | } 57 | ``` 58 | ``` 59 | (1..100).filter { it.isPrime } 60 | ``` 61 | 62 | 63 | 64 | 65 | -------------------------------------------------------------------------------- /Chapter01/AsymptoticBehavior.kt: -------------------------------------------------------------------------------- 1 | private fun find(input: IntArray, item: Int) { 2 | for (i in input.indices) { 3 | if (input[i] == item) { 4 | print("Item found at index - $i") 5 | } 6 | } 7 | } 8 | 9 | fun main(args: Array) { 10 | find(intArrayOf(32, 34, 45, 87, 343, 5667, 98, 67, 32, 543, 1), 67) 11 | } 12 | -------------------------------------------------------------------------------- /Chapter01/CountingInstruction.kt: -------------------------------------------------------------------------------- 1 | fun main(args: Array) { 2 | val x = 10 3 | val y = x * 2 4 | for (i in 0..y) { 5 | if (i % 2 == 0) { 6 | println("$i is Even") 7 | } else { 8 | println("$i is Odd"); 9 | } 10 | } 11 | } 12 | 13 | 14 | // Output 15 | /* 16 | 0 is Even 17 | 1 is Odd 18 | 2 is Even 19 | 3 is Odd 20 | 4 is Even 21 | 5 is Odd 22 | 6 is Even 23 | 7 is Odd 24 | 8 is Even 25 | 9 is Odd 26 | 10 is Even 27 | 11 is Odd 28 | 12 is Even 29 | 13 is Odd 30 | 14 is Even 31 | 15 is Odd 32 | 16 is Even 33 | 17 is Odd 34 | 18 is Even 35 | 19 is Odd 36 | 20 is Even 37 | */ -------------------------------------------------------------------------------- /Chapter01/Examples.kt: -------------------------------------------------------------------------------- 1 | // Below add function is an algorithm 2 | private fun add(x: Int, y: Int) = x + y 3 | 4 | // Below example is also an exampleof Algorithm 5 | val hourInMillis = 24 * 60 * 60 * 1000 6 | 7 | fun main(args: Array) { 8 | println("2 + 3 = ${add(2, 3)}") 9 | println("Milliseconds in a day : $hourInMillis") 10 | } 11 | -------------------------------------------------------------------------------- /Chapter01/UpperCasedList.kt: -------------------------------------------------------------------------------- 1 | class UpperCasedList : ArrayList() { 2 | 3 | override fun add(element: String): Boolean { 4 | return super.add(element.toUpperCase()) 5 | } 6 | 7 | override fun add(index: Int, element: String) { 8 | super.add(index, element.toUpperCase()) 9 | } 10 | 11 | override fun addAll(elements: Collection): Boolean { 12 | return super.addAll(elements.map { it -> it.toUpperCase() }) 13 | } 14 | 15 | override fun addAll(index: Int, elements: Collection): Boolean { 16 | return super.addAll(index, elements.map { it -> it.toUpperCase() }) 17 | } 18 | 19 | override fun set(index: Int, element: String): String { 20 | return super.set(index, element.toUpperCase()) 21 | } 22 | } 23 | 24 | fun main(args: Array) { 25 | val names = UpperCasedList() 26 | names.add("Chandra") 27 | names.add("Rivu") 28 | println("Names in upper case : $names") // Output - [CHANDRA, RIVU] 29 | } 30 | -------------------------------------------------------------------------------- /Chapter01/User.kt: -------------------------------------------------------------------------------- 1 | data class User(val firstName: String, 2 | val lastName: String, 3 | val phone: String, 4 | val email: String) 5 | 6 | fun main(args: Array) { 7 | val user = User("Chandra", "Sekhar", "123456789", "chansek@live.com") 8 | println("User : $user") 9 | } 10 | -------------------------------------------------------------------------------- /Chapter02/ArrayAccessExample.kt: -------------------------------------------------------------------------------- 1 | fun main(args: Array) { 2 | val languages = arrayOf("Kotlin", "Java", "C", "C++", "C#", "JavaScript", "Python") 3 | println("1st language in the array is : ${languages[0]}") 4 | println("2nd language in the array is : ${languages[1]}") 5 | println("3rd language in the array is : ${languages[2]}") 6 | println("4th language in the array is : ${languages[3]}") 7 | println("5th language in the array is : ${languages[4]}") 8 | println("6th language in the array is : ${languages[5]}") 9 | println("7th language in the array is : ${languages[6]}") 10 | // println("8th language in the array is : ${languages[7]}") 11 | 12 | val firstLanguage = languages[0] 13 | val secondLanguage = languages[1] 14 | val thirdLanguage = languages[2] 15 | // val invalidIndex = languages[-1] 16 | 17 | println() 18 | println("2nd language in the array is : ${languages.get(1)}") 19 | println("4th language in the array is : ${languages.get(3)}") 20 | println("5th language in the array is : ${languages.get(4)}") 21 | 22 | println() 23 | println("1st language in the array is : ${languages.component1()}") 24 | println("2nd language in the array is : ${languages.component2()}") 25 | 26 | println() 27 | val firstItem = languages.elementAt(0) 28 | val secItem = languages.elementAt(0) 29 | val tenthItem = languages.elementAtOrElse(9, {_ -> "Not Available"}) 30 | val eleventhItem = languages.elementAtOrNull(10) 31 | 32 | println("1st item - $firstItem") 33 | println("2nd item - $secItem") 34 | println("10th item - $tenthItem") 35 | println("11th item - $eleventhItem") 36 | } 37 | -------------------------------------------------------------------------------- /Chapter02/ArrayCreateExample.kt: -------------------------------------------------------------------------------- 1 | import java.util.Arrays 2 | 3 | data class User(val firstName: String, 4 | val lastName: String, 5 | val phone: String, 6 | val email: String) 7 | 8 | fun main(args: Array) { 9 | val friends = arrayOf("Rivu", "Subin", "Sid", "Susri", "Ramya", "Sachin") 10 | println("My friends are - ${Arrays.toString(friends)}") 11 | 12 | // String Array 13 | val languages = arrayOf("Kotlin", "Java", "C", "C++", "C#", "JavaScript", "Python") 14 | println("Some of the top programming languages around the world are : ${Arrays.toString(languages)}") 15 | 16 | // int Array 17 | val oddNums = intArrayOf(1, 3, 5, 7, 9) 18 | println("1st five odd numbers are : ${Arrays.toString(oddNums)}") 19 | 20 | // Integer Array (Wrapper Objects) 21 | val oddWrapperNums = arrayOf(1, 3, 5, 7, 9) 22 | println("1st five odd numbers in wrapped object form are : ${Arrays.toString(oddWrapperNums)}") 23 | 24 | // Array of Any 25 | var sachin = arrayOf("Sachin", 29, "sachin@xyz.com", "A", 5.4) 26 | println("Details of Sachin : ${Arrays.toString(sachin)}") 27 | 28 | // Assigning Array to Array 29 | // sachin = languages 30 | // println("Details of Sachin : ${Arrays.toString(sachin)}") 31 | 32 | // Array of User 33 | val users = arrayOf(User("Chandra Sekhar", "Nayak", "0909090909", "chansek@live.com"), 34 | User("Utkarsh", "Asthana", "1234123412", "utku@xyz.com"), 35 | User("Sachin", "Kamble", "7878787878", "sachin@abc.com"), 36 | User("Ramya", "K", "0000000000", "ramu@zzz.com"), 37 | User("Subin", "S", "1234512345", "sub@s.com")) 38 | println("Users are : ${Arrays.toString(users)}") 39 | println("Type of users is User : ${users[0] is User}") 40 | println("Type of users is Nothing : ${users[0] is Nothing}") 41 | 42 | // Array of nulls 43 | val nulls: Array = arrayOf(null, null, null) 44 | println("Users are : ${Arrays.toString(nulls)}") 45 | println("Type of nulls is User : ${nulls[0] is User}") 46 | println("Type of nulls is User? : ${nulls[0] is User?}") 47 | println("Type of nulls is Nothing : ${nulls[0] is Nothing}") 48 | println("Type of nulls is Nothing? : ${nulls[0] is Nothing?}") 49 | 50 | val biggerNullArray: Array = arrayOfNulls(100) 51 | println("Users are : ${Arrays.toString(biggerNullArray)}") 52 | 53 | // Creating array using constructor 54 | val squares = Array(51, {i -> i * i}) 55 | println("Squares of numbers upton 50 are : ${Arrays.toString(squares)}") 56 | 57 | val langsInUpperCase = Array(languages.size, { 58 | i -> languages[i].toUpperCase() 59 | }) 60 | println("programming languages in Upper Case : ${Arrays.toString(langsInUpperCase)}") 61 | } 62 | -------------------------------------------------------------------------------- /Chapter02/ArrayIterationExample.kt: -------------------------------------------------------------------------------- 1 | fun main(args: Array) { 2 | val languages = arrayOf("Kotlin", "Java", "C", "C++", "C#", "JavaScript", "Python") 3 | 4 | for (i in languages.indices) { 5 | if (i % 2 == 0) { 6 | println(languages[i]) 7 | } else { 8 | println(languages[i].toUpperCase()) 9 | } 10 | } 11 | 12 | for ((index, value) in languages.withIndex()) { 13 | if (index % 2 == 0) { 14 | println("The element at $index is $value") 15 | } else { 16 | println("The element at $index is ${value.toUpperCase()}") 17 | } 18 | } 19 | 20 | for (language in languages) { 21 | println("Language - $language") 22 | } 23 | 24 | languages.forEach { println("Language in Upper Case - ${it.toUpperCase()}") } 25 | } 26 | -------------------------------------------------------------------------------- /Chapter02/ArrayListExample.kt: -------------------------------------------------------------------------------- 1 | fun main(args: Array) { 2 | val companies = arrayListOf("Google", "Mocrosoft", "Facebook", "Apple", "JetBrains") 3 | println("Companies list - ${companies.toString()}") 4 | 5 | companies.add("Amazon") 6 | companies.add("Samsung") 7 | println("Companies list - ${companies.toString()}") 8 | 9 | companies.set(2, "Twitter") 10 | println("Companies list - ${companies.toString()}") 11 | 12 | companies.remove("Samsung") 13 | companies.removeAt(2) 14 | println("Companies list - ${companies.toString()}") 15 | } 16 | -------------------------------------------------------------------------------- /Chapter02/ArrayUpdateExample.kt: -------------------------------------------------------------------------------- 1 | import java.util.* 2 | 3 | fun main(args: Array) { 4 | val languages = arrayOf("Kotlin", "Java", "C", "C++", "C#", "JavaScript", "Python") 5 | 6 | languages[1] = "Swift" 7 | languages[4] = "Objective-C" 8 | println("Newly updated languages are - ${Arrays.toString(languages)}") 9 | 10 | println() 11 | languages.set(5, "TypeScript") 12 | languages.set(6, "Dart") 13 | println("Newly updated languages are - ${Arrays.toString(languages)}") 14 | } 15 | -------------------------------------------------------------------------------- /Chapter02/ImmutableDS.kt: -------------------------------------------------------------------------------- 1 | fun main(args: Array) { 2 | val days = listOf("Sunday", "Monday", "Tuesday", "Wednesday") 3 | val modifiedDays = days + "Thursday" 4 | println("Days - $days, Modified Days - $modifiedDays") 5 | 6 | val months = arrayListOf("January", "February", "March", "April") 7 | months.add("May") 8 | } 9 | -------------------------------------------------------------------------------- /Chapter02/ImmutableList.kt: -------------------------------------------------------------------------------- 1 | import java.util.Arrays 2 | 3 | class ImmutableList { 4 | private val minCapacityIncrement = 12 5 | 6 | var elements: Array 7 | private var size = 0 8 | 9 | constructor() { 10 | this.elements = arrayOf() 11 | } 12 | 13 | constructor(initialCapacity: Int) { 14 | if (initialCapacity > 0) { 15 | this.elements = Array(initialCapacity) {_ -> null} 16 | } else if (initialCapacity == 0) { 17 | this.elements = emptyArray() 18 | } else { 19 | throw IllegalArgumentException("Illegal Capacity: $initialCapacity") 20 | } 21 | } 22 | 23 | constructor(vararg items: E) { 24 | this.elements = items as Array 25 | size = items.size 26 | } 27 | 28 | fun add(element: E): ImmutableList { 29 | val s = size 30 | val newList = ImmutableList(s + 1) 31 | System.arraycopy(elements, 0, newList.elements, 0, s) 32 | newList.elements[s] = element 33 | newList.size = s + 1 34 | return newList 35 | } 36 | 37 | fun get(index: Int): E { 38 | if (index >= size) 39 | throwIndexOutOfBoundsException(index, size) 40 | return elements[index] as E 41 | } 42 | 43 | fun set(index: Int, element: E): ImmutableList { 44 | if (index >= size) 45 | throwIndexOutOfBoundsException(index, size) 46 | 47 | val s = size 48 | val newList = ImmutableList(s) 49 | System.arraycopy(elements, 0, newList.elements, 0, s) 50 | newList.elements[index] = element 51 | newList.size = s 52 | return newList 53 | } 54 | 55 | fun isEmpty() = size == 0 56 | 57 | fun size() = size 58 | 59 | operator fun contains(element: E): Boolean { 60 | return indexOf(element) >= 0 61 | } 62 | 63 | fun indexOf(element: E): Int { 64 | if (element == null) { 65 | for (i in 0 until size) 66 | if (elements[i] == null) 67 | return i 68 | } else { 69 | for (i in 0 until size) 70 | if (element == elements[i]) 71 | return i 72 | } 73 | return -1 74 | } 75 | 76 | fun lastIndexOf(element: E): Int { 77 | if (element == null) { 78 | for (i in size - 1 downTo 0) 79 | if (elements[i] == null) 80 | return i 81 | } else { 82 | for (i in size - 1 downTo 0) 83 | if (element == elements[i]) 84 | return i 85 | } 86 | return -1 87 | } 88 | 89 | fun toArray(): Array { 90 | return Arrays.copyOf(elements, size) 91 | } 92 | 93 | private fun newCapacity(currentCapacity: Int): Int { 94 | val increment = if (currentCapacity < minCapacityIncrement / 2) 95 | minCapacityIncrement 96 | else 97 | currentCapacity shr 1 98 | return currentCapacity + increment 99 | } 100 | 101 | private fun throwIndexOutOfBoundsException(index: Int, size: Int): IndexOutOfBoundsException { 102 | throw IndexOutOfBoundsException("Invalid index $index, size is $size") 103 | } 104 | 105 | override fun toString() = Arrays.toString(elements) 106 | } 107 | 108 | fun main(args: Array) { 109 | val immutableList = ImmutableList("A", "B", "C") 110 | val modifiedList = immutableList.add("D") 111 | println("Initial List - $immutableList") 112 | println("List after adding an element - $modifiedList") 113 | 114 | val list2 = immutableList.set(1, "E") 115 | println("Initial List - $immutableList") 116 | println("List after replacing an element at 1st index - $list2") 117 | } 118 | -------------------------------------------------------------------------------- /Chapter02/Matrix.kt: -------------------------------------------------------------------------------- 1 | import java.util.Arrays 2 | 3 | fun isValidMatrix(arr: Array>): Boolean { 4 | var isValid = true 5 | var sizeOfRow = arr[0].size 6 | for (row in arr) { // Can be optimized more by iterating from 1st index instead of 0th 7 | if (sizeOfRow != row.size) { 8 | isValid = false 9 | break 10 | } 11 | } 12 | return isValid 13 | } 14 | 15 | fun add(a: Array, b: Array): Array { 16 | val m = a.size 17 | val n = a[0].size 18 | val c = Array(m) { DoubleArray(n) } 19 | for (i in 0 until m) 20 | for (j in 0 until n) 21 | c[i][j] = a[i][j] + b[i][j] 22 | return c 23 | } 24 | 25 | fun subtract(a: Array, b: Array): Array { 26 | val m = a.size 27 | val n = a[0].size 28 | val c = Array(m) { DoubleArray(n) } 29 | for (i in 0 until m) 30 | for (j in 0 until n) 31 | c[i][j] = a[i][j] - b[i][j] 32 | return c 33 | } 34 | 35 | fun multiply(a: Array, b: Array): Array { 36 | val m1 = a.size 37 | val n1 = a[0].size 38 | val m2 = b.size 39 | val n2 = b[0].size 40 | if (n1 != m2) throw RuntimeException("Illegal matrix dimensions.") 41 | val c = Array(m1) { DoubleArray(n2) } 42 | for (i in 0 until m1) 43 | for (j in 0 until n2) 44 | for (k in 0 until n1) 45 | c[i][j] += a[i][k] * b[k][j] 46 | return c 47 | } 48 | 49 | fun multiply(a: Array, x: DoubleArray): DoubleArray { 50 | val m = a.size 51 | val n = a[0].size 52 | if (x.size != n) throw RuntimeException("Illegal matrix dimensions.") 53 | val y = DoubleArray(m) 54 | for (i in 0 until m) 55 | for (j in 0 until n) 56 | y[i] += a[i][j] * x[j] 57 | return y 58 | } 59 | 60 | fun multiply(x: DoubleArray, a: Array): DoubleArray { 61 | val m = a.size 62 | val n = a[0].size 63 | if (x.size != m) throw RuntimeException("Illegal matrix dimensions.") 64 | val y = DoubleArray(n) 65 | for (j in 0 until n) 66 | for (i in 0 until m) 67 | y[j] += a[i][j] * x[i] 68 | return y 69 | } 70 | 71 | fun transpose(a: Array): Array { 72 | val m = a.size 73 | val n = a[0].size 74 | val b = Array(n) { DoubleArray(m) } 75 | for (i in 0 until m) 76 | for (j in 0 until n) 77 | b[j][i] = a[i][j] 78 | return b 79 | } 80 | 81 | fun main(args: Array) { 82 | val matrix: Array> = arrayOf( 83 | arrayOf(1, 2, 3), 84 | arrayOf(4, 5, 6) 85 | ) 86 | println("${Arrays.deepToString(matrix)} is ${if (isValidMatrix(matrix)) "a valid" else "an invalid"} Matrix") 87 | 88 | val matrix1: Array> = arrayOf( 89 | arrayOf(1, 5), 90 | arrayOf(2, 7.8), 91 | arrayOf(3, 4, 3) 92 | ) 93 | println("${Arrays.deepToString(matrix1)} is ${if (isValidMatrix(matrix1)) "a valid" else "an invalid"} Matrix") 94 | 95 | val matrix2 = arrayOf(1, 3, 6) 96 | println("${Arrays.toString(matrix2)} is ${if (matrix2 is Array<*>) "a valid" else "an invalid"} Matrix") 97 | 98 | val matrix3 = arrayOf( 99 | doubleArrayOf(1.2, 2.5, 3.4), 100 | doubleArrayOf(1.2, 4.5, 5.46), 101 | doubleArrayOf(12.2, 23.5, 3.45) 102 | ) 103 | 104 | val matrix4 = arrayOf( 105 | doubleArrayOf(11.22, 223.5, 43.4), 106 | doubleArrayOf(13.2, 42.5, 53.46), 107 | doubleArrayOf(124.2, 23.5, 34.45) 108 | ) 109 | 110 | val matrix5 = doubleArrayOf(2.4, 3.7, 5.88) 111 | 112 | println("${Arrays.deepToString(matrix3)} and ${Arrays.deepToString(matrix4)}") 113 | println("===============================================================") 114 | println("Addition : ${Arrays.deepToString(add(matrix3, matrix4))}") 115 | println("Subtraction : ${Arrays.deepToString(subtract(matrix3, matrix4))}") 116 | println("Multiplication : ${Arrays.deepToString(multiply(matrix3, matrix4))}") 117 | 118 | println() 119 | println("${Arrays.deepToString(matrix3)} and ${Arrays.toString(matrix5)}") 120 | println("===============================================================") 121 | println("Multiplication : ${Arrays.toString(multiply(matrix3, matrix5))}") 122 | 123 | println() 124 | println("${Arrays.toString(matrix5)} and ${Arrays.deepToString(matrix3)}") 125 | println("===============================================================") 126 | println("Multiplication : ${Arrays.toString(multiply(matrix5, matrix3))}") 127 | 128 | println() 129 | println("Transpose of ${Arrays.deepToString(matrix3)} is ${Arrays.deepToString(transpose(matrix3))}") 130 | } 131 | -------------------------------------------------------------------------------- /Chapter02/MultiDimeArrays.kt: -------------------------------------------------------------------------------- 1 | import java.util.Arrays 2 | 3 | fun main(args: Array) { 4 | // Creating Multidimensional array 5 | val numbers = arrayOf( 6 | arrayOf(1, 2, 3), 7 | arrayOf(4, 5, 6), 8 | arrayOf(7, 8, 9) 9 | ) 10 | println(Arrays.deepToString(numbers)) 11 | println("Type of the numbers array - $numbers") 12 | 13 | val food = arrayOf( 14 | arrayOf("Apple", "Apricot", "Avocado"), 15 | arrayOf("Banana", "Broccoli", "Beetroot"), 16 | arrayOf("Cherry", "Carrot") 17 | ) 18 | println(Arrays.deepToString(food)) 19 | println("Type of the food array - $numbers") 20 | 21 | // Accessing elements froma multidimensional array 22 | val row1 = food[0] 23 | val row2 = food[1] 24 | println("Foods starting with A : ${Arrays.toString(row1)}") 25 | println("Foods starting with B : ${Arrays.toString(row2)}") 26 | 27 | val firstFoodWithA = row1[0] 28 | val firstFoodWithB = row2[0] 29 | println("1st food starting with A : $firstFoodWithA") 30 | println("1st food starting with B : $firstFoodWithB") 31 | 32 | println("2nd food item which starts from B is : ${food[1][1]}") 33 | println("2nd food item which starts from C is : ${food[2][1]}") 34 | 35 | food[0] = arrayOf("Date", "Damson", "Durian") 36 | println("Updated array - ${Arrays.deepToString(food)}") 37 | 38 | food[2][1] = "Coconut" 39 | println("Updated array - ${Arrays.deepToString(food)}") 40 | 41 | for (row in food) { 42 | print("Item : ") 43 | for (item in row) { 44 | print("$item ") 45 | } 46 | println() 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /Chapter02/Vector.kt: -------------------------------------------------------------------------------- 1 | import java.util.Arrays 2 | 3 | class Vector { 4 | private val minCapacityIncrement = 12 5 | 6 | var elements: Array 7 | private var size = 0 8 | 9 | constructor() { 10 | this.elements = arrayOf() 11 | } 12 | 13 | constructor(initialCapacity: Int) { 14 | if (initialCapacity > 0) { 15 | this.elements = Array(initialCapacity) {i -> null} 16 | } else if (initialCapacity == 0) { 17 | this.elements = emptyArray() 18 | } else { 19 | throw IllegalArgumentException("Illegal Capacity: $initialCapacity") 20 | } 21 | } 22 | 23 | fun add(element: E): Boolean { 24 | var a = elements 25 | val s = size 26 | if (s == a.size) { 27 | val newArray = arrayOfNulls(s + if (s < minCapacityIncrement / 2) 28 | minCapacityIncrement 29 | else 30 | s shr 1) 31 | System.arraycopy(a, 0, newArray, 0, s) 32 | a = newArray 33 | elements = a 34 | } 35 | a[s] = element 36 | size = s + 1 37 | return true 38 | } 39 | 40 | fun add(index: Int, element: E) { 41 | var a = elements 42 | val s = size 43 | if (index > s || index < 0) { 44 | throwIndexOutOfBoundsException(index, s) 45 | } 46 | if (s < a.size) { 47 | System.arraycopy(a, index, a, index + 1, s - index) 48 | } else { 49 | val newArray = arrayOfNulls(newCapacity(s)) 50 | System.arraycopy(a, 0, newArray, 0, index) 51 | System.arraycopy(a, index, newArray, index + 1, s - index) 52 | a = newArray 53 | elements = a 54 | } 55 | a[index] = element 56 | size = s + 1 57 | } 58 | 59 | fun get(index: Int): E { 60 | if (index >= size) 61 | throwIndexOutOfBoundsException(index, size) 62 | return elements[index] as E 63 | } 64 | 65 | fun set(index: Int, element: E): E { 66 | if (index >= size) 67 | throwIndexOutOfBoundsException(index, size) 68 | 69 | val oldValue = elements[index] as E 70 | elements[index] = element 71 | return oldValue 72 | } 73 | 74 | fun remove(index: Int): E { 75 | if (index >= size) 76 | throwIndexOutOfBoundsException(index, size) 77 | 78 | val oldValue = elements[index] as E 79 | 80 | val numMoved = size - index - 1 81 | if (numMoved > 0) 82 | System.arraycopy(elements, index + 1, elements, index, numMoved) 83 | elements[--size] = null // clear to let GC do its work 84 | 85 | return oldValue 86 | } 87 | 88 | fun remove(element: E): Boolean { 89 | for (index in 0 until size) { 90 | if (element == elements[index]) { 91 | val numMoved = size - index - 1 92 | if (numMoved > 0) 93 | System.arraycopy(elements, index + 1, elements, index, numMoved) 94 | elements[--size] = null // clear to let GC do its work 95 | return true 96 | } 97 | } 98 | return false 99 | } 100 | 101 | fun isEmpty() = size == 0 102 | 103 | fun size() = size 104 | 105 | operator fun contains(element: E): Boolean { 106 | return indexOf(element) >= 0 107 | } 108 | 109 | fun indexOf(element: E): Int { 110 | if (element == null) { 111 | for (i in 0 until size) 112 | if (elements[i] == null) 113 | return i 114 | } else { 115 | for (i in 0 until size) 116 | if (element == elements[i]) 117 | return i 118 | } 119 | return -1 120 | } 121 | 122 | fun lastIndexOf(element: E): Int { 123 | if (element == null) { 124 | for (i in size - 1 downTo 0) 125 | if (elements[i] == null) 126 | return i 127 | } else { 128 | for (i in size - 1 downTo 0) 129 | if (element == elements[i]) 130 | return i 131 | } 132 | return -1 133 | } 134 | 135 | fun clear() { 136 | // clear to let GC do its work 137 | for (i in 0 until size) 138 | elements[i] = null 139 | size = 0 140 | } 141 | 142 | fun toArray(): Array { 143 | return Arrays.copyOf(elements, size) 144 | } 145 | 146 | private fun newCapacity(currentCapacity: Int): Int { 147 | val increment = if (currentCapacity < minCapacityIncrement / 2) 148 | minCapacityIncrement 149 | else 150 | currentCapacity shr 1 151 | return currentCapacity + increment 152 | } 153 | 154 | private fun throwIndexOutOfBoundsException(index: Int, size: Int): IndexOutOfBoundsException { 155 | throw IndexOutOfBoundsException("Invalid index $index, size is $size") 156 | } 157 | 158 | override fun toString() = Arrays.toString(elements) 159 | } 160 | 161 | fun main(args: Array) { 162 | val vector = Vector(10) 163 | println("Size of newly created vector is : ${vector.size()}") 164 | println("Elements in vector are : $vector") 165 | 166 | vector.add("Kotlin") 167 | vector.add("Java") 168 | vector.add("Python") 169 | println("Elements in vector are : $vector") 170 | 171 | vector.set(2, "JavaScript") 172 | println("Elements in vector are : $vector") 173 | 174 | vector.remove("Java") 175 | println("Elements in vector are : $vector") 176 | 177 | vector.add("Python") 178 | vector.remove(2) 179 | println("Elements in vector are : $vector") 180 | } 181 | -------------------------------------------------------------------------------- /Chapter03/CircularLinkyList.kt: -------------------------------------------------------------------------------- 1 | class CircularLinkyList { 2 | private var size = 0 3 | private var head: Node? = null 4 | private var tail: Node? = null 5 | 6 | private inner class Node constructor(internal var prev: Node?, internal var element: E, internal var next: Node?) 7 | 8 | fun getFirst() = head?.element 9 | 10 | fun getLast() = tail?.element 11 | 12 | fun removeFirst() = if (head == null) null else unlink(head!!) 13 | 14 | fun removeLast() = if (tail == null) null else unlink(tail!!) 15 | 16 | fun addFirst(element: E) { 17 | linkHead(element) 18 | } 19 | 20 | fun addLast(element: E) { 21 | linkTail(element) 22 | } 23 | 24 | fun add(element: E) { 25 | linkTail(element) 26 | } 27 | 28 | fun remove(element: E): Boolean { 29 | if (head == null) return false 30 | var curr = head 31 | do { 32 | if (curr!!.element == element) { 33 | unlink(curr) 34 | return true 35 | } 36 | curr = curr.next 37 | } while (curr != head) 38 | return false 39 | } 40 | 41 | fun size() = size 42 | 43 | operator fun contains(element: E) = indexOf(element) != -1 44 | 45 | fun get(index: Int): E { 46 | validateElementIndex(index) 47 | return node(index).element 48 | } 49 | 50 | fun set(index: Int, element: E): E { 51 | validateElementIndex(index) 52 | val x = node(index) 53 | val oldVal = x.element 54 | x.element = element 55 | return oldVal 56 | } 57 | 58 | fun add(index: Int, element: E) { 59 | validatePositionIndex(index) 60 | if (index == size) { 61 | linkTail(element) 62 | } else { 63 | linkBefore(element, node(index)) 64 | } 65 | } 66 | 67 | fun remove(index: Int): E { 68 | validateElementIndex(index) 69 | return unlink(node(index)) 70 | } 71 | 72 | fun indexOf(element: E): Int { 73 | if (head == null) return -1 74 | var index = 0 75 | var x = head 76 | do { 77 | if (element == x!!.element) 78 | return index 79 | index++ 80 | x = x.next 81 | } while (x != tail) 82 | return -1 83 | } 84 | 85 | private fun linkHead(element: E) { 86 | val h = head 87 | val newNode = Node(tail, element, h) 88 | head = newNode 89 | if (h == null) { 90 | tail = newNode 91 | newNode!!.prev = tail 92 | newNode.next = head 93 | } else { 94 | h.prev = newNode 95 | tail!!.next = newNode 96 | } 97 | size++ 98 | } 99 | 100 | private fun linkTail(element: E) { 101 | val t = tail 102 | val newNode = Node(t, element, head) 103 | tail = newNode 104 | if (t == null) { 105 | head = newNode 106 | } else { 107 | t.next = newNode 108 | head!!.prev = newNode 109 | } 110 | size++ 111 | } 112 | 113 | private fun linkBefore(element: E, succ: Node) { 114 | val pred = succ.prev 115 | val newNode = Node(pred, element, succ) 116 | succ.prev = newNode 117 | if (pred == tail) { 118 | head = newNode 119 | } 120 | pred?.next = newNode 121 | size++ 122 | } 123 | 124 | private fun unlink(curr: Node): E { 125 | val element = curr.element 126 | val next = curr.next 127 | val prev = curr.prev 128 | 129 | if (curr == head) { 130 | head = next 131 | } 132 | if (curr == tail) { 133 | tail = prev 134 | } 135 | prev?.next = next 136 | next?.prev = prev 137 | curr.prev = null 138 | curr.next = null 139 | size-- 140 | if (size == 0) { 141 | head = null 142 | tail = null 143 | } 144 | return element 145 | } 146 | 147 | private fun node(index: Int): Node { 148 | if (index < size shr 1) { 149 | var x = head 150 | for (i in 0 until index) 151 | x = x!!.next 152 | return x!! 153 | } else { 154 | var x = tail 155 | for (i in size - 1 downTo index + 1) 156 | x = x!!.prev 157 | return x!! 158 | } 159 | } 160 | 161 | private fun validateElementIndex(index: Int) { 162 | if (index < 0 || index >= size) 163 | throw IndexOutOfBoundsException(outOfBoundsMsg(index)) 164 | } 165 | 166 | private fun validatePositionIndex(index: Int) { 167 | if (index < 0 && index > size) 168 | throw IndexOutOfBoundsException(outOfBoundsMsg(index)) 169 | } 170 | 171 | private fun outOfBoundsMsg(index: Int): String { 172 | return "Index: $index, Size: $size" 173 | } 174 | 175 | override fun toString(): String { 176 | var curr = head 177 | if (curr == null) return "[]" 178 | else { 179 | val sb = StringBuilder() 180 | sb.append('[') 181 | for (i in 0 until size) { 182 | sb.append(curr!!.element) 183 | curr = curr.next 184 | if (curr == head) { 185 | sb.append(']') 186 | } else { 187 | sb.append(',').append(' ') 188 | } 189 | } 190 | return sb.toString() 191 | } 192 | } 193 | } 194 | 195 | fun main(args: Array) { 196 | val list = CircularLinkyList() 197 | println("First item of the linky list is - ${list.getFirst()}") 198 | println("Last item of the linky list is - ${list.getLast()}") 199 | 200 | println() 201 | list.add("Kotlin") 202 | println("First item of the linky list is - ${list.getFirst()}") 203 | println("Last item of the linky list is - ${list.getLast()}") 204 | 205 | println() 206 | list.add("Java") 207 | println("First item of the linky list is - ${list.getFirst()}") 208 | println("Last item of the linky list is - ${list.getLast()}") 209 | 210 | list.add("C#") 211 | list.add("Python") 212 | list.add("JavaScript") 213 | 214 | println() 215 | println("Elements at list - $list") 216 | list.remove("JavaScript") 217 | println("Elements at list after removing JavaScript - $list") 218 | list.remove("Kotlin") 219 | println("Elements at list after removing Kotlin - $list") 220 | list.remove("C#") 221 | println("Elements at list after removing C# - $list") 222 | list.remove("Java") 223 | println("Elements at list after removing Java - $list") 224 | list.remove("Python") 225 | println("Elements at list after removing Python - $list") 226 | 227 | testGetFirst() 228 | testAdd() 229 | testGet() 230 | testSet() 231 | testRemoveFirst() 232 | testRemoveLast() 233 | testRemoveValue() 234 | } 235 | 236 | fun testGetFirst() { 237 | println() 238 | println("==================================") 239 | println("getFirst method testing started") 240 | val list = CircularLinkyList() 241 | println(list.getFirst() == null) 242 | 243 | list.add("Kotlin") 244 | println(list.getFirst() == "Kotlin") 245 | 246 | list.add("Java") 247 | println(list.getFirst() == "Kotlin") 248 | 249 | list.add("Python") 250 | println(list.getFirst() == "Kotlin") 251 | 252 | list.add(0, "Python") 253 | println(list.getFirst() == "Python") 254 | 255 | list.add(1, "JavaScript") 256 | println(list.getFirst() == "Python") 257 | 258 | list.set(0, "JavaScript") 259 | println(list.getFirst() == "JavaScript") 260 | 261 | list.set(1, "Kotlin") 262 | println(list.getFirst() == "JavaScript") 263 | 264 | list.addFirst("Kotlin") 265 | println(list.getFirst() == "Kotlin") 266 | 267 | list.addLast("JavaScript") 268 | println(list.getFirst() == "Kotlin") 269 | 270 | println("getFirst method testing ended") 271 | println("==================================") 272 | println() 273 | println("Elements at LinkyList - $list") 274 | 275 | list.addFirst("Kotlin") 276 | println("Elements at LinkyList - $list") 277 | 278 | list.addFirst("Kotlin") 279 | println("Elements at LinkyList - $list") 280 | 281 | list.addFirst("Java") 282 | println("Elements at LinkyList - $list") 283 | 284 | list.addFirst("Python") 285 | println("Elements at LinkyList - $list") 286 | } 287 | 288 | fun testAdd() { 289 | println() 290 | println("==================================") 291 | println("testAdd method testing started") 292 | val list = CircularLinkyList() 293 | list.add("Kotlin") 294 | list.add("Java") 295 | list.add("C#") 296 | list.add("C") 297 | list.add("C++") 298 | println("Elements at LinkyList - $list") 299 | 300 | println() 301 | list.add(1, "JavaScript") 302 | println("Elements at LinkyList - $list") 303 | 304 | println() 305 | list.add(2, "TypeScript") 306 | println("Elements at LinkyList - $list") 307 | 308 | println() 309 | list.add(3, "CofeeScript") 310 | println("Elements at LinkyList - $list") 311 | 312 | println() 313 | list.add(7, "MongoDB") 314 | println("Elements at LinkyList - $list") 315 | 316 | println() 317 | list.add(0, "SQL") 318 | println("Elements at LinkyList - $list") 319 | 320 | println("testAdd method testing started") 321 | println("==================================") 322 | } 323 | 324 | fun testGet() { 325 | println() 326 | println("=================================") 327 | println("Testing get started") 328 | val list = CircularLinkyList() 329 | list.add("Kotlin") 330 | list.add("Java") 331 | list.add("C#") 332 | list.add("C") 333 | list.add("C++") 334 | 335 | println("0th Index - ${list.get(0)}") 336 | println("1st Index - ${list.get(1)}") 337 | println("2nd Index - ${list.get(2)}") 338 | println("3rd Index - ${list.get(3)}") 339 | println("4th Index - ${list.get(4)}") 340 | println("Testing get ended") 341 | println("=================================") 342 | } 343 | 344 | fun testSet() { 345 | println() 346 | println("=================================") 347 | println("Testing set started") 348 | val list = CircularLinkyList() 349 | list.add("Kotlin") 350 | list.add("Java") 351 | list.add("C#") 352 | list.add("C") 353 | list.add("C++") 354 | 355 | println("0th Index - ${list.set(0, "Edited Kotlin")}") 356 | println("1st Index - ${list.set(1, "Edited Java")}") 357 | println("2nd Index - ${list.set(2, "Edited C#")}") 358 | println("3rd Index - ${list.set(3, "Edited C")}") 359 | println("4th Index - ${list.set(4, "Edited C++")}") 360 | println("Final list - $list") 361 | println("Testing set ended") 362 | println("=================================") 363 | } 364 | 365 | fun testRemoveFirst() { 366 | println() 367 | println("=================================") 368 | println("Testing removeFirst started") 369 | val list = CircularLinkyList() 370 | list.add("Kotlin") 371 | list.add("Java") 372 | list.add("C#") 373 | list.add("C") 374 | list.add("C++") 375 | 376 | println("List - $list") 377 | list.removeFirst() 378 | println("List - $list") 379 | list.removeFirst() 380 | println("List - $list") 381 | list.removeFirst() 382 | println("List - $list") 383 | list.removeFirst() 384 | println("List - $list") 385 | list.removeFirst() 386 | println("List - $list") 387 | println("Testing removeFirst ended") 388 | println("=================================") 389 | } 390 | 391 | fun testRemoveLast() { 392 | println() 393 | println("=================================") 394 | println("Testing removeLast started") 395 | val list = CircularLinkyList() 396 | list.add("Kotlin") 397 | list.add("Java") 398 | list.add("C#") 399 | list.add("C") 400 | list.add("C++") 401 | 402 | println("List - $list") 403 | list.removeLast() 404 | println("List - $list") 405 | list.removeLast() 406 | println("List - $list") 407 | list.removeLast() 408 | println("List - $list") 409 | list.removeLast() 410 | println("List - $list") 411 | list.removeLast() 412 | println("List - $list") 413 | println("Testing removeLast ended") 414 | println("=================================") 415 | } 416 | 417 | fun testRemoveValue() { 418 | println() 419 | println("=================================") 420 | println("Testing testRemoveValue started") 421 | val list = CircularLinkyList() 422 | list.add("Kotlin") 423 | list.add("Java") 424 | list.add("C#") 425 | list.add("C") 426 | list.add("C++") 427 | 428 | println("JavaScript" in list) 429 | println("Kotlin" in list) 430 | 431 | println("List - $list") 432 | list.remove("Java") 433 | println("List - $list") 434 | list.remove("Kotlin") 435 | println("List - $list") 436 | list.remove("JavaScript") 437 | println("List - $list") 438 | list.remove("Python") 439 | println("List - $list") 440 | println("Testing testRemoveValue ended") 441 | println("=================================") 442 | } 443 | -------------------------------------------------------------------------------- /Chapter03/LinkyList.kt: -------------------------------------------------------------------------------- 1 | class LinkyList { 2 | private var size = 0 3 | private var head: Node? = null 4 | private var tail: Node? = null 5 | 6 | private inner class Node constructor(internal var element: E, internal var next: Node?) 7 | 8 | fun getFirst() = head?.element 9 | 10 | fun getLast() = tail?.element 11 | 12 | fun removeFirst() = unlinkHead() 13 | 14 | fun removeLast() = unlinkTail() 15 | 16 | fun addFirst(element: E) { 17 | linkHead(element) 18 | } 19 | 20 | fun addLast(element: E) { 21 | linkTail(element) 22 | } 23 | 24 | fun add(element: E) { 25 | linkTail(element) 26 | } 27 | 28 | fun addAll(index: Int, arr: Array): Boolean where T : E { 29 | validatePositionIndex(index) 30 | 31 | val numNew = arr.size 32 | if (numNew == 0) return false 33 | 34 | var pred: Node? 35 | var succ: Node? 36 | when (index) { 37 | 0 -> { 38 | succ = head 39 | pred = null 40 | } 41 | size -> { 42 | succ = null 43 | pred = tail 44 | } 45 | else -> { 46 | pred = node(index - 1) 47 | succ = pred.next 48 | } 49 | } 50 | 51 | for (item in arr) { 52 | val e = item as E 53 | val newNode = Node(e, null) 54 | if (pred == null) 55 | head = newNode 56 | else 57 | pred.next = newNode 58 | pred = newNode 59 | } 60 | 61 | if (succ == null) { 62 | tail = pred 63 | } else { 64 | pred!!.next = succ 65 | } 66 | 67 | size += numNew 68 | return true 69 | } 70 | 71 | fun remove(element: E): Boolean { 72 | var curr = head 73 | while (curr != null) { 74 | if (curr.element == element) { 75 | unlink(curr) 76 | return true 77 | } 78 | curr = curr.next 79 | } 80 | return false 81 | } 82 | 83 | fun clear() { 84 | var x = head 85 | while (x != null) { 86 | val next = x.next 87 | x.next = null 88 | x = next 89 | } 90 | tail = null 91 | head = tail 92 | size = 0 93 | } 94 | 95 | fun size() = size 96 | 97 | operator fun contains(element: E) = indexOf(element) != -1 98 | 99 | fun get(index: Int): E { 100 | validateElementIndex(index) 101 | return node(index).element 102 | } 103 | 104 | fun set(index: Int, element: E): E { 105 | validateElementIndex(index) 106 | val x = node(index) 107 | val oldVal = x.element 108 | x.element = element 109 | return oldVal 110 | } 111 | 112 | fun add(index: Int, element: E) { 113 | validatePositionIndex(index) 114 | if (index == size) { 115 | linkTail(element) 116 | } else { 117 | linkBefore(element, node(index)) 118 | } 119 | } 120 | 121 | fun addV2(index: Int, element: E) { 122 | validatePositionIndex(index) 123 | if (index == 0) linkHead(element) 124 | else { 125 | var x = head 126 | val prevIndex = index - 1 127 | for (i in 0 until prevIndex) { 128 | x = x!!.next 129 | } 130 | val next = x!!.next 131 | val newNode = Node(element, next) 132 | x.next = newNode 133 | size++ 134 | } 135 | } 136 | 137 | fun remove(index: Int): E { 138 | validateElementIndex(index) 139 | return unlink(node(index)) 140 | } 141 | 142 | fun indexOf(element: E): Int { 143 | var index = 0 144 | var x = head 145 | while (x != null) { 146 | if (element == x.element) 147 | return index 148 | index++ 149 | x = x.next 150 | } 151 | return -1 152 | } 153 | 154 | private fun linkHead(element: E) { 155 | val h = head 156 | val newNode = Node(element, h) 157 | head = newNode 158 | if (h == null) { 159 | tail = newNode 160 | } 161 | size++ 162 | } 163 | 164 | private fun linkTail(element: E) { 165 | val t = tail 166 | val newNode = Node(element, null) 167 | tail = newNode 168 | if (t == null) { 169 | head = newNode 170 | } else { 171 | t.next = newNode 172 | } 173 | size++ 174 | } 175 | 176 | private fun linkBefore(element: E, succ: Node) { 177 | val pred = getPrevious(succ) 178 | val newNode = Node(element, succ) 179 | if (pred == null) { 180 | head = newNode 181 | } else { 182 | pred.next = newNode 183 | } 184 | size++ 185 | } 186 | 187 | private fun unlinkHead() { 188 | head?.let { 189 | val next = it.next 190 | it.next = null 191 | head = next 192 | if (next == null) { 193 | tail = null 194 | } 195 | size-- 196 | } 197 | } 198 | 199 | private fun unlinkTail() { 200 | tail?.let { 201 | val prev = getPrevious(it) 202 | tail = prev 203 | if (prev == null) { 204 | head = null 205 | } else { 206 | prev.next = null 207 | } 208 | size-- 209 | } 210 | } 211 | 212 | private fun unlink(curr: Node): E { 213 | val element = curr.element 214 | val next = curr.next 215 | val prev = getPrevious(curr) 216 | 217 | if (prev == null) { 218 | head = next 219 | } else { 220 | prev.next = next 221 | curr.next = null 222 | } 223 | 224 | if (next == null) { 225 | prev?.next = null 226 | tail = prev 227 | } else { 228 | prev?.next = next 229 | curr.next = null 230 | } 231 | 232 | size-- 233 | return element 234 | } 235 | 236 | private fun getPrevious(node: Node): Node? { 237 | if (head != null && node == head) return null 238 | var curr = head 239 | while (curr != null) { 240 | if (curr.next == node) { 241 | return curr 242 | } 243 | curr = curr.next 244 | } 245 | return null 246 | } 247 | 248 | private fun node(index: Int): Node { 249 | var x = head 250 | for (i in 0 until index) { 251 | x = x!!.next 252 | } 253 | return x!! 254 | } 255 | 256 | private fun validateElementIndex(index: Int) { 257 | if (index < 0 || index >= size) 258 | throw IndexOutOfBoundsException(outOfBoundsMsg(index)) 259 | } 260 | 261 | private fun validatePositionIndex(index: Int) { 262 | if (index < 0 && index > size) 263 | throw IndexOutOfBoundsException(outOfBoundsMsg(index)) 264 | } 265 | 266 | private fun outOfBoundsMsg(index: Int): String { 267 | return "Index: $index, Size: $size" 268 | } 269 | 270 | override fun toString(): String { 271 | var curr = head 272 | if (curr == null) return "[]" 273 | else { 274 | val sb = StringBuilder() 275 | sb.append('[') 276 | while (curr != null) { 277 | sb.append(curr.element) 278 | curr = curr.next 279 | if (curr?.element == null) { 280 | sb.append(']') 281 | } else { 282 | sb.append(',').append(' ') 283 | } 284 | } 285 | return sb.toString() 286 | } 287 | } 288 | } 289 | 290 | fun main(args: Array) { 291 | val linkyList = LinkyList() 292 | println("First item of the linky list is - ${linkyList.getFirst()}") 293 | println("Last item of the linky list is - ${linkyList.getLast()}") 294 | 295 | println() 296 | linkyList.add("Kotlin") 297 | println("First item of the linky list is - ${linkyList.getFirst()}") 298 | println("Last item of the linky list is - ${linkyList.getLast()}") 299 | 300 | println() 301 | linkyList.add("Java") 302 | println("First item of the linky list is - ${linkyList.getFirst()}") 303 | println("Last item of the linky list is - ${linkyList.getLast()}") 304 | 305 | linkyList.add("C#") 306 | linkyList.add("Python") 307 | linkyList.add("JavaScript") 308 | 309 | println() 310 | println("Elements at linkyList - $linkyList") 311 | linkyList.remove("JavaScript") 312 | println("Elements at linkyList after removing JavaScript - $linkyList") 313 | linkyList.remove("Kotlin") 314 | println("Elements at linkyList after removing Kotlin - $linkyList") 315 | linkyList.remove("C#") 316 | println("Elements at linkyList after removing C# - $linkyList") 317 | linkyList.remove("Java") 318 | println("Elements at linkyList after removing Java - $linkyList") 319 | linkyList.remove("Python") 320 | println("Elements at linkyList after removing Python - $linkyList") 321 | 322 | testGetFirst() 323 | testAddV2() 324 | testGet() 325 | testSet() 326 | testRemoveFirst() 327 | testRemoveLast() 328 | testRemoveValue() 329 | testAddAll() 330 | } 331 | 332 | fun testGetFirst() { 333 | println() 334 | println("==================================") 335 | println("getFirst method testing started") 336 | val linkyList = LinkyList() 337 | println(linkyList.getFirst() == null) 338 | 339 | linkyList.add("Kotlin") 340 | println(linkyList.getFirst() == "Kotlin") 341 | 342 | linkyList.add("Java") 343 | println(linkyList.getFirst() == "Kotlin") 344 | 345 | linkyList.add("Python") 346 | println(linkyList.getFirst() == "Kotlin") 347 | 348 | linkyList.add(0, "Python") 349 | println(linkyList.getFirst() == "Python") 350 | 351 | linkyList.add(1, "JavaScript") 352 | println(linkyList.getFirst() == "Python") 353 | 354 | linkyList.set(0, "JavaScript") 355 | println(linkyList.getFirst() == "JavaScript") 356 | 357 | linkyList.set(1, "Kotlin") 358 | println(linkyList.getFirst() == "JavaScript") 359 | 360 | linkyList.addFirst("Kotlin") 361 | println(linkyList.getFirst() == "Kotlin") 362 | 363 | linkyList.addLast("JavaScript") 364 | println(linkyList.getFirst() == "Kotlin") 365 | 366 | println("getFirst method testing ended") 367 | println("==================================") 368 | println() 369 | linkyList.clear() 370 | println("Elements at LinkyList - $linkyList") 371 | 372 | linkyList.addFirst("Kotlin") 373 | println("Elements at LinkyList - $linkyList") 374 | 375 | linkyList.addFirst("Kotlin") 376 | println("Elements at LinkyList - $linkyList") 377 | 378 | linkyList.addFirst("Java") 379 | println("Elements at LinkyList - $linkyList") 380 | 381 | linkyList.addFirst("Python") 382 | println("Elements at LinkyList - $linkyList") 383 | } 384 | 385 | fun testAddV2() { 386 | println() 387 | println("==================================") 388 | println("testAddV2 method testing started") 389 | val linkyList = LinkyList() 390 | linkyList.add("Kotlin") 391 | linkyList.add("Java") 392 | linkyList.add("C#") 393 | linkyList.add("C") 394 | linkyList.add("C++") 395 | println("Elements at LinkyList - $linkyList") 396 | 397 | println() 398 | linkyList.addV2(1, "JavaScript") 399 | println("Elements at LinkyList - $linkyList") 400 | 401 | println() 402 | linkyList.addV2(2, "TypeScript") 403 | println("Elements at LinkyList - $linkyList") 404 | 405 | println() 406 | linkyList.addV2(3, "CofeeScript") 407 | println("Elements at LinkyList - $linkyList") 408 | 409 | println() 410 | linkyList.addV2(7, "MongoDB") 411 | println("Elements at LinkyList - $linkyList") 412 | 413 | println() 414 | linkyList.addV2(0, "SQL") 415 | println("Elements at LinkyList - $linkyList") 416 | 417 | println("testAddV2 method testing started") 418 | println("==================================") 419 | } 420 | 421 | fun testGet() { 422 | println() 423 | println("=================================") 424 | println("Testing get started") 425 | val linkyList = LinkyList() 426 | linkyList.add("Kotlin") 427 | linkyList.add("Java") 428 | linkyList.add("C#") 429 | linkyList.add("C") 430 | linkyList.add("C++") 431 | 432 | println("0th Index - ${linkyList.get(0)}") 433 | println("1st Index - ${linkyList.get(1)}") 434 | println("2nd Index - ${linkyList.get(2)}") 435 | println("3rd Index - ${linkyList.get(3)}") 436 | println("4th Index - ${linkyList.get(4)}") 437 | println("Testing get ended") 438 | println("=================================") 439 | } 440 | 441 | fun testSet() { 442 | println() 443 | println("=================================") 444 | println("Testing set started") 445 | val linkyList = LinkyList() 446 | linkyList.add("Kotlin") 447 | linkyList.add("Java") 448 | linkyList.add("C#") 449 | linkyList.add("C") 450 | linkyList.add("C++") 451 | 452 | println("0th Index - ${linkyList.set(0, "Edited Kotlin")}") 453 | println("1st Index - ${linkyList.set(1, "Edited Java")}") 454 | println("2nd Index - ${linkyList.set(2, "Edited C#")}") 455 | println("3rd Index - ${linkyList.set(3, "Edited C")}") 456 | println("4th Index - ${linkyList.set(4, "Edited C++")}") 457 | println("Final list - $linkyList") 458 | println("Testing set ended") 459 | println("=================================") 460 | } 461 | 462 | fun testRemoveFirst() { 463 | println() 464 | println("=================================") 465 | println("Testing removeFirst started") 466 | val linkyList = LinkyList() 467 | linkyList.add("Kotlin") 468 | linkyList.add("Java") 469 | linkyList.add("C#") 470 | linkyList.add("C") 471 | linkyList.add("C++") 472 | 473 | println("List - $linkyList") 474 | linkyList.removeFirst() 475 | println("List - $linkyList") 476 | linkyList.removeFirst() 477 | println("List - $linkyList") 478 | linkyList.removeFirst() 479 | println("List - $linkyList") 480 | linkyList.removeFirst() 481 | println("List - $linkyList") 482 | println("Testing removeFirst ended") 483 | println("=================================") 484 | } 485 | 486 | fun testRemoveLast() { 487 | println() 488 | println("=================================") 489 | println("Testing removeLast started") 490 | val linkyList = LinkyList() 491 | linkyList.add("Kotlin") 492 | linkyList.add("Java") 493 | linkyList.add("C#") 494 | linkyList.add("C") 495 | linkyList.add("C++") 496 | 497 | println("List - $linkyList") 498 | linkyList.removeLast() 499 | println("List - $linkyList") 500 | linkyList.removeLast() 501 | println("List - $linkyList") 502 | linkyList.removeLast() 503 | println("List - $linkyList") 504 | linkyList.removeLast() 505 | println("List - $linkyList") 506 | println("Testing removeLast ended") 507 | println("=================================") 508 | } 509 | 510 | fun testRemoveValue() { 511 | println() 512 | println("=================================") 513 | println("Testing testRemoveValue started") 514 | val linkyList = LinkyList() 515 | linkyList.add("Kotlin") 516 | linkyList.add("Java") 517 | linkyList.add("C#") 518 | linkyList.add("C") 519 | linkyList.add("C++") 520 | 521 | println("JavaScript" in linkyList) 522 | println("Kotlin" in linkyList) 523 | 524 | println("List - $linkyList") 525 | linkyList.remove("Java") 526 | println("List - $linkyList") 527 | linkyList.remove("Kotlin") 528 | println("List - $linkyList") 529 | linkyList.remove("JavaScript") 530 | println("List - $linkyList") 531 | linkyList.remove("Python") 532 | println("List - $linkyList") 533 | println("Testing testRemoveValue ended") 534 | println("=================================") 535 | } 536 | 537 | fun testAddAll() { 538 | println() 539 | println("=================================") 540 | println("Testing testAddAll started") 541 | 542 | val linkyList = LinkyList() 543 | 544 | // Add few elements at begining of the linkedlist 545 | linkyList.addAll(0, arrayOf("C", "C++")) 546 | println("List - $linkyList") 547 | linkyList.addAll(0, arrayOf("Java", "Kotlin")) 548 | println("List - $linkyList") 549 | // Add few elements at middle of the linkedlist 550 | linkyList.addAll(2, arrayOf("Python", "R")) 551 | println("List - $linkyList") 552 | // Add few elements at end of the linkedlist 553 | linkyList.addAll(linkyList.size(), arrayOf("C#", "MATLAB")) 554 | println("List - $linkyList") 555 | println("Testing testAddAll ended") 556 | println("=================================") 557 | } 558 | -------------------------------------------------------------------------------- /Chapter04/CircularFixedQueue.kt: -------------------------------------------------------------------------------- 1 | import java.util.Arrays 2 | 3 | class CircularFixedQueue { 4 | 5 | private val capacity: Int 6 | private var front = -1 7 | private var rear = -1 8 | private val elements: Array 9 | 10 | constructor(capacity: Int) { 11 | this.capacity = capacity 12 | this.elements = arrayOfNulls(capacity) 13 | } 14 | 15 | fun enqueue(element: E) { 16 | if (isFull()) throw QueueOverflowException() 17 | rear = (rear + 1) % capacity 18 | elements[rear] = element 19 | if (front == -1 ) front = rear 20 | } 21 | 22 | fun dequeue(): E { 23 | if (isEmpty()) throw QueueUnderflowException() 24 | val oldVal = elements[front] 25 | elements[front] = null 26 | if (front == rear) { 27 | front = -1 28 | rear = -1 29 | } else front = (front + 1) % capacity 30 | return oldVal as E 31 | } 32 | 33 | fun front() = try { 34 | elements[front] as E 35 | } catch (e: IndexOutOfBoundsException) { 36 | throw QueueUnderflowException(); 37 | } 38 | 39 | fun rear() = try { 40 | elements[rear] as E 41 | } catch (e: IndexOutOfBoundsException) { 42 | throw QueueUnderflowException(); 43 | } 44 | 45 | fun isEmpty() = front == -1 46 | 47 | fun isFull() = (rear + 1) % capacity == front 48 | 49 | override fun toString(): String { 50 | return Arrays.toString(elements) 51 | } 52 | } 53 | 54 | class QueueUnderflowException : RuntimeException() 55 | 56 | class QueueOverflowException : RuntimeException() 57 | 58 | fun main() { 59 | val animals = CircularFixedQueue(10) 60 | System.out.println("$animals -- Empty? - ${animals.isEmpty()} -- Full? - ${animals.isFull()}") 61 | 62 | animals.enqueue("Lion") 63 | System.out.println("$animals -- Empty? - ${animals.isEmpty()} -- Full? - ${animals.isFull()}") 64 | animals.enqueue("Tiger") 65 | System.out.println("$animals -- Empty? - ${animals.isEmpty()} -- Full? - ${animals.isFull()}") 66 | animals.enqueue("Crocodile") 67 | System.out.println("$animals -- Empty? - ${animals.isEmpty()} -- Full? - ${animals.isFull()}") 68 | animals.enqueue("Cat") 69 | System.out.println("$animals -- Empty? - ${animals.isEmpty()} -- Full? - ${animals.isFull()}") 70 | animals.enqueue("Dog") 71 | System.out.println("$animals -- Empty? - ${animals.isEmpty()} -- Full? - ${animals.isFull()}") 72 | animals.enqueue("Cow") 73 | System.out.println("$animals -- Empty? - ${animals.isEmpty()} -- Full? - ${animals.isFull()}") 74 | animals.enqueue("Cangaroo") 75 | System.out.println("$animals -- Empty? - ${animals.isEmpty()} -- Full? - ${animals.isFull()}") 76 | animals.enqueue("Rat") 77 | System.out.println("$animals -- Empty? - ${animals.isEmpty()} -- Full? - ${animals.isFull()}") 78 | animals.enqueue("Bull") 79 | System.out.println("$animals -- Empty? - ${animals.isEmpty()} -- Full? - ${animals.isFull()}") 80 | animals.enqueue("Ox") 81 | System.out.println("$animals -- Empty? - ${animals.isEmpty()} -- Full? - ${animals.isFull()}") 82 | try { 83 | animals.enqueue("Zebra") 84 | } catch(e: QueueOverflowException) { 85 | System.out.println("Exception Expected!!!") 86 | } 87 | animals.dequeue() 88 | System.out.println("$animals -- Empty? - ${animals.isEmpty()} -- Full? - ${animals.isFull()}") 89 | animals.dequeue() 90 | System.out.println("$animals -- Empty? - ${animals.isEmpty()} -- Full? - ${animals.isFull()}") 91 | animals.dequeue() 92 | System.out.println("$animals -- Empty? - ${animals.isEmpty()} -- Full? - ${animals.isFull()}") 93 | animals.dequeue() 94 | System.out.println("$animals -- Empty? - ${animals.isEmpty()} -- Full? - ${animals.isFull()}") 95 | animals.dequeue() 96 | System.out.println("$animals -- Empty? - ${animals.isEmpty()} -- Full? - ${animals.isFull()}") 97 | animals.dequeue() 98 | System.out.println("$animals -- Empty? - ${animals.isEmpty()} -- Full? - ${animals.isFull()}") 99 | animals.dequeue() 100 | System.out.println("$animals -- Empty? - ${animals.isEmpty()} -- Full? - ${animals.isFull()}") 101 | animals.dequeue() 102 | System.out.println("$animals -- Empty? - ${animals.isEmpty()} -- Full? - ${animals.isFull()}") 103 | animals.dequeue() 104 | System.out.println("$animals -- Empty? - ${animals.isEmpty()} -- Full? - ${animals.isFull()}") 105 | animals.dequeue() 106 | System.out.println("$animals -- Empty? - ${animals.isEmpty()} -- Full? - ${animals.isFull()}") 107 | try { 108 | animals.dequeue() 109 | } catch (e: QueueUnderflowException) { 110 | System.out.println("Exception Expected!!!") 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /Chapter04/Deque.kt: -------------------------------------------------------------------------------- 1 | import java.util.Arrays 2 | 3 | class Deque { 4 | private val minCapacityIncrement = 12 5 | 6 | private var elements: Array 7 | private var size = 0 8 | 9 | constructor() { 10 | this.elements = arrayOf() 11 | } 12 | 13 | constructor(initialCapacity: Int) { 14 | this.elements = arrayOfNulls(initialCapacity) 15 | } 16 | 17 | fun enqueueFront(element: E) { 18 | if (size == elements.size) { 19 | val newArray = arrayOfNulls(size + if (size < minCapacityIncrement / 2) 20 | minCapacityIncrement 21 | else 22 | size shr 1) 23 | System.arraycopy(elements, 0, newArray, 1, size) 24 | elements = newArray 25 | } else { 26 | System.arraycopy(elements, 0, elements, 1, size) 27 | } 28 | elements[0] = element 29 | size++ 30 | } 31 | 32 | fun enqueueRear(element: E) { 33 | if (size == elements.size) { 34 | val newArray = arrayOfNulls(size + if (size < minCapacityIncrement / 2) 35 | minCapacityIncrement 36 | else 37 | size shr 1) 38 | System.arraycopy(elements, 0, newArray, 0, size) 39 | elements = newArray 40 | } 41 | elements[size++] = element 42 | } 43 | 44 | fun dequeueFront(): E { 45 | if (size == 0) throw DequeUnderflowException() 46 | val oldVal = elements[0] 47 | elements[0] = null 48 | System.arraycopy(elements, 1, elements, 0, --size) 49 | return oldVal as E 50 | } 51 | 52 | fun dequeueRear(): E { 53 | if (size == 0) throw DequeUnderflowException() 54 | val oldVal = elements[--size] 55 | elements[size] = null 56 | return oldVal as E 57 | } 58 | 59 | fun front() = try { 60 | elements[0] as E 61 | } catch (e: IndexOutOfBoundsException) { 62 | throw DequeUnderflowException(); 63 | } 64 | 65 | fun rear() = try { 66 | elements[size - 1] as E 67 | } catch (e: IndexOutOfBoundsException) { 68 | throw DequeUnderflowException(); 69 | } 70 | 71 | fun isEmpty() = size == 0 72 | 73 | fun isFull() = size == elements.size 74 | 75 | override fun toString(): String { 76 | if (size == 0) return "[]" 77 | val length = size - 1 78 | val builder = StringBuilder(size * 16) 79 | builder.append('[') 80 | for (i in 0 until length) { 81 | builder.append(elements[i]) 82 | builder.append(", ") 83 | } 84 | builder.append(elements[length]) 85 | builder.append(']') 86 | return builder.toString() 87 | } 88 | } 89 | 90 | class DequeUnderflowException : RuntimeException() 91 | 92 | fun main(args: Array) { 93 | val animals = Deque(10) 94 | System.out.println("$animals - Empty? -- ${animals.isEmpty()}") 95 | 96 | animals.enqueueRear("Lion") 97 | System.out.println("$animals - Empty? -- ${animals.isEmpty()}") 98 | 99 | animals.enqueueRear("Tiger") 100 | System.out.println("$animals - Empty? -- ${animals.isEmpty()}") 101 | 102 | animals.enqueueRear("Crocodile") 103 | animals.enqueueRear("Cat") 104 | animals.enqueueRear("Dog") 105 | animals.enqueueRear("Cow") 106 | animals.enqueueRear("Cangaroo") 107 | animals.enqueueRear("Rat") 108 | animals.enqueueRear("Bull") 109 | System.out.println("$animals - Empty? -- ${animals.isEmpty()}") 110 | animals.enqueueRear("Ox") 111 | System.out.println("$animals - Empty? -- ${animals.isEmpty()}") 112 | animals.enqueueRear("Zebra") 113 | System.out.println("$animals - Empty? -- ${animals.isEmpty()}") 114 | animals.dequeueFront() 115 | System.out.println("$animals - Empty? -- ${animals.isEmpty()}") 116 | animals.dequeueFront() 117 | println("Animals - $animals") 118 | animals.dequeueRear() 119 | println("Animals - $animals") 120 | animals.enqueueFront("Zebra") 121 | println("Animals - $animals") 122 | animals.enqueueRear("Tiger") 123 | println("Animals - $animals") 124 | animals.enqueueFront("Octopus") 125 | println("Animals - $animals") 126 | } 127 | -------------------------------------------------------------------------------- /Chapter04/FixedQueue.kt: -------------------------------------------------------------------------------- 1 | import java.util.Arrays 2 | 3 | class FixedQueue { 4 | 5 | private val elements: Array 6 | private var size = 0 7 | 8 | constructor(capacity: Int) { 9 | this.elements = arrayOfNulls(capacity) 10 | } 11 | 12 | fun enqueue(element: E) { 13 | if (size == elements.size) { 14 | throw QueueOverflowException() 15 | } 16 | elements[size++] = element 17 | } 18 | 19 | fun dequeue(): E { 20 | if (size == 0) throw QueueUnderflowException() 21 | val oldVal = elements[0] 22 | elements[0] = null 23 | System.arraycopy(elements, 1, elements, 0, --size) 24 | return oldVal as E 25 | } 26 | 27 | fun front() = try { 28 | elements[0] as E 29 | } catch (e: IndexOutOfBoundsException) { 30 | throw QueueUnderflowException(); 31 | } 32 | 33 | fun rear() = try { 34 | elements[size - 1] as E 35 | } catch (e: IndexOutOfBoundsException) { 36 | throw QueueUnderflowException(); 37 | } 38 | 39 | fun isEmpty() = size == 0 40 | 41 | fun isFull() = size == elements.size 42 | 43 | override fun toString(): String { 44 | if (size == 0) return "[]" 45 | val length = size - 1 46 | val builder = StringBuilder(size * 16) 47 | builder.append('[') 48 | for (i in 0 until length) { 49 | builder.append(elements[i]) 50 | builder.append(", ") 51 | } 52 | builder.append(elements[length]) 53 | builder.append(']') 54 | return builder.toString() 55 | } 56 | } 57 | 58 | class QueueUnderflowException : RuntimeException() 59 | 60 | class QueueOverflowException : RuntimeException() 61 | 62 | fun main(args: Array) { 63 | val animals = FixedQueue(10) 64 | System.out.println("$animals - Empty? -- ${animals.isEmpty()} - Full? -- ${animals.isFull()}") 65 | 66 | animals.enqueue("Lion") 67 | System.out.println("$animals - Empty? -- ${animals.isEmpty()} - Full? -- ${animals.isFull()}") 68 | 69 | animals.enqueue("Tiger") 70 | System.out.println("$animals - Empty? -- ${animals.isEmpty()} - Full? -- ${animals.isFull()}") 71 | 72 | animals.enqueue("Crocodile") 73 | animals.enqueue("Cat") 74 | animals.enqueue("Dog") 75 | animals.enqueue("Cow") 76 | animals.enqueue("Cangaroo") 77 | animals.enqueue("Rat") 78 | animals.enqueue("Bull") 79 | System.out.println("$animals - Empty? -- ${animals.isEmpty()} - Full? -- ${animals.isFull()}") 80 | animals.enqueue("Ox") 81 | System.out.println("$animals - Empty? -- ${animals.isEmpty()} - Full? -- ${animals.isFull()}") 82 | try { 83 | animals.enqueue("Zebra") 84 | } catch(e: QueueOverflowException) { 85 | System.out.println("Exception Expected!!!") 86 | } 87 | animals.dequeue() 88 | System.out.println("$animals - Empty? -- ${animals.isEmpty()} - Full? -- ${animals.isFull()}") 89 | animals.dequeue() 90 | println(animals) 91 | animals.dequeue() 92 | println(animals) 93 | animals.dequeue() 94 | println(animals) 95 | animals.dequeue() 96 | println(animals) 97 | animals.dequeue() 98 | println(animals) 99 | animals.dequeue() 100 | println(animals) 101 | animals.dequeue() 102 | println(animals) 103 | animals.dequeue() 104 | println(animals) 105 | animals.dequeue() 106 | println(animals) 107 | try { 108 | animals.dequeue() 109 | } catch(e: QueueUnderflowException) { 110 | System.out.println("FixedQueue is already empty!!!") 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /Chapter04/FixedStack.kt: -------------------------------------------------------------------------------- 1 | import java.util.Arrays 2 | 3 | class FixedStack { 4 | 5 | private val elements: Array 6 | private var size = 0 7 | 8 | constructor(capacity: Int) { 9 | this.elements = arrayOfNulls(capacity) 10 | } 11 | 12 | fun push(element: E) { 13 | if (size == elements.size) { 14 | throw StackOverflowException() 15 | } 16 | elements[size++] = element 17 | } 18 | 19 | fun pop(): E { 20 | if (size == 0) throw StackUnderflowException() 21 | val index = --size 22 | val obj = elements[index] 23 | elements[index] = null 24 | return obj as E 25 | } 26 | 27 | fun peek() = try { 28 | elements[size - 1] as E 29 | } catch (e: IndexOutOfBoundsException) { 30 | throw StackUnderflowException(); 31 | } 32 | 33 | fun isEmpty() = size == 0 34 | 35 | fun isFull() = size == elements.size 36 | 37 | override fun toString(): String { 38 | if (size == 0) return "[]" 39 | val length = size - 1 40 | val builder = StringBuilder(size * 16) 41 | builder.append('[') 42 | for (i in 0 until length) { 43 | builder.append(elements[i]) 44 | builder.append(", ") 45 | } 46 | builder.append(elements[length]) 47 | builder.append(']') 48 | return builder.toString() 49 | } 50 | } 51 | 52 | class StackUnderflowException : RuntimeException() 53 | 54 | class StackOverflowException : RuntimeException() 55 | 56 | fun main(args: Array) { 57 | val animals = FixedStack(10) 58 | System.out.println("$animals - Empty? -- ${animals.isEmpty()} - Full? -- ${animals.isFull()}") 59 | 60 | animals.push("Lion") 61 | System.out.println("$animals - Empty? -- ${animals.isEmpty()} - Full? -- ${animals.isFull()}") 62 | 63 | animals.push("Tiger") 64 | System.out.println("$animals - Empty? -- ${animals.isEmpty()} - Full? -- ${animals.isFull()}") 65 | 66 | animals.push("Crocodile") 67 | animals.push("Cat") 68 | animals.push("Dog") 69 | animals.push("Cow") 70 | animals.push("Cangaroo") 71 | animals.push("Rat") 72 | animals.push("Bull") 73 | System.out.println("$animals - Empty? -- ${animals.isEmpty()} - Full? -- ${animals.isFull()}") 74 | animals.push("Ox") 75 | System.out.println("$animals - Empty? -- ${animals.isEmpty()} - Full? -- ${animals.isFull()}") 76 | try { 77 | animals.push("Zebra") 78 | } catch(e: StackOverflowException) { 79 | System.out.println("Exception Expected!!!") 80 | } 81 | animals.pop() 82 | System.out.println("$animals - Empty? -- ${animals.isEmpty()} - Full? -- ${animals.isFull()}") 83 | } 84 | -------------------------------------------------------------------------------- /Chapter04/LinkedQueue.kt: -------------------------------------------------------------------------------- 1 | import java.util.Arrays 2 | 3 | class LinkedQueue { 4 | private var size = 0 5 | private var head: Node? = null 6 | private var tail: Node? = null 7 | 8 | private inner class Node constructor(internal var prev: Node?, internal var element: E, internal var next: Node?) 9 | 10 | fun enqueue(element: E) { 11 | val t = tail 12 | val newNode = Node(t, element, null) 13 | tail = newNode 14 | if (t == null) { 15 | head = newNode 16 | } else { 17 | t.next = newNode 18 | } 19 | size++ 20 | } 21 | 22 | fun dequeue(): E { 23 | head?.let { 24 | val next = it.next 25 | it.next = null 26 | head = next 27 | if (next == null) { 28 | tail = null 29 | } else { 30 | next.prev = null 31 | } 32 | size-- 33 | return it.element 34 | } ?: throw QueueUnderflowException() 35 | } 36 | 37 | fun front(): E { 38 | head?.let { 39 | return it.element 40 | } ?: throw QueueUnderflowException() 41 | } 42 | 43 | fun rear(): E { 44 | tail?.let { 45 | return it.element 46 | } ?: throw QueueUnderflowException() 47 | } 48 | 49 | fun isEmpty() = size == 0 50 | 51 | override fun toString(): String { 52 | var curr = head 53 | if (curr == null) return "[]" 54 | else { 55 | val sb = StringBuilder() 56 | sb.append('[') 57 | while (curr != null) { 58 | sb.append(curr.element) 59 | curr = curr.next 60 | if (curr?.element == null) { 61 | sb.append(']') 62 | } else { 63 | sb.append(',').append(' ') 64 | } 65 | } 66 | return sb.toString() 67 | } 68 | } 69 | } 70 | 71 | class QueueUnderflowException : RuntimeException() 72 | 73 | fun main(args: Array) { 74 | val animals = LinkedQueue() 75 | System.out.println("$animals - Empty? -- ${animals.isEmpty()}") 76 | 77 | animals.enqueue("Lion") 78 | System.out.println("$animals - Empty? -- ${animals.isEmpty()}") 79 | animals.enqueue("Tiger") 80 | System.out.println("$animals - Empty? -- ${animals.isEmpty()}") 81 | animals.enqueue("Crocodile") 82 | animals.enqueue("Cat") 83 | animals.enqueue("Dog") 84 | animals.enqueue("Cow") 85 | animals.enqueue("Cangaroo") 86 | animals.enqueue("Rat") 87 | animals.enqueue("Bull") 88 | System.out.println("$animals - Empty? -- ${animals.isEmpty()}") 89 | animals.enqueue("Ox") 90 | System.out.println("$animals - Empty? -- ${animals.isEmpty()}") 91 | animals.enqueue("Zebra") 92 | System.out.println("$animals - Empty? -- ${animals.isEmpty()}") 93 | animals.dequeue() 94 | System.out.println("$animals - Empty? -- ${animals.isEmpty()}") 95 | animals.dequeue() 96 | System.out.println("$animals - Empty? -- ${animals.isEmpty()}") 97 | animals.dequeue() 98 | System.out.println("$animals - Empty? -- ${animals.isEmpty()}") 99 | animals.dequeue() 100 | System.out.println("$animals - Empty? -- ${animals.isEmpty()}") 101 | // First element 102 | System.out.println("Front element - ${animals.front()} - Empty? -- ${animals.isEmpty()}") 103 | System.out.println("Front element - ${animals.front()} - Empty? -- ${animals.isEmpty()}") 104 | System.out.println("Front element - ${animals.front()} - Empty? -- ${animals.isEmpty()}") 105 | animals.dequeue() 106 | System.out.println("Front element - ${animals.front()} - Empty? -- ${animals.isEmpty()}") 107 | System.out.println("Front element - ${animals.front()} - Empty? -- ${animals.isEmpty()}") 108 | animals.enqueue("Peacock") 109 | System.out.println("Front element - ${animals.front()} - Empty? -- ${animals.isEmpty()}") 110 | System.out.println("Front element - ${animals.front()} - Empty? -- ${animals.isEmpty()}") 111 | System.out.println("Rear element - ${animals.rear()} - Empty? -- ${animals.isEmpty()}") 112 | System.out.println("Rear element - ${animals.rear()} - Empty? -- ${animals.isEmpty()}") 113 | animals.dequeue() 114 | animals.dequeue() 115 | animals.dequeue() 116 | animals.dequeue() 117 | animals.dequeue() 118 | animals.dequeue() 119 | System.out.println("Front element - ${animals.front()} - Empty? -- ${animals.isEmpty()}") 120 | animals.dequeue() 121 | try { 122 | System.out.println("Front element - ${animals.front()} - Empty? -- ${animals.isEmpty()}") 123 | } catch(e: QueueUnderflowException) { 124 | System.out.println("Already empty!!!") 125 | } 126 | } 127 | -------------------------------------------------------------------------------- /Chapter04/LinkedStack.kt: -------------------------------------------------------------------------------- 1 | import java.util.Arrays 2 | 3 | class LinkedStack { 4 | private var size = 0 5 | private var head: Node? = null 6 | private var tail: Node? = null 7 | 8 | private inner class Node constructor(internal var prev: Node?, internal var element: E, internal var next: Node?) 9 | 10 | fun push(element: E) { 11 | val t = tail 12 | val newNode = Node(t, element, null) 13 | tail = newNode 14 | if (t == null) { 15 | head = newNode 16 | } else { 17 | t.next = newNode 18 | } 19 | size++ 20 | } 21 | 22 | fun pop(): E { 23 | tail?.let { 24 | val prev = it.prev 25 | it.prev = null 26 | tail = prev 27 | if (prev == null) { 28 | head = null 29 | } else { 30 | prev.next = null 31 | } 32 | size-- 33 | return it.element 34 | } ?: throw StackUnderflowException() 35 | } 36 | 37 | fun peek(): E { 38 | tail?.let { 39 | return it.element 40 | } ?: throw StackUnderflowException() 41 | } 42 | 43 | fun isEmpty() = size == 0 44 | 45 | override fun toString(): String { 46 | var curr = head 47 | if (curr == null) return "[]" 48 | else { 49 | val sb = StringBuilder() 50 | sb.append('[') 51 | while (curr != null) { 52 | sb.append(curr.element) 53 | curr = curr.next 54 | if (curr?.element == null) { 55 | sb.append(']') 56 | } else { 57 | sb.append(',').append(' ') 58 | } 59 | } 60 | return sb.toString() 61 | } 62 | } 63 | } 64 | 65 | class StackUnderflowException : RuntimeException() 66 | 67 | fun main(args: Array) { 68 | val animals = LinkedStack() 69 | System.out.println("$animals - Empty? -- ${animals.isEmpty()}") 70 | 71 | animals.push("Lion") 72 | System.out.println("$animals - Empty? -- ${animals.isEmpty()}") 73 | 74 | animals.push("Tiger") 75 | System.out.println("$animals - Empty? -- ${animals.isEmpty()}") 76 | 77 | animals.push("Crocodile") 78 | animals.push("Cat") 79 | animals.push("Dog") 80 | animals.push("Cow") 81 | animals.push("Cangaroo") 82 | animals.push("Rat") 83 | animals.push("Bull") 84 | System.out.println("$animals - Empty? -- ${animals.isEmpty()}") 85 | animals.push("Ox") 86 | System.out.println("$animals - Empty? -- ${animals.isEmpty()}") 87 | animals.push("Zebra") 88 | System.out.println("$animals - Empty? -- ${animals.isEmpty()}") 89 | animals.pop() 90 | System.out.println("$animals - Empty? -- ${animals.isEmpty()}") 91 | animals.pop() 92 | System.out.println("$animals - Empty? -- ${animals.isEmpty()}") 93 | animals.pop() 94 | System.out.println("$animals - Empty? -- ${animals.isEmpty()}") 95 | animals.pop() 96 | System.out.println("$animals - Empty? -- ${animals.isEmpty()}") 97 | animals.pop() 98 | System.out.println("$animals - Empty? -- ${animals.isEmpty()}") 99 | animals.pop() 100 | System.out.println("$animals - Empty? -- ${animals.isEmpty()}") 101 | animals.pop() 102 | System.out.println("$animals - Empty? -- ${animals.isEmpty()}") 103 | animals.pop() 104 | System.out.println("$animals - Empty? -- ${animals.isEmpty()}") 105 | animals.pop() 106 | System.out.println("$animals - Empty? -- ${animals.isEmpty()}") 107 | animals.pop() 108 | System.out.println("$animals - Empty? -- ${animals.isEmpty()}") 109 | animals.pop() 110 | System.out.println("$animals - Empty? -- ${animals.isEmpty()}") 111 | try { 112 | animals.pop() 113 | System.out.println("$animals - Empty? -- ${animals.isEmpty()}") 114 | } catch(ex: StackUnderflowException) { 115 | println("Exception expected!!!") 116 | } 117 | animals.push("Buffalo") 118 | animals.push("Peacock") 119 | println("Peek element - ${animals.peek()}") 120 | println("Peek element - ${animals.peek()}") 121 | println("Peek element - ${animals.peek()}") 122 | println("Peek element - ${animals.peek()}") 123 | println("Peek element - ${animals.peek()}") 124 | println("Peek element - ${animals.peek()}") 125 | animals.pop() 126 | println("Peek element - ${animals.peek()}") 127 | println("Peek element - ${animals.peek()}") 128 | println("Peek element - ${animals.peek()}") 129 | animals.pop() 130 | try { 131 | println("Peek element - ${animals.peek()}") 132 | } catch(e: StackUnderflowException) { 133 | println("Empty Stack!!!") 134 | } 135 | } 136 | -------------------------------------------------------------------------------- /Chapter04/Queue.kt: -------------------------------------------------------------------------------- 1 | import java.util.Arrays 2 | 3 | class Queue { 4 | private val minCapacityIncrement = 12 5 | 6 | private var elements: Array 7 | private var size = 0 8 | 9 | constructor() { 10 | this.elements = arrayOf() 11 | } 12 | 13 | constructor(initialCapacity: Int) { 14 | this.elements = arrayOfNulls(initialCapacity) 15 | } 16 | 17 | constructor(elements: Array) { 18 | this.elements = elements as Array 19 | size += elements.size 20 | } 21 | 22 | fun enqueue(element: E) { 23 | if (size == elements.size) { 24 | val newArray = arrayOfNulls(size + if (size < minCapacityIncrement / 2) 25 | minCapacityIncrement 26 | else 27 | size shr 1) 28 | System.arraycopy(elements, 0, newArray, 0, size) 29 | elements = newArray 30 | } 31 | elements[size++] = element 32 | } 33 | 34 | fun enqueueAll(newElements: Array) { 35 | val newSize = size + newElements.size 36 | if (elements.size < newSize) { 37 | // New sizing can be of any logic as per requirement 38 | val newArray = arrayOfNulls(newSize + minCapacityIncrement) 39 | System.arraycopy(elements, 0, newArray, 0, size) 40 | elements = newArray 41 | } 42 | System.arraycopy(newElements, 0, elements, size, newElements.size) 43 | size = newSize 44 | } 45 | 46 | fun dequeue(): E { 47 | if (size == 0) throw QueueUnderflowException() 48 | val oldVal = elements[0] 49 | elements[0] = null 50 | System.arraycopy(elements, 1, elements, 0, --size) 51 | return oldVal as E 52 | } 53 | 54 | fun dequeue(count: Int) { 55 | if (size == 0 || size < count) throw QueueUnderflowException() 56 | System.arraycopy(elements, count, elements, 0, size - count) 57 | size -= count 58 | for (i in 0 until count) { 59 | elements[size + i] = null 60 | } 61 | } 62 | 63 | fun front() = try { 64 | elements[0] as E 65 | } catch (e: IndexOutOfBoundsException) { 66 | throw QueueUnderflowException(); 67 | } 68 | 69 | fun rear() = try { 70 | elements[size - 1] as E 71 | } catch (e: IndexOutOfBoundsException) { 72 | throw QueueUnderflowException(); 73 | } 74 | 75 | fun isEmpty() = size == 0 76 | 77 | fun isFull() = size == elements.size 78 | 79 | override fun toString(): String { 80 | if (size == 0) return "[]" 81 | val length = size - 1 82 | val builder = StringBuilder(size * 16) 83 | builder.append('[') 84 | for (i in 0 until length) { 85 | builder.append(elements[i]) 86 | builder.append(", ") 87 | } 88 | builder.append(elements[length]) 89 | builder.append(']') 90 | return builder.toString() 91 | } 92 | } 93 | 94 | class QueueUnderflowException : RuntimeException() 95 | 96 | inline fun queueOf(vararg elements: T) = Queue(elements as Array) 97 | 98 | fun main(args: Array) { 99 | val animals = Queue(10) 100 | System.out.println("$animals - Empty? -- ${animals.isEmpty()}") 101 | 102 | animals.enqueue("Lion") 103 | System.out.println("$animals - Empty? -- ${animals.isEmpty()}") 104 | 105 | animals.enqueue("Tiger") 106 | System.out.println("$animals - Empty? -- ${animals.isEmpty()}") 107 | 108 | animals.enqueue("Crocodile") 109 | animals.enqueue("Cat") 110 | animals.enqueue("Dog") 111 | animals.enqueue("Cow") 112 | animals.enqueue("Cangaroo") 113 | animals.enqueue("Rat") 114 | animals.enqueue("Bull") 115 | System.out.println("$animals - Empty? -- ${animals.isEmpty()}") 116 | animals.enqueue("Ox") 117 | System.out.println("$animals - Empty? -- ${animals.isEmpty()}") 118 | animals.enqueue("Zebra") 119 | System.out.println("$animals - Empty? -- ${animals.isEmpty()}") 120 | animals.dequeue() 121 | System.out.println("$animals - Empty? -- ${animals.isEmpty()}") 122 | 123 | println() 124 | val languages = Queue(arrayOf("Kotlin", "Java")) 125 | println("$languages - Empty? -- ${languages.isEmpty()}") 126 | languages.enqueue("C") 127 | println("$languages - Empty? -- ${languages.isEmpty()}") 128 | languages.dequeue() 129 | println("$languages - Empty? -- ${languages.isEmpty()}") 130 | languages.dequeue() 131 | println("$languages - Empty? -- ${languages.isEmpty()}") 132 | languages.dequeue() 133 | println("$languages - Empty? -- ${languages.isEmpty()}") 134 | 135 | testEnqueueDequeue() 136 | testQueueOf() 137 | } 138 | 139 | fun testEnqueueDequeue() { 140 | println() 141 | val numbers = Queue(10) 142 | numbers.enqueueAll(Array(100) { i -> i }) 143 | println(numbers) 144 | println() 145 | numbers.enqueueAll(arrayOf(300, 400, 500)) 146 | println(numbers) 147 | println() 148 | numbers.dequeue(10) 149 | println(numbers) 150 | println() 151 | numbers.enqueueAll(arrayOf(100, 200, 1000)) 152 | println(numbers) 153 | println() 154 | numbers.dequeue(60) 155 | println(numbers) 156 | println() 157 | } 158 | 159 | fun testQueueOf() { 160 | val languages = queueOf("Kotlin", "Java") 161 | println(languages) 162 | languages.enqueue("C") 163 | println(languages) 164 | languages.dequeue() 165 | println(languages) 166 | } 167 | -------------------------------------------------------------------------------- /Chapter04/Stack.kt: -------------------------------------------------------------------------------- 1 | import java.util.Arrays 2 | 3 | class Stack { 4 | private val minCapacityIncrement = 12 5 | 6 | private var elements: Array 7 | private var size = 0 8 | 9 | constructor() { 10 | this.elements = arrayOf() 11 | } 12 | 13 | constructor(initialCapacity: Int) { 14 | this.elements = arrayOfNulls(initialCapacity) 15 | } 16 | 17 | constructor(elements: Array) { 18 | this.elements = elements as Array 19 | size += elements.size 20 | } 21 | 22 | fun push(element: E) { 23 | if (size == elements.size) { 24 | val newArray = arrayOfNulls(size + if (size < minCapacityIncrement / 2) 25 | minCapacityIncrement 26 | else 27 | size shr 1) 28 | System.arraycopy(elements, 0, newArray, 0, size) 29 | elements = newArray 30 | } 31 | elements[size++] = element 32 | } 33 | 34 | fun pushAll(newElements: Array) { 35 | val newSize = size + newElements.size 36 | if (elements.size < newSize) { 37 | // New sizing can be of any logic as per requirement 38 | val newArray = arrayOfNulls(newSize + minCapacityIncrement) 39 | System.arraycopy(elements, 0, newArray, 0, size) 40 | elements = newArray 41 | } 42 | System.arraycopy(newElements, 0, elements, size, newElements.size) 43 | size = newSize 44 | } 45 | 46 | fun pop(): E { 47 | if (size == 0) throw StackUnderflowException() 48 | val index = --size 49 | val obj = elements[index] 50 | elements[index] = null 51 | return obj as E 52 | } 53 | 54 | fun pop(count: Int) { 55 | if (size == 0 || size < count) throw StackUnderflowException() 56 | for (i in 0 until count) { 57 | elements[--size] = null 58 | } 59 | } 60 | 61 | fun peek() = try { 62 | elements[size - 1] as E 63 | } catch (e: IndexOutOfBoundsException) { 64 | throw StackUnderflowException(); 65 | } 66 | 67 | fun isEmpty() = size == 0 68 | 69 | fun isFull() = size == elements.size 70 | 71 | override fun toString(): String { 72 | if (size == 0) return "[]" 73 | val length = size - 1 74 | val builder = StringBuilder(size * 16) 75 | builder.append('[') 76 | for (i in 0 until length) { 77 | builder.append(elements[i]) 78 | builder.append(", ") 79 | } 80 | builder.append(elements[length]) 81 | builder.append(']') 82 | return builder.toString() 83 | } 84 | } 85 | 86 | class StackUnderflowException : RuntimeException() 87 | 88 | inline fun stackOf(vararg elements: T) = Stack(elements as Array) 89 | 90 | fun main(args: Array) { 91 | val animals = Stack(10) 92 | System.out.println("$animals - Empty? -- ${animals.isEmpty()}") 93 | 94 | animals.push("Lion") 95 | System.out.println("$animals - Empty? -- ${animals.isEmpty()}") 96 | 97 | animals.push("Tiger") 98 | System.out.println("$animals - Empty? -- ${animals.isEmpty()}") 99 | 100 | animals.push("Crocodile") 101 | animals.push("Cat") 102 | animals.push("Dog") 103 | animals.push("Cow") 104 | animals.push("Cangaroo") 105 | animals.push("Rat") 106 | animals.push("Bull") 107 | System.out.println("$animals - Empty? -- ${animals.isEmpty()}") 108 | animals.push("Ox") 109 | System.out.println("$animals - Empty? -- ${animals.isEmpty()}") 110 | animals.push("Zebra") 111 | System.out.println("$animals - Empty? -- ${animals.isEmpty()}") 112 | animals.pop() 113 | System.out.println("$animals - Empty? -- ${animals.isEmpty()}") 114 | 115 | println() 116 | val languages = Stack(arrayOf("Kotlin", "Java")) 117 | println("$languages - Empty? -- ${languages.isEmpty()}") 118 | languages.push("C") 119 | println("$languages - Empty? -- ${languages.isEmpty()}") 120 | languages.pop() 121 | println("$languages - Empty? -- ${languages.isEmpty()}") 122 | languages.pop() 123 | println("$languages - Empty? -- ${languages.isEmpty()}") 124 | languages.pop() 125 | println("$languages - Empty? -- ${languages.isEmpty()}") 126 | 127 | testPushAll() 128 | testPop() 129 | testStackOf() 130 | } 131 | 132 | fun testPushAll() { 133 | println() 134 | println("Testing pushAll") 135 | val numbers = Stack(10) 136 | numbers.pushAll(Array(100) { i -> i }) 137 | println(numbers) 138 | numbers.pop() 139 | numbers.pushAll(arrayOf(1, 2, 12, 909)) 140 | println(numbers) 141 | } 142 | 143 | fun testPop() { 144 | println() 145 | println("Testing pop count") 146 | val numbers = Stack(10) 147 | numbers.pushAll(Array(100) { i -> i }) 148 | println(numbers) 149 | numbers.pop(20) 150 | numbers.pushAll(arrayOf(1, 2, 12, 909)) 151 | println(numbers) 152 | } 153 | 154 | fun testStackOf() { 155 | val languages = stackOf("Kotlin", "Java") 156 | println(languages) 157 | languages.push("C") 158 | println(languages) 159 | languages.pop() 160 | println(languages) 161 | } 162 | -------------------------------------------------------------------------------- /Chapter05/ArrayMap.kt: -------------------------------------------------------------------------------- 1 | import java.util.* 2 | 3 | class ArrayMap constructor(capacity: Int = 0) { 4 | 5 | private lateinit var hashes: IntArray 6 | private lateinit var array: Array 7 | 8 | var size = 0 9 | private set 10 | 11 | init { 12 | if(capacity <= 0) { 13 | hashes = IntArray(0) 14 | array = arrayOf() 15 | } else allocArrays(capacity) 16 | } 17 | 18 | fun isEmpty() = size <= 0 19 | 20 | operator fun get(key: K): V? { 21 | val index = indexOfKey(key) 22 | return if (index >= 0) array[(index shl 1) + 1] as V? else null 23 | } 24 | 25 | fun indexOfKey(key: K): Int { 26 | return if (key == null) indexOfNull() else indexOf(key) 27 | } 28 | 29 | fun containsKey(key: K) = indexOfKey(key) >= 0 30 | 31 | fun keyAt(index: Int) = array[index shl 1] as K 32 | 33 | fun valueAt(index: Int) = array[(index shl 1) + 1] as V 34 | 35 | fun setValueAt(index: Int, value: V): V? { 36 | val valueIndex = (index shl 1) + 1 37 | val old = array[valueIndex] as V 38 | array[valueIndex] = value 39 | return old 40 | } 41 | 42 | fun put(key: K, value: V): V? { 43 | val hash: Int 44 | var index: Int 45 | if (key == null) { 46 | hash = 0 47 | index = indexOfNull() 48 | } else { 49 | hash = key.hashCode() 50 | index = indexOf(key) 51 | } 52 | if (index >= 0) { 53 | // key-value pair already present 54 | index = (index shl 1) + 1 55 | val old = array[index] as V 56 | array[index] = value 57 | return old 58 | } 59 | 60 | index = index.inv() 61 | if (size >= hashes.size) { 62 | val newSize = if (size < 4) 4 else size + (size shr 1) 63 | val tempHashes = hashes 64 | val tempArray = array 65 | allocArrays(newSize) 66 | 67 | System.arraycopy(tempHashes, 0, hashes, 0, tempHashes.size) 68 | System.arraycopy(tempArray, 0, array, 0, tempArray.size) 69 | } 70 | 71 | if (index < size) { 72 | System.arraycopy(hashes, index, hashes, index + 1, size - index) 73 | System.arraycopy(array, index shl 1, array, (index + 1) shl 1, (size - index) shl 1) 74 | } 75 | 76 | hashes[index] = hash 77 | array[index shl 1] = key 78 | array[(index shl 1) + 1] = value 79 | size++ 80 | return null 81 | } 82 | 83 | fun remove(key: K): V? { 84 | val index = indexOfKey(key) 85 | if (index >= 0) return removeAt(index) 86 | return null 87 | } 88 | 89 | private fun removeAt(index: Int): V? { 90 | val oldVal = array[(index shl 1) + 1] 91 | val newSize = size - 1 92 | if (size <= 1) { 93 | // Empty Map 94 | hashes = IntArray(0) 95 | array = arrayOf() 96 | } else { 97 | System.arraycopy(hashes, index + 1, hashes, index, newSize - index) 98 | System.arraycopy(array, (index + 1) shl 1, array, index shl 1, (newSize - index) shl 1) 99 | array[newSize shl 1] = null 100 | array[(newSize shl 1) + 1] = null 101 | } 102 | size = newSize 103 | return oldVal as V 104 | } 105 | 106 | private fun indexOf(key: K): Int { 107 | val hash = key!!.hashCode() 108 | 109 | if (size == 0) return 0.inv() 110 | val index = Arrays.binarySearch(hashes, 0, size, hash) 111 | 112 | // Key not found, return -ve value 113 | if (index < 0) return index 114 | 115 | if (key == array[index shl 1]) return index 116 | 117 | // Search for a matching key after the index. 118 | var end = index + 1 119 | while (end < size && hashes[end] == hash) { 120 | if (key == array[end shl 1]) return end 121 | end++ 122 | } 123 | 124 | // Search for a matching key before the index. 125 | var i = index - 1 126 | while (i >= 0 && hashes[i] == hash) { 127 | if (key == array[i shl 1]) return i 128 | i-- 129 | } 130 | return end.inv() 131 | } 132 | 133 | private fun indexOfNull(): Int { 134 | if (size == 0) return 0.inv() 135 | val index = Arrays.binarySearch(hashes, 0, size, 0) 136 | 137 | // Key not found, return -ve value 138 | if (index < 0) return index 139 | 140 | if (null == array[index shl 1]) return index 141 | 142 | // Search for a matching key after the index. 143 | var end = index + 1 144 | while (end < size && hashes[end] == 0) { 145 | if (null == array[end shl 1]) return end 146 | end++ 147 | } 148 | 149 | // Search for a matching key before the index. 150 | var i = index - 1 151 | while (i >= 0 && hashes[i] == 0) { 152 | if (null == array[i shl 1]) return i 153 | i-- 154 | } 155 | return end.inv() 156 | } 157 | 158 | private fun allocArrays(size: Int) { 159 | hashes = IntArray(size) 160 | array = arrayOfNulls(size shl 1) // size * 2 161 | } 162 | } 163 | 164 | fun main(args: Array) { 165 | val map = ArrayMap() 166 | map.put("A", "Apple") 167 | map.put("B", "Banana") 168 | map.put("C", "Cucumber") 169 | 170 | // Testing get 171 | println(map.get("A")) 172 | println(map.get("B")) 173 | println(map.get("C")) 174 | println(map.get("D")) 175 | 176 | // Testing indexOfKey 177 | println("Index Of Key - ${map.indexOfKey("A")}") 178 | println("Index Of Key - ${map.indexOfKey("B")}") 179 | println("Index Of Key - ${map.indexOfKey("C")}") 180 | println("Index Of Key - ${map.indexOfKey("D")}") 181 | 182 | // Testing containsKey 183 | println("Contains Key - ${map.containsKey("A")}") 184 | println("Contains Key - ${map.containsKey("B")}") 185 | println("Contains Key - ${map.containsKey("C")}") 186 | println("Contains Key - ${map.containsKey("D")}") 187 | 188 | // Testing valueAt 189 | println("Value At - ${map.valueAt(0)}") 190 | println("Value At - ${map.valueAt(1)}") 191 | println("Value At - ${map.valueAt(2)}") 192 | println("Value At - ${map.valueAt(3)}") 193 | try { 194 | println("Value At - ${map.valueAt(8)}") 195 | } catch(e: ArrayIndexOutOfBoundsException) { 196 | e.printStackTrace() 197 | } 198 | try { 199 | println("Value At - ${map.valueAt(-2)}") 200 | } catch(e: ArrayIndexOutOfBoundsException) { 201 | e.printStackTrace() 202 | } 203 | 204 | // Testing keyAt 205 | println("Key At - ${map.keyAt(0)}") 206 | println("Key At - ${map.keyAt(1)}") 207 | println("Key At - ${map.keyAt(2)}") 208 | println("Key At - ${map.keyAt(3)}") 209 | 210 | // Testing setValueAt 211 | map.setValueAt(1, "Beet") 212 | println(map.get("A")) 213 | println(map.get("B")) 214 | println(map.get("C")) 215 | println(map.get("D")) 216 | 217 | // Testing remove 218 | println(map.remove("B")) 219 | println(map.get("A")) 220 | println(map.get("B")) 221 | println(map.get("C")) 222 | println(map.get("D")) 223 | map.put("B", "Banana") 224 | println(map.get("A")) 225 | println(map.get("B")) 226 | println(map.get("C")) 227 | println(map.get("D")) 228 | 229 | // Testing isEmpty 230 | println(map.isEmpty()) 231 | map.remove("A") 232 | map.remove("B") 233 | map.remove("C") 234 | println(map.isEmpty()) 235 | } 236 | -------------------------------------------------------------------------------- /Chapter05/HashFunction.kt: -------------------------------------------------------------------------------- 1 | fun hashCodeFromStudentId(studentId: String) = studentId[0].toUpperCase() - 'A' 2 | 3 | private fun hash(key: Char): Int { 4 | var h = key.hashCode() 5 | return h xor (h ushr 16) 6 | } 7 | 8 | fun main(args: Array) { 9 | println(hashCodeFromStudentId("Apple")) 10 | println(hashCodeFromStudentId("angle")) 11 | println(hashCodeFromStudentId("Bangle")) 12 | println(hashCodeFromStudentId("ball")) 13 | 14 | val alphabets = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" 15 | // Testing hash() function 16 | val size = alphabets.length 17 | for (i in 1 until size) { 18 | println("$i = ${hash(alphabets[i])}") 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Chapter05/HashMap.kt: -------------------------------------------------------------------------------- 1 | import java.util.* 2 | import kotlin.math.* 3 | 4 | class HashMap { 5 | 6 | private val minCapacity = 1 shl 4 7 | private val maxCapacity = 1 shl 30 8 | private val loadFactor = 0.75f 9 | 10 | private var table: Array?> 11 | var size = 0 12 | private set 13 | 14 | constructor() { 15 | this.table = arrayOfNulls(minCapacity) 16 | } 17 | 18 | constructor(capacity: Int) { 19 | if (capacity < 0) throw IllegalArgumentException("Invalid Capacity: $capacity") 20 | val finalCapacity = when { 21 | capacity < minCapacity -> minCapacity 22 | capacity > maxCapacity -> maxCapacity 23 | else -> fetchNearestCapacity(capacity) 24 | } 25 | this.table = arrayOfNulls(finalCapacity) 26 | } 27 | 28 | constructor(map: Map) { 29 | val size = map.size 30 | val newSize = when { 31 | size < minCapacity -> minCapacity 32 | else -> fetchNearestCapacity(size) 33 | } 34 | this.table = arrayOfNulls(newSize) 35 | if (size > 0) { 36 | for (entry in map) { 37 | putVal(entry.key, entry.value) 38 | } 39 | } 40 | } 41 | 42 | private fun hash(key: K): Int { 43 | val h = key?.hashCode() ?: 0 44 | return h xor (h ushr 16) 45 | } 46 | 47 | fun isEmpty() = size == 0 48 | 49 | fun get(key: K): V? { 50 | val e = getNode(hash(key), key) 51 | return if (e == null) null else e.value 52 | } 53 | 54 | fun getOrDefault(key: K, defaultVal: V): V? { 55 | val e = getNode(hash(key), key) 56 | return if (e == null) defaultVal else e.value 57 | } 58 | 59 | private fun getNode(hash: Int, key: K): Node? { 60 | val n = table.size 61 | if (n > 0) { 62 | val first = table[(n - 1) and hash] 63 | if (first != null) { 64 | if (first.hash == hash) { // Checking the 1st node 65 | val k = first.key 66 | if (k === key || k == key) return first 67 | } 68 | var e = first.next ?: return null 69 | do { 70 | if (e.hash == hash && e.key === key || e.key == key) return e 71 | } while (e.next != null) 72 | } 73 | } 74 | return null 75 | } 76 | 77 | fun containsKey(key: K) = getNode(hash(key), key) != null 78 | 79 | fun containsValue(value: V): Boolean { 80 | if (size > 0) { 81 | for (index in table.indices) { 82 | var e = table[index] 83 | while (e != null) { 84 | if (value === e.value || value == e.value) return true 85 | e = e.next 86 | } 87 | } 88 | } 89 | return false 90 | } 91 | 92 | fun put(key: K, value: V) { 93 | putVal(key, value) 94 | } 95 | 96 | fun putIfAbsent(key: K, value: V) { 97 | putVal(key, value, true) 98 | } 99 | 100 | fun replace(key: K, value: V) { 101 | val e = getNode(hash(key), key) 102 | if (e != null) e.value = value 103 | } 104 | 105 | private fun putVal(key: K, value: V, onlyIfAbsent: Boolean = false) { 106 | val hash = hash(key) 107 | val n = table.size 108 | val index = (n - 1) and hash 109 | var first = table[index] 110 | if (first == null) { 111 | table[index] = Node(hash, key, value, null) 112 | ++size 113 | } else { 114 | var node: Node? 115 | var k = first.key 116 | if (first.hash == hash && (k === key || k == key) && !onlyIfAbsent) first.value = value 117 | else { 118 | while(true) { 119 | node = first!!.next 120 | if (node == null) { 121 | first.next = Node(hash, key, value, null) 122 | break 123 | } 124 | k = node.key 125 | if (node.hash == hash 126 | && (k === key || k == key) 127 | && !onlyIfAbsent) { 128 | node.value = value 129 | break 130 | } 131 | first = node 132 | } 133 | } 134 | } 135 | } 136 | 137 | fun remove(key: K): V? { 138 | val hash = hash(key) 139 | val n = table.size 140 | val index = (n - 1) and hash 141 | var first = table[index] 142 | if (n > 0 && first != null) { 143 | var node: Node? = null 144 | var k = first.key 145 | if (first.hash == hash && (key === k || key == k)) node = first 146 | else { 147 | var nextNode = first.next 148 | if (nextNode != null) { 149 | do { 150 | k = nextNode!!.key 151 | if (nextNode.hash == hash && (key === k || key == k)) { 152 | node = nextNode 153 | break 154 | } 155 | first = nextNode 156 | nextNode = nextNode.next 157 | } while(nextNode != null) 158 | } 159 | } 160 | if (node != null) { 161 | if (node == first) table[index] = node.next 162 | else first!!.next = node.next 163 | --size 164 | return node.value 165 | } 166 | } 167 | return null 168 | } 169 | 170 | fun clear() { 171 | if (size > 0) { 172 | size = 0 173 | table.fill(null) 174 | } 175 | } 176 | 177 | private class Node( 178 | val hash: Int, 179 | val key: K, 180 | var value: V, 181 | var next: Node?) { 182 | 183 | override fun toString() = "$key=$value" 184 | 185 | override fun hashCode() = (key?.hashCode() ?: 0).xor(value?.hashCode() ?: 0) 186 | 187 | override fun equals(other: Any?): Boolean { 188 | if (other === this) return true 189 | if (other is Node<*, *> && this.key == other.key && this.value == other.value) return true 190 | return false 191 | } 192 | } 193 | 194 | private fun fetchNearestCapacity(i: Int): Int { 195 | var retVal = i - 1 // If input is a power of two, shift its high-order bit right. 196 | 197 | // "Smear" the high-order bit all the way to the right. 198 | retVal = retVal or retVal ushr 1 199 | retVal = retVal or retVal ushr 2 200 | retVal = retVal or retVal ushr 4 201 | retVal = retVal or retVal ushr 8 202 | retVal = retVal or retVal ushr 16 203 | 204 | return retVal + 1 205 | } 206 | } 207 | 208 | fun main(args: Array) { 209 | val map = HashMap() 210 | println(map.get("Test")) 211 | map.put("Test", "Testing") 212 | println(map.get("Test")) 213 | println(map.get("Testing")) 214 | println(map.size) 215 | 216 | map.put("Testing", "Test") 217 | println(map.get("Testing")) 218 | println(map.get("Test")) 219 | println(map.get("test")) 220 | 221 | println("After Removing Xyz") 222 | map.remove("Xyz") 223 | println(map.get("Test")) 224 | println(map.get("Testing")) 225 | 226 | println("After Removing Test") 227 | map.remove("Test") 228 | println(map.get("Test")) 229 | println(map.get("Testing")) 230 | 231 | 232 | println("Testing Put operation") 233 | val languages = HashMap() 234 | languages.put("K", "Kotlin") 235 | languages.put("J", "Java") 236 | languages.put("C", "C++") 237 | println("Getting C - ${languages.get("C")}") 238 | languages.put("C", "C#") 239 | println("Getting C After replacement - ${languages.get("C")}") 240 | languages.putIfAbsent("C", "C++") 241 | println("Getting C After replacement - ${languages.get("C")}") 242 | 243 | println() 244 | println("Testing constructor with Map Argument") 245 | val fruits = hashMapOf("A" to "Apple", "B" to "Banana", "C" to "Cherries") 246 | val moreFruites = HashMap(fruits) 247 | moreFruites.put("D", "Dates") 248 | println(moreFruites.get("A")) 249 | println(moreFruites.get("D")) 250 | println(moreFruites.get("M")) 251 | moreFruites.put("M", "Mango") 252 | println(moreFruites.get("M")) 253 | moreFruites.put("A", "Apricot") 254 | println(moreFruites.get("A")) 255 | } 256 | -------------------------------------------------------------------------------- /Chapter05/IntMap.kt: -------------------------------------------------------------------------------- 1 | import java.util.* 2 | 3 | class IntMap constructor(capacity: Int = 10) { 4 | 5 | private var hashes: IntArray 6 | private var array: Array 7 | 8 | var size = 0 9 | private set 10 | 11 | init { 12 | hashes = IntArray(if (capacity < 0) 0 else capacity) 13 | array = arrayOfNulls(if (capacity < 0) 0 else capacity) 14 | } 15 | 16 | fun isEmpty() = size <= 0 17 | 18 | operator fun get(key: Int) = get(key, null) 19 | 20 | fun get(key: Int, valueIfKeyNotFound: V?): V? { 21 | val index = Arrays.binarySearch(hashes, 0, size, key) 22 | return if (index < 0) valueIfKeyNotFound else array[index] as V 23 | } 24 | 25 | fun delete(key: Int) { 26 | val index = Arrays.binarySearch(hashes, 0, size, key) 27 | if (index >= 0) removeAt(index) 28 | } 29 | 30 | fun removeAt(index: Int) { 31 | System.arraycopy(hashes, index + 1, hashes, index, size - (index + 1)) 32 | System.arraycopy(array, index + 1, array, index, size - (index + 1)); 33 | size-- 34 | } 35 | 36 | fun put(key: Int, value: V) { 37 | var index = Arrays.binarySearch(hashes, 0, size, key) 38 | if (index >= 0) array[index] = value 39 | else { 40 | index = index.inv() 41 | if (size >= hashes.size) { 42 | val newSize = if (size < 4) 4 else size + (size shr 1) 43 | val tempHashes = hashes 44 | val tempArray = array 45 | allocArrays(newSize) 46 | 47 | System.arraycopy(tempHashes, 0, hashes, 0, tempHashes.size) 48 | System.arraycopy(tempArray, 0, array, 0, tempArray.size) 49 | } 50 | 51 | if (index < size) { 52 | System.arraycopy(hashes, index, hashes, index + 1, size - index) 53 | System.arraycopy(array, index, array, index + 1, size - index) 54 | } 55 | 56 | hashes[index] = key 57 | array[index] = value 58 | size++ 59 | } 60 | } 61 | 62 | fun indexOfKey(key: Int): Int { 63 | return Arrays.binarySearch(hashes, 0, size, key) 64 | } 65 | 66 | fun containsKey(key: Int) = indexOfKey(key) >= 0 67 | 68 | fun keyAt(index: Int) = hashes[index] 69 | 70 | fun valueAt(index: Int) = array[index] as V 71 | 72 | fun setValueAt(index: Int, value: V) { 73 | array[index] = value 74 | } 75 | 76 | private fun allocArrays(size: Int) { 77 | hashes = IntArray(size) 78 | array = arrayOfNulls(size) 79 | } 80 | } 81 | 82 | fun main(args: Array) { 83 | val map = IntMap() 84 | map.put(1, "Apple") 85 | map.put(2, "Banana") 86 | map.put(3, "Cucumber") 87 | 88 | // Testing get 89 | println(map.get(1)) 90 | println(map.get(2)) 91 | println(map.get(3)) 92 | println(map.get(4)) 93 | 94 | // Testing indexOfKey 95 | println("Index Of Key - ${map.indexOfKey(1)}") 96 | println("Index Of Key - ${map.indexOfKey(2)}") 97 | println("Index Of Key - ${map.indexOfKey(3)}") 98 | println("Index Of Key - ${map.indexOfKey(4)}") 99 | 100 | // Testing containsKey 101 | println("Contains Key - ${map.containsKey(1)}") 102 | println("Contains Key - ${map.containsKey(2)}") 103 | println("Contains Key - ${map.containsKey(3)}") 104 | println("Contains Key - ${map.containsKey(0)}") 105 | 106 | // Testing valueAt 107 | println("Value At - ${map.valueAt(0)}") 108 | println("Value At - ${map.valueAt(1)}") 109 | println("Value At - ${map.valueAt(2)}") 110 | println("Value At - ${map.valueAt(3)}") 111 | try { 112 | println("Value At - ${map.valueAt(8)}") 113 | } catch(e: ArrayIndexOutOfBoundsException) { 114 | e.printStackTrace() 115 | } 116 | try { 117 | println("Value At - ${map.valueAt(-2)}") 118 | } catch(e: ArrayIndexOutOfBoundsException) { 119 | e.printStackTrace() 120 | } 121 | 122 | // Testing keyAt 123 | println("Key At - ${map.keyAt(0)}") 124 | println("Key At - ${map.keyAt(1)}") 125 | println("Key At - ${map.keyAt(2)}") 126 | println("Key At - ${map.keyAt(3)}") 127 | 128 | // Testing setValueAt 129 | map.setValueAt(1, "Beet") 130 | println(map.get(1)) 131 | println(map.get(2)) 132 | println(map.get(3)) 133 | println(map.get(4)) 134 | 135 | // Testing delete 136 | println(map.delete(2)) 137 | println(map.get(1)) 138 | println(map.get(2)) 139 | println(map.get(3)) 140 | println(map.get(4)) 141 | map.put(2, "Banana") 142 | println(map.get(1)) 143 | println(map.get(2)) 144 | println(map.get(3)) 145 | println(map.get(4)) 146 | 147 | // Testing isEmpty 148 | println(map.isEmpty()) 149 | map.delete(1) 150 | map.delete(2) 151 | map.delete(3) 152 | println(map.isEmpty()) 153 | } 154 | -------------------------------------------------------------------------------- /Chapter06/KMPSearch.kt: -------------------------------------------------------------------------------- 1 | import java.util.* 2 | 3 | fun search(text: String, pattern: String): Int { 4 | val prefixArr = preparePrefixArray(pattern) 5 | val textLen = text.length 6 | val patternLen = pattern.length 7 | 8 | var patternIndex = 0 9 | var textIndex = 0 10 | while ((textIndex < textLen) and (patternIndex < patternLen)) { 11 | if (pattern[patternIndex] == text[textIndex]) { 12 | textIndex++ 13 | patternIndex++ 14 | } else { 15 | if (patternIndex != 0) patternIndex = prefixArr[patternIndex - 1] 16 | else textIndex++ 17 | } 18 | if (patternIndex == patternLen) { 19 | // We found the pattern 20 | return textIndex - patternIndex 21 | } 22 | } 23 | return -1 24 | } 25 | 26 | fun preparePrefixArray(pattern: String): IntArray { 27 | val patternLen = pattern.length 28 | val arr = IntArray(patternLen) 29 | var index = 0 30 | var i = 1 31 | while(i < patternLen) { 32 | if (pattern[i] == pattern[index]) { 33 | arr[i] = index + 1 34 | index++ 35 | i++ 36 | } else { 37 | if (index != 0) index = arr[index - 1] 38 | else { 39 | arr[i] = 0 40 | i++ 41 | } 42 | } 43 | } 44 | return arr 45 | } 46 | 47 | fun main(args:Array) { 48 | println(search("Hello Kotlin!!", "Ko")) 49 | println(search("Hello Kotlin!!", "Kos")) 50 | println(search("Hello", "el")) 51 | println(search("Hello Kotlin", "owel")) 52 | println(search("Hello", "lo")) 53 | println(search("Hello", "llw")) 54 | println(search("Hello", "llo")) 55 | } 56 | -------------------------------------------------------------------------------- /Chapter06/NaivePatternSearch.kt: -------------------------------------------------------------------------------- 1 | fun search(text: String, pattern: String): Int { 2 | var retVal = -1 3 | val patternLen = pattern.length 4 | val textLen = text.length - patternLen 5 | for (i in 0..textLen) { 6 | var isFound = true 7 | for (j in 0 until patternLen) { 8 | if (text[i + j] != pattern[j]) { 9 | isFound = false 10 | break 11 | } 12 | } 13 | if (isFound) { 14 | retVal = i 15 | break 16 | } 17 | } 18 | return retVal 19 | } 20 | 21 | fun main(args: Array) { 22 | println(search("Hello World!!", "abc")) 23 | println(search("Hello World!!", "Hel")) 24 | println(search("Hello World!!", "elo")) 25 | println(search("Hello World!!", "el")) 26 | println(search("Hello World!!", "wo")) 27 | println(search("Hello World!!", "Wo")) 28 | println(search("Hello World!!", "Wod")) 29 | println(search("Hello World!!", "!!")) 30 | } -------------------------------------------------------------------------------- /Chapter06/RabinKarp.kt: -------------------------------------------------------------------------------- 1 | fun search(text: String, pattern: String): Int { 2 | val patternLen = pattern.length 3 | val textLen = text.length - patternLen 4 | val patternHash = hash(pattern) 5 | var subText = text.substring(0, patternLen) 6 | var subTextHash = hash(subText) 7 | var isFound = false 8 | if ((patternHash == subTextHash) and subText.equals(pattern)) return 0 9 | 10 | for (i in 1..textLen) { 11 | subTextHash = rolledHash(text[i - 1], text[i + patternLen - 1], subTextHash, patternLen) 12 | if ((patternHash == subTextHash) and text.substring(i, i + patternLen).equals(pattern)) return i 13 | } 14 | return -1 15 | } 16 | 17 | private fun hash(input: String): Long { 18 | var result = 0L 19 | input.forEachIndexed { index, char -> 20 | result += (char.toDouble() * Math.pow(97.0, index.toDouble())).toLong() 21 | } 22 | return result 23 | } 24 | 25 | private fun rolledHash(oldChar: Char, newChar: Char, oldHash: Long, patternLen: Int): Long { 26 | val newHash = (((oldHash - oldChar.toLong()) / 97) 27 | + newChar.toDouble() * Math.pow(97.0, (patternLen - 1).toDouble())).toLong() 28 | return newHash 29 | } 30 | 31 | fun main(args: Array) { 32 | // Testing Hash function 33 | println(hash("hello")) 34 | val output: Long = 104L + 101L * 97L + 108L * 97L * 97L + 108L * 97L * 97L * 97L + 111L * 97L * 97L * 97L * 97L 35 | println(output) 36 | 37 | // Testing rolled Hash function 38 | println(hash("he")) 39 | val output1: Long = 104L + 101L * 97L 40 | println(output1) 41 | println(hash("el")) 42 | println(rolledHash('h', 'l', hash("he"), 2)) 43 | val output2: Long = 101L + 108L * 97L 44 | println(output2) 45 | 46 | println(search("Hello", "el")) 47 | println(search("Hello Kotlin", "owel")) 48 | println(search("Hello", "lo")) 49 | println(search("Hello", "llw")) 50 | println(search("Hello", "llo")) 51 | } 52 | -------------------------------------------------------------------------------- /Chapter06/Search.kt: -------------------------------------------------------------------------------- 1 | import kotlin.collections.Collection 2 | 3 | fun Array.linearSearch(element: E): Int { 4 | for ((index, value) in this.withIndex()) { 5 | if (value == element) return index 6 | } 7 | return -1 8 | } 9 | 10 | fun Collection.linearSearch(element: E): Int { 11 | for ((index, value) in this.withIndex()) { 12 | if (value == element) return index 13 | } 14 | return -1 15 | } 16 | 17 | fun > Array.linearSearchInSortedArray(element: E): Int { 18 | for ((index, value) in this.withIndex()) { 19 | if (value == element) return index 20 | else if (value > element) return -1 21 | } 22 | return -1 23 | } 24 | 25 | fun > Collection.linearSearchInSortedCollection(element: E): Int { 26 | for ((index, value) in this.withIndex()) { 27 | if (value == element) return index 28 | else if (value > element) return -1 29 | } 30 | return -1 31 | } 32 | 33 | fun > Array.binarySearch(element: E): Int { 34 | var left = 0 35 | var right = size - 1 36 | while (left <= right) { 37 | var mid = (left + right) / 2 38 | val midVal = this[mid] 39 | val compare = midVal.compareTo(element) 40 | 41 | if (compare < 0) left = mid + 1 42 | else if (compare > 0) right = mid - 1 43 | else return mid // element found 44 | } 45 | return -1 // element not found 46 | } 47 | 48 | fun > List.binarySearch(element: E): Int { 49 | var left = 0 50 | var right = size - 1 51 | while (left <= right) { 52 | var mid = (left + right) / 2 53 | val midVal = this[mid] 54 | val compare = midVal.compareTo(element) 55 | 56 | if (compare < 0) left = mid + 1 57 | else if (compare > 0) right = mid - 1 58 | else return mid // element found 59 | } 60 | return -1 // element not found 61 | } 62 | 63 | fun > Array.binarySearch(element: E, start: Int, end: Int): Int { 64 | var left = start 65 | var right = end - 1 66 | while (left <= right) { 67 | var mid = (left + right) / 2 68 | val midVal = this[mid] 69 | val compare = midVal.compareTo(element) 70 | 71 | if (compare < 0) left = mid + 1 72 | else if (compare > 0) right = mid - 1 73 | else return mid // element found 74 | } 75 | return -1 // element not found 76 | } 77 | 78 | fun > List.binarySearch(element: E, start: Int, end: Int): Int { 79 | var left = start 80 | var right = end - 1 81 | while (left <= right) { 82 | var mid = (left + right) / 2 83 | val midVal = this[mid] 84 | val compare = midVal.compareTo(element) 85 | 86 | if (compare < 0) left = mid + 1 87 | else if (compare > 0) right = mid - 1 88 | else return mid // element found 89 | } 90 | return -1 // element not found 91 | } 92 | 93 | fun > Array.jumpSearch(element: E): Int { 94 | val size = this.size 95 | var step = Math.sqrt(size.toDouble()).toInt() 96 | var prev = 0 97 | 98 | while (this[Math.min(step, size) - 1] < element) { 99 | prev = step 100 | step *= 2 101 | if (prev >= size.toInt()) return -1 102 | } 103 | 104 | while(this[prev] < element) { 105 | prev++ 106 | if (prev == Math.min(step, size)) return -1 107 | } 108 | 109 | if (this[prev] == element) { 110 | return prev 111 | } 112 | return -1 113 | } 114 | 115 | fun > List.jumpSearch(element: E): Int { 116 | val size = this.size 117 | var step = Math.sqrt(size.toDouble()).toInt() 118 | var prev = 0 119 | 120 | while (this[Math.min(step, size) - 1] < element) { 121 | prev = step 122 | step *= 2 123 | if (prev >= size.toInt()) return -1 124 | } 125 | 126 | while(this[prev] < element) { 127 | prev++ 128 | if (prev == Math.min(step, size)) return -1 129 | } 130 | 131 | if (this[prev] == element) return prev 132 | return -1 133 | } 134 | 135 | fun > Array.exponentialSearch(element: E): Int { 136 | if (this[0] == element) return 0 137 | 138 | var i = 1 139 | val len = this.size 140 | while(i < len && this[i] <= element) i *= 2 141 | 142 | return this.binarySearch(element, i/2, Math.min(i, len)) 143 | } 144 | 145 | fun > List.exponentialSearch(element: E): Int { 146 | if (this[0] == element) return 0 147 | 148 | var i = 1 149 | val len = this.size 150 | while(i < len && this[i] <= element) i *= 2 151 | 152 | return this.binarySearch(element, i/2, Math.min(i, len)) 153 | } 154 | 155 | fun main(args: Array) { 156 | val languages = arrayOf("Kotlin", "Java", "Scala", "JavaScript", "C#") 157 | println("Java is at - ${languages.linearSearch("Java")}") 158 | println("Scala is at - ${languages.linearSearch("Scala")}") 159 | 160 | val students = arrayListOf("Chanse", "Rivu", "Siddappa", "Chanse") 161 | println("Siddappa is at - ${students.linearSearch("Siddappa")}") 162 | 163 | val uniqueStudents = setOf("Chanse", "Rivu", "Siddappa", "Chanse") 164 | println("Siddappa is at - ${uniqueStudents.linearSearch("Siddappa")}") 165 | 166 | println("==========Binary Search==========") 167 | val numbers = arrayOf(1, 45, 67, 234, 678, 5678, 34567, 909090) 168 | println("1 is at - ${numbers.binarySearch(1)}") 169 | println("45 is at - ${numbers.binarySearch(45)}") 170 | println("67 is at - ${numbers.binarySearch(67)}") 171 | println("234 is at - ${numbers.binarySearch(234)}") 172 | println("678 is at - ${numbers.binarySearch(678)}") 173 | println("5678 is at - ${numbers.binarySearch(5678)}") 174 | println("34567 is at - ${numbers.binarySearch(34567)}") 175 | println("909090 is at - ${numbers.binarySearch(909090)}") 176 | println("4 is at - ${numbers.binarySearch(4)}") 177 | 178 | println("==========Jump Search==========") 179 | println("1 is at - ${numbers.jumpSearch(1)}") 180 | println("45 is at - ${numbers.jumpSearch(45)}") 181 | println("67 is at - ${numbers.jumpSearch(67)}") 182 | println("234 is at - ${numbers.jumpSearch(234)}") 183 | println("678 is at - ${numbers.jumpSearch(678)}") 184 | println("5678 is at - ${numbers.jumpSearch(5678)}") 185 | println("34567 is at - ${numbers.jumpSearch(34567)}") 186 | println("909090 is at - ${numbers.jumpSearch(909090)}") 187 | println("5 is at - ${numbers.jumpSearch(5)}") 188 | 189 | println("==========Exponential Search==========") 190 | println("1 is at - ${numbers.exponentialSearch(1)}") 191 | println("45 is at - ${numbers.exponentialSearch(45)}") 192 | println("67 is at - ${numbers.exponentialSearch(67)}") 193 | println("234 is at - ${numbers.exponentialSearch(234)}") 194 | println("678 is at - ${numbers.exponentialSearch(678)}") 195 | println("5678 is at - ${numbers.exponentialSearch(5678)}") 196 | println("34567 is at - ${numbers.exponentialSearch(34567)}") 197 | println("909090 is at - ${numbers.exponentialSearch(909090)}") 198 | println("5 is at - ${numbers.exponentialSearch(5)}") 199 | } 200 | -------------------------------------------------------------------------------- /Chapter07/BubbleSort.kt: -------------------------------------------------------------------------------- 1 | import java.util.* 2 | 3 | fun > Array.sort() { 4 | val len = size 5 | for (i in 0 until (len - 1)) { 6 | for (j in 0 until (len - i - 1)) { 7 | if (this[j].compareTo(this[j + 1]) > 0) { 8 | val temp = this[j] 9 | this[j] = this[j + 1] 10 | this[j + 1] = temp 11 | } 12 | } 13 | } 14 | } 15 | 16 | fun > Array.descending() { 17 | val len = size 18 | for (i in 0 until (len - 1)) { 19 | for (j in 0 until (len - i - 1)) { 20 | if (this[j].compareTo(this[j + 1]) < 0) { 21 | val temp = this[j] 22 | this[j] = this[j + 1] 23 | this[j + 1] = temp 24 | } 25 | } 26 | } 27 | } 28 | 29 | fun > MutableList.sort() { 30 | val len = size 31 | for (i in 0 until (len - 1)) { 32 | for (j in 0 until (len - i - 1)) { 33 | if (this[j].compareTo(this[j + 1]) > 0) { 34 | val temp = this[j] 35 | this[j] = this[j + 1] 36 | this[j + 1] = temp 37 | } 38 | } 39 | } 40 | } 41 | 42 | fun > List.sort(): List { 43 | val len = size 44 | val resultList = toMutableList() 45 | for (i in 0 until (len - 1)) { 46 | for (j in 0 until (len - i - 1)) { 47 | if (resultList[j].compareTo(resultList[j + 1]) > 0) { 48 | val temp = resultList[j] 49 | resultList[j] = resultList[j + 1] 50 | resultList[j + 1] = temp 51 | } 52 | } 53 | } 54 | return resultList 55 | } 56 | 57 | fun main(args: Array) { 58 | val nums = arrayOf(2, 12, 89, 23, 76, 43, 12) 59 | nums.sort() 60 | println(Arrays.toString(nums)) 61 | nums.descending() 62 | println(Arrays.toString(nums)) 63 | 64 | val languages = mutableListOf("Kotlin", "Java", "C#", "R", "Python", "Scala", "Groovy", "C", "C++") 65 | languages.sort() 66 | println(languages) 67 | 68 | val doubles = listOf(1.2, 2.6, 10.2, 3.5, 200.4, 34.54, 12.3) 69 | val sortedDoubles = doubles.sort() 70 | println("Doubles Before Sort - $doubles") 71 | println("Doubles After Sort - $sortedDoubles") 72 | } 73 | -------------------------------------------------------------------------------- /Chapter07/HeapSort.kt: -------------------------------------------------------------------------------- 1 | import java.util.* 2 | 3 | fun > Array.sort() { 4 | val middle = size / 2 - 1 5 | for (i in middle downTo 0) { 6 | heapify(this, size, i) 7 | } 8 | for (i in size - 1 downTo 0) { 9 | this[0] = this[i].also { this[i] = this[0] } 10 | heapify(this, i, 0) 11 | } 12 | } 13 | 14 | private fun > heapify(arr: Array, heapSize: Int, root: Int) { 15 | var largest = root 16 | val leftNode = 2 * root + 1 17 | val rightNode = 2 * root + 2 18 | if (leftNode < heapSize && arr[leftNode] > arr[largest]) largest = leftNode 19 | if (rightNode < heapSize && arr[rightNode] > arr[largest]) largest = rightNode 20 | 21 | if (largest != root) { 22 | arr[root] = arr[largest].also { arr[largest] = arr[root] } 23 | heapify(arr, heapSize, largest) 24 | } 25 | } 26 | 27 | fun > MutableList.sort() { 28 | val middle = size / 2 - 1 29 | for (i in middle downTo 0) { 30 | heapify(this, size, i) 31 | } 32 | for (i in size - 1 downTo 0) { 33 | this[0] = this[i].also { this[i] = this[0] } 34 | heapify(this, i, 0) 35 | } 36 | } 37 | 38 | private fun > heapify(arr: MutableList, heapSize: Int, root: Int) { 39 | var largest = root 40 | val leftNode = 2 * root + 1 41 | val rightNode = 2 * root + 2 42 | if (leftNode < heapSize && arr[leftNode] > arr[largest]) largest = leftNode 43 | if (rightNode < heapSize && arr[rightNode] > arr[largest]) largest = rightNode 44 | 45 | if (largest != root) { 46 | arr[root] = arr[largest].also { arr[largest] = arr[root] } 47 | heapify(arr, heapSize, largest) 48 | } 49 | } 50 | 51 | fun main(args: Array) { 52 | val nums = arrayOf(2, 12, 89, 23, 76, 43, 12) 53 | nums.sort() 54 | println(Arrays.toString(nums)) 55 | 56 | val languages = mutableListOf("Kotlin", "Java", "C#", "R", "Python", "Scala", "Groovy", "C", "C++") 57 | languages.sort() 58 | println(languages) 59 | } 60 | -------------------------------------------------------------------------------- /Chapter07/InsertionSort.kt: -------------------------------------------------------------------------------- 1 | import java.util.* 2 | 3 | fun > Array.sort() { 4 | val len = size 5 | for (i in 1 until len) { 6 | var key = this[i] 7 | var j = i - 1; 8 | 9 | while(j >= 0 && this[j] > key) { 10 | this[j + 1] = this[j] 11 | j-- 12 | } 13 | this[j + 1] = key 14 | } 15 | } 16 | 17 | fun > MutableList.sort() { 18 | val len = size 19 | for (i in 1 until len) { 20 | var key = this[i] 21 | var j = i - 1; 22 | 23 | while(j >= 0 && this[j].compareTo(key) > 0) { 24 | this[j + 1] = this[j] 25 | j-- 26 | } 27 | this[j + 1] = key 28 | } 29 | } 30 | 31 | fun > List.sort(): List { 32 | val len = size 33 | val resultList = toMutableList() 34 | for (i in 1 until len) { 35 | var key = resultList[i] 36 | var j = i - 1; 37 | 38 | while(j >= 0 && resultList[j].compareTo(key) > 0) { 39 | resultList[j + 1] = resultList[j] 40 | j-- 41 | } 42 | resultList[j + 1] = key 43 | } 44 | return resultList 45 | } 46 | 47 | fun main(args: Array) { 48 | val nums = arrayOf(2, 12, 89, 23, 76, 43, 12) 49 | nums.sort() 50 | println(Arrays.toString(nums)) 51 | 52 | val languages = mutableListOf("Kotlin", "Java", "C#", "R", "Python", "Scala", "Groovy", "C", "C++") 53 | languages.sort() 54 | println(languages) 55 | 56 | val nums1 = listOf(2, 12, 89, 23, 76, 43, 12) 57 | val result = nums1.sort() 58 | println(nums1) 59 | println(result) 60 | } 61 | -------------------------------------------------------------------------------- /Chapter07/MergeSort.kt: -------------------------------------------------------------------------------- 1 | import java.util.* 2 | 3 | fun > Array.sort(): Array { 4 | if (size <= 1) return this 5 | 6 | val middle = size / 2 7 | val left = copyOfRange(0, middle) 8 | val right = copyOfRange(middle, size) 9 | return merge(this, left.sort(), right.sort()) 10 | } 11 | 12 | private fun > merge(arr: Array, left: Array, right: Array): Array { 13 | val leftArrSize = left.size 14 | val rightArrSize = right.size 15 | var leftArrIndex = 0 16 | var rightArrIndex = 0 17 | var index = 0 18 | while(leftArrIndex < leftArrSize && rightArrIndex < rightArrSize) { 19 | if (left[leftArrIndex] <= right[rightArrIndex]) { 20 | arr[index] = left[leftArrIndex] 21 | leftArrIndex++ 22 | } else { 23 | arr[index] = right[rightArrIndex] 24 | rightArrIndex++ 25 | } 26 | index++ 27 | } 28 | 29 | while(leftArrIndex < leftArrSize) { 30 | arr[index] = left[leftArrIndex] 31 | leftArrIndex++ 32 | index++ 33 | } 34 | 35 | while(rightArrIndex < rightArrSize) { 36 | arr[index] = right[rightArrIndex] 37 | rightArrIndex++ 38 | index++ 39 | } 40 | return arr 41 | } 42 | 43 | fun > MutableList.sort(): MutableList { 44 | if (size <= 1) return this 45 | 46 | val middle = size / 2 47 | val left = subList(0, middle) 48 | val right = subList(middle, size) 49 | return merge(left.sort(), right.sort()) 50 | } 51 | 52 | private fun > merge(left: MutableList, right: MutableList): MutableList { 53 | val leftArrSize = left.size 54 | val rightArrSize = right.size 55 | var leftArrIndex = 0 56 | var rightArrIndex = 0 57 | val list: MutableList = mutableListOf() 58 | while(leftArrIndex < leftArrSize && rightArrIndex < rightArrSize) { 59 | if (left[leftArrIndex] <= right[rightArrIndex]) { 60 | list.add(left[leftArrIndex]) 61 | leftArrIndex++ 62 | } else { 63 | list.add(right[rightArrIndex]) 64 | rightArrIndex++ 65 | } 66 | } 67 | 68 | while(leftArrIndex < leftArrSize) { 69 | list.add(left[leftArrIndex]) 70 | leftArrIndex++ 71 | } 72 | 73 | while(rightArrIndex < rightArrSize) { 74 | list.add(right[rightArrIndex]) 75 | rightArrIndex++ 76 | } 77 | return list 78 | } 79 | 80 | fun main(args: Array) { 81 | val nums = arrayOf(2, 12, 89, 23, 76, 43, 12) 82 | nums.sort() 83 | println(Arrays.toString(nums)) 84 | 85 | val languages = mutableListOf("Kotlin", "Java", "C#", "R", "Python", "Scala", "Groovy", "C", "C++") 86 | val sortedLanguages = languages.sort() 87 | println(languages) 88 | println(sortedLanguages) 89 | } 90 | -------------------------------------------------------------------------------- /Chapter07/QuickSort.kt: -------------------------------------------------------------------------------- 1 | import java.util.* 2 | 3 | fun > Array.sort() { 4 | sort(this, 0, size - 1) 5 | } 6 | 7 | private fun > sort(arr: Array, low: Int, high: Int) { 8 | if (low < high) { 9 | val partitionIndex = partition(arr, low, high) 10 | 11 | sort(arr, low, partitionIndex - 1) 12 | sort(arr, partitionIndex + 1, high) 13 | } 14 | } 15 | 16 | private fun > partition(arr: Array, low: Int, high: Int): Int { 17 | val pivot = arr[high] 18 | var i = low - 1 19 | for (j in low until high) { 20 | if (arr[j] <= pivot) { 21 | i++ 22 | arr[i] = arr[j].also { arr[j] = arr[i] } 23 | } 24 | } 25 | arr[i + 1] = arr[high].also { arr[high] = arr[i + 1] } 26 | return i + 1; 27 | } 28 | 29 | fun > MutableList.sort() { 30 | sort(this, 0, size - 1) 31 | } 32 | 33 | private fun > sort(arr: MutableList, low: Int, high: Int) { 34 | if (low < high) { 35 | val partitionIndex = partition(arr, low, high) 36 | 37 | sort(arr, low, partitionIndex - 1) 38 | sort(arr, partitionIndex + 1, high) 39 | } 40 | } 41 | 42 | private fun > partition(arr: MutableList, low: Int, high: Int): Int { 43 | val pivot = arr[high] 44 | var i = low - 1 45 | for (j in low until high) { 46 | if (arr[j] <= pivot) { 47 | i++ 48 | arr[i] = arr[j].also { arr[j] = arr[i] } 49 | } 50 | } 51 | arr[i + 1] = arr[high].also { arr[high] = arr[i + 1] } 52 | return i + 1; 53 | } 54 | 55 | fun > List.sort(): List { 56 | val resultList = toMutableList() 57 | sort(resultList, 0, size - 1) 58 | return resultList 59 | } 60 | 61 | fun > Array.descending() { 62 | descending(this, 0, size - 1) 63 | } 64 | 65 | private fun > descending(arr: Array, low: Int, high: Int) { 66 | if (low < high) { 67 | val partitionIndex = descendingPartition(arr, low, high) 68 | 69 | descending(arr, low, partitionIndex - 1) 70 | descending(arr, partitionIndex + 1, high) 71 | } 72 | } 73 | 74 | private fun > descendingPartition(arr: Array, low: Int, high: Int): Int { 75 | val pivot = arr[high] 76 | var i = low - 1 77 | for (j in low until high) { 78 | if (arr[j] >= pivot) { 79 | i++ 80 | arr[i] = arr[j].also { arr[j] = arr[i] } 81 | } 82 | } 83 | arr[i + 1] = arr[high].also { arr[high] = arr[i + 1] } 84 | return i + 1; 85 | } 86 | 87 | fun > MutableList.descending() { 88 | descending(this, 0, size - 1) 89 | } 90 | 91 | private fun > descending(arr: MutableList, low: Int, high: Int) { 92 | if (low < high) { 93 | val partitionIndex = descendingPartition(arr, low, high) 94 | 95 | descending(arr, low, partitionIndex - 1) 96 | descending(arr, partitionIndex + 1, high) 97 | } 98 | } 99 | 100 | private fun > descendingPartition(arr: MutableList, low: Int, high: Int): Int { 101 | val pivot = arr[high] 102 | var i = low - 1 103 | for (j in low until high) { 104 | if (arr[j] >= pivot) { 105 | i++ 106 | arr[i] = arr[j].also { arr[j] = arr[i] } 107 | } 108 | } 109 | arr[i + 1] = arr[high].also { arr[high] = arr[i + 1] } 110 | return i + 1; 111 | } 112 | 113 | fun main(args: Array) { 114 | val nums = arrayOf(2, 12, 89, 23, 76, 43, 12) 115 | nums.sort() 116 | println(Arrays.toString(nums)) 117 | 118 | val numbers = arrayOf(17, 12, 29, 21, 5, 7) 119 | numbers.sort() 120 | println(Arrays.toString(numbers)) 121 | 122 | val languages = mutableListOf("Kotlin", "Java", "C#", "R", "Python", "Scala", "Groovy", "C", "C++") 123 | languages.sort() 124 | println(languages) 125 | 126 | println() 127 | println("Descending Order") 128 | val numbers2 = arrayOf(2, 12, 89, 23, 76, 43, 12) 129 | numbers2.descending() 130 | println(Arrays.toString(numbers2)) 131 | val list = arrayOf("Kotlin", "Java", "C", "C++", "R", "Python", "Matlab") 132 | list.descending() 133 | println(Arrays.toString(list)) 134 | 135 | println() 136 | println("Immutable list sorting") 137 | val immutableLangs = listOf("Kotlin", "Java", "C#", "R", "Python", "Scala", "Groovy", "C", "C++") 138 | val sortedLangs = immutableLangs.sort() 139 | println(immutableLangs) 140 | println(sortedLangs) 141 | } 142 | -------------------------------------------------------------------------------- /Chapter07/SelectionSort.kt: -------------------------------------------------------------------------------- 1 | import java.util.* 2 | 3 | fun > Array.sort() { 4 | val len = size 5 | // Find the minimum value of the array 6 | for (i in 0 until (len - 1)) { 7 | // Getting the index where minimum value is present 8 | var minIndex = i 9 | for (j in (i + 1) until len) { 10 | if (this[j].compareTo(this[minIndex]) < 0) minIndex = j 11 | } 12 | 13 | // We got the minimum element, now swap that to first element 14 | val temp = this[minIndex] 15 | this[minIndex] = this[i] 16 | this[i] = temp 17 | } 18 | } 19 | 20 | fun > MutableList.sort() { 21 | val len = size 22 | // Find the minimum value of the array 23 | for (i in 0 until (len - 1)) { 24 | // Getting the index where minimum value is present 25 | var minIndex = i 26 | for (j in (i + 1) until len) { 27 | if (this[j].compareTo(this[minIndex]) < 0) minIndex = j 28 | } 29 | 30 | // We got the minimum element, now swap that to first element 31 | val temp = this[minIndex] 32 | this[minIndex] = this[i] 33 | this[i] = temp 34 | } 35 | } 36 | 37 | fun > List.sort(): List { 38 | val len = size 39 | val resultList = toMutableList() 40 | // Find the minimum value of the array 41 | for (i in 0 until (len - 1)) { 42 | // Getting the index where minimum value is present 43 | var minIndex = i 44 | for (j in (i + 1) until len) { 45 | if (resultList[j].compareTo(resultList[minIndex]) < 0) minIndex = j 46 | } 47 | 48 | // We got the minimum element, now swap that to first element 49 | val temp = resultList[minIndex] 50 | resultList[minIndex] = resultList[i] 51 | resultList[i] = temp 52 | } 53 | return resultList 54 | } 55 | 56 | fun main(args: Array) { 57 | val nums = arrayOf(2, 12, 89, 23, 76, 43, 12) 58 | nums.sort() 59 | println(Arrays.toString(nums)) 60 | 61 | val languages = mutableListOf("Kotlin", "Java", "C#", "R", "Python", "Scala", "Groovy", "C", "C++") 62 | languages.sort() 63 | println(languages) 64 | 65 | val nums1 = listOf(2, 12, 89, 23, 76, 43, 12) 66 | val result = nums1.sort() 67 | println(nums1) 68 | println(result) 69 | } 70 | -------------------------------------------------------------------------------- /Chapter08/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /Chapter08/.gradle/3.5-rc-2/file-changes/last-build.bin: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Chapter08/.gradle/3.5-rc-2/taskHistory/fileHashes.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Hands-On-Data-Structures-and-Algorithms-with-Kotlin/f125372a286a3bf4f0248db109a7427f6bc643a7/Chapter08/.gradle/3.5-rc-2/taskHistory/fileHashes.bin -------------------------------------------------------------------------------- /Chapter08/.gradle/3.5-rc-2/taskHistory/fileSnapshots.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Hands-On-Data-Structures-and-Algorithms-with-Kotlin/f125372a286a3bf4f0248db109a7427f6bc643a7/Chapter08/.gradle/3.5-rc-2/taskHistory/fileSnapshots.bin -------------------------------------------------------------------------------- /Chapter08/.gradle/3.5-rc-2/taskHistory/taskHistory.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Hands-On-Data-Structures-and-Algorithms-with-Kotlin/f125372a286a3bf4f0248db109a7427f6bc643a7/Chapter08/.gradle/3.5-rc-2/taskHistory/taskHistory.bin -------------------------------------------------------------------------------- /Chapter08/.gradle/3.5-rc-2/taskHistory/taskHistory.lock: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Hands-On-Data-Structures-and-Algorithms-with-Kotlin/f125372a286a3bf4f0248db109a7427f6bc643a7/Chapter08/.gradle/3.5-rc-2/taskHistory/taskHistory.lock -------------------------------------------------------------------------------- /Chapter08/.gradle/4.4/fileChanges/last-build.bin: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Chapter08/.gradle/4.4/fileHashes/fileHashes.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Hands-On-Data-Structures-and-Algorithms-with-Kotlin/f125372a286a3bf4f0248db109a7427f6bc643a7/Chapter08/.gradle/4.4/fileHashes/fileHashes.bin -------------------------------------------------------------------------------- /Chapter08/.gradle/4.4/fileHashes/fileHashes.lock: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Hands-On-Data-Structures-and-Algorithms-with-Kotlin/f125372a286a3bf4f0248db109a7427f6bc643a7/Chapter08/.gradle/4.4/fileHashes/fileHashes.lock -------------------------------------------------------------------------------- /Chapter08/.gradle/4.4/taskHistory/taskHistory.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Hands-On-Data-Structures-and-Algorithms-with-Kotlin/f125372a286a3bf4f0248db109a7427f6bc643a7/Chapter08/.gradle/4.4/taskHistory/taskHistory.bin -------------------------------------------------------------------------------- /Chapter08/.gradle/4.4/taskHistory/taskHistory.lock: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Hands-On-Data-Structures-and-Algorithms-with-Kotlin/f125372a286a3bf4f0248db109a7427f6bc643a7/Chapter08/.gradle/4.4/taskHistory/taskHistory.lock -------------------------------------------------------------------------------- /Chapter08/.gradle/buildOutputCleanup/buildOutputCleanup.lock: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Hands-On-Data-Structures-and-Algorithms-with-Kotlin/f125372a286a3bf4f0248db109a7427f6bc643a7/Chapter08/.gradle/buildOutputCleanup/buildOutputCleanup.lock -------------------------------------------------------------------------------- /Chapter08/.gradle/buildOutputCleanup/cache.properties: -------------------------------------------------------------------------------- 1 | #Mon Apr 02 21:48:04 IST 2018 2 | gradle.version=4.4 3 | -------------------------------------------------------------------------------- /Chapter08/.gradle/buildOutputCleanup/outputFiles.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Hands-On-Data-Structures-and-Algorithms-with-Kotlin/f125372a286a3bf4f0248db109a7427f6bc643a7/Chapter08/.gradle/buildOutputCleanup/outputFiles.bin -------------------------------------------------------------------------------- /Chapter08/.idea/.name: -------------------------------------------------------------------------------- 1 | Collections -------------------------------------------------------------------------------- /Chapter08/.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Chapter08/.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 18 | -------------------------------------------------------------------------------- /Chapter08/.idea/libraries/Gradle__org_jetbrains_annotations_13_0.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Chapter08/.idea/libraries/Gradle__org_jetbrains_kotlin_kotlin_stdlib_1_2_20.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Chapter08/.idea/libraries/Gradle__org_jetbrains_kotlin_kotlin_stdlib_jdk7_1_2_20.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Chapter08/.idea/libraries/Gradle__org_jetbrains_kotlin_kotlin_stdlib_jdk8_1_2_20.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Chapter08/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Chapter08/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /Chapter08/.idea/modules/Collections.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /Chapter08/.idea/modules/Collections_main.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 20 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /Chapter08/.idea/modules/Collections_test.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 20 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /Chapter08/.idea/uiDesigner.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | -------------------------------------------------------------------------------- /Chapter08/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Collections 4 | Project Collections created by Buildship. 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.buildship.core.gradleprojectbuilder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.eclipse.buildship.core.gradleprojectnature 22 | 23 | 24 | -------------------------------------------------------------------------------- /Chapter08/.settings/org.eclipse.buildship.core.prefs: -------------------------------------------------------------------------------- 1 | #Wed May 02 22:40:01 IST 2018 2 | connection.project.dir= 3 | -------------------------------------------------------------------------------- /Chapter08/CustomObjectSet.kt: -------------------------------------------------------------------------------- 1 | data class MyDataClass (val someNumericValue: Int, val someStringValue: String) 2 | 3 | fun main(args: Array) { 4 | val dataClassSet = setOf( 5 | MyDataClass(1, "1st obj"), 6 | MyDataClass(2, "2nd obj"), 7 | MyDataClass(3, "3rd obj"), 8 | MyDataClass(2, "2nd obj"), 9 | MyDataClass(4, "4th obj"), 10 | MyDataClass(5, "5th obj"), 11 | MyDataClass(2, "will be added"), 12 | MyDataClass(3, "3rd obj") 13 | ) 14 | 15 | println("Printing items of dataClassSet one by one") 16 | for(item in dataClassSet) { 17 | println(item) 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Chapter08/CustomSort.kt: -------------------------------------------------------------------------------- 1 | import kotlin.Comparator 2 | 3 | data class Employee( 4 | val employeeID: Int, 5 | val employeeName: String 6 | ) 7 | 8 | fun main(args: Array) { 9 | val employeeList = listOf( 10 | Employee(2, "Chandra Sekhar Nayak"), 11 | Employee(1, "Rivu Chakraborty"), 12 | Employee(4, "Indranil Dutta"), 13 | Employee(3, "Sonkho Deep Mondal"), 14 | Employee(6, "Debraj Dey"), 15 | Employee(5, "Koushik Mridha") 16 | ) 17 | 18 | val sortedEmployeesList = employeeList.sortedWith(Comparator { e1, e2 -> 19 | when { 20 | e1?.employeeID ?: 0 <= e2?.employeeID ?: 0 -> -1 21 | e1?.employeeID ?: 0 == e2?.employeeID ?: 0 -> 0 22 | else -> 1 23 | } 24 | }) 25 | 26 | for (employee in sortedEmployeesList) { 27 | println(employee) 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Chapter08/FunctionalSort.kt: -------------------------------------------------------------------------------- 1 | import kotlin.Comparator 2 | 3 | data class Employee( 4 | val employeeID: Int, 5 | val employeeName: String 6 | ) 7 | 8 | fun main(args: Array) { 9 | val employeeList = listOf( 10 | Employee(2, "Chandra Sekhar Nayak"), 11 | Employee(1, "Rivu Chakraborty"), 12 | Employee(4, "Indranil Dutta"), 13 | Employee(3, "Sonkho Deep Mondal"), 14 | Employee(6, "Debraj Dey"), 15 | Employee(5, "Koushik Mridha") 16 | ) 17 | 18 | (employeeList.sortedBy { 19 | it.employeeID 20 | } as Iterable).forEach { 21 | println(it) 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Chapter08/LinkedList.kt: -------------------------------------------------------------------------------- 1 | import java.util.LinkedList 2 | 3 | fun main(args: Array) { 4 | val list = listOf(10, 8, 18, 45, 63, 49, 88, 15, 62) 5 | val linkedList = LinkedList(list) 6 | 7 | for (i in linkedList) { 8 | println("List Item $i") 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Chapter08/Maps.kt: -------------------------------------------------------------------------------- 1 | fun main(args: Array) { 2 | val map = mapOf( 3 | "Key One" to 1.0f, 4 | "Key Two" to 2.0f, 5 | "Key Three" to 3.0f, 6 | "Key Four" to 4.0f, 7 | "Key Five" to 5.0f, 8 | "Key Six" to 0.0f, //(1) 9 | "Key Six" to 6.0f //(2) 10 | ) 11 | 12 | println("The value at Key `Key Four` is ${map["Key Four"]}") 13 | 14 | println("Contents in map") 15 | for(entry in map) { 16 | println("Key ${entry.key}, Value ${entry.value}") 17 | } 18 | 19 | val mutableMap = map.toMutableMap() 20 | 21 | println("Replacing value at key - `Key Five` - ${mutableMap.put("Key Five",5.5f)}")//(3) 22 | 23 | println("Contents in mutableMap") 24 | for(entry in mutableMap) { 25 | println("Key ${entry.key}, Value ${entry.value}") 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Chapter08/MutableAccess.kt: -------------------------------------------------------------------------------- 1 | fun main(args: Array) { 2 | val list = mutableListOf(1, 2, 3, 0, 5, 6, 7, 8) 3 | 4 | list[3] = 4 5 | println("3rd and 4th Item on List -> ${list[3]}, ${list.get(4)}") 6 | } 7 | -------------------------------------------------------------------------------- /Chapter08/MutableList.kt: -------------------------------------------------------------------------------- 1 | fun main(args: Array) { 2 | val list = mutableListOf(1, 2, 5) 3 | 4 | println("-----Created With Items-----") 5 | for (i in list) { 6 | println("list item $i") 7 | } 8 | 9 | // Adding Items 10 | 11 | list.add(6) //(1) 12 | list.add(2, 3) //(2) 13 | list.add(3, 4) //(3) 14 | 15 | 16 | println("-----After Adding Items-----") 17 | for (i in list) { 18 | println("list item $i") 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Chapter08/OpDrop.kt: -------------------------------------------------------------------------------- 1 | fun main(args: Array) { 2 | val list = 1.until(50).toList() 3 | 4 | println("list.drop(20) -> ${list.drop(20)}") 5 | println("list.dropLast(20) -> ${list.dropLast(20)}") 6 | } 7 | -------------------------------------------------------------------------------- /Chapter08/OpFilter.kt: -------------------------------------------------------------------------------- 1 | import kotlin.math.roundToInt 2 | import kotlin.math.sqrt 3 | 4 | fun main(args: Array) { 5 | val list = 1.until(50).toList() 6 | 7 | println("Filtered List with Even Numbers-> ${list.filter { it % 2 == 0 }}") 8 | 9 | val filteredList = list.filter { 10 | val sqroot = sqrt(it.toDouble()).roundToInt() 11 | sqroot*sqroot==it 12 | } 13 | 14 | println("Filtered List with Perfect Squares -> $filteredList") 15 | } 16 | -------------------------------------------------------------------------------- /Chapter08/OpFlatMap.kt: -------------------------------------------------------------------------------- 1 | fun main(args: Array) { 2 | val list = listOf(10, 20, 30) 3 | println("flatMappedList -> ${ 4 | list.flatMap { 5 | it.rangeTo(it * 2).toList() 6 | } 7 | }") 8 | } 9 | -------------------------------------------------------------------------------- /Chapter08/OpMap.kt: -------------------------------------------------------------------------------- 1 | fun main(args: Array) { 2 | val list = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) 3 | 4 | println("modifiedList -> ${list.map { it + 3 }}") 5 | } 6 | -------------------------------------------------------------------------------- /Chapter08/OpTake.kt: -------------------------------------------------------------------------------- 1 | fun main() { 2 | val list = IntArray(100) { it -> it * 10 } 3 | 4 | println("list.take(10) -> ${list.take(10)}") //(1) 5 | println("list.takeLast(10) -> ${list.takeLast(10)}") //(2) 6 | println("list.takeWhile { it <= 50 } -> ${list.takeWhile { it <= 50 }}") //(3) 7 | println("list.takeLastWhile { it >= 900 } -> ${list.takeLastWhile { it >= 900 }}") //(4) 8 | } 9 | -------------------------------------------------------------------------------- /Chapter08/OpZip.kt: -------------------------------------------------------------------------------- 1 | fun main(args: Array) { 2 | val names = listOf("Chandra", "Rivu", "Nick", "Ahmed") 3 | val ages = listOf(30, 27, 35, 19) 4 | println(names.zip(ages)) 5 | } 6 | -------------------------------------------------------------------------------- /Chapter08/Search.kt: -------------------------------------------------------------------------------- 1 | fun main(args: Array) { 2 | val list = listOf(10, 8, 18, 45, 63, 49, 88, 15, 62) 3 | println("Index of 18 is ${list.binarySearch(18)}") 4 | } 5 | -------------------------------------------------------------------------------- /Chapter08/Set.kt: -------------------------------------------------------------------------------- 1 | fun main(args: Array) { 2 | val set = mutableSetOf(1, 2, 3, 4, 4, 1, 2) 3 | 4 | println("set before add $set") 5 | 6 | set.add(4) 7 | set.add(5) 8 | set.add(5) 9 | set.add(6) 10 | 11 | println("set after add $set") 12 | } 13 | -------------------------------------------------------------------------------- /Chapter08/Sort.kt: -------------------------------------------------------------------------------- 1 | import java.util.LinkedList 2 | 3 | fun main(args: Array) { 4 | val list = listOf(10, 8, 18, 45, 63, 49, 88, 15, 62) 5 | val linkedList = LinkedList(list) 6 | 7 | linkedList.sort() 8 | 9 | for (i in linkedList) { 10 | println("List Item $i") 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Chapter08/Sorted.kt: -------------------------------------------------------------------------------- 1 | import java.util.LinkedList 2 | 3 | fun main(args: Array) { 4 | val list = listOf(10,8,18,45,63,49,88,15,62) 5 | 6 | val sortedList = list.sorted() 7 | 8 | for (i in sortedList) { 9 | println("List Item $i") 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Chapter09/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'org.jetbrains.kotlin.jvm' version '1.3.11' 3 | } 4 | 5 | group 'com.rivuchk.packtpub.hodskotlin' 6 | version '1.0-SNAPSHOT' 7 | 8 | repositories { 9 | mavenCentral() 10 | } 11 | 12 | apply plugin: 'kotlin-kapt' 13 | def arrow_version = "0.8.2" 14 | 15 | dependencies { 16 | compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8" 17 | 18 | compile "io.arrow-kt:arrow-core:$arrow_version" 19 | compile "io.arrow-kt:arrow-syntax:$arrow_version" 20 | compile "io.arrow-kt:arrow-typeclasses:$arrow_version" 21 | compile "io.arrow-kt:arrow-data:$arrow_version" 22 | compile "io.arrow-kt:arrow-instances-core:$arrow_version" 23 | compile "io.arrow-kt:arrow-instances-data:$arrow_version" 24 | kapt "io.arrow-kt:arrow-annotations-processor:$arrow_version" 25 | 26 | compile "io.arrow-kt:arrow-query-language:$arrow_version" //optional 27 | compile "io.arrow-kt:arrow-free:$arrow_version" //optional 28 | compile "io.arrow-kt:arrow-instances-free:$arrow_version" //optional 29 | compile "io.arrow-kt:arrow-mtl:$arrow_version" //optional 30 | compile "io.arrow-kt:arrow-effects:$arrow_version" //optional 31 | compile "io.arrow-kt:arrow-effects-instances:$arrow_version" //optional 32 | compile "io.arrow-kt:arrow-effects-rx2:$arrow_version" //optional 33 | compile "io.arrow-kt:arrow-effects-rx2-instances:$arrow_version" //optional 34 | compile "io.arrow-kt:arrow-effects-reactor:$arrow_version" //optional 35 | compile "io.arrow-kt:arrow-effects-reactor-instances:$arrow_version" //optional 36 | compile "io.arrow-kt:arrow-effects-kotlinx-coroutines:$arrow_version" //optional 37 | compile "io.arrow-kt:arrow-effects-kotlinx-coroutines-instances:$arrow_version" //optional 38 | compile "io.arrow-kt:arrow-optics:$arrow_version" //optional 39 | compile "io.arrow-kt:arrow-generic:$arrow_version" //optional 40 | compile "io.arrow-kt:arrow-recursion:$arrow_version" //optional 41 | compile "io.arrow-kt:arrow-instances-recursion:$arrow_version" //optional 42 | } 43 | 44 | compileKotlin { 45 | kotlinOptions.jvmTarget = "1.8" 46 | } 47 | compileTestKotlin { 48 | kotlinOptions.jvmTarget = "1.8" 49 | } 50 | 51 | repositories { 52 | jcenter() 53 | } -------------------------------------------------------------------------------- /Chapter09/gradle.properties: -------------------------------------------------------------------------------- 1 | kotlin.code.style=official -------------------------------------------------------------------------------- /Chapter09/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Hands-On-Data-Structures-and-Algorithms-with-Kotlin/f125372a286a3bf4f0248db109a7427f6bc643a7/Chapter09/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /Chapter09/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Feb 11 22:43:42 IST 2019 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.10-all.zip 7 | -------------------------------------------------------------------------------- /Chapter09/gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /Chapter09/gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /Chapter09/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'category-theory' 2 | 3 | -------------------------------------------------------------------------------- /Chapter09/src/main/kotlin/arrowexamples/either-example.kt: -------------------------------------------------------------------------------- 1 | package arrowexamples 2 | 3 | import arrow.core.* 4 | import java.lang.Exception 5 | 6 | fun main() { 7 | printResponse(fetchAPIData(Some(10))) 8 | printResponse(fetchAPIData(None)) 9 | 10 | } 11 | 12 | fun fetchAPIData(someParameter: Option) : Either { 13 | if(someParameter is None) { 14 | return Either.left(Exception("No value passed")) 15 | } else { 16 | return Either.right("The value is ${someParameter.getOrElse { 0 }}") 17 | } 18 | listOf().size 19 | } 20 | 21 | fun printResponse(response : Either) { 22 | println(if(response.isRight()) "Success $response" else "Failure $response") 23 | } -------------------------------------------------------------------------------- /Chapter09/src/main/kotlin/arrowexamples/option-example-2.kt: -------------------------------------------------------------------------------- 1 | package arrowexamples 2 | 3 | import arrow.core.None 4 | import arrow.core.Option 5 | import arrow.core.Some 6 | import arrow.core.getOrElse 7 | import arrow.instances.option.foldable.get 8 | 9 | fun main() { 10 | printOptionalInt(Some(10)) 11 | printOptionalInt(Some(2)) 12 | printOptionalInt(None) 13 | printOptionalInt(Some(200)) 14 | printOptionalInt(None) 15 | } 16 | 17 | fun printOptionalInt(optionalInt: Option) { 18 | if(optionalInt is None) { 19 | println("It's blank") 20 | } else { 21 | println("It's ${optionalInt.getOrElse { 0 }}") 22 | } 23 | } -------------------------------------------------------------------------------- /Chapter09/src/main/kotlin/arrowexamples/option-example.kt: -------------------------------------------------------------------------------- 1 | package arrowexamples 2 | 3 | import arrow.core.None 4 | import arrow.core.Option 5 | import arrow.core.Some 6 | 7 | fun main() { 8 | var optionalVar: Option 9 | optionalVar = Some(10) 10 | println(optionalVar) 11 | optionalVar = None 12 | println(optionalVar) 13 | } -------------------------------------------------------------------------------- /Chapter09/src/main/kotlin/category/applicative-1.kt: -------------------------------------------------------------------------------- 1 | package category 2 | 3 | import arrow.core.* 4 | 5 | 6 | fun main() { 7 | val name: Option = Some("Rivu") 8 | val company: Option = Some("BYJUS") 9 | val city: Option = Some("Bangalore") 10 | 11 | val authorInfo: Option> = 12 | applicative>(name, company, city) { a, b, c -> 13 | Tuple3(a, b, c) 14 | } 15 | println("Author: $authorInfo") 16 | 17 | } 18 | 19 | fun applicative( 20 | optiona: Option, 21 | optionb: Option, 22 | optionc: Option, 23 | block: (A, B, C) -> R 24 | ): Option { 25 | return Some(block(optiona.getOrElse { None as A }, 26 | optionb.getOrElse { None as B }, 27 | optionc.getOrElse { None as C } 28 | )) 29 | } -------------------------------------------------------------------------------- /Chapter09/src/main/kotlin/category/functor-1.kt: -------------------------------------------------------------------------------- 1 | package category 2 | 3 | fun List.convertToStr(): List = 4 | if (size > 0) { 5 | val newList = ArrayList(size) 6 | for (item in this) { 7 | newList.add("Modified $item") 8 | } 9 | newList 10 | } else { 11 | emptyList() 12 | } 13 | 14 | 15 | fun main(args: Array) { 16 | val intList = listOf(1, 2, 3, 4, 5) 17 | println(intList.convertToStr()) 18 | } -------------------------------------------------------------------------------- /Chapter09/src/main/kotlin/category/functor-map-either.kt: -------------------------------------------------------------------------------- 1 | package category 2 | 3 | import arrow.* 4 | import arrow.core.* 5 | 6 | 7 | fun main() { 8 | var optionalVal: Option 9 | optionalVal = Some(10) 10 | println(optionalVal.map { "It is $it" }) 11 | optionalVal = None 12 | println(optionalVal.map { "It is $it" }) 13 | } -------------------------------------------------------------------------------- /Chapter09/src/main/kotlin/category/functor-map-for-monad.kt: -------------------------------------------------------------------------------- 1 | package category 2 | 3 | import arrow.* 4 | import arrow.core.* 5 | 6 | 7 | fun main() { 8 | var optionalVal: Option 9 | optionalVal = Some(10) 10 | println(optionalVal.map { it+1 })//1 11 | println(optionalVal.map { Some(it+1) })//2 12 | } -------------------------------------------------------------------------------- /Chapter09/src/main/kotlin/category/functor-map.kt: -------------------------------------------------------------------------------- 1 | package category 2 | 3 | fun main() { 4 | val intList = listOf(1, 2, 3, 4, 5) 5 | println(intList.map { it * 2 }) 6 | println(intList.map { "Mapped $it" }) 7 | println(intList.map { it.toDouble() }) 8 | } -------------------------------------------------------------------------------- /Chapter09/src/main/kotlin/category/monad-flatmap.kt: -------------------------------------------------------------------------------- 1 | package category 2 | 3 | import arrow.core.None 4 | import arrow.core.Option 5 | import arrow.core.Some 6 | 7 | fun main() { 8 | var someValue: Option = Some(10) 9 | println(someValue.flatMap { Some(it + 5) }) 10 | } -------------------------------------------------------------------------------- /Chapter09/src/main/kotlin/fpbasics/Calculator.kt: -------------------------------------------------------------------------------- 1 | package fpbasics 2 | 3 | class Calculator { 4 | var anyVariable: Int = 0 5 | 6 | fun add(a: Int, b: Int): Int = a + b // pure function 7 | fun multiply(a: Int, b: Int): Int = a * b // pure function 8 | fun subtract(a: Int, b: Int): Int = a - b // pure function 9 | fun divide(a: Int, b: Int): Int = a / b // pure function 10 | 11 | fun anyFunction(x: Int): Int { // not a pure function 12 | anyVariable = x + 2 13 | return anyVariable 14 | } 15 | } -------------------------------------------------------------------------------- /Chapter09/src/main/kotlin/fpbasics/callback-example.kt: -------------------------------------------------------------------------------- 1 | package fpbasics 2 | 3 | interface ACallBack { 4 | fun someCallBackFunction() 5 | } 6 | 7 | fun listenToSomeEvent(callback: ACallBack) { 8 | // Event occurred 9 | callback.someCallBackFunction() 10 | } 11 | 12 | fun listenToSomeEvent(lambda: () -> Unit) { 13 | // Event Occurred 14 | lambda() 15 | } 16 | 17 | fun main() { 18 | // Using callback 19 | listenToSomeEvent(object : ACallBack { 20 | override fun someCallBackFunction() { 21 | println("Event occurred") 22 | } 23 | }) 24 | 25 | // Using Lambda 26 | listenToSomeEvent { 27 | println("Event occurred") 28 | } 29 | } -------------------------------------------------------------------------------- /Chapter09/src/main/kotlin/fpbasics/hof.kt: -------------------------------------------------------------------------------- 1 | package fpbasics 2 | 3 | fun highOrder(anotherFunc: () -> Unit) { 4 | println("Before anotherFunc()") 5 | anotherFunc() 6 | println("After anotherFunc()") 7 | } 8 | 9 | fun main() { 10 | highOrder { 11 | println("anotherFunc()") 12 | } 13 | } -------------------------------------------------------------------------------- /Chapter09/src/main/kotlin/fpbasics/immutability-example.kt: -------------------------------------------------------------------------------- 1 | package fpbasics 2 | 3 | import java.util.* 4 | 5 | val immutableRandomValue: String by lazy { 6 | getRandomValue() 7 | } 8 | fun getRandomValue(): String { 9 | val rand = Random().nextInt() 10 | return "Value $rand" 11 | } 12 | 13 | fun main(vararg args:String) { 14 | println("getRandomValue() will return different values at each call") 15 | println("1. ${getRandomValue()}") 16 | println("2. ${getRandomValue()}") 17 | println("\nHowever, immutableRandomValue will return the same value at each call") 18 | println("1. $immutableRandomValue") 19 | println("2. $immutableRandomValue") 20 | } -------------------------------------------------------------------------------- /Chapter09/src/main/kotlin/fpbasics/immutable-collection.kt: -------------------------------------------------------------------------------- 1 | package fpbasics 2 | 3 | fun main(args: Array) { 4 | val employeeList = listOf( 5 | Employee(2, "Chandra Sekhar Nayak"), 6 | Employee(1, "Rivu Chakraborty"), 7 | Employee(4, "Indranil Dutta"), 8 | Employee(3, "Sonkho Deep Mondal"), 9 | Employee(6, "Debraj Dey"), 10 | Employee(5, "Koushik Mridha") 11 | ) 12 | 13 | employeeList.sortedBy { 14 | it.employeeID 15 | }.forEach { 16 | println(it) 17 | } 18 | } 19 | 20 | data class Employee(val employeeID: Int, val employeeName: String) -------------------------------------------------------------------------------- /Chapter09/src/main/kotlin/fpbasics/lambda.kt: -------------------------------------------------------------------------------- 1 | package fpbasics 2 | 3 | fun main() { 4 | var myFunc: (Int) -> Int 5 | myFunc = { it * 2 } 6 | println("10 * 2 ${ myFunc(10) }") 7 | myFunc = { it / 2 } 8 | println("10 / 2 ${ myFunc(10) }") 9 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Packt 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | # Hands-On Data Structures and Algorithms with Kotlin 5 | 6 | Hands-On Data Structures and Algorithms with Kotlin 7 | 8 | This is the code repository for [Hands-On Data Structures and Algorithms with Kotlin](https://www.packtpub.com/application-development/hands-data-structures-and-algorithms-kotlin?utm_source=github&utm_medium=repository&utm_campaign=9781788994019 ), published by Packt. 9 | 10 | **Level up your programming skills by understanding how Kotlin's data structure works** 11 | 12 | ## What is this book about? 13 | Data Structures and Algorithms are much more than theoretical concepts. Learning them gives you an ability to understand computational complexity, solve problems, and write efficient code. Kotlin Data structures and Algorithms enables you to write code that runs faster which is hugely important in web and mobile world. This book takes you through a pragmatic approach with techniques and real-world examples that you can use in your regular production environment. 14 | 15 | This book covers the following exciting features: 16 | * Understand the basic principles of algorithms, data structures, and measurement of complexity. 17 | * Find out what general purpose data structures are, including arrays, linked lists, double ended linked lists, and so on. 18 | * Get a grasp on the basics of abstract data types—stack, queue, and double ended queue. 19 | * See how to use recursive functions and immutability while understanding and in terms of recursion. 20 | * Handle reactive programming and its related data structures. 21 | * Use binary search, sorting, and efficient sorting—quicksort and merge sort. 22 | * Work with the important concept of trees and list all nodes of the tree, traversal of tree, search trees, and balanced search trees. 23 | * Gain a better understanding of the concept of graphs, directed and undirected graphs, undirected trees, and much more. 24 | 25 | If you feel this book is for you, get your [copy](https://www.amazon.com/dp/1788994019) today! 26 | 27 | https://www.packtpub.com/ 29 | 30 | ## Instructions and Navigations 31 | All of the code is organized into folders. For example, Chapter02. 32 | 33 | The code will look like the following: 34 | ``` 35 | val x = 10 36 | val y = x * 2 37 | for (i in 0..y) { 38 | if (i % 2 == 0) { 39 | println(“$i is Even”) 40 | } else { 41 | println(“$i is Odd”) 42 | } 43 | } 44 | ``` 45 | 46 | **Following is what you need for this book:** 47 | If you're a Kotlin developer who wants to learn the intricacies of implementing data structures and algorithms for scalable application development, this book is for you. 48 | 49 | With the following software and hardware list you can run all code files present in the book (Chapter 1-9). 50 | ### Software and Hardware List 51 | | Chapter | Software required | OS required | 52 | | -------- | ------------------------------------ | ----------------------------------- | 53 | | 1-9 | Basic IDE (IntelliJ IDEA/Eclipse) | Windows, Mac OS X, and Linux (Any) | 54 | 55 | 56 | We also provide a PDF file that has color images of the screenshots/diagrams used in this book. [Click here to download it](https://www.packtpub.com/sites/default/files/downloads/9781788994019_ColorImages.pdf). 57 | 58 | ### Related products 59 | * Hands-On Object-Oriented Programming with Kotlin [[Packt]](https://www.packtpub.com/application-development/hands-object-oriented-programming-kotlin?utm_source=github&utm_medium=repository&utm_campaign=9781789617726 ) [[Amazon]](https://www.amazon.com/dp/1789617723) 60 | 61 | * Learning Concurrency in Kotlin [[Packt]](https://www.packtpub.com/application-development/learning-concurrency-kotlin?utm_source=github&utm_medium=repository&utm_campaign=) [[Amazon]](https://www.amazon.com/dp/1788627164) 62 | 63 | ## Get to Know the Author 64 | **Chandra Sekhar Nayak** 65 | is an experienced Java Developer with an extensive knowledge in Android development. He has done a lot of Android applications with larger user base. He is an active member of communities around Java and Android. Being a Kotlin enthusiast, he also created a Kotlin User Group in Bengaluru, India called BlrKotlin. He runs a YouTube channel called Chanse Code. In his spare time he loves writing blogs. 66 | 67 | **Rivu Chakraborty** 68 | is a Google Certified Android Developer, Caster.io Instructor and a Kotlin Evangelist. With over 6 years of work experience; he is currently working as a Sr. Software Engineer (Android) at BYJU'S The Learning App. 69 | Rivu considers himself a Kotlin and Android enthusiast, cum evangelist. He has been using Kotlin since December 2015. Rivu created the KotlinKolkata User Group and before moving out to Bangalore, he had been the lead organiser for both Kotlin Kolkata User Group and GDG Kolkata. 70 | Along with organising events, he also speaks at events/conferences in India, including DroidJam India (India's premiere Android Conference) and a couple of DevFests. 71 | Rivu has authored multiple books on Kotlin and Android Development. 72 | 73 | ## Other books by the authors 74 | [Reactive Programming in Kotlin](https://www.packtpub.com/application-development/reactive-programming-kotlin?utm_source=github&utm_medium=repository&utm_campaign=9781788473026) 75 | 76 | [Functional Kotlin](https://www.packtpub.com/application-development/functional-kotlin?utm_source=github&utm_medium=repository&utm_campaign=9781788476485 ) 77 | 78 | ### Suggestions and Feedback 79 | [Click here](https://docs.google.com/forms/d/e/1FAIpQLSdy7dATC6QmEL81FIUuymZ0Wy9vH1jHkvpY57OiMeKGqib_Ow/viewform) if you have any feedback or suggestions. 80 | 81 | 82 | ### Download a free PDF 83 | 84 | If you have already purchased a print or Kindle version of this book, you can get a DRM-free PDF version at no cost.
Simply click on the link to claim your free PDF.
85 |

https://packt.link/free-ebook/9781788994019

--------------------------------------------------------------------------------