MustModify did a great review MustModify did a great review of your code as a whole. I won't retread all that but rather look at the overall cleanliness and style of the code.
MustModify did a great review of your code as a whole. I won't retread all that but rather look at the overall cleanliness and style of the code.
MustModify did a great review of your code as a whole. I won't retread all that but rather look at the overall cleanliness and style of the code.
Your indentation is all over the place, making it really hard to read at times. Ruby convention is to use 2 spaces of indentation. Spaces; not tabs.
You could do with more horizontal whitespace, again for readability. I.e.
x = 2
notx=2
, andx == foo
notx==foo
. The whitespace you do have is also all over the place.return
is implicit at a method's exit point. You only need explicitreturn
s if you're returning early. And for early returns you have post-fixed contionals, such asreturn 0 if d.zero?
(notice also the use of#zero?
instead of== 0
, but read on why you shouldn't do that either)Don't use zero as a NULL value. Ruby has
nil
and theObject#nil?
check method.nil
is also false'y, so in many cases you can skip the explicitnil?
check. So yourmin
andmax
variables should be initialized tonil
- not integer zero.Don't use "magic" strings when you have
Symbol
s. I.e."max"
and"min"
should rather be:max
and:min
Don't do assignments in conditions (e.g.
if (x = foo()) == bar
; there's really no reason to, and it makes it harder to follow what's happening. Compact code ≠ faster code.Don't abbreviate variables (yes, I'm referring to the
Node
ivars in particular). Again, hard to read and follow. Just call 'emleft
,right
, andvalue
.Don't specify an empty block for
Struct.new
- it's optional, so leave it out.
def insert(value, type, node = nil)
return Node.new(value) if node.nil? # struct ivars are auto-initialized to nil
# could also say:
# return Node.new(value) unless node
if (type == :max && value > node.value) || (type == :min && value < node.value)
tmp = node.value
node.value = value
node.left = insert(tmp, type, node.left)
else
node.left = insert(value, type, node.left)
end
node
end
Your indentation is all over the place, making it really hard to read at times. Ruby convention is to use 2 spaces of indentation. Spaces; not tabs.
You could do with more horizontal whitespace, again for readability. I.e.
x = 2
notx=2
, andx == foo
notx==foo
. The whitespace you do have is also all over the place.return
is implicit at a method's exit point. You only need explicitreturn
s if you're returning early. And for early returns you have post-fixed contionals, such asreturn 0 if d.zero?
(notice also the use of#zero?
instead of== 0
, but read on why you shouldn't do that either)Don't use zero as a NULL value. Ruby has
nil
and theObject#nil?
check method. So yourmin
andmax
variables should be initialized tonil
- not integer zero.Don't use "magic" strings when you have
Symbol
s. I.e."max"
and"min"
should rather be:max
and:min
Don't do assignments in conditions (e.g.
if (x = foo()) == bar
; there's really no reason to, and it makes it harder to follow what's happening. Compact code ≠ faster code.Don't abbreviate variables (yes, I'm referring to the
Node
ivars in particular). Again, hard to read and follow. Just call 'emleft
,right
, andvalue
.Don't specify an empty block for
Struct.new
- it's optional, so leave it out.
def insert(value, type, node = nil)
return Node.new(value) if node.nil? # struct ivars are auto-initialized to nil
if (type == :max && value > node.value) || (type == :min && value < node.value)
tmp = node.value
node.value = value
node.left = insert(tmp, type, node.left)
else
node.left = insert(value, type, node.left)
end
node
end
Your indentation is all over the place, making it really hard to read at times. Ruby convention is to use 2 spaces of indentation. Spaces; not tabs.
You could do with more horizontal whitespace, again for readability. I.e.
x = 2
notx=2
, andx == foo
notx==foo
. The whitespace you do have is also all over the place.return
is implicit at a method's exit point. You only need explicitreturn
s if you're returning early. And for early returns you have post-fixed contionals, such asreturn 0 if d.zero?
(notice also the use of#zero?
instead of== 0
, but read on why you shouldn't do that either)Don't use zero as a NULL value. Ruby has
nil
and theObject#nil?
check method.nil
is also false'y, so in many cases you can skip the explicitnil?
check. So yourmin
andmax
variables should be initialized tonil
- not integer zero.Don't use "magic" strings when you have
Symbol
s. I.e."max"
and"min"
should rather be:max
and:min
Don't do assignments in conditions (e.g.
if (x = foo()) == bar
; there's really no reason to, and it makes it harder to follow what's happening. Compact code ≠ faster code.Don't abbreviate variables (yes, I'm referring to the
Node
ivars in particular). Again, hard to read and follow. Just call 'emleft
,right
, andvalue
.Don't specify an empty block for
Struct.new
- it's optional, so leave it out.
def insert(value, type, node = nil)
return Node.new(value) if node.nil? # struct ivars are auto-initialized to nil
# could also say:
# return Node.new(value) unless node
if (type == :max && value > node.value) || (type == :min && value < node.value)
tmp = node.value
node.value = value
node.left = insert(tmp, type, node.left)
else
node.left = insert(value, type, node.left)
end
node
end
MustModify did a great review of your code as a whole. I won't retread all that but rather look at the overall cleanliness and style of the code.
Your indentation is all over the place, making it really hard to read at times. Ruby convention is to use 2 spaces of indentation. Spaces; not tabs.
You could do with more horizontal whitespace, again for readability. I.e.
x = 2
notx=2
, andx == foo
notx==foo
. The whitespace you do have is also all over the place.return
is implicit at a method's exit point. You only need explicitreturn
s if you're returning early. And for early returns you have post-fixed contionals, such asreturn 0 if d.zero?
(notice also the use of#zero?
instead of== 0
, but read on why you shouldn't do that either)Don't use zero as a NULL value. Ruby has
nil
and theObject#nil?
check method. So yourmin
andmax
variables should be initialized tonil
- not integer zero.Don't use "magic" strings when you have
Symbol
s. I.e."max"
and"min"
should rather be:max
and:min
Don't do assignments in conditions (e.g.
if (x = foo()) == bar
; there's really no reason to, and it makes it harder to follow what's happening. Compact code ≠ faster code.Don't abbreviate variables (yes, I'm referring to the
Node
ivars in particular). Again, hard to read and follow. Just call 'emleft
,right
, andvalue
.Don't specify an empty block for
Struct.new
- it's optional, so leave it out.
And finally, you just have a lot of duplication. For instance, insert
does the same thing in both its main branches apart for one byte: >
vs <
. And the story's the same for #delete
: The only difference between the main branches is >
vs <
- the rest of the code is identical and pure duplication.
Really basic refactoring of #insert
might be:
def insert(value, type, node = nil)
return Node.new(value) if node.nil? # struct ivars are auto-initialized to nil
if (type == :max && value > node.value) || (type == :min && value < node.value)
tmp = node.value
node.value = value
node.left = insert(tmp, type, node.left)
else
node.left = insert(value, type, node.left)
end
node
end
Since the count is given as the first number, another way to calculate the mean (not the median) without storing the entire input would be:
mean = 0
count = gets.chomp.to_i
count.times do
mean += gets.chomp.to_i
end
puts mean.fdiv(count)