Skip to content

Instantly share code, notes, and snippets.

@ryandgoldenberg1
ryandgoldenberg1 / cem.py
Created January 25, 2020 21:57
Cross Entropy Method for Optimization
# Adapted from Wikipedia page here: https://en.wikipedia.org/wiki/Cross-entropy_method
import numpy as np
def cem(mean, variance, maxits, num_samples, num_keep, objective_fn):
for t in range(1, maxits + 1):
# Sample according to current distribution
x = np.random.normal(loc=mean, scale=np.sqrt(variance), size=num_samples)
# Evaluate samples
require 'set'
class Set
def to_csv
self.reduce("") do |accum, el|
accum << ",#{el}"; accum
end[1..-1]
end
end
@ryandgoldenberg1
ryandgoldenberg1 / infinite_scroll_mixin.js
Created December 12, 2015 04:58
React Infinite Scroll
var InfiniteScrollMixin = {
SCROLL_RATIO: 0.75,
CONTAINER: window,
CONTENT: document,
componentDidMount: function() {
this._fetchInitialData();
$(this.CONTAINER).on("scroll", this._onScroll);
},
@ryandgoldenberg1
ryandgoldenberg1 / sudoku.rb
Created September 30, 2015 00:14
Terminal Sudoku in Ruby
require 'colorize'
class Tile
attr_reader :value, :given
def initialize(value = 0)
@value = value
@given = value != 0
end
@ryandgoldenberg1
ryandgoldenberg1 / maze.rb
Last active September 29, 2015 17:34
2D Maze Pathfinder
class Maze
attr_accessor :data, :workqueue, :seen, :start, :finish, :solution
def initialize(filename)
@data = read_data(filename)
@start = find_letter("S")
@finish = find_letter("E")
@workqueue = [ Node.new(start, [start]) ]
@seen = [start]
@ryandgoldenberg1
ryandgoldenberg1 / eight_queens.rb
Last active September 29, 2015 01:20
Eight Queens Problem
class EightQueens
def initialize
@workset = (0...64).map { |i| [i] }
end
def queen_attacks(pos)
(horizontal_attacks(pos) +
vertical_attacks(pos) +
main_diagonal_attacks(pos) +
secondary_diagonal_attacks(pos)).uniq
@ryandgoldenberg1
ryandgoldenberg1 / USCF_Rating.rb
Created May 31, 2015 22:07
Takes a file of USCF IDs and outputs a file of USCF ratings
require 'open-uri'
# first argument: file containing uscf id numbers
# second argument: file you wish to write ratings to
if ARGV.length < 2 then return nil end
# gets unofficial rating
def get_rating(id)
# opens page for corresponding to id number
f = open("http://www.uschess.org/msa/MbrDtlTnmtHst.php?#{id}")
@ryandgoldenberg1
ryandgoldenberg1 / Sudoku.rb
Last active August 29, 2015 14:22
Sudoku solver
class Sudoku
# board should be a string with numbers and dashes for spaces
def initialize(board)
@board = board.chars.to_a
end
# prints board
def show
(0...9).each {|r|