Skip to content

Instantly share code, notes, and snippets.

@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
@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 / 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 / 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 / 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 / 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 / coin_change.py
Created August 27, 2020 00:26
Coin change problem
import sys
from typing import List
def coin_change(coins: List[int], amount: int) -> int:
"""
Returns the minimum number of coins required to generate amount.
Complexity O(amount * len(coins))
:param coins:
:param amount:
@cunla
cunla / min_cost_for_tickets.py
Created August 27, 2020 00:58
Minimum Cost For Tickets interview problem
import sys
from typing import List
cost_options = [1, 7, 30]
def mincost_tickets(days: List[int], costs: List[int]) -> int:
"""
In a country popular for train travel, you have planned some train travelling one year in advance. The days of the year that you will travel is given as an array days. Each day is an integer from 1 to 365.
@cunla
cunla / is_scramble.py
Created August 27, 2020 01:26
Is one string a scramble of another string (leetcode)
def is_scramble(s1: str, s2: str) -> bool:
if len(s1) != len(s2):
return False
if len(s1) == 0 or s1 == s2:
return True
sorted_s1 = sorted(s1)
sorted_s2 = sorted(s2)
if sorted_s1 != sorted_s2:
return False

Keybase proof

I hereby claim:

  • I am cunla on github.
  • I am danielmoran (https://keybase.io/danielmoran) on keybase.
  • I have a public key whose fingerprint is 5EE7 4D9D 1454 BBAE 0EA7 57EE 2A74 F32F A0A7 E28C

To claim this, I am signing this object: