Skip to content

Instantly share code, notes, and snippets.

View arademaker's full-sized avatar
🎯
Focusing

Alexandre Rademaker arademaker

🎯
Focusing
View GitHub Profile
@arademaker
arademaker / simple-webserver.js
Created January 27, 2011 23:26
a simple webserver in node.js
var express = require('express'), app = express.createServer();
var fs = require('fs'), sys = require("sys");
var log = fs.createWriteStream('params.json', {'flags': 'a'});
app.use(express.logger());
app.get('/', function(req, res){
console.log(req.query);
log.write(JSON.stringify(req.query));
res.send('It\'s saved!');
@arademaker
arademaker / check-authors.R
Created March 1, 2011 02:19
Verificando autores com ordem de autoria repetida nos Lattes
library(XML)
check <- function(filename, dir = getwd()) {
doc <- xmlInternalTreeParse(paste(dir, filename, sep="/"))
tmp <- xpathSApply(doc,"//AUTORES", function(x)
c(xmlGetAttr(xmlParent(x), "SEQUENCIA-PRODUCAO"),
xmlGetAttr(x, "ORDEM-DE-AUTORIA")))
if(class(tmp) == "list" & length(tmp) == 0)
return(list(file = filename, resp = NA))
tmp <- t(tmp)
@arademaker
arademaker / truth-tables.R
Created March 3, 2011 02:15
constructing truth tables in R
library(gtools)
data <- permutations(2, 3, v = c(T,F), repeats.allowed=TRUE)
tt <- as.data.frame(data)
names(tt) <- letters[1:3]
tt$"! (a | b) | c" <- (!(tt$a | tt$b) | tt$c)
# unusual output!
xtable(tt)
@arademaker
arademaker / lista-2.py
Created April 1, 2011 00:17
Trabalho proposto para turma de LFA. Implementar fecho transitivo-reflexivo de uma relação representada como um grafo. Versão não otimizada, apenas uma possível solução.
import sys
# Leitura do arquivo de entrada no formato especificado. Linhas iniciadas com
# "#" sao comentarios. Nao era necessario os alunos implementarem isso.
grafoin = []
try:
infilename = sys.argv[1]
infile = open(infilename,'r')
for line in infile:
@arademaker
arademaker / fibonacci.py
Created April 6, 2011 01:06
Duas versões de Fibonacci apresentadas em sala (Mestrado EMAP/FGV). Exemplos do Livro Algorithms (http://books.google.com/books?id=3sCxQgAACAAJ).
def fib1(n):
if n == 0:
return 0
if n == 1:
return 1
else:
return fib1(n-1) + fib1(n-2)
def fib2(n):
@arademaker
arademaker / wordcloud-in-r.R
Created August 13, 2011 12:30
Wordcloud em R
library(RColorBrewer)
library(wordcloud)
pal2 <- brewer.pal(8,"Set2")
lines <- readLines("pg32519.txt")
lines <- lines[45:3192]
text <- paste(lines, collapse = " ")
words <- unlist(strsplit(tolower(text), "\\s+|,|\\(|\\)|:|\\.|!|\\?|;"))
@arademaker
arademaker / issn.lisp
Created September 14, 2011 00:07
Verifying ISSN's Check digit using Lisp
(defun check-issn (str)
" Parse a string of 8 digits in the format NNNN-NNNN representing an
ISSN number. The last N can be an X representing 10. "
(let ((digits (remove nil (loop for char across str
collect (cond ((equal char #\X) 10)
((equal char #\-) nil)
(t (parse-integer (string char))))))))
(if (not (equal (length digits) 8))
nil
(equal 0 (mod (loop for i from 8 downto 1
@arademaker
arademaker / bcb.R
Created January 2, 2012 13:16
getting data from BCB (Banco Central do Brasil)
# This script was originally written during for a summer course that I
# gave at FGV in January 2011. The script was used to show the
# students how to retrive data from Banco Central do Brasil
# (http://bcb.gov.br/?SERIETEMP) using SOAP protocol and R.
library(SSOAP)
library(XML)
library(RCurl)
wsdl <- getURL("https://www3.bcb.gov.br/sgspub/JSP/sgsgeral/FachadaWSSGS.wsdl",
@arademaker
arademaker / monty-hall.R
Created March 17, 2012 21:53
Monty Hall Problem in R
portas <- function (t){
teste <- 0
amostra <- 0
while (teste < t){
# dist é a distribuição dos conteúdos das portas, 1 marca a porta
# premiada
dist <- c(1,0,0)
dist <- dist[sample(3)]
a <- sample(3,1) # a será a porta que você escolhe
@arademaker
arademaker / chapter-3-ci.R
Created May 22, 2012 11:42
Script Gustavo L. A. chapter 3 of Collective Intelligence using R
corpus2 <- lapply(corpus, getWords)
tira.erro <- sapply(corpus2, length) > 0
corpus2 <- corpus2[tira.erro]
apcount <- table(unlist(lapply(corpus2, names)))
apcount.df <- as.data.frame(apcount, stringsAsFactors = FALSE)
apcount.df$f <- apcount.df$Freq / length(corpus2)