Skip to content

Instantly share code, notes, and snippets.

import sys
def negVector(lst):
return [-x for x in lst]
def addVectors(l1, l2):
return [l1[i] + l2[i] for i in range(len(l1))]
def multVectors(l1, l2):
return [l1[i] * l2[i] for i in range(len(l1))]
@AlexNisnevich
AlexNisnevich / .zshrc
Created July 8, 2012 02:54
zshrc stuff
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"
@AlexNisnevich
AlexNisnevich / gist:3140977
Created July 19, 2012 05:33
Github Pages for generated HTML

Assuming this directory structure:

project
|-- src
|   +-- (source files)
+-- html
    +-- (generated html)
@AlexNisnevich
AlexNisnevich / deep_sort.rb
Created July 19, 2012 19:51
Playing with Ruby hashes
class Hash
def deep_sort
Hash[sort.map {|k, v| [k, v.is_a?(Hash) ? v.deep_sort : v]}]
end
end
#!/bin/sh
export DISPLAY=:0.0
sudo killall chrome
google-chrome $1 &
sleep 2
xdotool key F11
@AlexNisnevich
AlexNisnevich / score_subreddit.rb
Created November 9, 2012 11:32
score_subreddit: Computes the Flesch-Kincaid grade level of the last 25 comments of the given subreddits.
#
# Usage: ruby score_subreddit.rb <list of subreddits>
#
# Computes the Flesch-Kincaid grade level of the last 25 comments of the given subreddits.
#
# For example:
# > ruby score_subreddit.rb science worldnews funny
# science: 8.33
# worldnews: 9.71
# funny: 5.32
@AlexNisnevich
AlexNisnevich / ling_diversity.rb
Last active December 10, 2015 23:29
Calculating the "Linguistic Diversity Index" (% chance that two random residents of a given country have different native languages)
require 'mechanize'
agent = Mechanize.new
language_regex = Regexp.new(/]\r\n\t\t\t\t\t\t([0-9,]*)(\.| )/)
index_page = agent.get('http://www.ethnologue.com/country_index.asp?place=all')
index_page.links_with(:href => /show_country.asp/).each do |country_link|
country = country_link.text().strip()
speakers = []
@AlexNisnevich
AlexNisnevich / lucky_numbers.rb
Created April 26, 2013 11:37
Calculates lucky numbers up to a given limit, and prints the list of unlucky numbers removed for each lucky number
limit = 1000
nums = (1..limit).to_a
([1] + nums).each do |a|
lucky_num = nums[a]
unlucky_nums = nums.values_at(* nums.each_index.select {|i| (i+1) % lucky_num == 0})
if unlucky_nums.size == 0
break
else
[num_features, num_states] = size(backup_states_phi); % [22, 92]
num_actions = size(R{1}{1}); % 40
num_blocks = block_idx; % 7
mu0 = ones(num_states, 1) / num_states;
% cvx_solver sedumi
cvx_begin
variable theta(num_features)
variable V(num_states, num_blocks)
minimize sum(dot(mu0, (transpose(theta) * backup_states_phi))) + C * norm(theta)
if (map.getPlayer().getX()>10) me.move('right');
if (map.getPlayer().getX()<10) me.move('left');
if (map.getPlayer().getY()>10) me.move('down');
if (map.getPlayer().getY()<10) me.move('up');