Skip to content

Instantly share code, notes, and snippets.

@stockwellb
stockwellb / py Sample
Created March 5, 2013 03:08
py Sample
<snippet>
<content><![CDATA[
Hello, ${1:this} is a ${2:snippet}.
]]></content>
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
<!-- <tabTrigger>hello</tabTrigger> -->
<!-- Optional: Set a scope to limit where the snippet will trigger -->
<!-- <scope>source.python</scope> -->
</snippet>
@stockwellb
stockwellb / assert_route_selection.py
Last active July 2, 2023 02:08
Selenium example
# -*- coding: utf-8 -*-
from selenium import selenium
import unittest, time, re
server = "http://example.com/"
def suite():
suite = unittest.TestSuite()
suite.addTest(AssertRouteSelection(u"Montréal-Québec",u"Route Status: MONTRÉAL-QUÉBEC"))
suite.addTest(AssertRouteSelection(u"Montréal-Ottawa",u"Route Status: MONTRÉAL-OTTAWA"))
@stockwellb
stockwellb / run_length.exs
Last active June 7, 2016 01:23
Run Length Encoding in Elixir.
defmodule RunLength do
def encode(data) do
String.graphemes(data) |> do_encode
end
defp do_encode([]) do
""
end
@stockwellb
stockwellb / word_count.exs
Created June 6, 2016 00:52
Word count in Elixir
defmodule WordCount do
def count(phrase) do
String.split(phrase)
|> Enum.reduce(%{}, fn(word, map) ->
Map.update(map, word, 1, &(&1 + 1))
end)
|> Enum.to_list()
end
end
@stockwellb
stockwellb / hello_world.exs
Last active June 7, 2016 01:25
Hello world in Elixir with some odd twists
defmodule HelloWorld do
def greet("") do
{:ok, "Hello World"}
end
def greet([]) do
{:ok, "Hello World"}
end
def greet(names) when is_list(names) do
@stockwellb
stockwellb / list_ops.exs
Created June 7, 2016 03:31
An exercise in map, reduce, length for lists
defmodule ListOps do
def length(list) do
reduce(list,0,fn(item,acc) -> acc + 1 end)
end
def reduce([], acc, _) do
acc
end
@stockwellb
stockwellb / list_truther.exs
Last active June 8, 2016 01:14
And-ing and Or-ing list elements
defmodule ListTruther do
def test([],_) do
false
end
def test(list, mode) do
case mode do
:and -> Enum.reduce_while(list, false, fn(item, _) ->
if item, do: {:cont, true}, else: {:halt, false}
@stockwellb
stockwellb / sublist.exs
Created June 12, 2016 02:24
Comparing lists for equal/sublist/superlist
defmodule Sublist do
def compare(a,b) when a === b, do: :equal
def compare(a,b) do
cond do
a === b -> :equal
sublist?(a,b) -> :sublist
sublist?(b,a) -> :superlist
true -> :unequal
end
@stockwellb
stockwellb / anagram.exs
Last active June 12, 2016 21:38
Find a sublist of anagrams, for a word, in a list of words
defmodule Anagram do
def match(word,words) do
Enum.filter(words, fn(w) ->
source = String.downcase(word)
compare = String.downcase(w)
if source === compare do
false
else
a = to_char_list(source) |> Enum.sort
b = to_char_list(compare) |> Enum.sort
@stockwellb
stockwellb / bracket_push.exs
Last active June 19, 2016 22:03
Bracket push solution
defmodule BracketPush do
def check_brackets(""), do: true
def check_brackets(s) do
do_check_brackets([],String.codepoints(s))
end
defp do_check_brackets(stack,[]) do
length(stack) === 0
end