Skip to content

Instantly share code, notes, and snippets.

View mauricioabreu's full-sized avatar
🇧🇷
always learning

Mauricio Antunes mauricioabreu

🇧🇷
always learning
View GitHub Profile
@mauricioabreu
mauricioabreu / browser_history.py
Created June 9, 2024 21:32
Browser History, leetcode solution
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)
# 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:
@mauricioabreu
mauricioabreu / sum_numbers_recursive.py
Last active May 19, 2024 12:03
Sum numbers recursive
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)
@mauricioabreu
mauricioabreu / invert_binary_tree.py
Last active November 3, 2023 18:14
Invert binary tree
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]
@mauricioabreu
mauricioabreu / select.lua
Created February 24, 2022 11:01
Lua DSL for select func without parentheses
local function select(key)
return function(m)
return m[key]
end
end
local name = select "brand_name" {
year = 1992,
brand = "VW",
name = "Beatle",
@mauricioabreu
mauricioabreu / even_length.ex
Created July 25, 2018 03:31
Using Elixir pattern matching to check if a list has an event length of elements
// 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
@mauricioabreu
mauricioabreu / import_lib_example.py
Last active July 25, 2018 14:12
importlib module example
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)