This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
######### Intro to R ############### | |
######### The Data Frame ########### | |
df <- data.frame( | |
row.names = c('HaLikud', 'Yesh Atid', 'HaAvoda', 'HaBait HaYehudi', 'Yehadut HaTora', 'Meretz', 'Shas'), | |
LeaderName = c('Netanyahu', 'Lapid', 'Yehimovitch', 'Bennet', 'Litzman', 'GalOn', 'Yishai'), | |
Category = c('Right', 'Center', 'Left', 'Right', 'Religious', 'Left', 'Religious'), | |
Mandates = c(31, 19, 15, 12, 7, 6, 11) | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RingBuffer < Array | |
attr_reader :max_size | |
def initialize(max_size, enum = nil) | |
@max_size = max_size | |
enum.each { |e| self << e } if enum | |
end | |
def <<(el) | |
if self.size < @max_size || @max_size.nil? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This is taken from Liam Gibbing's Python Cheat Sheet at https://docs.google.com/file/d/0B9VT_L2CDnKvODYyNTc5NjktYmMyOC00NDFkLTliNTctMzQzMTAzYjUyYmYy/view?pli=1&sle=true | |
I just don't like the DocX format so I imported it here. | |
FUNCTIONS, METHODS & ATTRIBUTES | |
NUMBERS | |
• math.pi and math.e: Returns values of both pi and exponential const. respectively. | |
• math.sqrt(integer1): Returns square root of integer, use cmath.sqrt for negatives. | |
• math.floor(integer1): Returns integer rounded down to nearest whole. | |
• math.ceil(integer1): Returns integer rounded up to nearest whole. | |
• round(integer1, [NumOfDigits]): Rounds integer depending on number of digits specified, default is to nearest whole. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Motivation: | |
# | |
# Using lazy evaluation is the way truly functional (not just 'functional style') programming languages | |
# achieve performance. Ruby is not there yet for several reasons, but at least some of them can be remedied | |
# with basic amendments to the standard library. | |
# | |
# If you want to find, for instance, the first number n<1000 such that the approximation (1+(1/n))^n to e is | |
# less than a certain distance, say, eps = 0.01. If you just write | |
(1..1000).to_enum.map { |d| ((1+(1.0/d))**(d) - (Math::E)).abs }.find_index { |x| x < 0.01 } | |
# It works, and returns 134. But you calculated 1000-134=876 more values than you had to. With the code below, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### Most of the summary is taken from the awesome R twotorials at http://www.twotorials.com/ by Anthony Damico | |
### Some of it are my additions from my experience. This is intended so you can Ctrl+F and find what you want using | |
### common names of functions and concepts from other languages or statistics. | |
### Troubleshooting: Search http://tolstoy.newcastle.edu.au/R/ , http://www.r-bloggers.com/, http://www.rseek.org/ | |
### Basics | |
traceback() # Get the call stack after an error, for debugging | |
32 %% 2 # == 0 mod operator | |
5 %/% 3 # == 1 integer division |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[I, J] = find(A > 0) % Col-order search | |
[J, I] = find(A' > 0) % Row-order (non-zero) search | |
arrayfun(@(x) length(x.field)>0, some_struct_array) % Apply length to 'field' in each element of a struct array | |
a_struct = struct('field1', value1, 'field2', value2) % Create a struct ("object") | |
list = [list 1] % add element to a vector |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## This is an example encompassing much of ruby's basic syntax. | |
## It is derived from various examples in the Pragmatic Bookshelf's "Programming | |
## Ruby" book. It is intended as a quick reference, and you should grep (search | |
## the file) for various terms like "splat args", "exception" or "iterator" to | |
## get a quick look at how they are used. It is probably very hard to make any | |
## sense of this by reading it linearly, nor did I attempt to have the example | |
## make any logical sense at all - the operations done here are not at all | |
## coherent | |
## | |
## This is freely distributable with the following credit: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
COMMANDS | |
rails new demo #Create a new rails project under 'demo' | |
rails server #Start a local server to test the project, port 3000 | |
rails generate controller ControllerName action1 action2 | |
# For example 'rails generate controller say hello goodbye' gives | |
# say/hello and say/goodbye links, which are implemented by a class | |
# app/controllers/say_controller.rb with hello and goodbye methods. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
collections.defaultdict(lambda: 0) # Hash map with default values | |
l = [t for t in l if not t in s] # Given a list l and a set s, returns l without all the elements in s | |
fun_with_many_args(*[1, 2, 3]) # Splat an array into an argument list | |
fun_with_named_args(**{'arg1': 1, 'arg2': 2}) # Splat a map (dictionary) for named argument functions! | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
./configure --enable-python27=yes --with-png --enable-ltdl-install --with-included-ltdl --enable-perl=no CC="gcc -arch x86_64" CXX="g++ -arch x86_64" | |
make | |
sudo make install |
NewerOlder