Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

My enumerable #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 93 additions & 16 deletions week03/3-My-Enumerable/solution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,66 +10,143 @@ def map
end

def filter
# Your code goes here
Array.new.tap do |arr|
each do |element|
arr << element if (yield element) == true
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Няма нужда от сравнението == true

В Ruby всичко е true, освен fasle, и nil

end
end
end

def reject
# Your code goes here
Array.new.tap do |arr|
each do |element|
arr << element if (yield element) == false
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if not == unless + същият коментар като за filter

end
end
end

def reduce(initial = nil)
# Your code goes here
each do |element|
if initial.nil?
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Защо не изнесеш тази проверка извън each-a?

initial = element
else
initial = yield element, initial
end
end
initial
end

def any?
# Your code goes here
each do |element|
if (yield element) == false
false
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Този false на кого го връщаш?

else
return true
end
end
end

def all?
# Your code goes here
each do |element|
if (yield element) == false
return false
else
true
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Това true на кого го връщаш?

end
end
end

def include?(element)
# Your code goes here
each do |item|
if item == element
return true
else
false
end
end
end

def count(element = nil)
# Your code goes here
counter = 0
each do |item|
counter += 1 if element.nil?
counter += 1 if item == element
end
counter
end

def size
# Your code goes here
counter = 0
each do
counter += 1
end
counter
end

def min
# Your code goes here.
reduce do |element, accumulator|
accumulator > element ? element : accumulator
end
end

def min_by
# Your code goes here.
reduce do |element, accumulator|
(yield accumulator) > element.size ? element : accumulator
end
end

def max
# Your code goes here.
reduce do |element, accumulator|
accumulator < element ? element : accumulator
end
end

def max_by
# Your code goes here.
reduce do |element, accumulator|
(yield accumulator) < element.size ? element : accumulator
end
end

def take(n)
# Your code goes here.
counter = 0
Array.new.tap do |arr|
each do |element|
return Array.new if n == 0
arr << element
counter += 1
break if counter == n
end
end
end

def take_while
# Your code goes here.
Array.new.tap do |arr|
each do |element|
if (yield element) == true
arr << element
else
break
end
end
return [] if arr.size == 0
end
end

def drop(n)
# Your code goes here.
counter = 0
Array.new.tap do |arr|
each do |element|
arr << element if counter >= n
counter += 1
end
end
end

def drop_while
# Your code goes here.
counter = 0
each do |element|
counter += 1 if (yield element) == true
return drop(counter) if (yield element) == false
end
end
end