Everybody else is doing it - Ruby Code Golf

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 4325 Views - Last Post: 04 February 2011 - 07:52 PM Rate Topic: -----

#1 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 894
  • View blog
  • Posts: 3,153
  • Joined: 12-May 09

Everybody else is doing it - Ruby Code Golf

Post icon  Posted 03 February 2011 - 11:04 AM

We've seen contests in wordy languages like C# and Java - Perl is known for being able to express programs in tiny, hard to read lines while Ruby provides a great level of succinctness without too many words.

The goal of the competition is to solve the challenge presented in as few characters as possible - which somewhat indirectly involves using a smaller number of commands and method calls. Whitespace is ignored in the count, so you can keep the program readable - you just want to use the fewest characters to create your solution.

The way I judge the length is to count any character that does not match the whitespace regular expression. The sum is your score, which you want to keep as low as possible. I also ignore comments.

First Challenge
However, we're going to get more meta than that - the first challenge is to write a script that counts the number of non-whitespace characters in a text file in the fewest non-whitespace characters and outputs it to the command line. The script must be run from the command line with the filename as its argument.

I would establish par for this challenge to be around 60 characters.

If you want to get really fancy, you can make it ignore comments also, but the requirement for the challenge is to assume all characters are significant.

I did the basic requirement in 36 - I'll post the source when we get a couple more responses.

Edit: I ignored single line comments in 67 characters. Let's see what you've got!

Edit: Updated numbers after I realized I was being redundant.

Updated the rules.

This post has been edited by xclite: 03 February 2011 - 02:17 PM


Is This A Good Question/Topic? 3
  • +

Replies To: Everybody else is doing it - Ruby Code Golf

#2 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1252
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Everybody else is doing it - Ruby Code Golf

Posted 03 February 2011 - 12:28 PM

Want to participate in the code golf but don't want to install the tools? You can use CodePad.org to code and run your entry.

Try it out!

http://www.codepad.org

Here's my entry from someone who has never coded in Ruby. I'm sure there are better more Rubyesque ways to pull this off. :P

http://codepad.org/bqra1XDW

a = "This is my example text. Also, Ruby is sorta cool. Sorta."
c = 0
a.split("").each do |i| 
    if i != " "
        c = c + 1
    end
end
puts c

This post has been edited by Sergio Tapia: 03 February 2011 - 12:35 PM

Was This Post Helpful? 1
  • +
  • -

#3 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 894
  • View blog
  • Posts: 3,153
  • Joined: 12-May 09

Re: Everybody else is doing it - Ruby Code Golf

Posted 03 February 2011 - 12:56 PM

View PostSergio Tapia, on 03 February 2011 - 04:28 PM, said:

Want to participate in the code golf but don't want to install the tools? You can use CodePad.org to code and run your entry.

Try it out!

http://www.codepad.org

Here's my entry from someone who has never coded in Ruby. I'm sure there are better more Rubyesque ways to pull this off. :P

http://codepad.org/bqra1XDW

a = "This is my example text. Also, Ruby is sorta cool. Sorta."
c = 0
a.split("").each do |i| 
    if i != " "
        c = c + 1
    end
end
puts c


A testament to how easy Ruby is =p One thing to note is that your program wouldn't ignore white space or tabs correctly (and that it doesn't read a file from the arguments).

file_name = ARGV[0]


to get the name from arguments
File.open(file_name)


Returns a file object.
Was This Post Helpful? 0
  • +
  • -

#4 diego_pmc  Icon User is offline

  • D.I.C Addict

Reputation: 81
  • View blog
  • Posts: 565
  • Joined: 13-May 09

Re: Everybody else is doing it - Ruby Code Golf

Posted 03 February 2011 - 01:52 PM

I can write any program you want in no more than one line of code. Just let me use a language that uses semi-colons to separate instructions and doesn't need tabs to determine where each block of code begins and ends. :P

On-topic: Nice challenge. I know neither Perl nor Ruby though, so I won't be participating. :(

This post has been edited by diego_pmc: 03 February 2011 - 01:53 PM

Was This Post Helpful? 0
  • +
  • -

#5 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 894
  • View blog
  • Posts: 3,153
  • Joined: 12-May 09

Re: Everybody else is doing it - Ruby Code Golf

Posted 03 February 2011 - 02:03 PM

It doesn't necessarily have to be one line of code - the goal is to minimize the number of nonwhitespace, not the number of lines. If you did one command on each line you could still beat my solution as long as your command character count was lower than mine.
Was This Post Helpful? 0
  • +
  • -

#6 5thWall  Icon User is offline

  • Occasional Member

Reputation: 31
  • View blog
  • Posts: 530
  • Joined: 17-September 08

Re: Everybody else is doing it - Ruby Code Golf

Posted 03 February 2011 - 02:09 PM

Here's my entry:
Spoiler

This post has been edited by 5thWall: 03 February 2011 - 03:14 PM

Was This Post Helpful? 1
  • +
  • -

#7 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 894
  • View blog
  • Posts: 3,153
  • Joined: 12-May 09

Re: Everybody else is doing it - Ruby Code Golf

Posted 03 February 2011 - 02:13 PM

Nice! Using some hints from yours I was able to drop mine. Here's what I've got:
Spoiler

This post has been edited by xclite: 03 February 2011 - 02:18 PM

Was This Post Helpful? 0
  • +
  • -

#8 T3hC13h  Icon User is offline

  • D.I.C Regular

Reputation: 65
  • View blog
  • Posts: 337
  • Joined: 05-February 08

Re: Everybody else is doing it - Ruby Code Golf

Posted 03 February 2011 - 03:01 PM

Picked up for the shorthand p and $* from xclite and came up with this.

32 char :)

Spoiler

This post has been edited by T3hC13h: 03 February 2011 - 03:02 PM

Was This Post Helpful? 1
  • +
  • -

#9 dorknexus  Icon User is offline

  • or something bad...real bad.
  • member icon

Reputation: 1255
  • View blog
  • Posts: 4,618
  • Joined: 02-May 04

Re: Everybody else is doing it - Ruby Code Golf

Posted 03 February 2011 - 03:05 PM

With some shorthand stolen from previous posts.

28!

Spoiler

This post has been edited by Dark_Nexus: 03 February 2011 - 03:07 PM

Was This Post Helpful? 4
  • +
  • -

#10 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 894
  • View blog
  • Posts: 3,153
  • Joined: 12-May 09

Re: Everybody else is doing it - Ruby Code Golf

Posted 03 February 2011 - 03:30 PM

@ Nice, I didn't realize that count accepted character classes.
Was This Post Helpful? 0
  • +
  • -

#11 dorknexus  Icon User is offline

  • or something bad...real bad.
  • member icon

Reputation: 1255
  • View blog
  • Posts: 4,618
  • Joined: 02-May 04

Re: Everybody else is doing it - Ruby Code Golf

Posted 03 February 2011 - 03:40 PM

I wasn't sure if it did or not but I figured since it accepted negation it might also take character classes. I don't know of any escape characters like \s so hopefully that means whitespace.
Was This Post Helpful? 0
  • +
  • -

#12 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 894
  • View blog
  • Posts: 3,153
  • Joined: 12-May 09

Re: Everybody else is doing it - Ruby Code Golf

Posted 03 February 2011 - 03:41 PM

I tried to do \S which is the negation of \s in regular expressions, but no such luck.
Was This Post Helpful? 0
  • +
  • -

#13 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 441
  • View blog
  • Posts: 1,488
  • Joined: 28-April 09

Re: Everybody else is doing it - Ruby Code Golf

Posted 03 February 2011 - 03:44 PM

Spoiler


32, but mine just reads a string.

Edit: Looked at a few of the others, I had no idea how easy it was to read a file in ruby

This post has been edited by Nakor: 03 February 2011 - 03:55 PM

Was This Post Helpful? 0
  • +
  • -

#14 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 894
  • View blog
  • Posts: 3,153
  • Joined: 12-May 09

Re: Everybody else is doing it - Ruby Code Golf

Posted 03 February 2011 - 04:29 PM

Ruby doesn't get in your way very much. It's not like reading a file in java =p
Was This Post Helpful? 0
  • +
  • -

#15 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: Everybody else is doing it - Ruby Code Golf

Posted 03 February 2011 - 04:32 PM

View Postxclite, on 03 February 2011 - 06:29 PM, said:

Ruby doesn't get in your way very much. It's not like reading a file in java =p


Uggh...I know...
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2