Skip to content

Instantly share code, notes, and snippets.

@jonathanagustin
jonathanagustin / cot.js
Created October 9, 2018 09:12 — forked from ianblenke/cot.js
Attempting to feed ATAK a Cursor on Target message
const dgram = require('dgram');
const socket = dgram.createSocket('udp4');
//var port = 4242
//var port = 8087
var port = 18999
var address = "192.168.14.151"
var msg = '<?xml version="1.0" standalone="yes"?><event version="2.0" uid="J-01334" type="a-h-A-M-F-U-M" time="2005-04-05T11:43:38.07Z" start="2005-04-05T11:43:38.07Z" stale="2005-04-05T11:45:38.07Z"><detail></detail><point lat="30.0090027" lon="-85.9578735" ce="45.3" hae="-42.6" le="99.5"/></event>'
@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
@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 / 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 / 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 / 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))

Docker

Lessons Learned

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

The above means you need to dos2unix the file.

Useful commands

@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)
@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 / 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)