Skip to content

Instantly share code, notes, and snippets.

Avatar

Rinat Tainov taintech

View GitHub Profile
@taintech
taintech / bubbleSort.scala
Created Mar 10, 2018
Scala bubble sort implementation
View bubbleSort.scala
object sortings {
def swap(ar: Array[Int], i: Int, j: Int): Array[Int] = {
val temp = ar(i)
ar(i) = ar(j)
ar(j) = temp
ar
}
def bubbleSort(ar: Array[Int]): Array[Int] = {
@taintech
taintech / mergeSort.scala
Created Mar 10, 2018
Scala merge sort implementation
View mergeSort.scala
import scala.annotation.tailrec
object sortings {
//p <= q < r
def merge(ar: Array[Int], p: Int, q: Int, r: Int): Array[Int] = {
val x = q - p + 1
val left = new Array[Int](x + 1)
Array.copy(ar, p, left, 0, x)
left(left.length - 1) = Int.MaxValue
@taintech
taintech / InsertionSort.scala
Last active Mar 5, 2018
Scala insertion sort implementation
View InsertionSort.scala
import scala.annotation.tailrec
object sortings {
def insertionSort(ar: Array[Int]): Array[Int] = {
@tailrec
def loop(i: Int, key: Int): Int =
if (i < 0 || ar(i) < key) i + 1
else {
ar(i + 1) = ar(i)
loop(i - 1, key)
@taintech
taintech / LoggingUtility.scala
Created Feb 4, 2018
Scala Utility methods for logging
View LoggingUtility.scala
trait Utility extends LazyLogging {
def log(future: Future[_])(
implicit executionContext: ExecutionContextExecutor): Unit = {
val start = System.currentTimeMillis()
future.onComplete {
case Success(res) =>
logger.info(
s"Result in ${System.currentTimeMillis() - start} ms is $res")
case Failure(e) =>
@taintech
taintech / print_case_class.scala
Last active Feb 3, 2018
Scala print case class members name and values
View print_case_class.scala
object helper {
def printCaseClass(caseClass: AnyRef): Unit = {
def caseClassToMap(caseClass: AnyRef): Map[String, Any] = {
(Map.empty[String, Any] /: caseClass.getClass.getDeclaredFields) {
(a, f) =>
f.setAccessible(true)
a + (f.getName -> f.get(caseClass))
}
View get_barcode_from_image.js
/*
* Copyright (c) 2010 Tobias Schneider
* This script is freely distributable under the terms of the MIT license.
*/
(function(){
var UPC_SET = {
"3211": '0',
"2221": '1',
"2122": '2',