Skip to content

Instantly share code, notes, and snippets.

View atonamy's full-sized avatar
🏠
Working from home

Atonamy atonamy

🏠
Working from home
  • Singapore
View GitHub Profile
package NearbyWords
import kotlin.collections.HashSet
fun main(args : Array<String>) {
println(nearbyWords("gi"))
}
fun nearbyWords(word: String): Set<String> {
val permutations = getAllWordPermutations(word, 0)
@atonamy
atonamy / NearbyWordsAlt.kt
Last active October 30, 2017 01:56
Alternative solution for https://www.facebook.com/Engineering/videos/10152735777427200/ which give complexity O(n^3)
package NearbyWordsAlt
import kotlin.collections.HashSet
fun main(args : Array<String>) {
println(nearbyWords("gi"))
}
fun getAllWordPermutations(word: String): Set<String> {
package NearbyWords
import java.util.LinkedList
import kotlin.collections.HashSet
typealias CharSet = MutableFifthly<Set<Char>, Iterator<Char>, Char?, Long, Long>
class MutableFifthly<A, B, C, D, E>(
var first: A,
var second: B,
@atonamy
atonamy / QuickSort.kt
Created October 30, 2017 15:25
Beautiful implementation of quick sort on Kotlin
package quicksort
import java.util.Collections
fun main(args : Array<String>) {
val list = "abcdefghijklmnopqrstuvwxyz".toMutableList()
Collections.shuffle(list)
println("before sort: $list")
list.quicksort()
fun main(args : Array<String>) {
println("A man, a plan, a canal, Panama!".isContainsPalindrome())
}
fun String.isContainsPalindrome(): Boolean {
// handel edge cases
if(length == 0)
return false
fun main(args : Array<String>) {
println("abc".generatePermutations())
}
fun StringBuilder.generatePermutations(start: Int = 0, finish: Int = length-1,
result: MutableSet<String> = HashSet()) : Set<String> {
if(start == finish)
result.add(String(this))
else
for(i in start .. finish) {
import java.io.*
import java.math.*
import java.text.*
import java.util.*
import java.util.regex.*
val mod = 1000000007
fun maxbit(value: Int): Int = if(value <= 1) value else maxbit(value shr 1) shl 1
@atonamy
atonamy / GenomicRangeQuery.kt
Created May 18, 2019 08:12
100% perfomance/correctness solution for GenomicRangeQuery problem https://app.codility.com/programmers/lessons/5-prefix_sums/genomic_range_query
val String.prefixSumOfGenoms: Array<IntArray>
get() {
val genoms = Array(3) {
IntArray(length+1)
}
for(i in 0 until length) {
var (a, c, g) = arrayOf<Short>(0, 0, 0)
when(this[i]) {
'A' -> {a = 1}
@atonamy
atonamy / gist:e7fae0f7ad0e80490ebbe9f2cc16cccc
Created May 21, 2019 12:12 — forked from patrickhammond/gist:0b13ec35160af758d98c
Sample for how to use the Google Play Services dynamic security provider to keep the SSL library that the app will use to up date.
package com.mycompany.myapp.app;
import android.app.Application;
import android.content.Intent;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.security.ProviderInstaller;
import com.google.android.gms.security.ProviderInstaller.ProviderInstallListener;
public class MainApplication extends Application {
fun maxDifference(arr: Array<Int>): Int {
var min: Pair<Int, Int> = Pair(0, 0)
var max: Pair<Int, Int> = Pair(0, 0)
var result = -1
for(i in 0 until arr.size) {
when {
i == 0 -> {
min = Pair(i, arr[i])