Skip to content

Instantly share code, notes, and snippets.

@zzak
zzak / squareroot.rb
Created June 30, 2010 16:34
find the square root in ruby
def sqrt(x)
square = lambda { |a| a * a }
average = lambda { |a,b| (a + b)/2.0 }
is_good_enough = lambda { |a| (square[a] - x).abs < 0.001 }
improve = lambda { |a| average[a, x/a] }
sqrt_iter = lambda { |a| is_good_enough[a] ? a : sqrt_iter[improve[a]] }
sqrt_iter[1.0]
end
setlocal query "twill Python"
go http://www.google.com/
fv 1 q $query
submit btnI
show
import re
import mechanize
response = mechanize.urlopen("http://www.google.com/")
print response.read()
import feedparser
d = feedparser.parse("http://planet.python.org/rss20.xml")
print d.feed.title
print d.feed.subtitle
print "============================"
print d.entries[0].title
print d.entries[0].description
n = raw_input("Whats your name? ")
print "Hello, " + n + "!"
print 'tuple is to freeze as list is to thaw'
sequence = ['a', 'b', 'c']
seqtxt = "Sequence: %s"
print seqtxt % sequence
glue='-'
gluetxt = "Glue: %s"
print gluetxt % glue
joined = glue.join(sequence)
jointxt = "Joined: %s"
sequence = ['a', 'b', 'c']
seqtxt = "Sequence: %s"
print seqtxt % sequence
glue='-'
gluetxt = "Glue: %s"
print gluetxt % glue
joined = glue.join(sequence)
jointxt = "Joined: %s"
#!/usr/bin/env python
import subprocess
subprocess.call(["ls", "-l","/tmp"])
#!/usr/bin/env python
import subprocess
uname = "uname"
uname_arg = "-a"
print "Gathering system information with %s command:\n" % uname
subprocess.call([uname, uname_arg])
diskspace = "df"
diskspace_arg = "-h"