Code Golf: Choose
Jan 16, 2008 - Comments
Here is my submission for the Choose challenge. Code size: 88 bytes.
def g(r)(1..r).inject(1){|m,n|m*n}end
x,y=gets.split.map{|n|n.to_i}
p g(x)/(g(y)*g(x-y))
Jan 16, 2008 - Comments
Here is my submission for the Choose challenge. Code size: 88 bytes.
def g(r)(1..r).inject(1){|m,n|m*n}end
x,y=gets.split.map{|n|n.to_i}
p g(x)/(g(y)*g(x-y))
Dec 08, 2007 - Comments
Here is my submission for the Numeric Diamonds challenge. Code size: 219 bytes.
[9,36,100].each{|n|s=Math.sqrt(n).to_i
w=n.to_s.size.to_i
a=[]
j=s+s-1
j.times{a<<[" "*w]*j}
x=0
y=s-1
f=x
g=y
1.upto(n){|t|a[x][y]=t.to_s.rjust w
t%s<1 ?(x=f+1;f=x;y=g-1;g=y):(x+=1;y+=1)}
a.each{|r|puts r.join.rstrip}}
Jun 09, 2007 - Comments
Parsing an RSS feed is insanely simple with Ruby. Two lines is all it takes. . .
require 'rss'
rss = RSS::Parser.parse(open('http://www.travisonrails.com/feed/posts').read, false)
Now you'll have an Array of the results, so you can do something like:
rss.items.each { |i| puts "#{i.title} - #{i.date}" }
Jun 07, 2007 - Comments
UPDATE: Changed the generate_password method slightly based on commenter Dave Burt's suggestion.
I'm working on a project where the user can reset their password if they've forgotten it. So, I need to generate a random password and email it to them so they can login and change it. Fortunately, I found this neat little Ruby snippet that will generate random text of a given length:
def generate_password(length=6)
chars = 'abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ23456789'
password = ''
length.times { |i| password << chars[rand(chars.length)] }
password
end
Some examples:
generate_password >> U48ydn generate_password(10) >> QzWXdAkDy5
Feb 07, 2007 - Comments
Here is my submission for the Roman to Deciman challenge. Code size: 121 bytes.
v={'I',1,'V',5,'X',10,'L',50,'C',?d,'D',500,'M',1000}
y=q=0
gets.chop.split('').each{|n|j=v[n]
y+=j-(q<j ?q*2:0)
q=j}
p y