Skip to content

Instantly share code, notes, and snippets.

View WillFK's full-sized avatar

William Fernandes Koehler WillFK

View GitHub Profile
@WillFK
WillFK / main.py
Created January 22, 2018 11:03
Testing 'with' on python
class controlled_exec:
def __init__(self, some_value):
self.some_value = some_value
def __enter__(self):
print('before exec')
return self.some_value+1
def __exit__(self, type, value, traceback):
@WillFK
WillFK / debounce.kt
Created February 3, 2018 20:03
Test with custom debounce
package com.fk
import io.reactivex.Observable
import io.reactivex.subjects.PublishSubject
import java.util.concurrent.TimeUnit
fun main(vararg args: String) {
val values = listOf("1", "2", "", "3", "4", "", "5")
val publisher = PublishSubject.create<String>()
publisher
class IsomorphismAlg {
/*
* both strings should be composed by characters between A and Z
* */
fun doIt(a: String, b: String): Boolean {
fun Char.simpleIndex() = this.toUpperCase() - 'A'
fun checkMapping(a: Int, b: Int, array: Array<Int>) =
class AnagramAlg {
/*
* Strings of the same size composed by characters from A to Z
* */
fun doIt(a: String, b: String): Boolean {
fun Char.simpleIndex() = this.toUpperCase() - 'A'
val array = Array(26) { 0 }
@WillFK
WillFK / SubstringLength.kt
Last active April 26, 2018 05:36
* Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.<br>
class SubstringAlg {
fun doIt(a: String): Int {
fun Char.simpleIndex() = this.toUpperCase() - 'A'
var length = 0
var topLength = 0
var array = Array(26) { -1 }
@WillFK
WillFK / Palindrome.kt
Created April 23, 2018 16:14
* Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, "Red rum, sir, is murder" is a palindrome, while "Programcreek is awesome" is not.<br>
class PalindromeAlg {
fun doIt(a: String): Boolean {
fun Char.validCharacter() =
this in 'A'..'z' || this in '0'..'9'
var indexA = 0
var indexB = a.length - 1
@WillFK
WillFK / ArrayRotator.kt
Created April 23, 2018 20:47
* Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].
package com.fk.tycho
class ArrayRotator {
fun doIt(array: Array<Int>, steps: Int): Array<Int> {
val result = Array(array.size) { 0 }
(0 until array.size).forEach { index ->
result[(index + steps) % array.size] = array[index]
}
return result
@WillFK
WillFK / ArraySumAlg.kt
Created April 24, 2018 16:17
* Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based. `For example: Input: numbers…
class ArraySumAlg {
fun doIt(array: Array<Int>, target: Int): Pair<Int, Int> {
var first = 0
var second = 1
while (first < array.size-1) {
while (second < array.size) {
if (array[first] + array[second] == target)
return Pair(first, second)
@WillFK
WillFK / ArrayLargestAlg.kt
Created May 3, 2018 05:59
* Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. For example, given [3,2,1,5,6,4] and k = 2, return 5. Note: You may assume k is always valid, 1 ≤ k ≤ array's length.
package com.fk.tycho.array
class ArrayLargestAlg {
fun doIt(array: Array<Int>, kth: Int): Int {
fun swap(a: Int, b: Int) {
// showing off swapping values without a temp value
array[a] = array[a] + array[b]
array[b] = array[a] - array[b]
@WillFK
WillFK / ArrayZigZagAlg.kt
Created May 4, 2018 15:37
* Given an array of integers, write a function that returns true if there is a triplet (a, b, c) that satisfies a2 + b2 = c2. Example: ```Input: arr[] = {3, 1, 4, 6, 5} Output: True There is a Pythagorean triplet (3, 4, 5). Input: arr[] = {10, 4, 6, 12, 5} Output: False There is no Pythagorean triplet.```
class ArrayZigZagAlg {
fun doIt(array: Array<Int>) {
fun swap(i: Int, j: Int) {
array[i] = array[i] + array[j]
array[j] = array[i] - array[j]
array[i] = array[i] - array[j]
}