This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Build an N-Tree from 2D array of Parent-Child pairs | |
import logging | |
from collections import deque | |
""" | |
TREE: | |
3 | |
/ | \ | |
4 5 6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
PROBLEM: | |
4-digit number: 2284 | |
- assume have dictionary isWord(String word) | |
- assume have phonenum pad digit to set of letters | |
- print all possible words of ANY length up to 4-digits | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Given String "ABC", print all 3-letter permutations of this | |
""" | |
# SOLUTION: - FULL decision tree | |
# - each level is character place, | |
# each branch a choice of one char | |
# - REMAINING unchosen characters |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# You have data for social network on who is friends with whom. | |
# You need to write a function that returns this data in the form of an adjacency list representation, i.e. a mapping of each employee ID to a list of his/her friends on the site | |
# # | |
# You have two input data sets to work with. The first data set is the employees at your company, and the second is all the pairs of employees who are virtually friends so far. It does not matter which employee's ID is in which column, the friendships are bidirectional. | |
# | |
# employees_input = [ | |
# "1,Richard,Engineering", | |
# "2,Erlich,HR", | |
# "3,Monica,Business", |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
MATCH BRACKETS | |
""" | |
""" | |
BAM: dynamic STACK - append opens, then match-pop on closes | |
DICT to match CLOSE to OPEN (as BACK-MATCH), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ATTN: import Q for BFS traversal | |
# https://stackoverflow.com/questions/717148/queue-queue-vs-collections-deque | |
from collections import deque | |
# ATTN: import simplest DICT for Graph Adjacency list | |
# https://stackoverflow.com/questions/5900578/how-does-collections-defaultdict-work | |
from collections import defaultdict | |
# ATTN: import simplest COUNTER dictionary for Distance accum for EQUAL-weighted OCCURRENCE counts but LESS GENERAL-FLEXIBLE than adding VARIABLE edge weight using DefaultDict! | |
# https://pymotw.com/2/collections/counter.html | |
from collections import Counter | |
from sys import maxsize |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
PROBLEM - Stack from two Queues | |
""" | |
""" | |
STACK - LIFO, I/O from the SAME, ONE side | |
- tracks TOP | |
- push/pop on TOP | |
QUEUE - FIFO, Input at END, Output from FRONT; from DIFFERENT, TWO sides |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Given: 1=>2=>3=>4=>5 | |
# Write function that takes head of list, n=2, and returns node 4 where n is distance from end | |
# TRICK: Need to traverse TWICE always in FORWARD direction | |
# 1) count index from 0 to LENGTH to find the END so we can calculate | |
# complement FORWARD distance! | |
# 2) traverse counting from 0 to (LENGTH - n) to return reference to node! | |
# CAREFUL: => Need to handle out-of-bounds on BOTH LOWER, and HIGHER end List | |
# and since this deals with (LENGTH - n), this translates to |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
PROBLEM: Implement MRU Cache | |
- i.e. cache the MOST-MOST-RECENTLY-USED-OR-ACCESSED, and drop LEAST-RECENTLY-USED | |
- FIXED-size cache | |
initialized with a size, never to exceed that size | |
- put(key, val), O(1) | |
- get(key) -> val (or None), O(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# PROBLEM: Count and Construct All Distinct Valid Paths Through a Maze | |
# TRICK0: | |
# - mutable heap-allocated structure to accumulate results to | |
# - cross-recursion-level visibility | |
# - use TUPLE to group MUTABLE ELEMENTs for state accumulation | |
# - globalValidPathCounter as first item of mutable LIST | |
# - used as INDEX into globalValidPaths Dict |
OlderNewer