// you’re reading...

Contests

DWITE 2006 – now with Ruby

[DWITE]

A quick, but exciting update on the DWITE programming contest – it now supports Ruby! Running the current stable 1.8.5 version, it should make the contest much more interesting.

Ruby has a very neat syntax, making it easy and enjoyable to write. Code redability is vital, and since DWITE is a team competition, it is important for team-mates to read each other’s code with ease.

Ruby is also a very powerful Object Oriented language. It is excellent for data analyzing, processing and manipulations, with many methods build in and readily available, making it an excellent choice for a number of common DWITE problems. Mr. S. has asked for some sample solutions to test out the judge system, so here’s a very typical DWITE entry problem to consider:

Write a program that will calculate the square of a number rounded to two decimal places.

The input file, DATA11.txt, will contain 5 numbers, on separate lines, each between -32768 and 32767.
For Example:
0
23.45
-1234
-32760.789
999

And a solution:

[ruby]
outFile = File.new(”OUT11.txt”,”w”)
File.new(”DATA11.txt”,”r”).each_line{|num|
outFile < < sprintf("%0.2f", num.to_f*num.to_f) << "\n"
}
outFile.close
[/ruby]

Input/Output File handling is phenominal. In the previous contest a team has wrote a working solution to one of the problems in C, but due to a messup with inclusion of header files and file access flags, the solution was not accepted. But with Ruby, there is no need to consern with flags, counting for loops, pesky EOF conditions, or other details.

[ruby]
File.new(”DATA.txt”,”r”).each_line{|input| input.do}
[/ruby]

All of the file handling has been reduced to a clean, readable, single line of code. How is this not amazing?

The next DWITE contest is on Tuesday, November 28, 2006 3:00 pm – 6:00 pm – Eastern Time and the registrations are open. You’ve got nearly a month, so if you haven’t already – download Ruby, check out tutorials, ask lots of questions, and enjoy the contest. The next round should be very interesting.

– Tony

Read more

Discussion

  1. Posted by wtd | November 3, 2006, 2:46 pm

    Your solution can be made much cleaner:

    File.new(”OUT11.txt”, “w”) do |out|
    IO.foreach(”DATA11.txt”) do |line|
    out.printf “%0.2f\n”, num.to_f ** 2
    end
    end

    Reply to comment

Post a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>