Skip to content

Instantly share code, notes, and snippets.

@mbeltagy
mbeltagy / gist:129a584c21ce56ac3b60
Last active August 29, 2015 14:20
Monkeys and Coconuts
x=zeros(Float64,5)
maxmultiple=1000
for m=1:maxmultiple
x[5]=4*5*m #It has to be divisalbe by 4 and 5
for i=4:-1:1
x[i]=5x[i+1]/4+1
end
x0=x[1]*5/4+1
if isinteger(x)&&isinteger(x0)
println(int(x))
@mbeltagy
mbeltagy / gist:834988ba7b4c0eb69922
Created July 16, 2015 15:28
A pairs function in julia that uses PyPlot to generate plots similar to R's pairs
function pairs(d::DataFrame)
varnames= map(string,names(d))
l=length(varnames)
count=0
for i=1:l
for j=1:l
count+=1
ax=subplot(l,l,count)
ax[:xaxis][:set_visible](false)
ax[:yaxis][:set_visible](false)
@mbeltagy
mbeltagy / gist:a5f5657dc092c8b3cc78
Created July 16, 2015 16:03
Box plot on Data Frames
function dataframeplot(d::DataFrame,facsym,idsym)
factors=sort(unique(d[facsym])) #We need this to do the boxpols
numpoints=size(d,1)
numfactors=length(factors)
data=Array(Array{Float64},numfactors)
for i=1:numfactors data[i]=[] end
for i=1:numpoints
idvar=d[idsym][i]
fac=d[facsym][i]
for j=1:numfactors
@mbeltagy
mbeltagy / backgammon_prob.jl
Created October 19, 2015 18:40
Calculating the probability of getting a double dice to move peg to any particular point after where it is in game of backgammon.
function backgammon_prob()
location_hits=zeros(Int,24)
for i=1:6, j=1:6
if(i!=j) # to avoid double counting doubles
location_hits[i]+=1
location_hits[j]+=1
else
location_hits[j]+=1
end
location_hits[i+j]+=1
@mbeltagy
mbeltagy / ErrorPropagation.ipynb
Created December 9, 2015 15:55
The error propagation enigma. Some problems are harder than they first appear
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@mbeltagy
mbeltagy / subfix.jl
Created June 4, 2016 01:20
Reading two subtitle files and harmonizing the times.
# This script fixes subtitle timing your timestamps
# from on file to adjust another
# Opening up the first file in swedish
using StringEncodings
open("The.Seventh.Seal.1957.PROPER.1080p.BluRay.x264-SADPANDA.srt") do io
global swe=readall(io,enc"ISO-8859-15")
end;
swesplit=matchall(r"(\d{1,4}\r\n\d.*?)(?=(\n\d|$))"s,swe);
# The second file in English
open("The\ Seventh\ Seal\ [Det\ Sjunde\ Inseglet].1957.BRRip.XviD.AC3-VLiS.srt") do io
@mbeltagy
mbeltagy / int_prob_gen.py
Last active September 23, 2016 22:14
Interactive Integer problem generator for kids
import random
def pm(x):
if x>0:
return "+"
else:
return ""
def int_prob_gen(n):
s=0
@mbeltagy
mbeltagy / int_prob_gen.jl
Last active October 22, 2016 13:16
Interactive Integer problem generator for kids
function int_prob_gen(n)
starting_time=time()
s=0 #successes
pm(x)=x>0? "+":""
for i=1:n
x=rand(-10:10)
y=rand(-10:10)
op=rand(["+","-"])
right_ans="($x)$op($y)"|>parse|>eval
parse_correct=false
@mbeltagy
mbeltagy / Probability.ipynb
Last active October 26, 2016 07:59
Julia translation of Norvig's notebook on "Probability, Paradox, and the Reasonable Person Principle"
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@mbeltagy
mbeltagy / parksim.R
Last active November 28, 2016 17:27
Similar to the parksim in the R probability book, but 10 times faster
parksim <-function(nreps) {
nvals<-rgeom(nreps,0.15)+1
dvals<-ifelse(nvals<=10,11-nvals,nvals-11)
mean(dvals)
}
system.time(parksim(100000000))