Skip to content

Instantly share code, notes, and snippets.

View edalorzo's full-sized avatar

Edwin Dalorzo edalorzo

View GitHub Profile
@edalorzo
edalorzo / SublimeTextMissingKeyBindings.json
Created June 4, 2014 21:46
Defines a few missing key bindings in Sublime Text
[
{ "keys": ["home"], "command": "move_to", "args": {"to": "bol"} },
{ "keys": ["end"], "command": "move_to", "args": {"to": "eol"} },
{ "keys": ["shift+end"], "command": "move_to", "args": {"to": "eol", "extend": true} },
{ "keys": ["shift+home"], "command": "move_to", "args": {"to": "bol", "extend": true } },
{ "keys": ["ctrl+home"], "command": "move_to", "args": {"to": "bof"} },
{ "keys": ["ctrl+end"], "command": "move_to", "args": {"to": "eof"} },
{ "keys": ["ctrl+shift+home"], "command": "move_to", "args": {"to": "bof", "extend": true} },
{ "keys": ["ctrl+shift+end"], "command": "move_to", "args": {"to": "eof", "extend": true} },
]
@edalorzo
edalorzo / StreamConcat.java
Created September 14, 2014 19:42
Stream Concatenation
public static <T> Stream<T> concatenate(Stream<T>... streams) {
return Stream.of(streams).reduce(Stream.empty(), Stream::concat);
}
public static void main(String[] args) {
Stream<? extends CharSequence> res = concatenate(
Stream.of("One"),
Stream.of(new StringBuffer("Two")),
Stream.of(new StringBuilder("Three"))
@edalorzo
edalorzo / streams.sml
Created November 7, 2014 14:07
Streams in SML
datatype 'a stream = Empty | Cons of 'a * (unit -> 'a stream)
fun from n = Cons(n, fn () => from(n +1))
val naturals = from(1)
fun filter f xs =
case xs of
Empty => Empty
| Cons(h,t) => if f(h)
then Cons(h, fn () => filter f (t()))
@edalorzo
edalorzo / PythonEuler1.py
Created November 24, 2012 05:46
Project Euler-Problem 1
def sumDivisible(m,n):
"""
Given a maximum integer m and a factor n, calculates the sum
of all divisors of n up to m.
"""
p = m // n
return n * (p * (p + 1)) // 2
def solve():
@edalorzo
edalorzo / HaskellEuler1.hs
Created November 24, 2012 04:49
Project Euler - Problem #1
module Euler.Problem1 where
-- | Given a maximum integer m and a factor n calculates the sum
-- of all divisors of the given factor up to m.
sumDivisible :: Int -> Int -> Int
sumDivisible m n = n * (p * (p + 1)) `div` 2 where p = m `div` n
result = (sumDivisible 999 3) + (sumDivisible 999 5) - (sumDivisible 999 15)
solve::String
@edalorzo
edalorzo / ScalaEuler1.scala
Created November 24, 2012 05:32
Project Euler-Problem 1
/**
* Given a maximum integer m and a factor n calculates the sum
* of all divisors of the given factor up to m.
*
* @param m maximum integer
* @param n given factor (i.e. 3 or 5)
* @return the sum of all divisors.
*/
def sumDivisible(m: Int, n:Int): Int = {
@edalorzo
edalorzo / RubyEuler1.rb
Created November 24, 2012 06:16
Project Euler-Problem 1
# Given a maximum integer m and a factor n, calculates the sum
# of all divisors of n up to m.
def sumDivisible(m,n)
p = m / n
n * (p * (p + 1)) / 2
end
def solve
result = sumDivisible(999,3) + sumDivisible(999,5) - sumDivisible(999, 15)
@edalorzo
edalorzo / PythonEuler4.py
Created November 25, 2012 02:52
Project Euler-Problem #4
def is_palindrome(n):
text = str(n)
return text == text[::-1]
def palindromes():
for j in range(100,1000):
for k in range(100,1000):
product = j * k
@edalorzo
edalorzo / PythonEuler13.py
Created November 25, 2012 03:11
Project Euler-Problem #13
import StringIO
def get_big_numbers():
number = """
37107287533902102798797998220837590246510135740250
46376937677490009712648124896970078050417018260538
74324986199524741059474233309513058123726617309629
91942213363574161572522430563301811072406154908250
23067588207539346171171980310421047513778063246676
89261670696623633820136378418383684178734361726757
@edalorzo
edalorzo / PythonEuler48.py
Created November 25, 2012 03:18
Project Euler-Problem #48
def next_number(stop=1000):
n = 1
while n <= stop:
yield n ** n
n += 1
if __name__ == '__main__':
result = str(sum(next_number()))[-10:]
print "The last ten digits of the series, 1^1 + 2^2 + 3^3 + ... + 1000^1000 is: %s" % result