Thursday, December 28, 2006

Solving 'The Python Challenge' with Ruby
Level 1 - what about making trans?

URL for Level 1 is here.

In the webpage, a picture with the followings drawn on a paper is shown:


K -> M
O -> Q
E -> G

and there are also the following messages:

everybody thinks twice before solving this.

g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq
glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb.
lmu ynnjw ml rfc spj.

After thinking, I know I need to translate the strange message above with the rule drawn on the paper. So what is the rule? After thinking again (totally thinking twice :)), I find that I need to replace every letter by the letter 2 places after it. Then I write the following function to do it:
def translate(str)
str.split(//).map do |c|
case c
when 'a'..'x': c.next.next
when 'y': 'a'
when 'z': 'b'
else c
end
end
end

Passing the strange message to this function, I get:

i hope you didnt translate it by hand. thats what computers are for. doing it in by hand is
inefficient and that's why this text is so long. using string.maketrans() is recommended.
now apply on the url.

It tells me to apply the same translation on the URL: 'map', which gives 'ocr'. Therefore the url for level 2 is here.


Updated: there is built-in string function to do the same thing:

String#tr(fromString, toString)

So:

"map".tr('a-z','c-zab')

will give 'ocr'.

Wednesday, December 27, 2006

Solving 'The Python Challenge' with Ruby
Level 0 - warming up

As I'm learning Ruby, the most efficient way is practice. Therefore, I try to solve the The Python Challenge using Ruby. Here is the level 0 - warming up . In the webpage, a picture is shown:




Obviously, solving the calculation can get the answer. So I execute the following command:

ruby -e "puts 2**38"

and I get:

274877906944

Therefore, the URL of the next level is here.

Thursday, December 21, 2006

Java SE 6 released!

Get the latest version here.

Wednesday, December 20, 2006

Build Ajax into your Web apps with Rails

Build Ajax into your Web apps with Rails is a practical example of how to use Ajax with Ruby on Rails provided by IBM developerWorks.

Ruby on Rails is an open-source Web Framework written in Ruby.

Wednesday, December 13, 2006

Using Ruby to post articles to Blogger

I'm learning Ruby and so I think I may write some Ruby code to post articles to Blogger.  After studying the Google Blogger API, I wrap the API into Ruby functions:

Authentication


Posting article to Blogger requires authentication using HTTPS POST request:

# Login to Google
# @email - your google email address
# @passwd - your login password
# returns true if login successful, otherwise false
# @auth holds the returned authentication keys

def login
h = Net::HTTP.new 'www.google.com', 443
h.use_ssl = true
resp, data = h.post '/accounts/ClientLogin',
"Email=#{@email}&Passwd=#{@passwd}&service=blogger",
{ 'Content-Type' => 'application/x-www-form-urlencoded' }
return false if resp.code != '200'
# save the authentication keys
@auth = {}
data.each do |s|
key, val = s.chomp.split('=')
@auth[key] = val
end
true
end


Posting


After successfully authenticated, you can now post articles:

# Post an article to Blogger:
# title - title of the article
# body - content of the article
# labels - array of labels for the article, e.g. [ 'Ruby', 'Programming' ]
# @userid - your Blogger ID

def post title, body, labels
headers = { 'Authorization' => "GoogleLogin auth=#{@auth['Auth']}",
'Content-Type' => 'application/atom+xml' }
data = "<?xml version='1.0'?>"
data << "<entry xmlns='http://www.w3.org/2005/Atom'>"
data << "<title type='text'>#{title}</title>"
data << "<content type='xhtml'>#{body}</content>"
labels.each do |lbl|
data << "<category scheme='http://www.blogger.com/atom/ns#' term='#{lbl}'></category>"
end
data << "</entry>"
h = Net::HTTP.new "#{@userid}.blogspot.com"
resp, data = h.post '/feeds/posts/default', data, headers
end

This function is very primitive and doesn't handle HTML tags inside the
body that may cause problem, but it shows the possibility of posting
content using Ruby.

JRuby 0.9.2 released!

JRuby 0.9.2 is released.  JRuby is an 100% pure-Java implementation of the excellent Ruby programming language.  The latest version can be downloaded here.

Below is an example using JRuby and SWT:


require 'java'
include_class %w(Display Shell Label).map {|clss| "org.eclipse.swt.widgets.#{clss}"}
include_class "org.eclipse.swt.layout.FillLayout"
include_class "org.eclipse.swt.SWT"

display = Display.new

shell = Shell.new display
shell.set_text "My First JRuby/SWT App"
shell.set_size 200, 200
shell.set_layout FillLayout.new
Label.new(shell,SWT::NONE).set_text "Hello, World!"

shell.open
while not shell.is_disposed
display.sleep unless display.read_and_dispatch
end

Saturday, December 09, 2006

As a programmer, I like to improve my programming technique and learn new things. About a year ago, I started to learn Python and I found a wonderful place Python Challenge to learn and practice Python. Python is an easy to learn scripting language, the syntax is simple and easy to understand. After several months of hard working, I finally solved the 33 riddles. I solved most of the riddles using Python. Do you want to try?