Skip to content

Instantly share code, notes, and snippets.

View RafaelBroseghini's full-sized avatar
🥋

Rafael Broseghini RafaelBroseghini

🥋
View GitHub Profile
matrix = [
[1,2,3],
[4,5,6]
]
def transpose(matrix: list) -> list:
if len(matrix) == 0:
return []
rows, cols = len(matrix), len(matrix[0])
new_matrix = [[None] * rows for i in range(cols)]
@RafaelBroseghini
RafaelBroseghini / reverse_integer.py
Last active February 13, 2019 16:09
Reverse an integer without casting as a string
def reverse_int(n: int) -> int:
num, reverse_num, size_of_num = n, 0, -1
copy = num
while copy >= 1:
copy //= 10
size_of_num += 1
while num >= 1:
@RafaelBroseghini
RafaelBroseghini / asterik-pyramids.py
Created December 15, 2018 18:03
Draw Asteriks Pyramids in any direction
"""
This short script allows to draw a asteriks pyramid
in any direction desired. 'Left' is the default direction.
A 'middle' pyramid has n steps with (n*2-1) asteriks.
"""
def draw_pyramid(steps: int, direction: str = "left") -> None:
possible_directions = set(["left", "right", "middle"])
@RafaelBroseghini
RafaelBroseghini / udpate-upgrade-clean-remove.sh
Last active November 26, 2018 03:32
Update, Upgrade, Clean, Remove
#!/bin/bash
sudo apt update
sudo apt upgrade
sudo apt autoremove
sudo apt autoclean
@RafaelBroseghini
RafaelBroseghini / pull-from-all-repos.sh
Created November 26, 2018 03:09
Pull from all your git repos (run from home)
#!/bin/bash
for d in ./*/
do (cd "$d" &&
if [[ -d .git ]]; then
echo "Pulling from $d"
git pull
fi;
)
done;
@RafaelBroseghini
RafaelBroseghini / path-between-employees.py
Created November 26, 2018 02:00
Find the path between an employee to its highest manager
"""
Given a source and a destination, let us
find the path (if existing) between the two.
"""
company = {
"Bill":"Ada",
"Monty":"Alan",
"Ada":"Bob",
"John":"Linus",
@RafaelBroseghini
RafaelBroseghini / queue-as-two-stacks.py
Created November 21, 2018 04:52
Implement a Queue using Two Stacks
"""
Stack and Queue being implemented with Python List.
Both structures follow the design below:
Bottom -> [...] -> Top.
"""
def dequeue(array: list) -> int:
return array.pop()
@RafaelBroseghini
RafaelBroseghini / find-middle-spot-in-matrix.py
Created November 21, 2018 04:35
Find the exact middle of a given matrix
"""
This program will find the element at the exact
middle of any columns (N) x rows (Y < N) matrix where
the number of columns and rows are odd.
# xxxxx | # xxxxx | # xxxx | # xxx
# xx(x)xx | # xxxxx | # xxxx | # x(x)x
# xxxxx | # No middle | # xxxx # No middle | # xxx
"""
@RafaelBroseghini
RafaelBroseghini / binary-search-tree-find-successor.py
Created November 20, 2018 00:33
Find the successor to a node in a Binary Search Tree before deleting from the tree
"""
Assume we have defined a BST class:
class BST(object):
class __Node(object):
def __init__(self, data, right=None, left=None):
self.data = data
self.right = right
self.left = left
def __init__(self, root=None):
@RafaelBroseghini
RafaelBroseghini / add-to-ordered-linked-list-recursively.py
Created November 19, 2018 22:58
Adding a node to a Ordered Linked List, recursively
import sys
class LinkedList(object):
class __Node(object):
def __init__(self, val, next=None):
self.val = val
self.next = next
def setNext(self, newNext):
self.next = newNext