Skip to content

Instantly share code, notes, and snippets.

@jonathanagustin
jonathanagustin / topKFrequent1.py
Last active April 29, 2020 10:15
Top K Frequent Words - leetcode
"""
https://leetcode.com/problems/top-k-frequent-words/
Given a non-empty list of words, return the k most frequent elements.
Your answer should be sorted by frequency from highest to lowest.
If two words have the same frequency, then the word with the
lower alphabetical order comes first.
collections.Counter([iterable-or-mapping]) counts hashable objects
@jonathanagustin
jonathanagustin / sort.py
Last active April 29, 2020 10:41
Example python sort with lambda function that determines what to do if there is a tie
"""
https://py.checkio.org/forum/post/9298/sorted-function-explanation-please/
This will sort a list of tuples by strings (second tuple item)
and then by number in reversed order when strings are equal.
"""
list_of_tuples = [(1, 'a'), (1, 'b'), (2, 'a'), (2, 'b')]
list_of_tuples.sort(key=lambda t:(t[1], -t[0]))
print(list_of_tuples)
@jonathanagustin
jonathanagustin / sort2Lists.py
Created April 29, 2020 07:14
Python parallel sorting of lists
# Parallel sorting of lists
data = zip(list1, list2)
data.sort()
list1, list2 = map(lambda t: list(t), zip(*data))
@jonathanagustin
jonathanagustin / inOrder.py
Created April 29, 2020 06:36
Inorder Traversal - geeks4geeks
"""
https://practice.geeksforgeeks.org/problems/inorder-traversal/1
"""
def InOrder(root):
if root is None:
return
InOrder(root.left)

Docker

Lessons Learned

/root/compile: line 2: $'\r': command not found

The above means you need to dos2unix the file.

Useful commands

@jonathanagustin
jonathanagustin / height.py
Last active April 29, 2020 05:56
Find height of tree
"""
https://practice.geeksforgeeks.org/problems/height-of-binary-tree/1
- use recursion to find the height
- use max on left and right branch on every recursion (add 1 every iteration)
"""
def height(root):
if root is None:
return 0
return 1 + max(height(root.left), height(root.right))
@jonathanagustin
jonathanagustin / findMid.py
Last active April 29, 2020 06:15
findMid - geeks4geeks
"""
https://practice.geeksforgeeks.org/problems/finding-middle-element-in-a-linked-list/1
- Use "tortoise and the hare" method to find the middle
- Use a pointer that increments half the "speed" of another pointer
"""
def findMid(head):
if head is None:
return None
@jonathanagustin
jonathanagustin / branchSums.py
Last active April 29, 2020 06:17
Branch Sums - algoexpert.io
"""
https://www.algoexpert.io/questions/Branch%20Sums
"""
class BinaryTree:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
@jonathanagustin
jonathanagustin / findClosestValueInBST1.py
Last active April 29, 2020 06:22
Find Closest Value in BST - algoexpert.io
"""
https://www.algoexpert.io/questions/Find%20Closest%20Value%20In%20BST
Average: Theta(logn) time | Theta(logn) space
Worst: O(n) time | O(n) space
This solution takes up O(n) space because of recursion.
float('inf') is used to set a unbounded upper value for comparison
"""
@jonathanagustin
jonathanagustin / twoNumberSum1.py
Last active April 29, 2020 06:20
Two Number Sum - algoexpert.io
"""
https://www.algoexpert.io/questions/Two%20Number%20Sum
O(nlogn) time | O(1) space
This algorithm sorts the arrays and then puts two index
pointers at the edges and then advances towards the
middle to find a solution.
"""
def twoNumberSum(array, targetSum):
# sort array (really a list) first