Tuesday, April 24, 2012

1: Programming Review

Project description.
Concerns
  • line 21: I'm not sure if this is the best way to check the command line inputs and save them.
  • line 27: I feel like there should be a way to combine this with line 24? like or die in perl 
  • I can't believe the there aren't any Math.sum Math.average or Math.median functions but I do like the way you can easily insert methods into a core class ( line 1 - 11 )
  • line 31 - 36: I swear I've heard of an cleaner way to parse in a file, but my solution works 
  • line 40: I'm not really sure what ensure does
  • line 2: inject(:+) ??? what the crap? this is cool, but not intuitive at all. Someone please explain
  • I could make it a little more object oriented by making a print result method ... but meh
Let me know what you think. 

# Add sum, mean, & median to Array class
class Array; def sum; inject(:+) end end
class Array; def mean; sum.to_f / size end end
class Array
def median(arry)
sorted = arry.sort
medpt = arry.length / 2
medpt1 = ( ( arry.length - 1 ).to_f / 2 )
(sorted[medpt] + sorted[medpt1]).to_f / 2
end
end
def empty
puts "Sum: 0.0"
puts "Mean: 0.0"
puts "Median 0.0"
exit
end
begin
# Get file from cmd :: Default file is input1.txt
if ARGV[0] != nil; input = ARGV[0] else input = "input1.txt" end
#Read in a file
file = File.new(input, 'r')
# check file size
if File.size(input) <= 0; empty end
#Parse file
nums = Array.new
while (line = file.gets)
num = line.split(" ")
num.length.times do |n|
nums.push(Float(num[n]))
end
end
rescue Errno::ENOENT
puts "The file was not found: #{$!}"
exit
ensure
file.close if file
end
#Print out sum,mean & median
puts "Sum: #{sprintf("%.2f", nums.sum)}"
puts "Mean: #{sprintf("%.2f",nums.mean)}"
puts "Median: #{sprintf("%.2f",nums.median(nums) )}"
view raw Review.rb hosted with ❤ by GitHub

No comments:

Post a Comment