Skip to content

Instantly share code, notes, and snippets.

View akey7's full-sized avatar

Alicia Key akey7

View GitHub Profile
@akey7
akey7 / sorted_array_to_BST.py
Created February 24, 2019 23:35
Sorted array to BST, Python
class Solution:
def make_bst(self, nums, start, end):
if end < start:
return None
mid = (start + end) // 2
n = TreeNode(nums[mid])
n.left = self.make_bst(nums, start, mid - 1)
n.right = self.make_bst(nums, mid + 1, end)
return n
@akey7
akey7 / one_edit_away.py
Created February 24, 2019 23:33
Test if two strings are one edit away from each other, Python
def one_edit(a, b):
edits = 0
if a == b:
return True
elif len(a) == len(b):
for x, y in zip(a, b):
if x != y:
edits += 1
if edits > 1:
return False
@akey7
akey7 / merge_sorted_lists.py
Created February 24, 2019 23:31
Merge sorted lists, Python
def mergeTwoLists(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
if l1 is not None and l2 is not None:
if l1.val < l2.val:
head = ListNode(l1.val)
tail = head
@akey7
akey7 / linked_list_cycle_detect.py
Created February 24, 2019 23:29
Detect cycle in linked list, Python
def hasCycle(self, head):
"""
:type head: ListNode
:rtype: bool
"""
if head is not None:
nodes = []
nodes.append(head)
current = head
while current.next is not None:
@akey7
akey7 / knn_from_scratch.py
Created February 24, 2019 23:28
kNN from scratch
import numpy as np
import pandas as pd
class AliciaKNN:
def __init__(self, df, class_col_name):
self.df = df
self.class_col_name = class_col_name
@property
@akey7
akey7 / balance_parens_1.py
Created February 24, 2019 23:27
Balance parens, take 1
from collections import deque
class Solution:
def isValid(self, s: 'str') -> 'bool':
stack = deque()
for char in s:
if char == '(':
stack.append(char)
elif char == ')':
if len(stack) == 0 or stack.pop() != '(':
@akey7
akey7 / binary_search.py
Created February 24, 2019 23:25
Binary search, Python
from math import floor
def binary_search(target, lst):
"""
:param target: The item to find.
:param lst: The sorted list to look through
:return: -1 if the target is not found. Index if found otherwise
"""
@akey7
akey7 / bfs_graph.py
Created February 24, 2019 23:23
BFS graph
from collections import deque
class GraphNode:
def __init__(self, id):
self.id = id
self.neighbors = []
def bfs_search(root, target_id):
if root is None:
@akey7
akey7 / array_intersection.py
Created February 24, 2019 23:23
Find array intersection, Python
class Solution:
def intersect(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: List[int]
"""
nums1_count = {}
for x in nums1:
if x in nums1_count:
@akey7
akey7 / SelectionSort.java
Created February 24, 2019 23:19
Selection Sort, Java
/*** CODE ***/
class SelectionSort {
int[] sort(int[] arr) {
// We start i=0 because j = i + 1. i is always less than j.
// This OUTER loop moves left to right, with i marking the
// LEFT boundary of the UNSORTED section.
for (int i = 0; i < arr.length - 1; i++) {