Skip to content

Instantly share code, notes, and snippets.

View toantran-ea's full-sized avatar

Toan Tran toantran-ea

View GitHub Profile
@toantran-ea
toantran-ea / tailrecursion.kt
Created January 8, 2020 12:10
Tail Recursion function to solve Reachable Number problem
tailrec fun removeZeroes(x: Int): Int =
if (x % 10 == 0) removeZeroes(x / 10) else x
fun f(x: Int) = removeZeroes(x + 1)
@toantran-ea
toantran-ea / hackerrank_sparse_array.kt
Created December 25, 2019 05:07
Kotlin Solution for Sparse Array problem
// Complete the matchingStrings function below.
fun matchingStrings(strings: Array<String>, queries: Array<String>): Array<Int> {
val result = Array<Int>(queries.size) { 0 }
queries.forEachIndexed { index, item ->
result[index] = strings.count { item == it }
}
return result
}
@toantran-ea
toantran-ea / FoodEntry.kt
Created November 30, 2019 10:20
Data Model 2
@Serializable
data class FoodEntry(
val id: String = UUID.randomUUID().toString(),
val name: String,
val time: Long = System.currentTimeMillis(),
val description: String,
val energyCount: Long = 0,
val updated: Long = System.currentTimeMillis()
)
@toantran-ea
toantran-ea / WebApplication.kt
Created November 30, 2019 09:53
Connect attempt 1
package com.example.demo
// import
val dataRepo = DataRepository()
fun Application.main() {
// ...
post("entries") {
val entries = fromJsonStringToList(call.receiveText())
@toantran-ea
toantran-ea / DataRepo.kt
Created November 30, 2019 09:33
Data Repository
package com.example.demo.data
import com.google.appengine.api.datastore.*
class DataRepository {
private val dataStore = DatastoreServiceFactory.getDatastoreService()
private val foodIntakeEntry = dataStore.prepare(Query(FOOD_INTAKE_ENTRY))
// READ
fun listAll(): List<FoodEntry> =
@toantran-ea
toantran-ea / FoodEntry.kt
Created November 30, 2019 09:32
Data model for Food Intake Entry
package com.example.demo.data
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import kotlinx.serialization.list
import kotlinx.serialization.parse
@Serializable
data class FoodEntry(
val id: Long,
@toantran-ea
toantran-ea / WebApplication.kt
Last active November 30, 2019 09:16
init application routing
package com.example.demo
/* ktlint-disable no-wildcard-imports */
import io.ktor.application.Application
import io.ktor.application.call
import io.ktor.application.install
import io.ktor.features.CallLogging
import io.ktor.features.DefaultHeaders
import io.ktor.response.respond
import io.ktor.routing.get
@toantran-ea
toantran-ea / versions.gradle
Created July 7, 2019 07:12
versions.gradle accrossing projects
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@toantran-ea
toantran-ea / sample_gitlab_runner.config.toml
Created April 5, 2019 15:21
/etc/gitlab-runner/config.toml
concurrent = 1
check_interval = 0
[session_server]
session_timeout = 1800
[[runners]]
name = "My runner description"
url = "https://mygitlab.domain/"
token = "my-gitlab-runner-token"
@toantran-ea
toantran-ea / .zshrc
Last active April 3, 2019 12:32
zshrc
# If you come from bash you might have to change your $PATH.
export ADB=/Users/inspectorio01/Library/Android/sdk/platform-tools
export PATH=$HOME/bin:/usr/local/bin:$PATH:$ADB
# Path to your oh-my-zsh installation.
export ZSH="/Users/inspectorio01/.oh-my-zsh"
# Set name of the theme to load. Optionally, if you set this to "random"
# it'll load a random theme each time that oh-my-zsh is loaded.
# See https://github.com/robbyrussell/oh-my-zsh/wiki/Themes