Last active
February 21, 2020 22:13
-
-
Save delbetu/14cd2a6f538412c3fcd892552dd64514 to your computer and use it in GitHub Desktop.
Zype Questions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a = [1,2,3,4,5] | |
b = [2,4,5,9,8,7] | |
result = [] | |
a.each do |ai| | |
result << b.find { |bj| bj == ai } | |
end | |
return result | |
Orden a.length * b.length |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Devolver el 3 item empezando desde atras | |
input = Node(1) -> Node(2) -> Node(3) -> Node(4) -> nil | |
# Solution using a stack | |
while input.next() do |node| | |
stack.push(node) | |
end | |
stack.pop() | |
stack.pop() | |
return stack.pop() | |
# with recursion | |
def rec(node) | |
next = node.next() | |
if next | |
position = rec(next) | |
if position < 3 | |
return position + 1 | |
else | |
return node.value # At this point I'm in the 3rd position starting from the tail | |
end | |
else | |
return 0 # last item start to count on 0 | |
end | |
end | |
##### THAT Recursion doesn't work but illustrates the idea ##### | |
##### The recursive solution would be this one ##### | |
def rec(node) | |
next_node = node.next() | |
if next_node | |
counter, result = rec(next_node) | |
if counter < 2 | |
return [counter+1, nil] | |
elsif counter == 2 | |
# At this point I'm in the 3rd position starting from the tail | |
return [counter+1, node.value] | |
else | |
return [counter+1, result] # continue counting and but keep forwarding the result back | |
end | |
else | |
# last item start to count in 1 | |
return [1, nil] | |
end | |
end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Improve the following code: | |
case item_type | |
when 'ENTITLED' | |
destroy_transaction('purchase') | |
when 'CONSUMABLE' | |
destroy_transaction('rental') | |
when 'SUBSCRIPTION' | |
destroy_subscription | |
end | |
# code replace conditional with polymorphism | |
def create_case_object(item_type) | |
# All returned objects comform to the same interface | |
case item_type | |
when 'ENTITLED' | |
EntiledItem.new() | |
when 'CONSUMABLE' | |
Consumable.new() | |
when 'SUBSCRIPTION' | |
Subscrioption.new() | |
end | |
end | |
item = create_case_object('ENTITLED') | |
item.destroy_transaction # no matter which object it returns I can call `destroy_transaction` over it | |
class Item | |
def destroy_transaction(param) | |
... | |
end | |
end | |
class EntiledItem < Item | |
def destroy_transaction(param) | |
super('purchase') | |
end | |
end | |
class ConsumableItem < Item | |
def destroy_transaction(param) | |
super('rental') | |
end | |
end | |
class Subscription < Item | |
def destroy_transaction(param) | |
# destroy_subscription behaviour | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment