Skip to content

Instantly share code, notes, and snippets.

View binshi's full-sized avatar

Conan, the barbarian binshi

View GitHub Profile
@binshi
binshi / Compute100.scala
Created May 18, 2017 10:44
Compute to 100: Given the digits 123456789, build an expression by inserting "+" or "-" anywhere BETWEEN the digits so that it results to 100. Your answer should return all possible combinations. Example: 1 + 23 - 4 + 5 + 6 + 78 - 9 = 100
import scala.collection.mutable.ListBuffer
object Main {
val digits = 123456789
val digitsSplit = digits.toString.map(_.asDigit)
val sum = 100
def attachPrefix(prefix:ListBuffer[String], paths:ListBuffer[ListBuffer[String]]) = {
paths.filter(_.nonEmpty).map(p => prefix ++ p)
}
@binshi
binshi / EvenOdd.scala
Created May 18, 2017 10:42
Given a variable length array of integers, partition them such that the even integers precede the odd integers in the array. Your must operate on the array in-place, with a constant amount of extra space. The answer should scale linearly in time with respect to the size of the array.
object Main {
def sortOddEven(numbers:Array[Int]) = {
println(numbers.mkString(","))
var left = 0
var right = numbers.length - 1
while (left < right) {
//Increment left index till we see an odd
while (numbers(left) % 2 == 0 && left < right)
left+=1
@binshi
binshi / GoogleAPIServiceAccountAuth.scala
Created January 27, 2017 11:21
Simple class to get access token based on service key
import java.io.{IOException, StringReader, Reader, ByteArrayInputStream}
import java.nio.charset.Charset
import java.security.{NoSuchAlgorithmException, KeyFactory, PrivateKey}
import java.security.spec.{InvalidKeySpecException, PKCS8EncodedKeySpec}
import java.util.Collections
import com.google.api.client.json.webtoken.{JsonWebToken, JsonWebSignature}
import com.google.api.client.json.{JsonFactory, GenericJson, JsonObjectParser}
import com.google.api.client.json.jackson2.JacksonFactory
import com.google.api.client.util.{SecurityUtils, PemReader, Joiner}
import java.io.File
import java.util
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient
import com.amazonaws.services.dynamodbv2.document.spec._
import com.amazonaws.services.dynamodbv2.document.utils.{NameMap, ValueMap}
import com.amazonaws.services.dynamodbv2.document._
import com.amazonaws.services.dynamodbv2.model._
import com.fasterxml.jackson.core.JsonFactory