How do I check if a file exists using Python?
This file contains hidden or 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
/** | |
* Replace whitespace with %20 in a string. | |
*/ | |
public class ReplaceWhitespace{ | |
/** | |
* Solution: Use string built-in method - replaceAll | |
* Time Complexity: O(N), where N means the length of string | |
*/ | |
public static String replaceSpaces(String str) { |
This file contains hidden or 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
/** | |
* Compress data | |
*/ | |
public class Compressoin{ | |
/** | |
* Solution: | |
* 1. Insert the picked character from source into the destination string | |
* 2. Count the number of subsequent occurences and append it to the destination. | |
* 3. Pick the next character and repeat. |
This file contains hidden or 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
/** | |
* Rotate a matrix clockwise. | |
*/ | |
public class RotateMatrix { | |
/** | |
* Solution: | |
* We can use standard Matrix Rotation algorithm. | |
*/ | |
public static int[][] rotateClockwise(int[][] matrix) { | |
final int M = matrix.length; |
This file contains hidden or 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
/** | |
* The parameter weekday is true if it is a weekday, and the parameter vacation | |
* is true if we are on vacation. We sleep in if it is not a weekday or we're | |
* on vacation. Return true if we sleep in. | |
*/ | |
public boolean sleepIn(boolean weekday, boolean vacation) { | |
return !weekday || vacation; | |
} | |
/** |
This file contains hidden or 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
#!/usr/bin/env python | |
__author__ = 'Rio' | |
def sleep_in(weekday, vacation): | |
return True if not weekday or vacation else False | |
def monkey_trouble(a_smile, b_smile): |
This file contains hidden or 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
class Solution: | |
# @param tokens, a list of string | |
# @return an integer | |
def evalRPN(self, tokens): | |
"""Calculates tokens and returns the result. | |
Use stack to store numbers and pop two numbers once | |
meets operators then push the calculated result back. | |
""" | |
OPS = { |
This file contains hidden or 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
#!/usr/bin/env python | |
__author__ = 'Rio' | |
class TreeNode: | |
def __init__(self, x): | |
self.val = x | |
self.next = None | |
class Solution: |
This file contains hidden or 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
class Solution: | |
# @param matrix, a list of lists of integers | |
# @param target, an integer | |
# @return a boolean | |
def searchMatrix(self, matrix, target): | |
if not matrix or len(matrix) == 0: | |
return False | |
# Flatten the matrix into 1D array, complexity is O(K). | |
# K means the length of the new array. |
This gist is part of a blog post. Check it out at:
http://jasonrudolph.com/blog/2011/08/09/programming-achievements-how-to-level-up-as-a-developer
NewerOlder