Skip to content

Instantly share code, notes, and snippets.

# coding: utf-8
import random
import timeit
from collections import deque
list = range(50000*2)
random.shuffle(list)
#print list
k = 5000
@eranation
eranation / Solution.java
Created May 8, 2015 15:59
Square Root Puzzle
package square;
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.Random;
/**
* A suggested solution to the puzzle by Wouter Coekaerts from Square.
* The puzzle can be found <a href="http://corner.squareup.com/2013/03/puzzle-square-root.html">here</a>
*
@eranation
eranation / iterative.scala
Last active August 29, 2015 14:20
Silly programming problem: add either +, - or nothing between 1 to 9, which combinations yields 100?
import javax.script.ScriptEngineManager
// I *could* write an evaluation engine by hand, since it's only + and - it would be dead easy, but... not this time...
val e = new ScriptEngineManager().getEngineByName("javascript")
val operators:Seq[String] = Seq("+","-","")
val choose1 = for {
c1 <- operators
c2 <- operators
c3 <- operators
@eranation
eranation / GoEquivalentBinaryTrees.go
Created June 4, 2013 16:51
I'm sure there is a better solution, but this is the only thing I came up with that worked (for http://tour.golang.org/#69)
package main
import "code.google.com/p/go-tour/tree"
import "fmt"
import "sort"
// Walk walks the tree t sending all values
// from the tree to the channel ch.
func Walk(t *tree.Tree, ch chan int) {
@eranation
eranation / NextFib.go
Created May 23, 2013 21:39
Solution to Exercise: Fibonacci closure from http://tour.golang.org/#43
package main
import "fmt"
// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
// first 2 negative fib numbers - see bidirectional sequence at http://en.wikipedia.org/wiki/Fibonacci_number
fib1 := -1
fib2 := 1
@eranation
eranation / WordCount.go
Created May 23, 2013 19:53
Solution to the maps exercise at http://tour.golang.org/#40
package main
import (
"code.google.com/p/go-tour/wc"
"strings"
)
func WordCount(s string) map[string]int {
fields := strings.Fields(s)
m := make(map[string]int)
@eranation
eranation / GoSlices.go
Created May 23, 2013 19:39
Solution to the slices exercise at http://tour.golang.org/#35
package main
import "code.google.com/p/go-tour/pic"
func Pic(dx, dy int) [][]uint8 {
ys := make([][]uint8, dy)
for y := range ys {
xs := make([]uint8, dx)
ys[y] = xs
@eranation
eranation / jquery.autogrowinput.js
Created August 29, 2012 02:07
jQuery autogrow plugin for text fields
(function($){
$.fn.autoGrowInput = function(o) {
o = $.extend({
maxWidth: 1000,
minWidth: 0,
comfortZone: 70
}, o);
@eranation
eranation / gist:3241664
Created August 2, 2012 22:56
Enable Aptana Studio 3 Ruby 1.9.3 debug (ruby-debug-ide) on Windows 7 64 bit (after rails installer)
gem install ruby-debug-ide --pre
gem install ruby-debug-base19x --pre
@eranation
eranation / gist:3241616
Created August 2, 2012 22:51
MongoDB Aggregation Framework way for "select count (distinct fieldName) from someTable" for large collections
//equivalent of "select count (distinct fieldName) from someTable"
db.someCollection.aggregate([{ $group: { _id: "$fieldName"} },{ $group: { _id: 1, count: { $sum: 1 } } } ])