Skip to content

Instantly share code, notes, and snippets.

#Newbie programmer
def factorial(x):
if x == 0:
return 1
else:
return x * factorial(x - 1)
print factorial(6)
#First year programmer, studied Pascal
#Using csv reader
import csv
csvreader=csv.reader(open('x.csv','r'))
list = [ x[0] for x in csvreader]
#Using zip
columns = zip(*csvreader)
nocolumns=len(columns)
#!/usr/bin/python
import codecs
import csv
try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
f = [1, 2, 3, 4, 5, 5, 5, 6]
set(f)
print list(set(f))
#A set object is an unordered collection of distinct hashable objects. Common uses include membership testing, removing duplicates from a sequence, and computing mathematical operations such as intersection, union, difference, and symmetric difference.
<!DOCTYPE html>
<!--
Copyright 2010 Google Inc.
All rights reserved.
Original slides:
Marcin Wichary
Modifications:
Ernest Delgado
Alex Russell
require 'rubygems'
require 'sinatra'
require 'redis'
# To use, simply start your Redis server and boot this
# example app with:
# ruby example_note_keeping_app.rb
#
# Point your browser to http://localhost:4567 and enjoy!
#
require 'rubygems'
require 'neo4j'
include Neo4j
Transaction.new
# create some people
andreas = Node.new :name => 'andreas'
peter = Node.new :name => 'peter'
kalle = Node.new :name => 'kalle'
#Parsing with plain Ruby
filename = 'data.csv'
file = File.new(filename, 'r')
file.each_line("\n") do |row|
columns = row.split(",")
break if file.lineno > 10
end
def csv_to_array(file_location)
csv = CSV::parse(File.open(file_location, 'r') {|f| f.read })
fields = csv.shift
csv.collect { |record| Hash[*(0..(fields.length - 1)).collect {|index| [fields[index],record[index].to_s] }.flatten ] }
end
# 1) Put this file in your working directory
# 2) run 'sudo gem install active_support grit'
# 3) run 'ruby git-line-count.rb'
# 4) run 'open loc.html'
# 5) yay
require 'rubygems'
require 'active_support'
require 'grit'
require 'open-uri'