Skip to content

Instantly share code, notes, and snippets.

View humpydonkey's full-sized avatar

Asia humpydonkey

  • San Francisco, Bay Area
  • 23:56 (UTC -07:00)
  • LinkedIn in/yazhoucao
View GitHub Profile
@humpydonkey
humpydonkey / EnforceHttps.scala
Last active February 2, 2019 07:18 — forked from guillaumebort/gist:8314512
[Enforce HTTPS (Play!)]Force HTTPS decorator for Play framework #scala #play #ssl #web
def forceHttps(handler: Handler, request: RequestHeader): Handler = {
(isHTTPSRequired, isRequestSecure(request), request.method, request.uri) match {
// HTTPS is supported but not required for the API
case (_, _, _, uri) if uri.startsWith("/api") => handler
// HTTPS is required here, redirect GET requests
case (true, false, "GET", uri) => Action(Results.Redirect(s"https://${request.domain}${request.uri}"))
// HTTPS is required but we can't redirect
@humpydonkey
humpydonkey / SSLPoke.java
Last active February 2, 2019 07:14 — forked from 4ndrej/SSLPoke.java
[SSL poke]Test of java SSL / keystore / cert setup. #java #ssl #test
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import java.io.*;
/** Establish a SSL connection to a host and port, writes a byte and
* prints the response. See
* http://confluence.atlassian.com/display/JIRA/Connecting+to+SSL+services
*/
public class SSLPoke {
public static void main(String[] args) {
@humpydonkey
humpydonkey / check_missing_files.py
Last active February 2, 2019 07:12
[check missing files]check missing files, if missing then copy them to somewhere #python #os #tool #file
import os
from typing import Set
import shutil
def check_existence_or_copy(source_dir: str, check_against_dir: str, copy_dst_dir: str) -> None:
missing_files = check_missing(source_dir, check_against_dir)
copy_missing_files(source_dir, copy_dst_dir, missing_files)
@humpydonkey
humpydonkey / flat_directory.py
Last active February 2, 2019 07:13
[flat directory]Move all the files under src directory to the root of src directory. #os #python #file #tool
import os
def flat_directory(source_dir: str) -> None:
"""
Move all the files under src directory to the root of src directory.
Flatten the structure.
"""
count = 0
for currDir, subDirs, files in os.walk(source_dir):
class Solution:
def checkSubarraySum(self, nums: List[int], k: int) -> bool:
"""
Trick: remainder + n * k = some prefix sum (i...j)
=> (remainder + n * k) % k = some prefix sum (i...j) % k
=> remainder = some prefix sum (i...j) % k
In other words:
a % k = x
b % k = x
(a - b) % k = x -x = 0
class Solution:
def subarraySum(self, nums: List[int], k: int) -> int:
"""
Idea: find (i, j) where prefix_sums_j - prefix_sums_i == k
tip: sum(i,j)=sum(0,j)-sum(0,i),
where sum(i,j) represents the sum of all the elements from index i to j-1.
"""
sum_counts = defaultdict(int)
sum_counts[0] = 1
accumulate_sum = 0
class SparseVector:
"""
Time: O(n) for __init__, O(L) for dotProduct
Space: O(L), where L is the number of non-zero values
"""
def __init__(self, nums: List[int]):
self.values = []
self.index = []
for i in range(len(nums)):
if nums[i] != 0:
"""
# Definition for Employee.
class Employee:
def __init__(self, id: int, importance: int, subordinates: List[int]):
self.id = id
self.importance = importance
self.subordinates = subordinates
"""
class Solution:
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
"""
Three pointer solution built on top of the two-sum problem.
Time: O(n^2)
Space: O(1)
The trick is how to handle duplicate. The order of checking two pointers' equality matters: one must move pointer first,
then check equality against the prior value. The case starting from the prior value included the second case.
E.g. -1, 0, 0, 1
0, 0, 1 included the case of 0, 1. But the case starts from the second 0 doesn't include the prior case.
class Solution:
def isPalindrome(self, s: str) -> bool:
l, r = 0, len(s)-1
while l < r:
if not s[l].isalnum():
l += 1
elif not s[r].isalnum():
r -= 1
else:
if s[r].lower() != s[l].lower():