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
class History: | |
def __init__(self, url, prev=None, next_node=None): | |
self.url = url | |
self.prev = prev | |
self.next = next_node | |
class BrowserHistory: | |
def __init__(self, homepage: str): | |
self.head = History(homepage) |
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
# recursive approach | |
# time = O(n) | |
# space = O(n) | |
def is_univalue_list(head, prev=None): | |
if not head: | |
return True | |
if head.val == prev or not prev: | |
return is_univalue_list(head.next, head.val) | |
else: |
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
def sum_numbers_recursive(numbers): | |
if not numbers: | |
return 0 | |
return numbers.pop() + sum_numbers_recursive(numbers) | |
sum_numbers_recursive([1, 2, 3]) | |
# complexity | |
# time = O(n) |
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
class Node: | |
def __init__(self, value, left=None, right=None): | |
self.value = value | |
self.left = left | |
self.right = right | |
def invert_binary_tree(root): | |
if root: | |
stack = [root] |
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
local function select(key) | |
return function(m) | |
return m[key] | |
end | |
end | |
local name = select "brand_name" { | |
year = 1992, | |
brand = "VW", | |
name = "Beatle", |
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
// Write a function even_length? that uses pattern matching only to return false | |
// if the list you pass it has an odd number of elements, true otherwise. | |
def even_length?([_, _ | t]) do | |
even_length?(t) | |
end | |
def even_length?([_ | []]) do | |
false | |
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
import importlib | |
# Dynamic input | |
my_class = "Ping" | |
my_module = "projeto.challenges.{dynamic_module}".format(dynamic_module=my_class.lower()) | |
# Dynamic import | |
module = importlib.import_module(my_module) | |
class_i_want = getattr(module, my_class) |