Skip to content

Instantly share code, notes, and snippets.

@cunla
cunla / RequestParser.py
Created June 18, 2020 22:28
Parse http raw request to python http request
import requests
CRLF = '\r\n'
DEFAULT_HTTP_VERSION = 'HTTP/1.1'
class RequestParser(object):
def __parse_request_line(self, request_line):
request_parts = request_line.split(' ')
@cunla
cunla / kclosest.py
Created June 5, 2020 18:23
Find k closest points in python
import heapq
def k_closest_points(points_list, point, k):
# Find k closest points to point in points_list
points_distance = []
for p in points_list:
distance = (p[0] - point[0]) ** 2 + (p[1] - point[1]) ** 2
points_distance.append((distance, p))
heapq.heapify(points_distance)
@cunla
cunla / gsheets.py
Last active January 6, 2020 03:26
Connect to google sheets and read data from spreadsheet
from __future__ import print_function
import pickle
import os.path
from io import StringIO
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly']
@cunla
cunla / click-for-options.ts
Last active September 14, 2017 14:55
Ionic 3: This directive is used with <ion-item-sliding> tag to enable clicking to see the option buttons seen when swiping.
import {Directive, ElementRef, Renderer2} from '@angular/core';
/**
* This directive is used with <ion-item-sliding> tag to enable clicking to see
* the option buttons seen when swiping.
*/
@Directive({
selector: '[click-for-options]',
host: {
'(click)': 'toggleOptions()',
@cunla
cunla / bucket_fill.py
Created March 28, 2017 17:40
Bucket Fill Exercise
"""
Bucket Fill Exercise
Imagine you are working on an image editing application. You need to implement a bucket fill tool similar to the one
in paint. The user will use the tool by selecting a color and clicking on the canvas. The tool fills the selected
region of color with the new color.
When a pixel is filled, all of its neighbors (above, below, left, or right) of the same color must also be filled,
as well as their neighbors, and so on, until the entire region has been filled with the new color.
@cunla
cunla / gist:6a20cbe36c3297071f508cf69da5d931
Last active October 24, 2019 18:32
sorting array of objects in javascript
var objs = [
{
"name":"a",
"age":19
}, {
"name":"b",
"age":15
}, {
"name":"c",
"age":16