Skip to content

Commit

Permalink
Update session4 solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshCheek committed Dec 8, 2014
1 parent 99a96c0 commit cf1dc39
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 47 deletions.
2 changes: 1 addition & 1 deletion session4/solved/1.rb
Expand Up @@ -4,7 +4,7 @@

class Stack
def inspect
current = @head
current = @head
to_return = "("
while current
to_return << current.data.inspect
Expand Down
8 changes: 4 additions & 4 deletions session4/solved/3.rb
@@ -1,6 +1,6 @@
def passthrough( enumerable , to_pass )
enumerable.each do |element|
to_pass = yield to_pass , element
def passthrough(enumerable, to_pass, &block)
enumerable.each do |element|
to_pass = block.call(to_pass, element)
end
to_pass
end
end
19 changes: 2 additions & 17 deletions session4/solved/5.rb
@@ -1,21 +1,6 @@
# you will be given a filen name
# inside the file will be a series of numbers
# find the largest number on each line
# return their sum
#
# EXAMPLE
# file: nums.txt
# 406 217 799 116 45 651 808 780
# 205 919 474 567 712
# 776 170 681 86 822 9 100 540 812
# 602 117 181 169 876 336
# 434 165 725 187 974 48
#
# line_sums('nums.txt') # => 808 + 919 + 822 + 876 + 974 # => 4399

def line_sums(filename)
File.readlines(filename).inject 0 do |sum,line|
File.readlines(filename).inject 0 do |sum, line|
numbers = line.split.map { |number| number.to_i }
sum + numbers.max
end
end
end
21 changes: 6 additions & 15 deletions session4/solved/6.rb
@@ -1,20 +1,11 @@
class ApplicationController


class ApplicationController
def body_class

unless @body_class
@body_class = String.new
class << @body_class
def <<(str)
concat ' ' unless length.zero?
concat str
end
end
return @body_class if @body_class
@body_class = String.new
def @body_class.<<(str)
concat ' ' unless length.zero?
concat str
end

@body_class
end


end
15 changes: 5 additions & 10 deletions session4/solved/8.rb
Expand Up @@ -2,15 +2,10 @@ class RubyKickstartException < Exception
end

def exception_raiser(num)
if num == 1
raise "No 1s allowed!"
elsif num == 2
raise ArgumentError.new("No 2s allowed!")
elsif num == 3
raise Exception.new("No 3s allowed!")
elsif num == 4
raise SyntaxError.new("No 4s allowed!")
elsif num == 5
raise RubyKickstartException.new("No 5s allowed!")
if num == 1 then raise "No 1s allowed!"
elsif num == 2 then raise ArgumentError, "No 2s allowed!"
elsif num == 3 then raise Exception, "No 3s allowed!"
elsif num == 4 then raise SyntaxError, "No 4s allowed!"
elsif num == 5 then raise RubyKickstartException, "No 5s allowed!"
end
end

0 comments on commit cf1dc39

Please sign in to comment.