Skip to content

Instantly share code, notes, and snippets.

func getWordCount(text string) map[string]int {
words := strings.Fields(text)
wordCounter := make(map[string]int)
for _, word := range words {
word = strings.ToLower(word)
if wordCounter[word] > 0 {
wordCounter[word]++
} else {
wordCounter[word] = 1
func removePunctuation(text string) string {
// match a sequence of unicode letters which may contain an apostrophe
re := regexp.MustCompile("\\p{L}+('\\p{L}+|\\p{L}*)")
return strings.Join(re.FindAllString(text, -1), " ")
}
@odhranroche
odhranroche / TicTacToe.erl
Created June 27, 2014 14:20
My first attempt at a functional game of TicTacToe. Plays randomly against itself only.
-module(tictactoe).
-export([play/0]).
%-compile(export_all).
%% -1 is X and false
%% 1 is O and true
%% 0 is _
wins() ->
[[1,2,3], [4,5,6], [7,8,9],
// static evaluate function
// adapted from http://kartikkukreja.wordpress.com/2013/03/30/heuristic-function-for-tic-tac-toe/
// original author : Kartik Kukreja
val wins = Array( Array(0,1,2), Array(3,4,5), Array(6,7,8),
Array(0,3,6), Array(1,4,7), Array(2,5,8),
Array(0,4,8), Array(2,4,6) )
val heuristic = Array( Array(0, -10, -100, -1000),
Array(10, 0, 0 , 0),
@odhranroche
odhranroche / RecursionJava.java
Last active December 20, 2015 05:08
Recursive functions in Java to compare recursion with Lua
// Tested using Java version 1.7.0_21 in Eclipse Juno
//
// 3 recursive functions to compute the sum of the first n odd positive integers
// n can reach 4000
public static int sumOdds1(int n, int sum, int counter){ // call : sumOdds1(20, 0, 0)
if (n == 0){
return sum;
}else{
@odhranroche
odhranroche / RecursionLua.lua
Last active December 20, 2015 05:08
Recursive functions in Lua to compare with Java recursion
-- Tested with Lua 5.1.4
-- 3 recursive functions to compute the sum of the first n odd positive integers
-- n can reach 100,000,000
function sumOdds1(n, sum, counter) -- call: sumOdds(20, 0, 0)
if n == 0 then
return sum
else
if counter % 2 == 1 then