Skip to content

Instantly share code, notes, and snippets.

@cronin101
cronin101 / spiral.rb
Created October 31, 2013 12:02
Flattening a matrix stored as a list of lists, in clockwise spiral pattern. From a coding interview - sorry about the Ruby, recruiters (I'm not actually sorry.)
def spiral_flatten(matrix_chunks)
[].tap do |result|
until matrix_chunks.empty?
result.concat matrix_chunks.shift
matrix_chunks[0..-2].to_a.each { |chunk| result << chunk.pop }
break if matrix_chunks.empty?
result.concat matrix_chunks.pop.reverse
matrix_chunks[1..-1].to_a.reverse_each { |chunk| result << chunk.shift }
end
end
@cronin101
cronin101 / test.rb
Created November 8, 2013 14:11
foo_test
def foo
puts "This is a test"
end
@cronin101
cronin101 / mrCount.hs
Last active December 28, 2015 00:09
MapReduce and token-counting in Haskell.
import Control.Arrow
import Data.Function
import Data.List
import Data.Ord
-- mapReduce key_func val_func reduce_func --
mapReduce :: Ord c => (b1 -> c) -> (b1 -> b) -> ([b] -> c1) -> [b1] -> [(c, c1)]
mapReduce k r v = reduce' r . group' . map' k v
where
reduce' f = map (fst . head &&& f . map snd)
@cronin101
cronin101 / practical1.R
Created December 11, 2013 15:28
Arse Crypt
# Read the CSV file into R
citations <- read.csv("citations2013.csv")
# At this point, people were told to attach(). Apparently that's icky.
print("Dataset contains the following columns:")
print(names(citations))
print("Length of citations$Citations2013:")
print(length(citations$Citations2013))
@cronin101
cronin101 / traversal.rb
Last active January 1, 2016 00:39
Interview question about tree reconstruction.
# Exercise:
# Given the Pre-order and In-order traversals of a tree,
# reconstruct the original tree structure.
#
# Pre-order node output: Node (Left) (Right)
# In-order node output: (Left) Node (Right)
#
# Example:
# 'a'
# | |
@cronin101
cronin101 / report.tex
Last active January 3, 2016 21:08
example latex layout
\documentclass[10pt, a4paper]{article} % use "amsart" instead of "article" for AMSLaTeX format
\usepackage{microtype}
\usepackage[textwidth=17cm,textheight=24cm]{geometry}
\usepackage{fullpage} % See geometry.pdf to learn the layout options. There are lots.
\usepackage{helvet}
\usepackage{listings}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage[normalem]{ulem}
\usepackage{epstopdf}
\usepackage{rotating}
@cronin101
cronin101 / replacement_selection_sort.py
Last active August 29, 2015 13:55
Replacement Selection Sort - from Advanced Databases Lecture 6
import heapq
import random
class HeapSorter:
def __init__(self, num_blocks, input_sequence):
self.num_blocks = num_blocks
self.input_sequence = input_sequence
def sorted(self):
return heapq.merge(*self.__sorted_subsequences__())
@cronin101
cronin101 / features.rb
Last active August 29, 2015 13:56
All of the features
Benchmark.realtime do
File.new('./one_to_a_million.txt')
.readlines.map(&:to_i)
.map { |x| x - 999_990}
.select { |x| x >= 0 }
end
#=> 0.598499
Benchmark.realtime do
File.new('./one_to_a_million.txt')[Int]
#!/usr/bin/env python
True, False = False, True
class ExamBoard(object):
def submit(self, work):
work_is_passing_grade = len([work]) > 0
return work_is_passing_grade
#!/usr/bin/env python
from __future__ import braces
True, False = False, True
class ExamBoard(object):
def submit(self, work):
work_is_passing_grade = len([work]) > 0