forked from YearOfProgramming/2017Challenges
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/YearOfProgramming/2017Cha…
- Loading branch information
Showing
56 changed files
with
1,283 additions
and
5 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Challenge 0 - Python | ||
|
||
Basic function that prints - Hello World! |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
print("Hello World!") |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Valid Closers | ||
|
||
## How to run | ||
|
||
``` | ||
javac ValidClosers.java | ||
java ValidClosers | ||
``` | ||
|
||
## Discussion | ||
|
||
* Runtime: O(n) | ||
* Space: O(n) | ||
|
||
The solution is a stack implementation, the stack must follow these rules: | ||
|
||
* `openers` get pushed to the stack | ||
* `openers` will be poped from the stack if the `closers` meet the following conditions: | ||
* `stack` is not empty | ||
* The first element on the stack is in `openers` | ||
* The `closers` must close the `openers` popped from the `stack` | ||
* `stack` must be empty after the string has been processed |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Stack; | ||
|
||
/** | ||
* @author zmiller | ||
*/ | ||
public class ValidClosers extends YOP { | ||
|
||
private final String FALSE = "false"; | ||
private final String TRUE = "true"; | ||
private final List<Character> openers = Arrays.asList('{', '[', '('); | ||
private final List<Character> closers = Arrays.asList('}', ']', ')'); | ||
|
||
/** | ||
* Validates that the given input has a correct sequence of openers and | ||
* closers | ||
* | ||
* @param input | ||
*/ | ||
protected void run(String input) { | ||
|
||
Stack<Character> stack = new Stack<>(); | ||
for(char c : input.toCharArray()) { | ||
|
||
// No rules for openers, add it to the stack | ||
if (openers.contains(c)) { | ||
stack.push(c); | ||
} | ||
|
||
// Openers have a few rules | ||
if (closers.contains(c)) { | ||
|
||
// Stack cannot be empty | ||
if(stack.empty()) { | ||
System.out.println(FALSE); | ||
return; | ||
} | ||
|
||
// The last element pushed must be an opener | ||
int openerIdx = openers.indexOf(stack.pop()); | ||
if(openerIdx == -1) { | ||
System.out.println(FALSE); | ||
return; | ||
} | ||
|
||
// The opener and closer must match | ||
Character expected = closers.get(openerIdx); | ||
if (!expected.equals(c)) { | ||
System.out.println(FALSE); | ||
return; | ||
} | ||
} | ||
} | ||
|
||
// Stack must be empty | ||
if (stack.size() != 0) { | ||
System.out.println(FALSE); | ||
return; | ||
} | ||
|
||
System.out.println(TRUE); | ||
} | ||
|
||
public static void main(String args[]) { | ||
launch(new ValidClosers()); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import java.util.Scanner; | ||
|
||
/** | ||
* @author zmiller | ||
*/ | ||
public abstract class YOP { | ||
|
||
/** | ||
* Executes the life cycle of the solution. | ||
* | ||
* @param yop - class to execute | ||
*/ | ||
protected static void launch(YOP yop) { | ||
|
||
String input; | ||
Scanner scan = new Scanner(System.in); | ||
|
||
System.out.println("Type 'done' to terminate.\n"); | ||
while(true) { | ||
System.out.print("Input: "); | ||
input = scan.nextLine(); | ||
if(input.toLowerCase().equals("done")) { | ||
return; | ||
} | ||
|
||
yop.run(input); | ||
} | ||
} | ||
|
||
/** | ||
* Executes the solution | ||
* | ||
* @param input - command line input | ||
*/ | ||
protected abstract void run(String input); | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
##Valid Closers | ||
|
||
Language Version: Python 2.7.12 | ||
|
||
|
||
###Solution | ||
|
||
stack! push and pop | ||
|
||
###Testing | ||
|
||
$ python unit_test.py |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Created on Wed Jan 11 09:21:20 2017 | ||
""" | ||
def solution(strr): | ||
"""check if all brackets closed properly""" | ||
|
||
closerof = {'{':'}', '[':']','(':')'} | ||
Start = ('[','{','(') | ||
Closer = (']','}',')') | ||
stack = [] | ||
|
||
for i in strr: | ||
# push into stack, if new bracket found | ||
if i in Start: | ||
stack.append(i) | ||
# pop out if proper closer found, else False | ||
elif i in Closer: | ||
if closerof[stack[-1]] == i: | ||
stack.pop() | ||
else: | ||
return False | ||
|
||
# check leftover in stack | ||
return not stack |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Created on Wed Jan 11 09:21:20 2017 | ||
@author: Shaowen Liu | ||
""" | ||
import unittest | ||
from solution import solution | ||
|
||
class solution_test(unittest.TestCase): | ||
|
||
def test_case1(self): | ||
strr = '{{{}}}' | ||
self.assertTrue(solution(strr)) | ||
|
||
def test_case2(self): | ||
strr = '{{{{{{{{{adfkjaefia}}}}}}}' | ||
self.assertFalse(solution(strr)) | ||
|
||
def test_case3(self): | ||
strr = '{{{{{{{{{[[[[[[kadfa{{{{{{{((({daljfdaf({{{[]}}kaldjfs})})))}}}}}}}]]]]]]}kjfela}}}}}}}}' | ||
self.assertTrue(solution(strr)) | ||
|
||
def test_case4(self): | ||
strr = '{{{[}}}}' | ||
self.assertFalse(solution(strr)) | ||
|
||
def test_case5(self): | ||
strr = '{{{{{{{{{}}}}}}}}}' | ||
self.assertTrue(solution(strr)) | ||
|
||
def test_case6(self): | ||
strr = '[[[[[[[[[kafjalfeianfailfeja;fjai;efa;sfj]]]]]]]]]kjajdain' | ||
self.assertTrue(solution(strr)) | ||
|
||
def test_case7(self): | ||
strr = '([{)}]' | ||
self.assertFalse(solution(strr)) | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Removing a Node from a BST | ||
====== | ||
Idea | ||
------ | ||
Binary Trees are a big deal in computer science. It's only fitting that we have a couple challenges that utilize them. The purpose of this assignment is to create a function capable of removing a node from a [Binary Search Tree](https://en.wikipedia.org/wiki/Binary_search_tree). Every node should have a left, right and data attribute. Left and right will eventually be set to either another Node or null/None, the data attribute will be an int. Along with removing a node from a binary search tree, you should also have a function that prints the tree out in pre-order (mostly for testing purposes). | ||
|
||
A few other functions that might be helpful | ||
* findSmallest - finds and returns the node with the smallest data value | ||
* findLargest - finds and returns the node with the largest data value | ||
* insert - inserts a node into the BST, mostly useful for testing. | ||
|
||
Testing | ||
----- | ||
Testing for this challenge is fairly straight forward. Simply insert a bunch of nodes into the BST, remove one then print the BST to ensure that it was removed. | ||
|
||
Notes: | ||
* Please implement functionality to read standard input. Upon activating the program the user should be prompted with a message asking them to input space separated values (lets say characters) which are to be put inserted into the tree one after the other. These values will not repeat. | ||
* The tree should now be printed in pre-order | ||
* A new prompt should appear asking what value(s) the user would like to delete from the tree. This input will be space seperated and non repeating. | ||
* The program should output the tree again, in pre order then terminate. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
#!/usr/bin/env python3 | ||
# @author slandau3 | ||
import random | ||
|
||
|
||
class Node: | ||
|
||
def __init__(self, data): | ||
self.data = data | ||
self.left = None | ||
self.right = None | ||
|
||
|
||
def insert(data, root): | ||
""" | ||
Inserts a node into the tree. | ||
:param data: The data which will be given to the inserted node | ||
:param root: The root of the tree | ||
:return: The new root of the tree, with a new node inserted | ||
""" | ||
if root is None: | ||
return Node(data) | ||
elif data > root.data: | ||
root.right = insert(data,root.right) | ||
elif data < root.data: | ||
root.left = insert(data,root.left) | ||
|
||
return root | ||
|
||
|
||
def createTree(num_nodes): | ||
""" | ||
A function created to construct the tree from 0 to num_nodes | ||
The root will be the the middle number of num_nodes | ||
:param num_nodes: The number of nodes to create in the list | ||
:return: The root of the newly assembled tree | ||
""" | ||
root = Node(num_nodes//2) | ||
|
||
for i in range(num_nodes): | ||
if i == num_nodes//2: | ||
continue | ||
|
||
root = insert(i, root) | ||
return root | ||
|
||
|
||
def printPreOrder(root): | ||
""" | ||
Prints the tree in preorder | ||
:param root: The root of the tree | ||
:return: None | ||
""" | ||
if root is None: | ||
return | ||
print(root.data) | ||
printPreOrder(root.left) | ||
printPreOrder(root.right) | ||
|
||
|
||
def remove(root, data): | ||
if root is None: | ||
return root | ||
elif data < root.data: | ||
# If the data is less than that of the current node | ||
# We know we have to look to the left | ||
root.left = remove(root.left, data) | ||
elif data > root.data: | ||
# If the data is greater than that of hte current node | ||
# we know that we have to look to the right | ||
root.right = remove(root.right, data) | ||
elif data == root.data: | ||
# We have found the node we are looking for | ||
# In order to solve this problem the node we are removing (assuming its a non leaf node) | ||
# will need to be replaced with something. After some experimenting and soul searching | ||
# you will see that the node we need to replace it with is the closest node to what it was. | ||
# In other words we need the largest node in the left sub-tree or the smallest node in the | ||
# Right sub-tree. We are going to worry about swapping the data only to make things a lot easier. | ||
if root.left is not None: | ||
# If we know the left subtree exists, we need to look for hte largest node in it. | ||
largest = findBiggest(root.left).data | ||
elif root.right is not None: | ||
# If we know the right subtree exists (which at this point means the left does not) | ||
# means that we need to search for the smallest value in the right subtree. | ||
largest = findSmallest(root.right).data | ||
else: | ||
# If the root is a leaf node then we can easily remove it | ||
root = None | ||
return root | ||
|
||
# Search through the list and find the largest value (which will either just remove directly | ||
# or cause us to remove a few others) | ||
root = remove(root, largest) | ||
root.data = largest | ||
|
||
return root | ||
|
||
|
||
def findBiggest(root): | ||
""" | ||
Finds the largest node in the tree | ||
:param root: The starting node of the subnodes you want to saerch through | ||
:return: The largest Node in the given subtree | ||
""" | ||
if root.right is None: | ||
return root | ||
else: | ||
return findBiggest(root.right) | ||
|
||
|
||
def findSmallest(root): | ||
""" | ||
Finds the smallest node in the subtree | ||
:param root: The root node of the tree or subtree | ||
:return: The smallest node in the given subtree | ||
""" | ||
if root.left is None: | ||
return root | ||
else: | ||
return findSmallest(root.left) | ||
|
||
|
||
root = createTree(100) | ||
#printPreOrder(root) | ||
print('\n\n') | ||
root = remove(root, 50) | ||
root = remove(root, 99) | ||
root = remove(root, 98) | ||
root = remove(root, 0) | ||
print('\n\n') | ||
printPreOrder(root) | ||
|
Oops, something went wrong.