Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First round of converted samples #6

Merged
merged 52 commits into from
Aug 13, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
7f61067
Adding to BigIntMod python code
vprusso Jun 28, 2019
d1ad539
Adding FindDuplicates python code
vprusso Jun 28, 2019
6732f06
Adding IntToRoman python
vprusso Jun 28, 2019
7ce9b4b
Rename to be consistent
vprusso Jun 28, 2019
2b1be53
Adding LinkedListCycle
vprusso Jun 28, 2019
23bf9de
Adding NthToLast Python code
vprusso Jun 28, 2019
5f97fbc
Added ZeroMatrix Python file
vprusso Jun 28, 2019
bc5daf7
Adding to ConsecutiveArray Python file
vprusso Jun 29, 2019
bc29339
Adding to BuildOrder Python file
vprusso Jul 1, 2019
3264df1
Adding to Python solutions
vprusso Jul 3, 2019
1b46d29
Update to README
vprusso Jul 3, 2019
20a791b
Adding to README
vprusso Jul 3, 2019
4ff2e40
Adding to README
vprusso Jul 3, 2019
439e265
Adding Knapsack to code
vprusso Jul 6, 2019
aa910e7
Updated README
vprusso Jul 6, 2019
b40e81d
Added to README
vprusso Jul 6, 2019
d3e8213
Updating README file
vprusso Jul 6, 2019
75803d6
Updating README
vprusso Jul 6, 2019
90d3380
Adding to README
vprusso Jul 6, 2019
e430ca1
Adding CPP FizzBuzz
vprusso Jul 6, 2019
80237f4
Updates to readme
vprusso Jul 6, 2019
ce3d44d
Adding Java FizzBuzz
vprusso Jul 7, 2019
645eb31
Adding Ones Binary in Python and Java
vprusso Jul 7, 2019
1fc57cf
Adding to README
vprusso Jul 7, 2019
31a700e
Adding to Anagrams and OnesInBinary
vprusso Jul 9, 2019
40cb86b
Updates to README
vprusso Jul 9, 2019
f87536f
Adding to String Deletion
vprusso Jul 9, 2019
ec20fd4
Adding Sum problem for Python and Java
vprusso Jul 11, 2019
cd02973
Adding to Fibonacci for Python and Java
vprusso Jul 11, 2019
f97743c
Adding CPP for Fibonacci
vprusso Jul 11, 2019
8422507
Adding to smallest change for Java and Python
vprusso Jul 11, 2019
9cb7b5d
t
vprusso Jul 11, 2019
505ff75
Adding to Swap variables for all CPP,Python, and Java
vprusso Jul 11, 2019
4a329e8
Adding to ClockAngle code
vprusso Jul 11, 2019
160cbb1
IntToRoman C++
vprusso Jul 18, 2019
367653f
Adding to dedup linked list
vprusso Jul 19, 2019
5beac7b
Adding to dedup linked list
vprusso Jul 21, 2019
bda4e27
Adding to Random linked list
vprusso Jul 22, 2019
262f3a9
Adding to FindDuplicates for C++
vprusso Jul 25, 2019
f03af31
Adding to Two Missing
vprusso Jul 25, 2019
bccd4f4
Adding to ReverseStack for Java and Python
vprusso Jul 31, 2019
0ec613f
Adding to ReverseStack
vprusso Aug 1, 2019
4ed5c47
Adding to Gray Code
vprusso Aug 1, 2019
8b2a18b
Adding to PrintReversedLsit
vprusso Aug 1, 2019
70e366a
Adding to README
vprusso Aug 1, 2019
d6d50e6
Adding to RotateBits
vprusso Aug 2, 2019
ce8e85b
Adding to string compression for Java and Python
vprusso Aug 3, 2019
a62f894
Adding README
vprusso Aug 3, 2019
13775cd
Updates to StringCompression python file
vprusso Aug 4, 2019
5441eb0
Adding to line intersection
vprusso Aug 4, 2019
ccb3f6b
Adding to MaxStack
vprusso Aug 12, 2019
86d8111
Adding to TreeLevelOrder
vprusso Aug 12, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Adding to Python solutions
  • Loading branch information
vprusso committed Jul 3, 2019
commit 3264df10b3b871f5c731286f541e53a8f230f312
92 changes: 82 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,109 +7,181 @@ I would love to compile solutions to all of the problems here, as well as offer
### Index of Questions
* [Median of Arrays](http://www.byte-by-byte.com/median/)
Find the median of two sorted arrays.
* [Priority Queue](http://www.byte-by-byte.com/priorityqueue/) ([Github](https://github.com/samgh/Byte-by-Byte-Solutions/blob/master/java/PriorityQueue.java))

* [Priority Queue](http://www.byte-by-byte.com/priorityqueue/)
([Java](https://github.com/samgh/Byte-by-Byte-Solutions/blob/master/java/PriorityQueue.java))
([Python](https://github.com/samgh/Byte-by-Byte-Solutions/blob/master/python/PriorityQueue.py)
Implement a Priority Queue.

* [0-1 Knapsack](http://www.byte-by-byte.com/01knapsack/) ([Github](https://github.com/samgh/Byte-by-Byte-Solutions/blob/master/java/Knapsack.java))
Given a list of items with values and weights, as well as a max weight, find the maximum value you can generate from items where the sum of the weights is less than the max.

* [Matrix Product](http://www.byte-by-byte.com/matrixproduct/) ([Github](https://github.com/samgh/Byte-by-Byte-Solutions/blob/master/java/MatrixProduct.java))
Given a matrix, find the path from top left to bottom right with the greatest product by moving only down and right.
* [Find Duplicates](http://www.byte-by-byte.com/findduplicates/) ([Github](https://github.com/samgh/Byte-by-Byte-Solutions/blob/master/java/FindDuplicates.java))

* [Find Duplicates](http://www.byte-by-byte.com/findduplicates/)
([Java](https://github.com/samgh/Byte-by-Byte-Solutions/blob/master/java/FindDuplicates.java))
([Python](https://github.com/samgh/Byte-by-Byte-Solutions/blob/master/python/FindDuplicates.py))
Given an array of integers where each value `1 <= x <= len(array)`, write a function that finds all the duplicates in the array.
* [Integer to Roman Numeral](http://www.byte-by-byte.com/inttoroman/) ([Github](https://github.com/samgh/Byte-by-Byte-Solutions/blob/master/java/IntToRoman.java))

* [Integer to Roman Numeral](http://www.byte-by-byte.com/inttoroman/)
([Java](https://github.com/samgh/Byte-by-Byte-Solutions/blob/master/java/IntToRoman.java))
([Python](https://github.com/samgh/Byte-by-Byte-Solutions/blob/master/pythopython/IntToRoman.py))
Given an integer, write a function to return its roman numeral representation.

* [Autocomplete](http://www.byte-by-byte.com/autocomplete/) ([Github](https://github.com/samgh/Byte-by-Byte-Solutions/blob/master/java/Autocomplete.java))
Write an autocomplete class that returns all dictionary words with a given prefix.
* [Linked List Cycle](http://www.byte-by-byte.com/listcycles/) ([Github](https://github.com/samgh/Byte-by-Byte-Solutions/blob/master/java/LinkedListCycle.java))

* [Linked List Cycle](http://www.byte-by-byte.com/listcycles/)
([Java](https://github.com/samgh/Byte-by-Byte-Solutions/blob/master/java/LinkedListCycle.java))
([Python](https://github.com/samgh/Byte-by-Byte-Solutions/blob/master/python/LinkedListCycle.py))
Given a linked list, determine whether it contains a cycle.
* [Build Order](http://www.byte-by-byte.com/buildorder/) ([Github](https://github.com/samgh/Byte-by-Byte-Solutions/blob/master/java/BuildOrder.java))

* [Build Order](http://www.byte-by-byte.com/buildorder/)
([Java](https://github.com/samgh/Byte-by-Byte-Solutions/blob/master/java/BuildOrder.java))
([Python](https://github.com/samgh/Byte-by-Byte-Solutions/blob/master/python/BuildOrder.py))
Given a list of packages that need to be built and the dependencies for each package, determine a valid order in which to build the packages.
* [Consecutive Array](http://www.byte-by-byte.com/consecutivearray/) ([Github](https://github.com/samgh/Byte-by-Byte-Solutions/blob/master/java/ConsecutiveArray.java))

* [Consecutive Array](http://www.byte-by-byte.com/consecutivearray/)
([Java](https://github.com/samgh/Byte-by-Byte-Solutions/blob/master/java/ConsecutiveArray.java))
([Python](https://github.com/samgh/Byte-by-Byte-Solutions/blob/master/python/ConsecutiveArray.py))
Given an unsorted array, find the length of the longest sequence of consecutive numbers in the array.

* [Zero Matrix](http://www.byte-by-byte.com/zeromatrix/) ([Github](https://github.com/samgh/Byte-by-Byte-Solutions/blob/master/java/ZeroMatrix.java))
Given a boolean matrix, update it so that if any cell is true, all the cells in that row and column are true.

* [Random Binary Tree](http://www.byte-by-byte.com/randombinarytree/) ([Github](https://github.com/samgh/Byte-by-Byte-Solutions/blob/master/java/RandomTree.java))
Implement a binary tree with a method getRandomNode() that returns a random node.

* [Two Missing Numbers](http://www.byte-by-byte.com/twomissingnumbers/) ([Github](https://github.com/samgh/Byte-by-Byte-Solutions/blob/master/java/TwoMissingNumbers.java))
Given an array containing all the numbers from 1 to n except two, find the two missing numbers.

* [Shortest Path](http://www.byte-by-byte.com/shortestpath/) ([Github](https://github.com/samgh/Byte-by-Byte-Solutions/blob/master/java/ShortestPath.java))
Given a directed graph, find the shortest path between two nodes if one exists.
* [Square Submatrix](http://www.byte-by-byte.com/squaresubmatrix/) ([Github](https://github.com/samgh/Byte-by-Byte-Solutions/blob/master/java/SquareSubmatrix.java))

* [Square Submatrix](http://www.byte-by-byte.com/squaresubmatrix/)
([Java](https://github.com/samgh/Byte-by-Byte-Solutions/blob/master/java/SquareSubmatrix.java))
([Python](https://github.com/samgh/Byte-by-Byte-Solutions/blob/master/python/SquareSubmatrix.py))
Given a 2D array of 1s and 0s, find the largest square subarray of all 1s.

* [Random Linked List](http://www.byte-by-byte.com/randomlinkedlist/) ([Github](https://github.com/samgh/Byte-by-Byte-Solutions/blob/master/java/RandomLinkedList.java))
Given a linked list where each node has two pointers, one to the next node and one to a random node in the list, clone the linked list.
* [Big Int Mod](http://www.byte-by-byte.com/bigintmod/) ([Github](https://github.com/samgh/Byte-by-Byte-Solutions/blob/master/java/BigIntMod.java))

* [Big Int Mod](http://www.byte-by-byte.com/bigintmod/)
([Java](https://github.com/samgh/Byte-by-Byte-Solutions/blob/master/java/BigIntMod.java))
([Python](https://github.com/samgh/Byte-by-Byte-Solutions/blob/master/python/BigIntMod.py))
Given a list of bytes `a`, each representing one byte of a larger integer (ie. `{0x12, 0x34, 0x56, 0x78}` represents the integer `0x12345678`), and an integer `b`, find `a % b`.

* [Merge K Arrays](http://www.byte-by-byte.com/mergekarrays/)
Given k sorted arrays, merge them into a single sorted array.

* [Dedup Linked List](http://www.byte-by-byte.com/deduplinkedlist/)
Given an unsorted linked list, write a function to remove all the duplicates.

* [Lowest Common Ancestor](http://www.byte-by-byte.com/lowestcommonancestor/)
Given two nodes in a binary tree, write a function to find the lowest common ancestor.

* [String Deletion](http://www.byte-by-byte.com/stringdeletion/)
Given a string and a dictionary HashSet, write a function to determine the minimum number of characters to delete to make a word.

* [Sum](http://www.byte-by-byte.com/sum/)
Given two integers, write a function to sum the numbers without using any arithmetic operators.

* [Reverse Stack](http://www.byte-by-byte.com/reversestack/)
Given a stack, reverse the items without creating any additional data structures.

* [Swap Variables](http://www.byte-by-byte.com/swapvariables/)
Given two integers, write a function that swaps them without using any temporary variables.

* [Matrix Search](http://www.byte-by-byte.com/matrixsearch/)
Given an `n x m` array where all rows and columns are in sorted order, write a function to determine whether the array contains an element `x`.

* [Clock Angle](http://www.byte-by-byte.com/clockangle/)
Given two integers, an hour and a minute, write a function to calculate the angle between the two hands on a clock representing that time.

* [Fibonacci](http://www.byte-by-byte.com/fibonacci/)
Given an integer n, write a function to compute the nth Fibonacci number.

* [Tree to Doubly Linked List](http://www.byte-by-byte.com/treetolist/)
Given a tree, write a function to convert it into a circular doubly linked list from left to right by only modifying the existing pointers.

* [Line Intersection](http://www.byte-by-byte.com/lineintersection/)
Given two lines on a Cartesian plane, write a function to determine whether or not the lines intersect.

* [Longest Consecutive Branch](http://www.byte-by-byte.com/longestbranch/)
Given a tree, write a function to find the length of the longest branch of nodes in increasing consecutive order.

* [Sort Stacks](http://www.byte-by-byte.com/sortstacks/)
Given a stack, sort the elements in the stack using one additional stack.

* [Print Reversed List](http://www.byte-by-byte.com/printreversedlist/)
Given a linked list, write a function that prints the nodes of the list in reverse order.

* [Longest Common Substring](http://www.byte-by-byte.com/longestsubstring/)
Given two strings, write a function that returns the longest common substring.

* [Stack from Queues](http://www.byte-by-byte.com/stackfromqueues/)
Implement a LIFO stack with basic functionality (push and pop) using FIFO queues to store the data.

* [Balanced Binary Tree](http://www.byte-by-byte.com/balancedtree/)
Given a binary tree, write a function to determine whether the tree is balanced.

* [Gray Code](http://www.byte-by-byte.com/graycode/)
Given two integers, write a function to determine whether or not their binary representations differ by a single bit.

* [Merge Arrays](http://www.byte-by-byte.com/mergearrays/)
Given 2 sorted arrays, A and B, where A is long enough to hold the contents of A and B, write a function to copy the contents of B into A without using any buffer or additional memory.

* [Zero Sum Subarray](http://www.byte-by-byte.com/zerosum/)
Given an array, write a function to find any subarray that sums to zero, if one exists.

* [Three Sum](http://www.byte-by-byte.com/threesum/)
Given a list of integers, write a function that returns all sets of 3 numbers in the list, `a`, `b`, and `c`, so that `a + b + c == 0`.

* [Rotate Bits](http://www.byte-by-byte.com/rotatebits/)
Given a number, write a function to rotate the bits (ie circular shift).

* [Palindromes](http://www.byte-by-byte.com/palindromes/)
Given a linked list, write a function to determine whether the list is a palindrome.

* [Permutations](http://www.byte-by-byte.com/permutations/)
Write a function that returns all permutations of a given list.

* [N Stacks](http://www.byte-by-byte.com/nstacks/)
Implement N > 0 stacks using a single array to store all stack data (you may use auxiliary arrays in your stack object, but all of the objects in all of the stacks must be in the same array). No stack should be full unless the entire array is full.

* [Tree Level Order](http://www.byte-by-byte.com/treelevelorder/)
Given a tree, write a function that prints out the nodes of the tree in level order.

* [Split Linked List](http://www.byte-by-byte.com/splitlinkedlist/)
Given a linked list, write a function to split the list into two equal halves.

* [Kth Most Frequest String](http://www.byte-by-byte.com/kthmostfrequentstring/)
Given a list of strings, write a function to get the kth most frequently occurring string.

* [String Compression](http://www.byte-by-byte.com/stringcompression/)
Given a string, write a function to compress it by shortening every sequence of the same character to that character followed by the number of repetitions. If the compressed string is longer than the original, you should return the original string.

* [Anagrams](http://www.byte-by-byte.com/anagrams/)
Given two strings, write a function to determine whether they are anagrams.

* [Binary Search Tree Verification](http://www.byte-by-byte.com/binarysearchtree/)
Given a binary tree, write a function to test if the tree is a binary search tree.

* [Max Stack](http://www.byte-by-byte.com/maxstack/)
Implement a LIFO stack that has a `push()`, `pop()`, and `max()` function, where `max()` returns the maximum value in the stack. All of these functions should run in `O(1)` time.

* [Number of Ones in Binary](http://www.byte-by-byte.com/onesinbinary/)
Given an integer, write a function to compute the number of ones in the binary representation of the number.

* [Smallest Change](http://www.byte-by-byte.com/smallestchange/)
Given an input amount of change `x`, write a function to determine the minimum number of coins required to make that amount of change.
* [Nth-to-last Element](http://www.byte-by-byte.com/nthtolastelement) ([Github](https://github.com/samgh/Byte-by-Byte-Solutions/blob/master/java/NthToLast.java))

* [Nth-to-last Element](http://www.byte-by-byte.com/nthtolastelement)
([Java](https://github.com/samgh/Byte-by-Byte-Solutions/blob/master/java/NthToLast.java))
([Python](https://github.com/samgh/Byte-by-Byte-Solutions/blob/master/python/NthToLast.py))
Given a linked list, and an input n, write a function that returns the nth-to-last element of the linked list.

* [FizzBuzz](http://www.byte-by-byte.com/fizzbuzz/)
Output numbers from `1` to `x`. If the number is divisible by `3`, replace it with `“Fizz”`. If it is divisible by `5`, replace it with `“Buzz”`. If it is divisible by `3` and `5` replace it with `“FizzBuzz”`.
Output numbers from `1` to `x`. If the number is divisible by `3`, replace it with `“Fizz”`. If it is divisible by `5`, replace it with `“Buzz”`. If it is divisible by `3` and `5` replace it with `“FizzBuzz”`.

4 changes: 2 additions & 2 deletions python/BigIntMod.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
Given a list of bytes a, each representing one byte of a larger integer
(ie. {0x12, 0x34, 0x56, 0x78} represents the integer 0x12345678), and an
integer b, find a % b.

Execution: python BigIntMod.py

For more details, check out http://www.byte-by-byte.com/bigintmod/
"""
import unittest
Expand Down
69 changes: 69 additions & 0 deletions python/InorderTraversal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
class Node(object):
def __init__(self, value):
self.value = value
self.left = None
self.right = None


class BinaryTree(object):
def __init__(self, root):
self.root = Node(root)
self.stack = []

def traverse(self, node):
curr = node

while True:

if curr:
self.stack.append(curr)
curr = curr.left
elif self.stack:
curr = self.stack.pop()
print(curr.value)

curr = curr.right
else:
break


"""
Sample test case

8
/ \
4 12
/ \ / \
1 6 9 15
/ \
5 10

Should print:
1
4
5
6
8
9
10
12
15
"""

# Set up tree:
tree = BinaryTree(8)
tree.left = Node(4)
tree.left.left = Node(1)
tree.left.right = Node(6)
tree.left.right.left = Node(5)
tree.right = Node(12)
tree.right.left = Node(9)
tree.right.left.right = Node(10)
tree.right.right = Node(15)
tree.traverse(tree)

#print(tree.print_tree("preorder"))
#print(tree.print_tree("inorder"))
#print(tree.print_tree("postorder"))


3 changes: 2 additions & 1 deletion python/IntToRoman.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
numerals = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"]
values = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]


def int_to_roman(value):
if value > 3999 or value < 1:
raise ValueError(f"Value {value} is above 3999 or below 1.")
Expand All @@ -29,7 +30,7 @@ def int_to_roman(value):
return ''.join(numeral)


class TestBigIntMod(unittest.TestCase):
class TestIntToRoman(unittest.TestCase):
def test_I(self):
self.assertEqual(int_to_roman(1), "I")
print("1 converts to I")
Expand Down
59 changes: 59 additions & 0 deletions python/MatrixProduct.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
"""
Title: Matrix Product

Given a matrix, find the path from top left to bottom right with the greatest
product by moving only down and right.

Execution: python MatrixProduct.py

For more details, check out http://www.byte-by-byte.com/matrixproduct/

"""
import unittest

def matrix_product(mat):
"""
Bottom-up dynamic programming solution.
"""
if len(mat) == 0 or len(mat[0]) == 0:
return 0

# Create cache of min and max product to a given cell.
max_cache = [[1] * len(mat[0])] * len(mat)
min_cache = [[1] * len(mat[0])] * len(mat)

# Fill caches. We start at the top left and iteratively find the greatest
# at smallest path to each subsequent cell by considering the greatest and
# smallest path to the cells above and to the left of the current cell.
for i in range(len(mat)):
for j in range(len(mat[0])):
max_val = float("inf")
min_val = float("-inf")

# If you're in the top left corner, just copy to cache
if i == 0 and j == 0:
max_val = mat[i][j]
min_val = mat[i][j]

# If we're not at the top, consider the value above.
if i > 0:
temp_max = max(mat[i][j] * max_cache[i-1][j], mat[i][j] * min_cache[i-1][j])
max_val = max(temp_max, max_val)
temp_min = min(mat[i][j] * max_cache[i-1][j], mat[i][j] * min_cache[i-1][j])
min_val = min(temp_min, min_val)

# If we're not on the left, consider the value to the left
if j > 0:
temp_max = max(mat[i][j] * max_cache[i][j-1], mat[i][j] * min_cache[i][j-1])
max_val = max(temp_max, max_val)
temp_min = min(mat[i][j] * max_cache[i][j-1], mat[i][j] * min_cache[i][j-1])
min_val = min(temp_min, min_val)

max_cache[i][j] = max_val
min_cache[i][j] = min_val
return max_cache[len(max_cache) - 1][len(max_cache[0]) - 1]


mat = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
x = matrix_product(mat)
print(x)
Loading