Friday 6 June 2014

Friday Challenge - ZOMBIES!

Today at Metis is a friday, and fridays are basically 'do whatever you want days'.

For me, this is fixing up the blog, and doing a few of our programming challenges. We also had a talk by Ben Orenstein about the things we as budding programmers should consider for our future. Very informative, very helpful!

So: the programming challenges vary in difficulty. Some are fairly easy (such as summing up all fibonacci numbers up to 4 million):


class Fibonacci
 def initialize
  @num_interim = 0
  @total_sum = 0
  @num_1 = 1
  @num_2 = 2
 end

 def fib_sequence(limit)

  until @num_interim >= limit
   @num_interim = @num_1 + @num_2
   @num_1 = @num_2
   @num_2 = @num_interim
   seq_check
  end
  puts @total_sum
 end
 
 def seq_check
  if @num_interim % 2 == 0
   @total_sum += @num_interim
  end
 end
end 

wombat = Fibonacci.new
wombat.fib_sequence(4000000) 

But even with how I managed to get what I needed, I'm positive there's better ways to write that code. That's part of what we're taught at Metis - the importance of refactoring and keeping code clean and easy to read.

That said, the real difficulty comes in the form of this particular challenge. In short, it's trying to make sense of what seems like plain gibberish (zombie speech, according to the scenario), by loading up various scenarios that contain a dictionary and looking up zombie garble to check if the words are a match, then suggest if that was what the zombie was implying. We had an input file, with a few numbers, then a list of words, then a number, then a list of words, and so on for the rest of the number of scenarios. The numbers either described the number of scenarios, the number of the words that would come after the number, and the number of zombie garble that would come after it. It's a bit of a confusing file to read at the start.

Here's a snippet of what was 4 hours of work:


 def read_file
  File.open("./zombies.in", "r") do |file|
   #get number of scenarios
   @input_file = file.readlines
   set_scenario_number
   until @current_scenario == @scenario
    if @word_number == 0 
     set_number_words
    elsif @garble_number == 0
     set_number_garble
    else
     next_scenario
    end
    puts "Currentline is now #{@currentline}"
   end
  end
 end

It had a LOT of forms before it came to this. All this ends up doing is loading up words in each scenario, shoving it into specific variables in a specified order (The .zombies.in file is written in such a way that this is necessary, part of the challenge) so I had to create a loop to make sure I got every word in the dictionary and every zombie garble into a list for each scenario before it moves on to the next one.

It hasn't done any of the features it needs to after the fact, but for now it was VERY satisfying to see it run after awhile. A few things that I learned:

The .readlines command - incredibly helpful because it turns your entire file into an array of strings. Now, the array had some strings that contained some numbers, which posed issues unless I knew exactly when the numbers would come (so I can safely predict and use .to_i - which I did!). You can't call .times on these strings without calling .to_i. And initially I used .is_a? Integer to check what type of element the current line was reading, but when the loop failed, that's when I found out .readlines returns an array of strings.

So, in short, integers are really wonky and hard to work with when they're already strings, so you have to be very creative. X.x

Here's to hoping tomorrow I can finish the rest of the program!

No comments:

Post a Comment