Skip to content

Instantly share code, notes, and snippets.

View ekeitho's full-sized avatar

Keith Abdulla ekeitho

View GitHub Profile
@ekeitho
ekeitho / MoshiLocaleDateAdapter.kt
Created December 13, 2022 06:31
Kotlinx Locale Date Moshi Custom Adapter + Retrofit
import kotlinx.datetime.LocalDate
import kotlinx.datetime.toLocalDate
// Working with Retrofit + Moshi + KotlinX DateTime and want to use LocalDate
// as an object to deserialize/serialize with? Wabam!
// If this helped you maybe https://www.buymeacoffee.com/ekeitho <3
object LocaleDateAdapter {
@ToJson
fun localDateToJson(localDate: LocalDate): String {
@ekeitho
ekeitho / buildorder.kt
Last active July 24, 2018 15:00
Build Order
package cracking.ch4
import org.junit.Test
import java.util.*
class BuildOrder {
@Test
fun test() {
buildOrder(charArrayOf('a', 'b', 'c', 'd', 'e', 'f'),
arrayListOf('a' to 'd', 'f' to 'b', 'b' to 'd', 'f' to 'a', 'd' to 'c')
@ekeitho
ekeitho / prefixmapsum.kt
Created July 23, 2018 18:40
PreFixMapSum
package cracking.ch4
import org.junit.Test
import java.util.*
class PrefixMapSum {
var trie = Trie(0)
@Test
@ekeitho
ekeitho / validatebst.kt
Created July 23, 2018 03:46
validate bst
@Test
fun test() {
val n = BinaryNode(4)
.apply {
addLeft(2).apply {
addLeft(1)
}
addRight(6).apply {
addRight(7)
}
@ekeitho
ekeitho / minheap.kt
Created July 21, 2018 23:51
MinHeap Impl
package cracking.ch4
import org.junit.Test
class MinHeap {
val heap = arrayListOf<Int>()
@Test
fun test() {
@ekeitho
ekeitho / cfp.kt
Last active July 21, 2018 22:49
Generic Complete Full Perfect BTrees
fun <T> isComplete(root: BinaryNode<T>?) : Boolean {
// if left is null, but right isnt, it isn't complete
if (root?.left == null && root?.right != null) return false
// if left and right is null, we have found a child
if (root?.left == null && root?.right == null) return true
return isComplete(root.left) && isComplete(root.right)
}
fun <T> isFull(root: BinaryNode<T>?) : Boolean {
if (root?.left == null && root?.right == null) return true
@ekeitho
ekeitho / partition.kt
Created July 19, 2018 05:42
partition linked list
fun partition(head: Node?, int: Int): Node? {
var left = head
var curr = head
while (curr != null) {
if (curr.data < int) {
val temp = left!!.data
left.data = curr.data
fun isMatch(word: String, pattern: String): Boolean {
if (pattern.length == 2 && pattern[0] == '.' && pattern[1] == '*') {
// recursively if we ever get to this, we know to return true regardless
return true
}
if (word.isEmpty() && pattern.isEmpty()) {
return true
}
@ekeitho
ekeitho / combo.kt
Created July 12, 2018 03:40
Make all combinations of size k
fun comboOfSizeK(word: String, r: Int): List<String> {
val list = arrayListOf<String>()
fun comobs(word: String, created: String, start: Int, end: Int, sizeOfCombo: Int) {
if (created.length == sizeOfCombo) {
list.add(created)
return
}
if (start > end) {
@ekeitho
ekeitho / roman.kt
Created July 9, 2018 22:27
Roman Numeral Problem
data class Roman(val letter: Char, val amount: Int)
var map = mapOf('M' to Roman('M', 1000),
'D' to Roman('D', 500),
'C' to Roman('C', 100),
'L' to Roman('L', 50),
'X' to Roman('X', 10),
'V' to Roman('V', 5),
'I' to Roman('I', 1)
)