Posts Tagged ‘RPCFN

10
Dec
09

Ruby**Fun, part 1

This fourth challenge looks like a fun one. It’s the kind that is pretty simple, but you know a super elegant solution exists. I wouldn’t be surprised if there was a pretty simple one-liner for this. I’m excited to give this a try:

You just started working for CoolNewCompany which is developing mathematics related software. Since you are new to the team, your boss gives you an easy task to test your abilities. Write a class that pretty-prints polynomials, following some simple rules:

  • if a coefficient is 1, it doesn’t get printed
  • if a coefficient is negative, you have to display something like “- 2x^3″, not “+ -2x^3″
  • if a coefficient is 0, nothing gets added to the output
  • for x^1 the ^1 part gets omitted
  • x^0 == 1, so we don’t need to display it

Here’s a couple of usage examples:

puts Polynomial.new([-3,-4,1,0,6]) # => -3x^4-4x^3+x^2+6
puts Polynomial.new([1,0,2]) # => x^2+2

Don’t concern yourself too much with error handling, but if somebody tries to create a polynomial with less than 2 elements, your program has to raise an ArgumentError with the message “Need at least 2 coefficients.”

Please check the provided unit tests for more examples and make sure to use them for verifying your solution!

Requirements: This has to be a pure Ruby script, using only the Ruby Standard Libraries (meaning, no external Gems). You do not need to build a gem for this. Pure Ruby code is all that is needed.

This seems like a fun time to use RSpec so I’ll go ahead and make the first test:

it “should create a polynomial object”

This is simple, you just make a new class, run spec and it passes! Let’s make the next test do something that requires code:

it “should throw an ArgumentError if there are less than 2 elements in creation”

Again, this is straightforward, expect an argument with len > 2. This is where I really like Ruby’s syntax of

do this if this. It makes everything look super clean. Great, it passes two tests! On to the third:

it “should ‘puts’ the correct term for each value”

This is where it gets tricky, it’s so easy to just brute force it, but I’m trying to learn the proper “Rubyist” format.

It’s late though and I’m off to bed. Until next time!

14
Oct
09

Average Arrival Time For A Flight, part 1

To be honest this was a bit of a disappointment when I first read it, but upon starting the challenge I realized there are way more subtleties than I first gave it credit for. My initial thought was to take a base time as milliseconds then convert all inputs to milliseconds, find the difference, average and put it back into regular hours. I started reading Date classes and found a pretty easy way to do this.
After working on this for a while I realized that I would have to use more than just DateTime. As usual, it was pretty much in the instructions to use Time. Once I brought that class in, this problem becomes pretty trivial. I’ll post my solution once it ends, but I think it’s pretty good. It’s only 18 lines, any tips on how to make it more Ruby-like would be much appreciated.

27
Sep
09

Shift Subtitle, part 3

Back again for the third time in one day…clearly it’s been a lazy Sunday :]

I decided to implement a SubtitleTime class in order to parse the times for me. This is the class I came up with. There’s probably a cleaner way to split the initial string, but I think this works just fine:

http://charpcfn.pastebin.com/f410f2574

With that class implemented all that’s left is to instantiate the objects and then manipulate them, but wait, there’s a huge problem here. I’ve only accounted for addition to subtitle time. I haven’t even considered subtraction! That’s not good at all. This class is becoming rather verbose, but it’s working well so I’ll just keep going with it. It will get the job done.

Ok at first I had add_hour, add_min, add_sec etc as well as equivalent subtraction methods. But, this would make the main file loop pretty ugly and unreadable. However, the way I have it set up is really inefficient to continuously check the @modifier variable of the class. This will work, it’s just unnecessary steps.

SO after this is all implemented and vaguely tested and broken, my current code is this

http://charpcfn.pastebin.com/f1644cce3

What is wrong with this code:

It pretty much destroys DRY concepts, but I find that’s pretty unavoidable when you first start using a language. Hopefully it will be apparent as I progress in the Ruby language that I follow DRY more closely. This code is pretty verbose and not very well documented. But it’s really not doing anything terribly complicated. Also when you use the params -o add -t 01,001 it will add the milliseconds but not the seconds. I’d figure it out now, but I’m pretty tired.

Things I’ve learned this iteration: I like how easy Ruby classes are. For some reason I just get them more than Python classes. I like the attr_accessor and how that works, it’s pretty neat and useful. The initialize method makes perfect sense and the syntax isn’t terrible. I’d much prefer seeing @var than self.var all over the place.

Until tomorrow!

27
Sep
09

Shift Subtitle, part 2

I find myself with some more free time today so I’ll forge ahead with this challenge!

The first thing I’m going to do here is add functionality to the options and arguments and make sure they are being read in correctly. Here is my resulting code:

http://charpcfn.pastebin.com/f454f0012

Now that those are all set I can start working on file I/O. I have no idea how Ruby handles this, but it’s probably pretty easy. Time for more google!

As I expected File I/O is pretty easy. infile is going to be read in line by line and then as the times are updated each line will be written to the outfile. I’m thinking about making a class SubtitleTime and have that handle the conversion from HH:MM:SS,mmm to an object, but I’m gonna sit on that idea for a while before implementing it.

I’m spending a lot of my time looking up specific syntax here. I made sure the ternary operator was normal as well as looking up lambdas which, apparently, you have to do function.call in order for it to work…That one was a stumper. I also really like how Ruby does regular expressions. My first introduction to REs were in Perl so this is a great throwback seeing the =~ operator again.

Anyway, I’m finished programming for now, here is the code I ended up with:

http://charpcfn.pastebin.com/f1e1b9214

Overall I spent more time looking up syntax than I did library function calls but am definitely learning a lot about the Ruby language.

27
Sep
09

Shift Subtitle, part 1

RPCFN: Shift Subtitle (#1)

Outline: The goal for this problem is to create a command line program that will take a subtitle file, specifically an .srt file, and shift the timings either forwards or backwards by any amount of time. The example command line operation given is:

shift_subtitle --operation add --time 02,110 input_file output_file

My initial thoughts are not so much about how the algorithm will work, but on how to create a command line operation. I would normally just have a script do_something.rb and then just run it with the interpreter, ruby do_something.rb parameters.

Well, as it turns out, making a command line application in Ruby is pretty straightforward. All you really need to do is include the line #!/usr/bin/env ruby at the top of your script and change the permissions. I can’t seem to make it run properly without the ./ in front of it, but that seems like a minor thing that I’ll figure out later. I’d really like to start with the code.

Since this problem is pretty well presented I know what the options are going to be so I’ll start by looking at how Ruby handles arguments and options first since I’d like to get that out of the way. It looks like I’ll be needing the ‘optparse’ class from the Ruby standard library.

This is the initial code I just wrote up (essentially copy/paste) to parse options and flags. I’ll be using charpcfn.pastebin.com for larger snippets of code, but eventually I’d like to integrate them into the blog.. http://charpcfn.pastebin.com/d3b3fe761

So it looks like I’ll be doing this in parts. I feel like this is a good start to the program, get the basic stuff out of the way and think about the algorithm for a while. I’ll probably be back soon, learning a new language is pretty fun.

edit: One should fully read the problem before starting. ‘optparse’ was mentioned, and in fact required for the actual problem, and I didn’t even realize. Always read the whole problem before starting :]

27
Sep
09

hello there internet!

The other day I found out about an interesting contest of sorts called RPCFN, which stands for Ruby Programming Challenge For Newbies. I’ve been interested in Ruby for a while, but currently have a job that is primarily using Python. I thought this challenge might be a fun way for me to learn Ruby and figured I might as well document my thought process while working through these problems. I’m not sure how this is going to work. I might post one entry per challenge, there is one challenge every two weeks, or I might post multiple entries per challenge.

So I hope this works out and I hope you enjoy reading through what I’m thinking, and I encourage all to respond and tell me how terrible (or awesome) my code is with ways or links that would help me improve.

edit: I should probably note that I’ve followed a couple of tutorials on Ruby before and while I didn’t memorize a whole lot of it, I am familiar with the Ruby syntax. This blog is more about learning the standard library and the idioms of Ruby than the syntax of the language.




Challenges

How often I post

July 2017
M T W T F S S
« Dec    
 12
3456789
10111213141516
17181920212223
24252627282930
31  

This is everything